|
1 | 1 | import { GraphQLError } from 'graphql';
|
2 |
| -import { createSchema, createYoga, Repeater } from '../src/index.js'; |
3 |
| - |
4 |
| -function eventStream<TType = unknown>(source: ReadableStream<Uint8Array>) { |
5 |
| - return new Repeater<TType>(async (push, end) => { |
6 |
| - const cancel: Promise<{ done: true }> = end.then(() => ({ done: true })); |
7 |
| - const iterable = source[Symbol.asyncIterator](); |
8 |
| - // eslint-disable-next-line no-constant-condition |
9 |
| - while (true) { |
10 |
| - const result = await Promise.race([cancel, iterable.next()]); |
11 |
| - |
12 |
| - if (result.done) { |
13 |
| - break; |
14 |
| - } |
15 |
| - |
16 |
| - const values = result.value.toString().split('\n\n').filter(Boolean); |
17 |
| - for (const value of values) { |
18 |
| - if (!value.startsWith('data: ')) { |
19 |
| - continue; |
20 |
| - } |
21 |
| - const result = value.replace('data: ', ''); |
22 |
| - push(JSON.parse(result)); |
23 |
| - } |
24 |
| - } |
25 |
| - |
26 |
| - iterable.return?.(); |
27 |
| - end(); |
28 |
| - }); |
29 |
| -} |
| 2 | +import { createSchema, createYoga, maskError, Plugin } from '../src/index.js'; |
| 3 | +import { eventStream } from './utilities.js'; |
30 | 4 |
|
31 | 5 | describe('Subscription', () => {
|
32 | 6 | test('eventStream', async () => {
|
@@ -85,6 +59,10 @@ describe('Subscription', () => {
|
85 | 59 | counter++;
|
86 | 60 | }
|
87 | 61 | }
|
| 62 | + |
| 63 | + if (counter !== 3) { |
| 64 | + throw new Error('Did not receive all events'); |
| 65 | + } |
88 | 66 | });
|
89 | 67 |
|
90 | 68 | test('should issue pings while connected', async () => {
|
@@ -318,6 +296,69 @@ event: complete
|
318 | 296 | `);
|
319 | 297 | });
|
320 | 298 |
|
| 299 | + test('erroring event stream should be handled (non GraphQL error; disabled error masking)', async () => { |
| 300 | + const schema = createSchema({ |
| 301 | + typeDefs: /* GraphQL */ ` |
| 302 | + type Subscription { |
| 303 | + hi: String! |
| 304 | + } |
| 305 | + type Query { |
| 306 | + hi: String! |
| 307 | + } |
| 308 | + `, |
| 309 | + resolvers: { |
| 310 | + Subscription: { |
| 311 | + hi: { |
| 312 | + async *subscribe() { |
| 313 | + yield { hi: 'hi' }; |
| 314 | + throw new Error('hi'); |
| 315 | + }, |
| 316 | + }, |
| 317 | + }, |
| 318 | + }, |
| 319 | + }); |
| 320 | + |
| 321 | + const logging = { |
| 322 | + debug: jest.fn(), |
| 323 | + info: jest.fn(), |
| 324 | + warn: jest.fn(), |
| 325 | + error: jest.fn(), |
| 326 | + }; |
| 327 | + |
| 328 | + const yoga = createYoga({ schema, logging, maskedErrors: false }); |
| 329 | + const response = await yoga.fetch('http://yoga/graphql', { |
| 330 | + method: 'POST', |
| 331 | + headers: { |
| 332 | + 'content-type': 'application/json', |
| 333 | + accept: 'text/event-stream', |
| 334 | + }, |
| 335 | + body: JSON.stringify({ |
| 336 | + query: /* GraphQL */ ` |
| 337 | + subscription { |
| 338 | + hi |
| 339 | + } |
| 340 | + `, |
| 341 | + }), |
| 342 | + }); |
| 343 | + const text = await response.text(); |
| 344 | + |
| 345 | + expect(text).toMatchInlineSnapshot(` |
| 346 | +": |
| 347 | +
|
| 348 | +event: next |
| 349 | +data: {"data":{"hi":"hi"}} |
| 350 | +
|
| 351 | +event: next |
| 352 | +data: {"errors":[{"message":"hi","locations":[{"line":2,"column":11}]}]} |
| 353 | +
|
| 354 | +event: complete |
| 355 | +
|
| 356 | +" |
| 357 | +`); |
| 358 | + // errors are only logged when error masking is enabled |
| 359 | + expect(logging.error).toBeCalledTimes(0); |
| 360 | + }); |
| 361 | + |
321 | 362 | test('erroring event stream should be handled (GraphQL error)', async () => {
|
322 | 363 | const schema = createSchema({
|
323 | 364 | typeDefs: /* GraphQL */ `
|
@@ -382,6 +423,177 @@ event: complete
|
382 | 423 | });
|
383 | 424 | });
|
384 | 425 |
|
| 426 | +describe('subscription plugin hooks', () => { |
| 427 | + test('onNext and onEnd is invoked for event source', async () => { |
| 428 | + const source = (async function* foo() { |
| 429 | + yield { hi: 'hi' }; |
| 430 | + yield { hi: 'hello' }; |
| 431 | + })(); |
| 432 | + |
| 433 | + const schema = createSchema({ |
| 434 | + typeDefs: /* GraphQL */ ` |
| 435 | + type Subscription { |
| 436 | + hi: String! |
| 437 | + } |
| 438 | + type Query { |
| 439 | + hi: String! |
| 440 | + } |
| 441 | + `, |
| 442 | + resolvers: { |
| 443 | + Subscription: { |
| 444 | + hi: { |
| 445 | + subscribe: () => source, |
| 446 | + }, |
| 447 | + }, |
| 448 | + }, |
| 449 | + }); |
| 450 | + |
| 451 | + const onNextCalls: unknown[] = []; |
| 452 | + let didInvokeOnEnd = false; |
| 453 | + |
| 454 | + const plugin: Plugin = { |
| 455 | + onSubscribe() { |
| 456 | + return { |
| 457 | + onSubscribeResult() { |
| 458 | + return { |
| 459 | + onNext(ctx) { |
| 460 | + onNextCalls.push(ctx.result); |
| 461 | + }, |
| 462 | + onEnd() { |
| 463 | + expect(onNextCalls).toHaveLength(2); |
| 464 | + didInvokeOnEnd = true; |
| 465 | + }, |
| 466 | + }; |
| 467 | + }, |
| 468 | + }; |
| 469 | + }, |
| 470 | + }; |
| 471 | + |
| 472 | + const yoga = createYoga({ schema, plugins: [plugin] }); |
| 473 | + |
| 474 | + const response = await yoga.fetch('http://yoga/graphql', { |
| 475 | + method: 'POST', |
| 476 | + headers: { |
| 477 | + 'content-type': 'application/json', |
| 478 | + accept: 'text/event-stream', |
| 479 | + }, |
| 480 | + body: JSON.stringify({ |
| 481 | + query: /* GraphQL */ ` |
| 482 | + subscription { |
| 483 | + hi |
| 484 | + } |
| 485 | + `, |
| 486 | + }), |
| 487 | + }); |
| 488 | + |
| 489 | + let counter = 0; |
| 490 | + |
| 491 | + for await (const chunk of eventStream(response.body!)) { |
| 492 | + if (counter === 0) { |
| 493 | + expect(chunk).toEqual({ data: { hi: 'hi' } }); |
| 494 | + counter++; |
| 495 | + } else if (counter === 1) { |
| 496 | + expect(chunk).toEqual({ data: { hi: 'hello' } }); |
| 497 | + counter++; |
| 498 | + } |
| 499 | + } |
| 500 | + |
| 501 | + expect(counter).toBe(2); |
| 502 | + expect(onNextCalls).toEqual([{ data: { hi: 'hi' } }, { data: { hi: 'hello' } }]); |
| 503 | + expect(didInvokeOnEnd).toBe(true); |
| 504 | + }); |
| 505 | + |
| 506 | + test('onSubscribeError and onEnd is invoked if error is thrown from event source', async () => { |
| 507 | + const source = (async function* foo() { |
| 508 | + yield { hi: 'hi' }; |
| 509 | + throw new GraphQLError('hi'); |
| 510 | + })(); |
| 511 | + |
| 512 | + const schema = createSchema({ |
| 513 | + typeDefs: /* GraphQL */ ` |
| 514 | + type Subscription { |
| 515 | + hi: String! |
| 516 | + } |
| 517 | + type Query { |
| 518 | + hi: String! |
| 519 | + } |
| 520 | + `, |
| 521 | + resolvers: { |
| 522 | + Subscription: { |
| 523 | + hi: { |
| 524 | + subscribe: () => source, |
| 525 | + }, |
| 526 | + }, |
| 527 | + }, |
| 528 | + }); |
| 529 | + |
| 530 | + let onNextCallCounter = 0; |
| 531 | + let didInvokeOnEnd = false; |
| 532 | + let didInvokeOnSubscribeError = false; |
| 533 | + |
| 534 | + const plugin: Plugin = { |
| 535 | + onSubscribe() { |
| 536 | + return { |
| 537 | + onSubscribeError() { |
| 538 | + didInvokeOnSubscribeError = true; |
| 539 | + }, |
| 540 | + onSubscribeResult() { |
| 541 | + return { |
| 542 | + onNext() { |
| 543 | + onNextCallCounter++; |
| 544 | + }, |
| 545 | + onEnd() { |
| 546 | + expect(onNextCallCounter).toEqual(1); |
| 547 | + didInvokeOnEnd = true; |
| 548 | + }, |
| 549 | + }; |
| 550 | + }, |
| 551 | + }; |
| 552 | + }, |
| 553 | + }; |
| 554 | + |
| 555 | + const maskErrorFn = jest.fn(maskError); |
| 556 | + const yoga = createYoga({ |
| 557 | + schema, |
| 558 | + plugins: [plugin], |
| 559 | + maskedErrors: { maskError: maskErrorFn }, |
| 560 | + }); |
| 561 | + |
| 562 | + const response = await yoga.fetch('http://yoga/graphql', { |
| 563 | + method: 'POST', |
| 564 | + headers: { |
| 565 | + 'content-type': 'application/json', |
| 566 | + accept: 'text/event-stream', |
| 567 | + }, |
| 568 | + body: JSON.stringify({ |
| 569 | + query: /* GraphQL */ ` |
| 570 | + subscription { |
| 571 | + hi |
| 572 | + } |
| 573 | + `, |
| 574 | + }), |
| 575 | + }); |
| 576 | + |
| 577 | + let counter = 0; |
| 578 | + |
| 579 | + for await (const chunk of eventStream(response.body!)) { |
| 580 | + if (counter === 0) { |
| 581 | + expect(chunk).toEqual({ data: { hi: 'hi' } }); |
| 582 | + counter++; |
| 583 | + } else if (counter === 1) { |
| 584 | + expect(chunk).toMatchObject({ errors: [{ message: 'hi' }] }); |
| 585 | + counter++; |
| 586 | + } |
| 587 | + } |
| 588 | + |
| 589 | + expect(counter).toBe(2); |
| 590 | + expect(onNextCallCounter).toEqual(1); |
| 591 | + expect(didInvokeOnEnd).toBe(true); |
| 592 | + expect(didInvokeOnSubscribeError).toBe(true); |
| 593 | + expect(maskErrorFn).toBeCalledTimes(1); |
| 594 | + }); |
| 595 | +}); |
| 596 | + |
385 | 597 | type Deferred<T = void> = {
|
386 | 598 | resolve: (value: T) => void;
|
387 | 599 | reject: (value: unknown) => void;
|
|
0 commit comments