Sha256: 15df91a856e3d72d5f9c800969b268d6b27ce7337e9eca98adfecb3ee7014aaa

Contents?: true

Size: 1.19 KB

Versions: 25

Compression:

Stored size: 1.19 KB

Contents

class GraphQL::StaticValidation::FragmentsAreUsed
  include GraphQL::StaticValidation::Message::MessageHelper

  def validate(context)
    v = context.visitor
    used_fragments = []
    defined_fragments = []
    v[GraphQL::Language::Nodes::FragmentSpread] << -> (node, parent) { used_fragments <<  node }
    v[GraphQL::Language::Nodes::FragmentDefinition] << -> (node, parent) { defined_fragments << node}
    v[GraphQL::Language::Nodes::Document].leave << -> (node, parent) { add_errors(context.errors, used_fragments, defined_fragments) }
  end

  private

  def add_errors(errors, used_fragments, defined_fragments)
    undefined_fragments = find_difference(used_fragments, defined_fragments.map(&:name))
    undefined_fragments.each do |fragment|
      errors << message("Fragment #{fragment.name} was used, but not defined", fragment)
    end

    unused_fragments = find_difference(defined_fragments, used_fragments.map(&:name))
    unused_fragments.each do |fragment|
      errors << message("Fragment #{fragment.name} was defined, but not used", fragment)
    end
  end

  def find_difference(fragments, allowed_fragment_names)
    fragments.select {|f| !allowed_fragment_names.include?(f.name) }
  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_used.rb
graphql-0.12.0 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.11.1 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.11.0 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.9 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.8 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.7 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.6 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.5 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.4 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.3 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.2 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.1 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.10.0 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.9.5 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.9.4 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.9.3 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.9.2 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.8.1 lib/graphql/static_validation/rules/fragments_are_used.rb
graphql-0.8.0 lib/graphql/static_validation/rules/fragments_are_used.rb