Sha256: da87d72a5f7e4d970b24df8460dfcca1b9c1bbc56158b73b3255b1184d565b7c

Contents?: true

Size: 824 Bytes

Versions: 25

Compression:

Stored size: 824 Bytes

Contents

class GraphQL::StaticValidation::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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.12.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.11.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.11.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.9 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.8 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.7 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.6 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.5 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.4 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.3 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.10.0 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.9.5 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.9.4 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.9.3 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.9.2 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.8.1 lib/graphql/static_validation/rules/fragments_are_finite.rb
graphql-0.8.0 lib/graphql/static_validation/rules/fragments_are_finite.rb