Skip to content

Commit 5098895

Browse files
author
Robert Mosolgo
authored
Merge pull request #1842 from rmosolgo/migrate-dummy-schema
Migrate dummy schema to class-based
2 parents 3777475 + 7fcb098 commit 5098895

21 files changed

+397
-393
lines changed

lib/graphql/base_type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def self.resolve_related_type(type_arg)
187187
resolve_related_type(type_arg.call)
188188
when String
189189
# Get a constant by this name
190-
Object.const_get(type_arg)
190+
resolve_related_type(Object.const_get(type_arg))
191191
else
192192
if type_arg.respond_to?(:graphql_definition)
193193
type_arg.graphql_definition

lib/graphql/schema/field.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def scoped?
126126
# @param complexity [Numeric] When provided, set the complexity for this field
127127
# @param scope [Boolean] If true, the return type's `.scope_items` method will be called on the return value
128128
# @param subscription_scope [Symbol, String] A key in `context` which will be used to scope subscription payloads
129-
def initialize(type: nil, name: nil, owner: nil, null: nil, field: nil, function: nil, description: nil, deprecation_reason: nil, method: nil, connection: nil, max_page_size: nil, scope: nil, resolve: nil, introspection: false, hash_key: nil, camelize: true, complexity: 1, extras: [], resolver_class: nil, subscription_scope: nil, arguments: {}, &definition_block)
129+
def initialize(type: nil, name: nil, owner: nil, null: nil, field: nil, function: nil, description: nil, deprecation_reason: nil, method: nil, connection: nil, max_page_size: nil, scope: nil, resolve: nil, introspection: false, hash_key: nil, camelize: true, trace: nil, complexity: 1, extras: [], resolver_class: nil, subscription_scope: nil, arguments: {}, &definition_block)
130130

