|
1 |
| -import { expect } from 'chai'; |
| 1 | +import { assert, expect } from 'chai'; |
2 | 2 | import { describe, it } from 'mocha';
|
3 | 3 |
|
4 | 4 | import { expectJSON } from '../../__testUtils__/expectJSON.js';
|
5 | 5 | import { expectPromise } from '../../__testUtils__/expectPromise.js';
|
6 | 6 | import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';
|
7 | 7 |
|
| 8 | +import { isPromise } from '../../jsutils/isPromise.js'; |
| 9 | + |
8 | 10 | import type { DocumentNode } from '../../language/ast.js';
|
| 11 | +import { Kind } from '../../language/kinds.js'; |
9 | 12 | import { parse } from '../../language/parser.js';
|
10 | 13 |
|
| 14 | +import type { FieldDetails } from '../../type/definition.js'; |
11 | 15 | import {
|
12 | 16 | GraphQLList,
|
13 | 17 | GraphQLNonNull,
|
@@ -226,6 +230,174 @@ describe('Execute: defer directive', () => {
|
226 | 230 | },
|
227 | 231 | });
|
228 | 232 | });
|
| 233 | + it('Can provides correct info about deferred execution state when resolver could defer', async () => { |
| 234 | + let fieldDetails: ReadonlyArray<FieldDetails> | undefined; |
| 235 | + let deferPriority; |
| 236 | + let published; |
| 237 | + let resumed; |
| 238 | + |
| 239 | + const SomeType = new GraphQLObjectType({ |
| 240 | + name: 'SomeType', |
| 241 | + fields: { |
| 242 | + someField: { |
| 243 | + type: GraphQLString, |
| 244 | + resolve: () => Promise.resolve('someField'), |
| 245 | + }, |
| 246 | + deferredField: { |
| 247 | + type: GraphQLString, |
| 248 | + resolve: async (_parent, _args, _context, info) => { |
| 249 | + fieldDetails = info.fieldDetails; |
| 250 | + deferPriority = info.deferPriority; |
| 251 | + published = info.published; |
| 252 | + await published; |
| 253 | + resumed = true; |
| 254 | + }, |
| 255 | + }, |
| 256 | + }, |
| 257 | + }); |
| 258 | + |
| 259 | + const someSchema = new GraphQLSchema({ query: SomeType }); |
| 260 | + |
| 261 | + const document = parse(` |
| 262 | + query { |
| 263 | + someField |
| 264 | + ... @defer { |
| 265 | + deferredField |
| 266 | + } |
| 267 | + } |
| 268 | + `); |
| 269 | + |
| 270 | + const operation = document.definitions[0]; |
| 271 | + assert(operation.kind === Kind.OPERATION_DEFINITION); |
| 272 | + const fragment = operation.selectionSet.selections[1]; |
| 273 | + assert(fragment.kind === Kind.INLINE_FRAGMENT); |
| 274 | + const field = fragment.selectionSet.selections[0]; |
| 275 | + |
| 276 | + const result = experimentalExecuteIncrementally({ |
| 277 | + schema: someSchema, |
| 278 | + document, |
| 279 | + }); |
| 280 | + |
| 281 | + expect(fieldDetails).to.equal(undefined); |
| 282 | + expect(deferPriority).to.equal(undefined); |
| 283 | + expect(published).to.equal(undefined); |
| 284 | + expect(resumed).to.equal(undefined); |
| 285 | + |
| 286 | + const initialPayload = await result; |
| 287 | + assert('initialResult' in initialPayload); |
| 288 | + const iterator = initialPayload.subsequentResults[Symbol.asyncIterator](); |
| 289 | + await iterator.next(); |
| 290 | + |
| 291 | + assert(fieldDetails !== undefined); |
| 292 | + expect(fieldDetails[0].node).to.equal(field); |
| 293 | + expect(fieldDetails[0].target?.priority).to.equal(1); |
| 294 | + expect(deferPriority).to.equal(1); |
| 295 | + expect(isPromise(published)).to.equal(true); |
| 296 | + expect(resumed).to.equal(true); |
| 297 | + }); |
| 298 | + it('Can provides correct info about deferred execution state when deferred field is masked by non-deferred field', async () => { |
| 299 | + let fieldDetails: ReadonlyArray<FieldDetails> | undefined; |
| 300 | + let deferPriority; |
| 301 | + let published; |
| 302 | + |
| 303 | + const SomeType = new GraphQLObjectType({ |
| 304 | + name: 'SomeType', |
| 305 | + fields: { |
| 306 | + someField: { |
| 307 | + type: GraphQLString, |
| 308 | + resolve: (_parent, _args, _context, info) => { |
| 309 | + fieldDetails = info.fieldDetails; |
| 310 | + deferPriority = info.deferPriority; |
| 311 | + published = info.published; |
| 312 | + return 'someField'; |
| 313 | + }, |
| 314 | + }, |
| 315 | + }, |
| 316 | + }); |
| 317 | + |
| 318 | + const someSchema = new GraphQLSchema({ query: SomeType }); |
| 319 | + |
| 320 | + const document = parse(` |
| 321 | + query { |
| 322 | + someField |
| 323 | + ... @defer { |
| 324 | + someField |
| 325 | + } |
| 326 | + } |
| 327 | + `); |
| 328 | + |
| 329 | + const operation = document.definitions[0]; |
| 330 | + assert(operation.kind === Kind.OPERATION_DEFINITION); |
| 331 | + const node1 = operation.selectionSet.selections[0]; |
| 332 | + const fragment = operation.selectionSet.selections[1]; |
| 333 | + assert(fragment.kind === Kind.INLINE_FRAGMENT); |
| 334 | + const node2 = fragment.selectionSet.selections[0]; |
| 335 | + |
| 336 | + const result = experimentalExecuteIncrementally({ |
| 337 | + schema: someSchema, |
| 338 | + document, |
| 339 | + }); |
| 340 | + |
| 341 | + const initialPayload = await result; |
| 342 | + assert('initialResult' in initialPayload); |
| 343 | + expect(initialPayload.initialResult).to.deep.equal({ |
| 344 | + data: { |
| 345 | + someField: 'someField', |
| 346 | + }, |
| 347 | + pending: [{ path: [] }], |
| 348 | + hasNext: true, |
| 349 | + }); |
| 350 | + |
| 351 | + assert(fieldDetails !== undefined); |
| 352 | + expect(fieldDetails[0].node).to.equal(node1); |
| 353 | + expect(fieldDetails[0].target).to.equal(undefined); |
| 354 | + expect(fieldDetails[1].node).to.equal(node2); |
| 355 | + expect(fieldDetails[1].target?.priority).to.equal(1); |
| 356 | + expect(deferPriority).to.equal(0); |
| 357 | + expect(published).to.equal(true); |
| 358 | + }); |
| 359 | + it('Can provides correct info about deferred execution state when resolver need not defer', async () => { |
| 360 | + let deferPriority; |
| 361 | + let published; |
| 362 | + const SomeType = new GraphQLObjectType({ |
| 363 | + name: 'SomeType', |
| 364 | + fields: { |
| 365 | + deferredField: { |
| 366 | + type: GraphQLString, |
| 367 | + resolve: (_parent, _args, _context, info) => { |
| 368 | + deferPriority = info.deferPriority; |
| 369 | + published = info.published; |
| 370 | + }, |
| 371 | + }, |
| 372 | + }, |
| 373 | + }); |
| 374 | + |
| 375 | + const someSchema = new GraphQLSchema({ query: SomeType }); |
| 376 | + |
| 377 | + const document = parse(` |
| 378 | + query { |
| 379 | + ... @defer { |
| 380 | + deferredField |
| 381 | + } |
| 382 | + } |
| 383 | + `); |
| 384 | + |
| 385 | + const result = experimentalExecuteIncrementally({ |
| 386 | + schema: someSchema, |
| 387 | + document, |
| 388 | + }); |
| 389 | + |
| 390 | + expect(deferPriority).to.equal(undefined); |
| 391 | + expect(published).to.equal(undefined); |
| 392 | + |
| 393 | + const initialPayload = await result; |
| 394 | + assert('initialResult' in initialPayload); |
| 395 | + const iterator = initialPayload.subsequentResults[Symbol.asyncIterator](); |
| 396 | + await iterator.next(); |
| 397 | + |
| 398 | + expect(deferPriority).to.equal(1); |
| 399 | + expect(published).to.equal(true); |
| 400 | + }); |
229 | 401 | it('Does not disable defer with null if argument', async () => {
|
230 | 402 | const document = parse(`
|
231 | 403 | query HeroNameQuery($shouldDefer: Boolean) {
|
|
0 commit comments