Sha256: 9f53c3a88fd3c926be6e5a3368fb45dac534d3053f966a1b3b763c644030bc25

Contents?: true

Size: 961 Bytes

Versions: 2

Compression:

Stored size: 961 Bytes

Contents

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

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

  def validate(context)
    visitor = context.visitor
    visitor[GraphQL::Nodes::Field] << -> (node, parent) {
      parent_type = context.object_types[-2]
      parent_type = parent_type.kind.unwrap(parent_type)
      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::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::Visitor::SKIP
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-0.3.0 lib/graph_ql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.2.0 lib/graph_ql/static_validation/fields_are_defined_on_type.rb