131131
if name.nil?
132132
raise ArgumentError, "missing first `name` argument or keyword `name:`"
@@ -170,6 +170,7 @@ def initialize(type: nil, name: nil, owner: nil, null: nil, field: nil, function
170170
@extras = extras
171171
@resolver_class = resolver_class
172172
@scope = scope
173+
@trace = trace
173174

174175
# Override the default from HasArguments
175176
@own_arguments = {}
@@ -258,6 +259,10 @@ def to_graphql
258259
field_defn.metadata[:resolver] = @resolver_class
259260
end
260261

262+
if !@trace.nil?
263+
field_defn.trace = @trace
264+
end
265+
261266
field_defn.resolve = self.method(:resolve_field)
262267
field_defn.connection = connection?
263268
field_defn.connection_max_page_size = @max_page_size

spec/graphql/analysis/analyze_query_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def call(memo, visit_type, irep_node)
7878

7979
it "calls the defined analyzers" do
8080
collected_types, node_counts = reduce_result
81-
expected_visited_types = [Dummy::DairyAppQueryType, Dummy::CheeseType, GraphQL::INT_TYPE, GraphQL::STRING_TYPE]
81+
expected_visited_types = [Dummy::DairyAppQuery.graphql_definition, Dummy::Cheese.graphql_definition, GraphQL::INT_TYPE, GraphQL::STRING_TYPE]
8282
assert_equal expected_visited_types, collected_types
8383
expected_node_counts = {
8484
GraphQL::Language::Nodes::OperationDefinition => 1,

spec/graphql/base_type_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
end
1212

1313
it "can be compared" do
14-
obj_type = Dummy::MilkType
14+
obj_type = Dummy::Milk.graphql_definition
1515
assert_equal(!GraphQL::INT_TYPE, !GraphQL::INT_TYPE)
1616
refute_equal(!GraphQL::FLOAT_TYPE, GraphQL::FLOAT_TYPE)
1717
assert_equal(
@@ -25,7 +25,7 @@
2525
end
2626

2727
it "Accepts arbitrary metadata" do
28-
assert_equal ["Cheese"], Dummy::CheeseType.metadata[:class_names]
28+
assert_equal ["Cheese"], Dummy::Cheese.graphql_definition.metadata[:class_names]
2929
end
3030

3131
describe "#name" do
@@ -63,7 +63,7 @@ class BaseType < GraphQL::Schema::Object
6363
end
6464

6565
describe "forwards-compat with new api" do
66-
let(:type_defn) { Dummy::CheeseType }
66+
let(:type_defn) { Dummy::Cheese.graphql_definition }
6767
it "responds to new methods" do
6868
assert_equal "Cheese", type_defn.graphql_name
6969
assert_equal type_defn, type_defn.graphql_definition

spec/graphql/enum_type_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require "spec_helper"
33

44
describe GraphQL::EnumType do
5-
let(:enum) { Dummy::DairyAnimalEnum }
5+
let(:enum) { Dummy::DairyAnimal.graphql_definition }
66

77
it "coerces names to underlying values" do
88
assert_equal("YAK", enum.coerce_isolated_input("YAK"))

spec/graphql/execution/typecast_spec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@ def subtype?(*args)
88
end
99

1010
it "counts the same type as a subtype" do
11-
assert subtype?(Dummy::MilkType, Dummy::MilkType)
12-
assert !subtype?(Dummy::MilkType, Dummy::CheeseType)
13-
assert subtype?(Dummy::MilkType.to_list_type.to_non_null_type, Dummy::MilkType.to_list_type.to_non_null_type)
11+
assert subtype?(Dummy::Milk.graphql_definition, Dummy::Milk.graphql_definition)
12+
assert !subtype?(Dummy::Milk.graphql_definition, Dummy::Cheese.graphql_definition)
13+
assert subtype?(Dummy::Milk.graphql_definition.to_list_type.to_non_null_type, Dummy::Milk.graphql_definition.to_list_type.to_non_null_type)
1414
end
1515

1616
it "counts member types as subtypes" do
17-
assert subtype?(Dummy::EdibleInterface, Dummy::CheeseType)
18-
assert subtype?(Dummy::EdibleInterface, Dummy::MilkType)
19-
assert subtype?(Dummy::DairyProductUnion, Dummy::MilkType)
20-
assert subtype?(Dummy::DairyProductUnion, Dummy::CheeseType)
17+
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Cheese.graphql_definition)
18+
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Milk.graphql_definition)
19+
assert subtype?(Dummy::DairyProduct.graphql_definition, Dummy::Milk.graphql_definition)
20+
assert subtype?(Dummy::DairyProduct.graphql_definition, Dummy::Cheese.graphql_definition)
2121

22-
assert !subtype?(Dummy::DairyAppQueryType, Dummy::DairyProductUnion)
23-
assert !subtype?(Dummy::CheeseType, Dummy::DairyProductUnion)
24-
assert !subtype?(Dummy::EdibleInterface, Dummy::DairyProductUnion)
25-
assert !subtype?(Dummy::EdibleInterface, GraphQL::STRING_TYPE)
26-
assert !subtype?(Dummy::EdibleInterface, Dummy::DairyProductInputType)
22+
assert !subtype?(Dummy::DairyAppQuery.graphql_definition, Dummy::DairyProduct.graphql_definition)
23+
assert !subtype?(Dummy::Cheese.graphql_definition, Dummy::DairyProduct.graphql_definition)
24+
assert !subtype?(Dummy::Edible.graphql_definition, Dummy::DairyProduct.graphql_definition)
25+
assert !subtype?(Dummy::Edible.graphql_definition, GraphQL::STRING_TYPE)
26+
assert !subtype?(Dummy::Edible.graphql_definition, Dummy::DairyProductInput.graphql_definition)
2727
end
2828

2929
it "counts lists as subtypes if their inner types are subtypes" do
30-
assert subtype?(Dummy::EdibleInterface.to_list_type, Dummy::MilkType.to_list_type)
31-
assert subtype?(Dummy::DairyProductUnion.to_list_type, Dummy::MilkType.to_list_type)
32-
assert !subtype?(Dummy::CheeseType.to_list_type, Dummy::DairyProductUnion.to_list_type)
33-
assert !subtype?(Dummy::EdibleInterface.to_list_type, Dummy::DairyProductUnion.to_list_type)
34-
assert !subtype?(Dummy::EdibleInterface.to_list_type, GraphQL::STRING_TYPE.to_list_type)
30+
assert subtype?(Dummy::Edible.graphql_definition.to_list_type, Dummy::Milk.graphql_definition.to_list_type)
31+
assert subtype?(Dummy::DairyProduct.graphql_definition.to_list_type, Dummy::Milk.graphql_definition.to_list_type)
32+
assert !subtype?(Dummy::Cheese.graphql_definition.to_list_type, Dummy::DairyProduct.graphql_definition.to_list_type)
33+
assert !subtype?(Dummy::Edible.graphql_definition.to_list_type, Dummy::DairyProduct.graphql_definition.to_list_type)
34+
assert !subtype?(Dummy::Edible.graphql_definition.to_list_type, GraphQL::STRING_TYPE.to_list_type)
3535
end
3636

3737
it "counts non-null types as subtypes of nullable parent types" do
38-
assert subtype?(Dummy::MilkType, Dummy::MilkType.to_non_null_type)
39-
assert subtype?(Dummy::EdibleInterface, Dummy::MilkType.to_non_null_type)
40-
assert subtype?(Dummy::EdibleInterface.to_non_null_type, Dummy::MilkType.to_non_null_type)
38+
assert subtype?(Dummy::Milk.graphql_definition, Dummy::Milk.graphql_definition.to_non_null_type)
39+
assert subtype?(Dummy::Edible.graphql_definition, Dummy::Milk.graphql_definition.to_non_null_type)
40+
assert subtype?(Dummy::Edible.graphql_definition.to_non_null_type, Dummy::Milk.graphql_definition.to_non_null_type)
4141
assert subtype?(
4242
GraphQL::STRING_TYPE.to_non_null_type.to_list_type,
4343
GraphQL::STRING_TYPE.to_non_null_type.to_list_type.to_non_null_type,

spec/graphql/field_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def acts_like_default_resolver(field, old_prop, new_prop)
116116

117117
describe "#metadata" do
118118
it "accepts user-defined metadata" do
119-
similar_cheese_field = Dummy::CheeseType.get_field("similarCheese")
119+
similar_cheese_field = Dummy::Cheese.graphql_definition.get_field("similarCheese")
120120
assert_equal [:cheeses, :milks], similar_cheese_field.metadata[:joins]
121121
end
122122
end

spec/graphql/interface_type_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
require "spec_helper"
33

44
describe GraphQL::InterfaceType do
5-
let(:interface) { Dummy::EdibleInterface }
5+
let(:interface) { Dummy::Edible.graphql_definition }
66
let(:dummy_query_context) { OpenStruct.new(schema: Dummy::Schema) }
77

88
it "has possible types" do
9-
assert_equal([Dummy::CheeseType, Dummy::HoneyType, Dummy::MilkType], Dummy::Schema.possible_types(interface))
9+
assert_equal([Dummy::Cheese.graphql_definition, Dummy::Honey.graphql_definition, Dummy::Milk.graphql_definition], Dummy::Schema.possible_types(interface))
1010
end
1111

1212
describe "query evaluation" do
@@ -96,11 +96,11 @@
9696
it "copies orphan types without affecting the original" do
9797
interface = GraphQL::InterfaceType.define do
9898
name "AInterface"
99-
orphan_types [Dummy::HoneyType]
99+
orphan_types [Dummy::Honey]
100100
end
101101

102102
interface_2 = interface.dup
103-
interface_2.orphan_types << Dummy::CheeseType
103+
interface_2.orphan_types << Dummy::Cheese
104104
assert_equal 1, interface.orphan_types.size
105105
assert_equal 2, interface_2.orphan_types.size
106106
end

spec/graphql/introspection/input_value_type_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
]
4141
}
4242
}}
43-
assert_equal(expected, result)
43+
assert_equal(expected, result.to_h)
4444
end
4545

