Class: GraphQL::Schema

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/graphql/schema.rb,
lib/graphql/schema/printer.rb,
lib/graphql/schema/type_map.rb,
lib/graphql/schema/type_reducer.rb,
lib/graphql/schema/possible_types.rb,
lib/graphql/schema/type_validator.rb,
lib/graphql/schema/field_validator.rb,
lib/graphql/schema/type_expression.rb,
lib/graphql/schema/middleware_chain.rb,
lib/graphql/schema/rescue_middleware.rb,
lib/graphql/schema/each_item_validator.rb,
lib/graphql/schema/implementation_validator.rb

Overview

A GraphQL schema which may be queried with Query.

Defined Under Namespace

Modules: Printer Classes: EachItemValidator, FieldValidator, ImplementationValidator, InvalidTypeError, MiddlewareChain, PossibleTypes, RescueMiddleware, TypeExpression, TypeMap, TypeReducer, TypeValidator

Constant Summary collapse

DIRECTIVES =
[GraphQL::Directive::SkipDirective, GraphQL::Directive::IncludeDirective]
DYNAMIC_FIELDS =
["__type", "__typename", "__schema"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, mutation: nil, subscription: nil, max_depth: nil, types: []) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • query (GraphQL::ObjectType)

    the query root for the schema

  • mutation (GraphQL::ObjectType) (defaults to: nil)

    the mutation root for the schema

  • subscription (GraphQL::ObjectType) (defaults to: nil)

    the subscription root for the schema

  • max_depth (Integer) (defaults to: nil)

    maximum query nesting (if it’s greater, raise an error)

  • types (Array<GraphQL::BaseType>) (defaults to: [])

    additional types to include in this schema



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/graphql/schema.rb', line 24

def initialize(query:, mutation: nil, subscription: nil, max_depth: nil, types: [])
  @query    = query
  @mutation = mutation
  @subscription = subscription
  @max_depth = max_depth
  @orphan_types = types
  @directives = DIRECTIVES.reduce({}) { |m, d| m[d.name] = d; m }
  @static_validator = GraphQL::StaticValidation::Validator.new(schema: self)
  @rescue_middleware = GraphQL::Schema::RescueMiddleware.new
  @middleware = [@rescue_middleware]
  # Default to the built-in execution strategy:
  self.query_execution_strategy = GraphQL::Query::SerialExecution
  self.mutation_execution_strategy = GraphQL::Query::SerialExecution
  self.subscription_execution_strategy = GraphQL::Query::SerialExecution
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



9
10
11
# File 'lib/graphql/schema.rb', line 9

def directives
  @directives
end

#max_depthObject

Returns the value of attribute max_depth.



10
11
12
# File 'lib/graphql/schema.rb', line 10

def max_depth
  @max_depth
end

#middlewareArray<#call> (readonly)

Returns Middlewares suitable for MiddlewareChain, applied to fields during execution.

Returns:

  • (Array<#call>)

    Middlewares suitable for MiddlewareChain, applied to fields during execution



17
18
19
# File 'lib/graphql/schema.rb', line 17

def middleware
  @middleware
end

#mutationObject (readonly)

Returns the value of attribute mutation.



9
10
11
# File 'lib/graphql/schema.rb', line 9

def mutation
  @mutation
end

#mutation_execution_strategyObject

Override these if you don’t want the default executor:



12
13
14
# File 'lib/graphql/schema.rb', line 12

def mutation_execution_strategy
  @mutation_execution_strategy
end

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/graphql/schema.rb', line 9

def query
  @query
end

#query_execution_strategyObject

Override these if you don’t want the default executor:



12
13
14
# File 'lib/graphql/schema.rb', line 12

def query_execution_strategy
  @query_execution_strategy
end

#static_validatorObject (readonly)

Returns the value of attribute static_validator.



9
10
11
# File 'lib/graphql/schema.rb', line 9

def static_validator
  @static_validator
end

#subscriptionObject (readonly)

Returns the value of attribute subscription.



9
10
11
# File 'lib/graphql/schema.rb', line 9

def subscription
  @subscription
end

#subscription_execution_strategyObject

Override these if you don’t want the default executor:



12
13
14
# File 'lib/graphql/schema.rb', line 12

def subscription_execution_strategy
  @subscription_execution_strategy
end

Instance Method Details

#execute(*args) ⇒ Hash

Execute a query on itself. See Query#initialize for arguments.

Returns:

  • (Hash)

    query result, ready to be serialized as JSON



53
54
55
56
# File 'lib/graphql/schema.rb', line 53

def execute(*args)
  query = GraphQL::Query.new(self, *args)
  query.result
end

#get_field(parent_type, field_name) ⇒ Object

Resolve field named ‘field_name` for type `parent_type`. Handles dynamic fields `__typename`, `__type` and `__schema`, too



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/schema.rb', line 60

def get_field(parent_type, field_name)
  defined_field = parent_type.get_field(field_name)
  if defined_field
    defined_field
  elsif field_name == "__typename"
    GraphQL::Introspection::TypenameField.create(parent_type)
  elsif field_name == "__schema" && parent_type == query
    GraphQL::Introspection::SchemaField.create(self)
  elsif field_name == "__type" && parent_type == query
    GraphQL::Introspection::TypeByNameField.create(self.types)
  else
    nil
  end
end

#possible_types(type_defn) ⇒ Array<GraphQL::ObjectType>

Returns types which belong to ‘type_defn` in this schema.

Parameters:

Returns:



81
82
83
84
# File 'lib/graphql/schema.rb', line 81

def possible_types(type_defn)
  @interface_possible_types ||= GraphQL::Schema::PossibleTypes.new(self)
  @interface_possible_types.possible_types(type_defn)
end

#type_from_ast(ast_node) ⇒ Object



75
76
77
# File 'lib/graphql/schema.rb', line 75

def type_from_ast(ast_node)
  GraphQL::Schema::TypeExpression.new(self, ast_node).type
end

#typesGraphQL::Schema::TypeMap

Returns ‘{ name => type }` pairs of types in this schema.

Returns:



43
44
45
46
47
48
# File 'lib/graphql/schema.rb', line 43

def types
  @types ||= begin
    all_types = @orphan_types + [query, mutation, GraphQL::Introspection::SchemaType]
    TypeReducer.find_all(all_types.compact)
  end
end