Repository navigation
Expand file tree
/
Copy pathPeopleCompaniesAndProductsDataLoaderTest.groovy
More file actions
212 lines (176 loc) · 6.79 KB
/
Copy pathPeopleCompaniesAndProductsDataLoaderTest.groovy
File metadata and controls
212 lines (176 loc) · 6.79 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package graphql.execution.instrumentation.dataloader
import com.github.javafaker.Faker
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.TestUtil
import graphql.execution.instrumentation.dataloader.models.Company
import graphql.execution.instrumentation.dataloader.models.Person
import graphql.execution.instrumentation.dataloader.models.Product
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
import org.dataloader.BatchLoader
import org.dataloader.DataLoader
import org.dataloader.DataLoaderFactory
import org.dataloader.DataLoaderRegistry
import spock.lang.Specification
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.stream.Collectors
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring
class PeopleCompaniesAndProductsDataLoaderTest extends Specification {
int personBatchLoadInvocationCount = 0
int personBatchLoadKeyCount = 0
int companyBatchLoadInvocationCount = 0
int companyBatchLoadKeyCount = 0
void setup() {
personBatchLoadInvocationCount = 0
personBatchLoadKeyCount = 0
companyBatchLoadInvocationCount = 0
companyBatchLoadKeyCount = 0
}
Faker faker = new Faker()
String schema = """
type Company {
id: Int!
name: String!
}
type Person {
id: Int!
name: String!
company: Company!
}
type Product {
id: Int!
name: String!
suppliedBy: Person!
madeBy: [Person]
}
type QueryType {
products: [Product!]!
}
schema {
query: QueryType
}
"""
BatchLoader<Integer, Person> personBatchLoader = new BatchLoader<Integer, Person>() {
@Override
CompletionStage<List<Person>> load(List<Integer> keys) {
personBatchLoadInvocationCount += 1
personBatchLoadKeyCount += keys.size()
CompletableFuture.supplyAsync({
keys.stream()
.map({ id -> new Person(id, faker.name().fullName(), id + 200) })
.collect(Collectors.toList())
})
}
}
BatchLoader<Integer, Company> companyBatchLoader = new BatchLoader<Integer, Company>() {
@Override
CompletionStage<List<Company>> load(List<Integer> keys) {
companyBatchLoadInvocationCount += 1
companyBatchLoadKeyCount += keys.size()
CompletableFuture.supplyAsync({
return keys.stream()
.map({ id -> new Company(id, faker.company().name()) })
.collect(Collectors.toList())
})
}
}
DataFetcher productsDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) {
List<Product> products = new ArrayList<>();
for (int id = 0; id < 5; id++) {
List<Integer> madeBy = [id * 10001, id * 10002, id * 10003, id * 10004, id * 10005]
products.add(new Product(id.toString(), faker.commerce().productName(), id + 200, madeBy))
}
return products
}
}
DataFetcher suppliedByDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) {
Product source = environment.getSource()
DataLoaderRegistry dlRegistry = environment.getGraphQlContext().get("registry")
DataLoader<Integer, Person> personDL = dlRegistry.getDataLoader("person")
return personDL.load(source.getSuppliedById())
}
}
DataFetcher madeByDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) {
Product source = environment.getSource()
DataLoaderRegistry dlRegistry = environment.getGraphQlContext().get("registry")
DataLoader<Integer, Person> personDL = dlRegistry.getDataLoader("person")
return personDL.loadMany(source.getMadeByIds())
}
}
DataFetcher companyDF = new DataFetcher() {
@Override
Object get(DataFetchingEnvironment environment) {
Person source = environment.getSource()
DataLoaderRegistry dlRegistry = environment.getGraphQlContext().get("registry")
DataLoader<Integer, Company> companyDL = dlRegistry.getDataLoader("company")
return companyDL.load(source.getCompanyId())
}
}
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("QueryType", { builder -> builder.dataFetcher("products", productsDF) })
.type("Product", { builder -> builder.dataFetcher("suppliedBy", suppliedByDF) })
.type("Product", { builder -> builder.dataFetcher("madeBy", madeByDF) })
.type("Person", { builder -> builder.dataFetcher("company", companyDF) })
.build()
def graphQLSchema = TestUtil.schema(schema, runtimeWiring)
def query = """
query Products {
products {
id
name
suppliedBy {
id
name
company {
id
name
}
}
madeBy {
id
name
company {
id
name
}
}
}
}
"""
private DataLoaderRegistry buildRegistry() {
DataLoader<Integer, Person> personDataLoader = DataLoaderFactory.newDataLoader(personBatchLoader)
DataLoader<Integer, Company> companyDataLoader = DataLoaderFactory.newDataLoader(companyBatchLoader)
DataLoaderRegistry registry = new DataLoaderRegistry()
registry.register("person", personDataLoader)
registry.register("company", companyDataLoader)
return registry
}
def "ensure performant loading with field tracking"() {
DataLoaderRegistry registry = buildRegistry()
GraphQL graphQL = GraphQL
.newGraphQL(graphQLSchema)
.build()
when:
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(query)
.graphQLContext(["registry": registry])
.dataLoaderRegistry(registry)
.build()
def executionResult = graphQL.execute(executionInput)
then:
(executionResult.data["products"] as Map).size() == 5
personBatchLoadKeyCount == 26
personBatchLoadInvocationCount == 1
companyBatchLoadKeyCount == 26
companyBatchLoadInvocationCount == 1
}
}