Sha256: c43ca3ed594cc316cd0264f342ea83d6d2784bb3c7363fb000597f21d787f99e

Contents?: true

Size: 942 Bytes

Versions: 13

Compression:

Stored size: 942 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, context: context)
          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.18.12 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.11 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.10 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.9 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.8 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.7 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.6 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.5 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.4 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.3 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.18.0 lib/graphql/static_validation/rules/fragments_are_finite.rb