Sha256: e9a741b623eff6e3d1d4e7dfd56e584c4ca5f26181fa71a2519da2f9ba919989

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 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.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::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

5 entries across 5 versions & 1 rubygems

Version Path
graphql-0.7.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.7.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.6.2 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.6.1 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb
graphql-0.6.0 lib/graphql/static_validation/rules/fields_are_defined_on_type.rb