-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_spec.rb
80 lines (68 loc) · 2.26 KB
/
query_spec.rb
1
2
3
4
5
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'rails_helper'
RSpec.describe 'Query tests' do
def execute_query(query)
Schema.execute(
query,
variables: {},
context: {},
).to_h.with_indifferent_access
end
# This scenario shouldn't really work because you can't load a bunch of posts for a bunch of users
# AND paginate those posts. The SQL query just doesn't make sense.
it 'users and posts' do
user_one = FactoryBot.create(:user)
user_two = FactoryBot.create(:user)
FactoryBot.create_list(:post, 3, user: user_one)
FactoryBot.create_list(:post, 3, user: user_two)
query = <<~GRAPHQL
query {
users {
nodes {
posts(first: 2) {
nodes { id }
}
}
}
}
GRAPHQL
p execute_query(query)
end
# This scenario paginates the posts for one user, but it does result in an extra query:
# one that is unpaginated and fetches all posts for a user,
# and another that is paginated and fetches all posts for a user
it 'user and posts, with batch loading' do
user_one = FactoryBot.create(:user)
FactoryBot.create_list(:post, 3, user: user_one)
query = <<~GRAPHQL
query {
user {
postsWithBatchLoading(first: 2) {
nodes { id }
}
}
}
GRAPHQL
p execute_query(query)
# generated SQL:
# User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
# Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 1]]
# Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 2]]
end
it 'user and posts, without batch loading' do
user_one = FactoryBot.create(:user)
FactoryBot.create_list(:post, 3, user: user_one)
query = <<~GRAPHQL
query {
user {
postsWithoutBatchLoading(first: 2) {
nodes { id }
}
}
}
GRAPHQL
p execute_query(query)
# generated SQL:
# User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
# Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 2]]
end
end