Class: GraphQL::StaticValidation::Validator::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/static_validation/validator.rb

Overview

The validation context gets passed to each validator.

It exposes a Language::Visitor where validators may add hooks. (Visitor#visit is called in #validate)

It provides access to the schema & fragments which validators may read from.

It holds a list of errors which each validator may add to.

It also provides limited access to the TypeStack instance, which tracks state as you climb in and out of different fields.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, document) ⇒ Context



42
43
44
45
46
47
48
49
50
51
# File 'lib/graphql/static_validation/validator.rb', line 42

def initialize(schema, document)
  @schema = schema
  @document = document
  @fragments = document.parts.each_with_object({}) do |part, memo|
    part.is_a?(GraphQL::Language::Nodes::FragmentDefinition) && memo[part.name] = part
  end
  @errors = []
  @visitor = GraphQL::Language::Visitor.new
  @type_stack = GraphQL::StaticValidation::TypeStack.new(schema, visitor)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



41
42
43
# File 'lib/graphql/static_validation/validator.rb', line 41

def document
  @document
end

#errorsObject (readonly)

Returns the value of attribute errors.



41
42
43
# File 'lib/graphql/static_validation/validator.rb', line 41

def errors
  @errors
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



41
42
43
# File 'lib/graphql/static_validation/validator.rb', line 41

def fragments
  @fragments
end

#schemaObject (readonly)

Returns the value of attribute schema.



41
42
43
# File 'lib/graphql/static_validation/validator.rb', line 41

def schema
  @schema
end

#visitorObject (readonly)

Returns the value of attribute visitor.



41
42
43
# File 'lib/graphql/static_validation/validator.rb', line 41

def visitor
  @visitor
end

Instance Method Details

#argument_definitionGraphQL::Argument?



68
69
70
71
72
# File 'lib/graphql/static_validation/validator.rb', line 68

def argument_definition
  # Don't get the _last_ one because that's the current one.
  # Get the second-to-last one, which is the parent of the current one.
  @type_stack.argument_definitions[-2]
end

#directive_definitionGraphQL::Directive?



63
64
65
# File 'lib/graphql/static_validation/validator.rb', line 63

def directive_definition
  @type_stack.directive_definitions.last
end

#field_definitionGraphQL::Field?



58
59
60
# File 'lib/graphql/static_validation/validator.rb', line 58

def field_definition
  @type_stack.field_definitions.last
end

#object_typesObject



53
54
55
# File 'lib/graphql/static_validation/validator.rb', line 53

def object_types
  @type_stack.object_types
end

#skip_field?(field_name) ⇒ Boolean

Don’t try to validate dynamic fields since they aren’t defined by the type system



76
77
78
# File 'lib/graphql/static_validation/validator.rb', line 76

def skip_field?(field_name)
  GraphQL::Schema::DYNAMIC_FIELDS.include?(field_name)
end