Module: GraphQL::Rails::Schema

Extended by:
Schema
Included in:
Schema
Defined in:
lib/graphql/rails/schema.rb

Overview

Defines the GraphQL schema, consisting of queries, mutations, and subscriptions.

Constant Summary collapse

TYPES =
[:query, :mutation, :subscription]

Instance Method Summary collapse

Instance Method Details

#clearObject

Clear internal state, probably due to a Rails reload.



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

def clear
  @schema = nil
  @fields = Hash.new { |hash, key| hash[key] = [] }
end

#instanceObject

Lazily build the GraphQL schema instance.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphql/rails/schema.rb', line 25

def instance
  @schema ||= GraphQL::Schema.new begin
    TYPES.reduce({
      max_depth: Rails.config.max_depth,
      types: Types.explicit,
    }) do |schema, type|
      fields = @fields[type]
      unless fields.empty?
        # Build an object for each operation type.
        schema[type] = GraphQL::ObjectType.define do
          name type.to_s.capitalize
          description "Root #{type.to_s} for this schema"
          # Add a field for each operation.
          fields.each do |value|
            field value.name, field: value
          end
          # Add the global node ID lookup query.
          if Rails.config.global_ids && type == :query
            field :node, field: NodeIdentification.field
          end
        end
      end
      schema
    end
  end
end