Class: GraphQL::StaticValidation::NoDefinitionsArePresent

Inherits:
Object
  • Object
show all
Includes:
Message::MessageHelper
Defined in:
lib/graphql/static_validation/rules/no_definitions_are_present.rb

Instance Method Summary collapse

Methods included from Message::MessageHelper

#message

Instance Method Details

#validate(context) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/static_validation/rules/no_definitions_are_present.rb', line 7

def validate(context)
  schema_definition_nodes = []
  register_node = ->(node, _p) {
    schema_definition_nodes << node
    GraphQL::Language::Visitor::SKIP
  }

  visitor = context.visitor
  visitor[GraphQL::Language::Nodes::DirectiveDefinition] << register_node
  visitor[GraphQL::Language::Nodes::SchemaDefinition] << register_node
  visitor[GraphQL::Language::Nodes::ScalarTypeDefinition] << register_node
  visitor[GraphQL::Language::Nodes::ObjectTypeDefinition] << register_node
  visitor[GraphQL::Language::Nodes::InputObjectTypeDefinition] << register_node
  visitor[GraphQL::Language::Nodes::InterfaceTypeDefinition] << register_node
  visitor[GraphQL::Language::Nodes::UnionTypeDefinition] << register_node
  visitor[GraphQL::Language::Nodes::EnumTypeDefinition] << register_node

  visitor[GraphQL::Language::Nodes::Document].leave << ->(node, _p) {
    if schema_definition_nodes.any?
      context.errors << message(%|Query cannot contain schema definitions|, schema_definition_nodes, context: context)
    end
  }
end