-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathloadSupergraphSdlFromStorage.test.ts
355 lines (305 loc) · 11.4 KB
/
loadSupergraphSdlFromStorage.test.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { loadSupergraphSdlFromStorage } from '../loadSupergraphSdlFromStorage';
import { getDefaultFetcher } from '../..';
import {
mockSupergraphSdlRequestSuccess,
graphRef,
apiKey,
mockCloudConfigUrl,
mockSupergraphSdlRequest,
} from './integration/nockMocks';
describe('loadSupergraphSdlFromStorage', () => {
it('fetches Supergraph SDL as expected', async () => {
mockSupergraphSdlRequestSuccess();
const fetcher = getDefaultFetcher();
const result = await loadSupergraphSdlFromStorage({
graphRef,
apiKey,
endpoint: mockCloudConfigUrl,
fetcher,
});
expect(result).toMatchInlineSnapshot(`
Object {
"id": "originalId-1234",
"supergraphSdl": "schema
@core(feature: \\"https://specs.apollo.dev/core/v0.1\\"),
@core(feature: \\"https://specs.apollo.dev/join/v0.1\\")
{
query: Query
mutation: Mutation
}
directive @core(feature: String!) repeatable on SCHEMA
directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet) on FIELD_DEFINITION
directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on OBJECT | INTERFACE
directive @join__owner(graph: join__Graph!) on OBJECT | INTERFACE
directive @join__graph(name: String!, url: String!) on ENUM_VALUE
directive @stream on FIELD
directive @transform(from: String!) on FIELD
union AccountType = PasswordAccount | SMSAccount
type Amazon {
referrer: String
}
union Body = Image | Text
type Book implements Product
@join__owner(graph: BOOKS)
@join__type(graph: BOOKS, key: \\"isbn\\")
@join__type(graph: INVENTORY, key: \\"isbn\\")
@join__type(graph: PRODUCT, key: \\"isbn\\")
@join__type(graph: REVIEWS, key: \\"isbn\\")
{
isbn: String! @join__field(graph: BOOKS)
title: String @join__field(graph: BOOKS)
year: Int @join__field(graph: BOOKS)
similarBooks: [Book]! @join__field(graph: BOOKS)
metadata: [MetadataOrError] @join__field(graph: BOOKS)
inStock: Boolean @join__field(graph: INVENTORY)
isCheckedOut: Boolean @join__field(graph: INVENTORY)
upc: String! @join__field(graph: PRODUCT)
sku: String! @join__field(graph: PRODUCT)
name(delimeter: String = \\" \\"): String @join__field(graph: PRODUCT, requires: \\"title year\\")
price: String @join__field(graph: PRODUCT)
details: ProductDetailsBook @join__field(graph: PRODUCT)
reviews: [Review] @join__field(graph: REVIEWS)
relatedReviews: [Review!]! @join__field(graph: REVIEWS, requires: \\"similarBooks{isbn}\\")
}
union Brand = Ikea | Amazon
type Car implements Vehicle
@join__owner(graph: PRODUCT)
@join__type(graph: PRODUCT, key: \\"id\\")
@join__type(graph: REVIEWS, key: \\"id\\")
{
id: String! @join__field(graph: PRODUCT)
description: String @join__field(graph: PRODUCT)
price: String @join__field(graph: PRODUCT)
retailPrice: String @join__field(graph: REVIEWS, requires: \\"price\\")
}
type Error {
code: Int
message: String
}
type Furniture implements Product
@join__owner(graph: PRODUCT)
@join__type(graph: PRODUCT, key: \\"upc\\")
@join__type(graph: PRODUCT, key: \\"sku\\")
@join__type(graph: INVENTORY, key: \\"sku\\")
@join__type(graph: REVIEWS, key: \\"upc\\")
{
upc: String! @join__field(graph: PRODUCT)
sku: String! @join__field(graph: PRODUCT)
name: String @join__field(graph: PRODUCT)
price: String @join__field(graph: PRODUCT)
brand: Brand @join__field(graph: PRODUCT)
metadata: [MetadataOrError] @join__field(graph: PRODUCT)
details: ProductDetailsFurniture @join__field(graph: PRODUCT)
inStock: Boolean @join__field(graph: INVENTORY)
isHeavy: Boolean @join__field(graph: INVENTORY)
reviews: [Review] @join__field(graph: REVIEWS)
}
type Ikea {
asile: Int
}
type Image implements NamedObject {
name: String!
attributes: ImageAttributes!
}
type ImageAttributes {
url: String!
}
scalar join__FieldSet
enum join__Graph {
ACCOUNTS @join__graph(name: \\"accounts\\" url: \\"https://accounts.api.com\\")
BOOKS @join__graph(name: \\"books\\" url: \\"https://books.api.com\\")
DOCUMENTS @join__graph(name: \\"documents\\" url: \\"https://documents.api.com\\")
INVENTORY @join__graph(name: \\"inventory\\" url: \\"https://inventory.api.com\\")
PRODUCT @join__graph(name: \\"product\\" url: \\"https://product.api.com\\")
REVIEWS @join__graph(name: \\"reviews\\" url: \\"https://reviews.api.com\\")
}
type KeyValue {
key: String!
value: String!
}
type Library
@join__owner(graph: BOOKS)
@join__type(graph: BOOKS, key: \\"id\\")
@join__type(graph: ACCOUNTS, key: \\"id\\")
{
id: ID! @join__field(graph: BOOKS)
name: String @join__field(graph: BOOKS)
userAccount(id: ID! = 1): User @join__field(graph: ACCOUNTS, requires: \\"name\\")
}
union MetadataOrError = KeyValue | Error
type Mutation {
login(username: String!, password: String!): User @join__field(graph: ACCOUNTS)
reviewProduct(upc: String!, body: String!): Product @join__field(graph: REVIEWS)
updateReview(review: UpdateReviewInput!): Review @join__field(graph: REVIEWS)
deleteReview(id: ID!): Boolean @join__field(graph: REVIEWS)
}
type Name {
first: String
last: String
}
interface NamedObject {
name: String!
}
type PasswordAccount
@join__owner(graph: ACCOUNTS)
@join__type(graph: ACCOUNTS, key: \\"email\\")
{
email: String! @join__field(graph: ACCOUNTS)
}
interface Product {
upc: String!
sku: String!
name: String
price: String
details: ProductDetails
inStock: Boolean
reviews: [Review]
}
interface ProductDetails {
country: String
}
type ProductDetailsBook implements ProductDetails {
country: String
pages: Int
}
type ProductDetailsFurniture implements ProductDetails {
country: String
color: String
}
type Query {
user(id: ID!): User @join__field(graph: ACCOUNTS)
me: User @join__field(graph: ACCOUNTS)
book(isbn: String!): Book @join__field(graph: BOOKS)
books: [Book] @join__field(graph: BOOKS)
library(id: ID!): Library @join__field(graph: BOOKS)
body: Body! @join__field(graph: DOCUMENTS)
product(upc: String!): Product @join__field(graph: PRODUCT)
vehicle(id: String!): Vehicle @join__field(graph: PRODUCT)
topProducts(first: Int = 5): [Product] @join__field(graph: PRODUCT)
topCars(first: Int = 5): [Car] @join__field(graph: PRODUCT)
topReviews(first: Int = 5): [Review] @join__field(graph: REVIEWS)
}
type Review
@join__owner(graph: REVIEWS)
@join__type(graph: REVIEWS, key: \\"id\\")
{
id: ID! @join__field(graph: REVIEWS)
body(format: Boolean = false): String @join__field(graph: REVIEWS)
author: User @join__field(graph: REVIEWS, provides: \\"username\\")
product: Product @join__field(graph: REVIEWS)
metadata: [MetadataOrError] @join__field(graph: REVIEWS)
}
type SMSAccount
@join__owner(graph: ACCOUNTS)
@join__type(graph: ACCOUNTS, key: \\"number\\")
{
number: String @join__field(graph: ACCOUNTS)
}
type Text implements NamedObject {
name: String!
attributes: TextAttributes!
}
type TextAttributes {
bold: Boolean
text: String
}
union Thing = Car | Ikea
input UpdateReviewInput {
id: ID!
body: String
}
type User
@join__owner(graph: ACCOUNTS)
@join__type(graph: ACCOUNTS, key: \\"id\\")
@join__type(graph: ACCOUNTS, key: \\"username name{first last}\\")
@join__type(graph: INVENTORY, key: \\"id\\")
@join__type(graph: PRODUCT, key: \\"id\\")
@join__type(graph: REVIEWS, key: \\"id\\")
{
id: ID! @join__field(graph: ACCOUNTS)
name: Name @join__field(graph: ACCOUNTS)
username: String @join__field(graph: ACCOUNTS)
birthDate(locale: String): String @join__field(graph: ACCOUNTS)
account: AccountType @join__field(graph: ACCOUNTS)
metadata: [UserMetadata] @join__field(graph: ACCOUNTS)
goodDescription: Boolean @join__field(graph: INVENTORY, requires: \\"metadata{description}\\")
vehicle: Vehicle @join__field(graph: PRODUCT)
thing: Thing @join__field(graph: PRODUCT)
reviews: [Review] @join__field(graph: REVIEWS)
numberOfReviews: Int! @join__field(graph: REVIEWS)
goodAddress: Boolean @join__field(graph: REVIEWS, requires: \\"metadata{address}\\")
}
type UserMetadata {
name: String
address: String
description: String
}
type Van implements Vehicle
@join__owner(graph: PRODUCT)
@join__type(graph: PRODUCT, key: \\"id\\")
@join__type(graph: REVIEWS, key: \\"id\\")
{
id: String! @join__field(graph: PRODUCT)
description: String @join__field(graph: PRODUCT)
price: String @join__field(graph: PRODUCT)
retailPrice: String @join__field(graph: REVIEWS, requires: \\"price\\")
}
interface Vehicle {
id: String!
description: String
price: String
retailPrice: String
}
",
}
`);
});
describe('errors', () => {
it('throws on a malformed response', async () => {
mockSupergraphSdlRequest().reply(200, 'Invalid JSON');
const fetcher = getDefaultFetcher();
await expect(
loadSupergraphSdlFromStorage({
graphRef,
apiKey,
endpoint: mockCloudConfigUrl,
fetcher,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"An error occurred while fetching your schema from Apollo: 200 invalid json response body at https://example.cloud-config-url.com/cloudconfig/ reason: Unexpected token I in JSON at position 0"`,
);
});
it('throws errors from JSON on 400', async () => {
const message = 'Query syntax error';
mockSupergraphSdlRequest().reply(
400,
JSON.stringify({
errors: [{ message }],
}),
);
const fetcher = getDefaultFetcher();
await expect(
loadSupergraphSdlFromStorage({
graphRef,
apiKey,
endpoint: mockCloudConfigUrl,
fetcher,
}),
).rejects.toThrowError(message);
});
it("throws on non-OK status codes when `errors` isn't present in a JSON response", async () => {
mockSupergraphSdlRequest().reply(500);
const fetcher = getDefaultFetcher();
await expect(
loadSupergraphSdlFromStorage({
graphRef,
apiKey,
endpoint: mockCloudConfigUrl,
fetcher,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"An error occurred while fetching your schema from Apollo: 500 Internal Server Error"`,
);
});
});
});