4646
let(:cheese_type) {

spec/graphql/object_type_spec.rb

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require "spec_helper"
33

44
describe GraphQL::ObjectType do
5-
let(:type) { Dummy::CheeseType }
5+
let(:type) { Dummy::Cheese.graphql_definition }
66

77
it "doesn't allow double non-null constraints" do
88
assert_raises(GraphQL::DoubleNonNullTypeError) {
@@ -45,10 +45,10 @@
4545
describe "interfaces" do
4646
it "may have interfaces" do
4747
assert_equal([
48-
Dummy::EdibleInterface,
49-
Dummy::EdibleAsMilkInterface,
50-
Dummy::AnimalProductInterface,
51-
Dummy::LocalProductInterface
48+
Dummy::Edible.graphql_definition,
49+
Dummy::EdibleAsMilk.graphql_definition,
50+
Dummy::AnimalProduct.graphql_definition,
51+
Dummy::LocalProduct.graphql_definition
5252
], type.interfaces)
5353
end
5454

@@ -85,7 +85,7 @@
8585
end
8686

8787
it "accepts fields definition" do
88-
last_produced_dairy = GraphQL::Field.define(name: :last_produced_dairy, type: Dummy::DairyProductUnion)
88+
last_produced_dairy = GraphQL::Field.define(name: :last_produced_dairy, type: Dummy::DairyProduct)
8989
cow_type = GraphQL::ObjectType.define(name: "Cow", fields: [last_produced_dairy])
9090
assert_equal([last_produced_dairy], cow_type.fields)
9191
end
@@ -94,54 +94,54 @@
9494
it "adds an interface" do
9595
type = GraphQL::ObjectType.define do
9696
name 'Hello'
97-
implements Dummy::EdibleInterface
98-
implements Dummy::AnimalProductInterface
97+
implements Dummy::Edible
98+
implements Dummy::AnimalProduct
9999

100100
field :hello, types.String
101101
end
102102

103-
assert_equal([Dummy::EdibleInterface, Dummy::AnimalProductInterface], type.interfaces)
103+
assert_equal([Dummy::Edible.graphql_definition, Dummy::AnimalProduct.graphql_definition], type.interfaces)
104104
end
105105

106106
it "adds many interfaces" do
107107
type = GraphQL::ObjectType.define do
108108
name 'Hello'
109-
implements Dummy::EdibleInterface, Dummy::AnimalProductInterface
109+
implements Dummy::Edible, Dummy::AnimalProduct
110110

111111
field :hello, types.String
112112
end
113113

114-
assert_equal([Dummy::EdibleInterface, Dummy::AnimalProductInterface], type.interfaces)
114+
assert_equal([Dummy::Edible.graphql_definition, Dummy::AnimalProduct.graphql_definition], type.interfaces)
115115
end
116116

117117
it "preserves existing interfaces and appends a new one" do
118118
type = GraphQL::ObjectType.define do
119119
name 'Hello'
120-
interfaces [Dummy::EdibleInterface]
121-
implements Dummy::AnimalProductInterface
120+
interfaces [Dummy::Edible]
121+
implements Dummy::AnimalProduct
122122

123123
field :hello, types.String
124124
end
125125

126-
assert_equal([Dummy::EdibleInterface, Dummy::AnimalProductInterface], type.interfaces)
126+
assert_equal([Dummy::Edible.graphql_definition, Dummy::AnimalProduct.graphql_definition], type.interfaces)
127127
end
128128

129129
it "can be used to inherit fields from the interface" do
130130
type_1 = GraphQL::ObjectType.define do
131131
name 'Hello'
132-
implements Dummy::EdibleInterface
133-
implements Dummy::AnimalProductInterface
132+
implements Dummy::Edible
133+
implements Dummy::AnimalProduct
134134
end
135135

136136
type_2 = GraphQL::ObjectType.define do
137137
name 'Hello'
138-
implements Dummy::EdibleInterface
139-
implements Dummy::AnimalProductInterface, inherit: true
138+
implements Dummy::Edible
139+
implements Dummy::AnimalProduct, inherit: true
140140
end
141141

142142
type_3 = GraphQL::ObjectType.define do
143143
name 'Hello'
144-
implements Dummy::EdibleInterface, Dummy::AnimalProductInterface, inherit: true
144+
implements Dummy::Edible, Dummy::AnimalProduct, inherit: true
145145
end
146146

147147
assert_equal [], type_1.all_fields.map(&:name)
@@ -158,23 +158,28 @@
158158
end
159159

160160
it "exposes defined field property" do
161-
field_without_prop = Dummy::CheeseType.get_field("flavor")
162-
field_with_prop = Dummy::CheeseType.get_field("fatContent")
161+
field_without_prop = Dummy::Cheese.graphql_definition.get_field("flavor")
162+
field_with_prop = Dummy::Cheese.graphql_definition.get_field("fatContent")
163163
assert_equal(field_without_prop.property, nil)
164-
assert_equal(field_with_prop.property, :fat_content)
164+
# This used to lookup `:fat_content`, but not since migrating to class-based
165+
assert_equal(field_with_prop.property, nil)
165166
end
166167

167168
it "looks up from interfaces" do
168-
field_from_self = Dummy::CheeseType.get_field("fatContent")
169-
field_from_iface = Dummy::MilkType.get_field("fatContent")
170-
assert_equal(field_from_self.property, :fat_content)
169+
field_from_self = Dummy::Cheese.graphql_definition.get_field("fatContent")
170+
field_from_iface = Dummy::Milk.graphql_definition.get_field("fatContent")
171+
# This used to lookup `:fat_content`, but not since migrating to class-based
172+
assert_equal(field_from_self.property, nil)
171173
assert_equal(field_from_iface.property, nil)
172174
end
173175
end
174176

175177
describe "#dup" do
176178
it "copies fields and interfaces without altering the original" do
177179
type.interfaces # load the internal cache
180+
assert_equal 4, type.interfaces.size
181+
assert_equal 9, type.fields.size
182+
178183
type_2 = type.dup
179184

180185
# IRL, use `+=`, not this
@@ -185,8 +190,8 @@
185190

186191
assert_equal 4, type.interfaces.size
187192
assert_equal 5, type_2.interfaces.size
188-
assert_equal 8, type.fields.size
189-
assert_equal 9, type_2.fields.size
193+
assert_equal 9, type.fields.size
194+
assert_equal 10, type_2.fields.size
190195
end
191196
end
192197
end

0 commit comments

Comments
 (0)