Module: GraphQL::Compatibility::ExecutionSpecification::CounterSchema

Defined in:
lib/graphql/compatibility/execution_specification/counter_schema.rb

Class Method Summary collapse

Class Method Details

.build(execution_strategy) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/graphql/compatibility/execution_specification/counter_schema.rb', line 6

def self.build(execution_strategy)
  counter_type = nil
  schema = nil

  has_count_interface = GraphQL::InterfaceType.define do
    name "HasCount"
    field :count, types.Int
    field :counter, ->{ has_count_interface }
  end

  counter_type = GraphQL::ObjectType.define do
    name "Counter"
    interfaces [has_count_interface]
    field :count, types.Int, resolve: ->(o,a,c) { schema.[:count] += 1 }
    field :counter, has_count_interface, resolve: ->(o,a,c) { :counter }
  end

  alt_counter_type = GraphQL::ObjectType.define do
    name "AltCounter"
    interfaces [has_count_interface]
    field :count, types.Int, resolve: ->(o,a,c) { schema.[:count] += 1 }
    field :counter, has_count_interface, resolve: ->(o,a,c) { :counter }
  end

  has_counter_interface = GraphQL::InterfaceType.define do
    name "HasCounter"
    field :counter, has_count_interface
  end

  query_type = GraphQL::ObjectType.define do
    name "Query"
    interfaces [has_counter_interface]
    field :counter, has_count_interface, resolve: ->(o,a,c) { :counter }
  end

  schema = GraphQL::Schema.define(
    query: query_type,
    resolve_type: ->(t, o, c) { o == :counter ? counter_type : nil },
    orphan_types: [alt_counter_type, counter_type],
    query_execution_strategy: execution_strategy,
  )
  schema.[:count] = 0
  schema
end