|
3 | 3 | const assert = require('node:assert/strict') |
4 | 4 |
|
5 | 5 | const axios = require('axios') |
| 6 | +const sinon = require('sinon') |
6 | 7 |
|
7 | 8 | const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants.js') |
8 | 9 | const agent = require('../../dd-trace/test/plugins/agent.js') |
@@ -552,6 +553,161 @@ describe('Plugin', () => { |
552 | 553 | }) |
553 | 554 | }) |
554 | 555 | }) |
| 556 | + |
| 557 | + describe('with hooks configuration', () => { |
| 558 | + const config = { |
| 559 | + hooks: { |
| 560 | + request: sinon.spy((span, ctx) => {}), |
| 561 | + validate: sinon.spy((span, ctx) => {}), |
| 562 | + plan: sinon.spy((span, ctx) => {}), |
| 563 | + execute: sinon.spy((span, ctx) => {}), |
| 564 | + fetch: sinon.spy((span, ctx) => {}), |
| 565 | + postprocessing: sinon.spy((span, ctx) => {}), |
| 566 | + }, |
| 567 | + } |
| 568 | + |
| 569 | + before(() => agent.load('apollo', config)) |
| 570 | + before(() => setupFixtures()) |
| 571 | + before(() => setupApollo(version)) |
| 572 | + |
| 573 | + afterEach(() => Object.keys(config.hooks).forEach( |
| 574 | + key => config.hooks[key].resetHistory() |
| 575 | + )) |
| 576 | + |
| 577 | + it('should run request, plan, execute and postprocessing hooks before spans are finished', done => { |
| 578 | + const operationName = 'MyQuery' |
| 579 | + const source = `query ${operationName} { hello(name: "world") }` |
| 580 | + const variableValues = { who: 'world' } |
| 581 | + |
| 582 | + agent |
| 583 | + .assertSomeTraces((traces) => { |
| 584 | + sinon.assert.calledOnce(config.hooks.request) |
| 585 | + sinon.assert.calledOnce(config.hooks.plan) |
| 586 | + sinon.assert.calledOnce(config.hooks.execute) |
| 587 | + sinon.assert.calledOnce(config.hooks.postprocessing) |
| 588 | + |
| 589 | + const requestSpan = config.hooks.request.firstCall.args[0] |
| 590 | + const requestCtx = config.hooks.request.firstCall.args[1] |
| 591 | + const planSpan = config.hooks.plan.firstCall.args[0] |
| 592 | + const executeSpan = config.hooks.execute.firstCall.args[0] |
| 593 | + const postprocessingSpan = config.hooks.postprocessing.firstCall.args[0] |
| 594 | + |
| 595 | + assert.strictEqual(requestSpan.context()._name, expectedSchema.server.opName) |
| 596 | + assert.strictEqual(planSpan.context()._name, 'apollo.gateway.plan') |
| 597 | + assert.strictEqual(executeSpan.context()._name, 'apollo.gateway.execute') |
| 598 | + assert.strictEqual(postprocessingSpan.context()._name, 'apollo.gateway.postprocessing') |
| 599 | + assert.strictEqual(requestCtx.requestContext.operationName, operationName) |
| 600 | + }) |
| 601 | + .then(done) |
| 602 | + .catch(done) |
| 603 | + |
| 604 | + gateway() |
| 605 | + .then(({ executor }) => { |
| 606 | + return execute(executor, source, variableValues, operationName).then(() => {}) |
| 607 | + }) |
| 608 | + }) |
| 609 | + |
| 610 | + it('should run validate hook on validation failure and keep error tagging', done => { |
| 611 | + let error |
| 612 | + const source = `#graphql |
| 613 | + query InvalidVariables($first: Int!, $second: Int!) { |
| 614 | + topReviews(first: $first) { |
| 615 | + body |
| 616 | + } |
| 617 | + }` |
| 618 | + const variableValues = { who: 'world' } |
| 619 | + |
| 620 | + agent |
| 621 | + .assertSomeTraces((traces) => { |
| 622 | + sinon.assert.calledOnce(config.hooks.request) |
| 623 | + sinon.assert.calledOnce(config.hooks.validate) |
| 624 | + sinon.assert.notCalled(config.hooks.plan) |
| 625 | + sinon.assert.notCalled(config.hooks.execute) |
| 626 | + sinon.assert.notCalled(config.hooks.fetch) |
| 627 | + sinon.assert.notCalled(config.hooks.postprocessing) |
| 628 | + |
| 629 | + const validateSpan = config.hooks.validate.firstCall.args[0] |
| 630 | + const validateCtx = config.hooks.validate.firstCall.args[1] |
| 631 | + |
| 632 | + assert.strictEqual(validateSpan.context()._name, 'apollo.gateway.validate') |
| 633 | + assert.ok(Array.isArray(validateCtx.result)) |
| 634 | + assert.strictEqual(validateCtx.result.at(-1).message, error.message) |
| 635 | + |
| 636 | + assertObjectContains(traces[0][1], { |
| 637 | + name: 'apollo.gateway.validate', |
| 638 | + error: 1, |
| 639 | + meta: { |
| 640 | + [ERROR_TYPE]: error.name, |
| 641 | + [ERROR_MESSAGE]: error.message, |
| 642 | + [ERROR_STACK]: error.stack, |
| 643 | + }, |
| 644 | + }) |
| 645 | + }) |
| 646 | + .then(done) |
| 647 | + .catch(done) |
| 648 | + |
| 649 | + gateway() |
| 650 | + .then(({ executor }) => { |
| 651 | + return execute(executor, source, variableValues, 'InvalidVariables').then((result) => { |
| 652 | + error = result.errors[1] |
| 653 | + }) |
| 654 | + }) |
| 655 | + }) |
| 656 | + |
| 657 | + it('should run request and fetch hooks on fetch failure', done => { |
| 658 | + let error |
| 659 | + const operationName = 'MyQuery' |
| 660 | + const source = `query ${operationName} { hello(name: "world") }` |
| 661 | + const variableValues = { who: 'world' } |
| 662 | + |
| 663 | + agent |
| 664 | + .assertSomeTraces((traces) => { |
| 665 | + sinon.assert.calledOnce(config.hooks.request) |
| 666 | + sinon.assert.calledOnce(config.hooks.fetch) |
| 667 | + |
| 668 | + const requestSpan = config.hooks.request.firstCall.args[0] |
| 669 | + const requestCtx = config.hooks.request.firstCall.args[1] |
| 670 | + const fetchSpan = config.hooks.fetch.firstCall.args[0] |
| 671 | + const fetchCtx = config.hooks.fetch.firstCall.args[1] |
| 672 | + |
| 673 | + assert.strictEqual(requestSpan.context()._name, expectedSchema.server.opName) |
| 674 | + assert.strictEqual(fetchSpan.context()._name, 'apollo.gateway.fetch') |
| 675 | + assert.ok(requestCtx?.result?.errors?.length) |
| 676 | + assert.ok(fetchCtx?.error) |
| 677 | + |
| 678 | + assertObjectContains(traces[0][0], { |
| 679 | + name: expectedSchema.server.opName, |
| 680 | + error: 1, |
| 681 | + }) |
| 682 | + |
| 683 | + assertObjectContains(traces[0][4], { |
| 684 | + name: 'apollo.gateway.fetch', |
| 685 | + error: 1, |
| 686 | + meta: { |
| 687 | + [ERROR_TYPE]: error.name, |
| 688 | + [ERROR_MESSAGE]: error.message, |
| 689 | + [ERROR_STACK]: error.stack, |
| 690 | + }, |
| 691 | + }) |
| 692 | + }) |
| 693 | + .then(done) |
| 694 | + .catch(done) |
| 695 | + |
| 696 | + const gateway = new ApolloGateway({ |
| 697 | + localServiceList: fixtures, |
| 698 | + fetcher: () => { |
| 699 | + throw Error('Nooo') |
| 700 | + }, |
| 701 | + }) |
| 702 | + gateway.load().then(resp => { |
| 703 | + return execute(resp.executor, source, variableValues, operationName) |
| 704 | + .then((result) => { |
| 705 | + const errors = result.errors |
| 706 | + error = errors[errors.length - 1] |
| 707 | + }) |
| 708 | + }) |
| 709 | + }) |
| 710 | + }) |
555 | 711 | }) |
556 | 712 | }) |
557 | 713 | }) |
|
0 commit comments