Sha256: 0c18d74331430714e423d71dc4f45b4240466104c3282cf7390c875fd5856769

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

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

  IS_FIELD = Proc.new {|f| f.is_a?(GraphQL::Language::Nodes::Field) }

  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.fields[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

6 entries across 6 versions & 1 rubygems

Version Path
graphql-0.9.5 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.9.4 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.9.3 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.9.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.8.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.8.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb