Class: GraphQL::StaticValidation::ValidationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/static_validation/validation_context.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 GraphQL::StaticValidation::Validator#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(query) ⇒ ValidationContext

Returns a new instance of ValidationContext.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql/static_validation/validation_context.rb', line 15

def initialize(query)
  @query = query
  @schema = query.schema
  @document = query.document
  @fragments = {}
  @operations = {}

  document.definitions.each do |definition|
    case definition
    when GraphQL::Language::Nodes::FragmentDefinition
      @fragments[definition.name] = definition
    when GraphQL::Language::Nodes::OperationDefinition
      @operations[definition.name] = definition
    end
  end

  @errors = []
  @visitor = GraphQL::Language::Visitor.new(document)
  @type_stack = GraphQL::StaticValidation::TypeStack.new(schema, visitor)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def document
  @document
end

#errorsObject (readonly)

Returns the value of attribute errors.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def errors
  @errors
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def fragments
  @fragments
end

#operationsObject (readonly)

Returns the value of attribute operations.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def operations
  @operations
end

#queryObject (readonly)

Returns the value of attribute query.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def query
  @query
end

#schemaObject (readonly)

Returns the value of attribute schema.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def schema
  @schema
end

#visitorObject (readonly)

Returns the value of attribute visitor.



14
15
16
# File 'lib/graphql/static_validation/validation_context.rb', line 14

def visitor
  @visitor
end

Instance Method Details

#argument_definitionGraphQL::Argument?

Returns The most-recently-entered GraphQL::Argument, if currently inside one.

Returns:

  • (GraphQL::Argument, nil)

    The most-recently-entered GraphQL::Argument, if currently inside one



66
67
68
69
70
# File 'lib/graphql/static_validation/validation_context.rb', line 66

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?

Returns The most-recently-entered GraphQL::Directive, if currently inside one.

Returns:

  • (GraphQL::Directive, nil)

    The most-recently-entered GraphQL::Directive, if currently inside one



61
62
63
# File 'lib/graphql/static_validation/validation_context.rb', line 61

def directive_definition
  @type_stack.directive_definitions.last
end

#field_definitionGraphQL::Field?

Returns The most-recently-entered GraphQL::Field, if currently inside one.

Returns:

  • (GraphQL::Field, nil)

    The most-recently-entered GraphQL::Field, if currently inside one



51
52
53
# File 'lib/graphql/static_validation/validation_context.rb', line 51

def field_definition
  @type_stack.field_definitions.last
end

#object_typesObject



36
37
38
# File 'lib/graphql/static_validation/validation_context.rb', line 36

def object_types
  @type_stack.object_types
end

#parent_type_definitionGraphQL::BaseType

Returns The type which the current type came from.

Returns:



46
47
48
# File 'lib/graphql/static_validation/validation_context.rb', line 46

def parent_type_definition
  object_types[-2]
end

#pathArray<String>

Returns Field names to get to the current field.

Returns:

  • (Array<String>)

    Field names to get to the current field



56
57
58
# File 'lib/graphql/static_validation/validation_context.rb', line 56

def path
  @type_stack.path.dup
end

#skip_field?(field_name) ⇒ Boolean

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/graphql/static_validation/validation_context.rb', line 74

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

#type_definitionGraphQL::BaseType

Returns The current object type.

Returns:



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

def type_definition
  object_types.last
end