Class: GraphQL::Schema

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

Overview

A GraphQL schema which may be queried with Query.

Defined Under Namespace

Classes: EachItemValidator, FieldValidator, ImplementationValidator, InvalidTypeError, 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) ⇒ Schema

Returns a new instance of Schema.

Parameters:



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

def initialize(query:, mutation: nil)
  @query    = query
  @mutation = mutation
  @directives = DIRECTIVES.reduce({}) { |m, d| m[d.name] = d; m }
  @static_validator = GraphQL::StaticValidation::Validator.new(schema: self)
end

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def directives
  @directives
end

#mutationObject (readonly)

Returns the value of attribute mutation.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def mutation
  @mutation
end

#queryObject (readonly)

Returns the value of attribute query.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def query
  @query
end

#static_validatorObject (readonly)

Returns the value of attribute static_validator.



6
7
8
# File 'lib/graphql/schema.rb', line 6

def static_validator
  @static_validator
end

Instance Method Details

#get_field(parent_type, field_name) ⇒ Object

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



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

def get_field(parent_type, field_name)
  defined_field = parent_type.fields[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

#typesObject

A ‘{ name => type }` hash of types in this schema



19
20
21
# File 'lib/graphql/schema.rb', line 19

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