Skip to content

Commit 4d18909

Browse files
committed
Add more test cases
1 parent 94bc0c3 commit 4d18909

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

lib/graphql/query_resolver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def self.map_dependencies(class_name, ast_node)
2929
current_class_name = selection.name.singularize.classify.constantize
3030
dependencies[name] = map_dependencies(current_class_name, selection)
3131
rescue NameError
32-
selection_name = class_name.reflections[selection.name.to_sym].options[:class_name]
32+
selection_name = class_name.reflections.with_indifferent_access[selection.name].options[:class_name]
3333
current_class_name = selection_name.singularize.classify.constantize
3434
dependencies[selection.name.to_sym] = map_dependencies(current_class_name, selection)
3535

spec/graphql/query_resolver_spec.rb

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,99 @@
66
TestData.create_netto
77
end
88

9-
it 'does something useful' do
9+
it 'groups queries' do
1010
data = nil
1111

1212
queries = track_queries do
1313
data = GQL.query(%{
1414
query {
1515
recipes {
16-
#title
16+
title
17+
ingredients { name }
18+
}
19+
}
20+
})
21+
end
22+
23+
expect(queries.size).to eq(2)
24+
expect(queries.first).to eq('SELECT "recipes".* FROM "recipes"')
25+
expect(queries.last).to eq('SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" IN (1, 2, 3, 4)')
26+
end
27+
28+
it 'works with multiple levels of nesting' do
29+
data = nil
1730

31+
queries = track_queries do
32+
data = GQL.query(%{
33+
query {
34+
recipes {
35+
title
1836
ingredients {
37+
name, quantity
38+
vendor { name }
39+
}
40+
}
41+
}
42+
})
43+
end
44+
45+
expect(queries.size).to eq(3)
46+
expect(queries[0]).to eq('SELECT "recipes".* FROM "recipes"')
47+
expect(queries[1]).to eq('SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" IN (1, 2, 3, 4)')
48+
expect(queries[2]).to eq('SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)')
49+
end
50+
51+
it 'issues one SELECT call per level' do
52+
data = nil
53+
54+
queries = track_queries do
55+
data = GQL.query(%{
56+
query {
57+
restaurant(id: 1) {
58+
name
59+
owner {
1960
name
20-
#vendor { name }
61+
recipes {
62+
title
63+
ingredients {
64+
name, quantity
65+
vendor {
66+
name
67+
}
68+
}
69+
}
2170
}
22-
}
71+
}
2372
}
2473
})
2574
end
2675

27-
puts queries
76+
expected_queries = [
77+
'SELECT "restaurants".* FROM "restaurants" WHERE "restaurants"."id" = ? LIMIT ?',
78+
'SELECT "chefs".* FROM "chefs" WHERE "chefs"."id" = 1',
79+
'SELECT "recipes".* FROM "recipes" WHERE "recipes"."chef_id" = 1',
80+
'SELECT "ingredients".* FROM "ingredients" WHERE "ingredients"."recipe_id" IN (1, 2, 3, 4)',
81+
'SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)'
82+
]
83+
84+
expect(queries).to eq(expected_queries)
85+
end
86+
87+
it 'works with alias reflections' do
88+
# Owner is an instance of Chef
89+
query = %{
90+
query {
91+
restaurant(id: 1) {
92+
name
93+
owner { name }
94+
}
95+
}
96+
}
97+
98+
queries = track_queries do
99+
GQL.query(query)
100+
end
28101

29-
expect(true).to eq(true)
102+
expect(queries.size).to eq(2)
30103
end
31104
end

spec/support/graphql_schema.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,7 @@
6868
field :id, types.ID
6969
field :name, types.String
7070
field :quantity, types.Int
71-
72-
field :vendor do
73-
type VendorType
74-
75-
resolve -> (obj, args, ctx) {
76-
obj.vendor
77-
}
78-
end
71+
field :vendor, VendorType
7972
end
8073

8174
QueryRoot = GraphQL::ObjectType.define do
@@ -98,7 +91,10 @@
9891

9992
resolve -> (obj, args, ctx) {
10093
id = args['id']
101-
Restaurant.find(id)
94+
95+
GraphQL::QueryResolver::run(Restaurant, ctx, RestaurantType) do
96+
Restaurant.find(id)
97+
end
10298
}
10399
end
104100
end

0 commit comments

Comments
 (0)