Sha256: f70e736ef525953323ea4b5c765eb9193fb5b564b52d60e9fa36a579023ba924

Contents?: true

Size: 962 Bytes

Versions: 3

Compression:

Stored size: 962 Bytes

Contents

class GraphQL::StaticValidation::FieldsAreDefinedOnType
  include GraphQL::StaticValidation::Message::MessageHelper

  def validate(context)
    visitor = context.visitor
    visitor[GraphQL::Language::Nodes::Field] << -> (node, parent) {
      return if context.skip_field?(node.name)
      parent_type = context.object_types[-2]
      parent_type = parent_type.unwrap
      validate_field(context.errors, node, parent_type, parent)
    }
  end

  private

  def validate_field(errors, ast_field, parent_type, parent)
    if parent_type.kind.union?
      errors << message("Selections can't be made directly on unions (see selections on #{parent_type.name})", parent)
      return GraphQL::Language::Visitor::SKIP
    end

    field =  parent_type.get_field(ast_field.name)
    if field.nil?
      errors << message("Field '#{ast_field.name}' doesn't exist on type '#{parent_type.name}'", parent)
      return GraphQL::Language::Visitor::SKIP
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphql-0.12.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.12.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.11.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb