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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe LooksAhead do
include GraphqlHelpers
let_it_be(:the_user) { create(:user) }
let_it_be(:label_a) { create(:label) }
let_it_be(:label_b) { create(:label) }
let_it_be(:issue_a) { create(:issue, author: the_user, labels: [label_a, label_b]) }
let_it_be(:issue_b) { create(:issue, author: the_user, labels: [label_a]) }
let_it_be(:issue_c) { create(:issue, author: the_user, labels: [label_b]) }
# Simplified schema to test lookahead
let_it_be(:schema) do
issues_resolver = Class.new(Resolvers::BaseResolver) do
include LooksAhead
def resolve_with_lookahead(**args)
apply_lookahead(object.issues)
end
def preloads
{ labels: [:labels] }
end
end
label = Class.new(GraphQL::Schema::Object) do
graphql_name 'Label'
field :id, Integer, null: false
end
issue = Class.new(GraphQL::Schema::Object) do
graphql_name 'Issue'
field :title, String, null: true
field :labels, label.connection_type, null: true
end
user = Class.new(GraphQL::Schema::Object) do
graphql_name 'User'
field :name, String, null: true
field :issues, issue.connection_type,
null: true
field :issues_with_lookahead, issue.connection_type,
extras: [:lookahead],
resolver: issues_resolver,
null: true
end
Class.new(GraphQL::Schema) do
query(Class.new(GraphQL::Schema::Object) do
graphql_name 'Query'
field :find_user, user, null: true do
argument :username, String, required: true
end
def find_user(username:)
context[:user_db].find { |u| u.username == username }
end
end)
end
end
def query(doc = document)
GraphQL::Query.new(schema,
document: doc,
context: { user_db: [the_user] },
variables: { username: the_user.username })
end
let(:document) do
GraphQL.parse <<-GRAPHQL
query($username: String!){
findUser(username: $username) {
name
issues {
nodes {
title
labels { nodes { id } }
}
}
issuesWithLookahead {
nodes {
title
labels { nodes { id } }
}
}
}
}
GRAPHQL
end
def run_query(gql_query)
query(GraphQL.parse(gql_query)).result
end
shared_examples 'a working query on the test schema' do
it 'has a good test setup', :aggregate_failures do
expected_label_ids = [label_a, label_b].cycle.take(4).map(&:id)
issue_titles = [issue_a, issue_b, issue_c].map(&:title)
res = query.result
expect(res['errors']).to be_blank
expect(res.dig('data', 'findUser', 'name')).to eq(the_user.name)
%w(issues issuesWithLookahead).each do |field|
expect(all_issue_titles(res, field)).to match_array(issue_titles)
expect(all_label_ids(res, field)).to match_array(expected_label_ids)
end
end
end
it_behaves_like 'a working query on the test schema'
it 'preloads labels on issues' do
expect(the_user.issues).to receive(:preload).with(:labels)
query.result
end
it 'issues fewer queries than the naive approach' do
the_user.reload # ensure no attributes are loaded before we begin
naive = <<-GQL
query($username: String!){
findUser(username: $username) {
name
issues {
nodes {
labels { nodes { id } }
}
}
}
}
GQL
with_lookahead = <<-GQL
query($username: String!){
findUser(username: $username) {
name
issuesWithLookahead {
nodes {
labels { nodes { id } }
}
}
}
}
GQL
expect { run_query(with_lookahead) }.to issue_fewer_queries_than { run_query(naive) }
end
private
def all_label_ids(result, field_name)
result.dig('data', 'findUser', field_name, 'nodes').flat_map do |node|
node.dig('labels', 'nodes').map { |n| n['id'] }
end
end
def all_issue_titles(result, field_name)
result.dig('data', 'findUser', field_name, 'nodes').map do |node|
node['title']
end
end
end
|