Sha256: b3cf82a2b51ca052fd92915a15da5e603e0e49f52308b2604a35895b124e2e73

Contents?: true

Size: 924 Bytes

Versions: 13

Compression:

Stored size: 924 Bytes

Contents

module GraphQL
  module StaticValidation
    class FragmentsAreFinite
      include GraphQL::StaticValidation::Message::MessageHelper

      def validate(context)
        context.visitor[GraphQL::Language::Nodes::FragmentDefinition] << -> (node, parent) {
          if has_nested_spread(node, [], context)
            context.errors << message("Fragment #{node.name} contains an infinite loop", node)
          end
        }
      end

      private

      def has_nested_spread(fragment_def, parent_fragment_names, context)
        nested_spreads = fragment_def.selections
          .select {|f| f.is_a?(GraphQL::Language::Nodes::FragmentSpread)}

        nested_spreads.any? do |spread|
          nested_def = context.fragments[spread.name]
          parent_fragment_names.include?(spread.name) || has_nested_spread(nested_def, parent_fragment_names + [fragment_def.name], context)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
graphql-0.17.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.17.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.17.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.16.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.16.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.15.3 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.15.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.14.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.15.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.15.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.14.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.14.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.13.0 lib/graphql/static_validation/rules/fragments_are_finite.rb