apollo-server-plugin.e2e-spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { mergeConfig } from '@vendure/core';
  2. import {
  3. ApolloServerPlugin,
  4. GraphQLRequestContext,
  5. GraphQLRequestListener,
  6. GraphQLServiceContext,
  7. } from 'apollo-server-plugin-base';
  8. import gql from 'graphql-tag';
  9. import path from 'path';
  10. import { initialData } from '../../../e2e-common/e2e-initial-data';
  11. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  12. import { createTestEnvironment } from '../../testing/lib/create-test-environment';
  13. class MyApolloServerPlugin implements ApolloServerPlugin {
  14. static serverWillStartFn = jest.fn();
  15. static requestDidStartFn = jest.fn();
  16. static willSendResponseFn = jest.fn();
  17. static reset() {
  18. this.serverWillStartFn = jest.fn();
  19. this.requestDidStartFn = jest.fn();
  20. this.willSendResponseFn = jest.fn();
  21. }
  22. serverWillStart(service: GraphQLServiceContext): Promise<void> | void {
  23. MyApolloServerPlugin.serverWillStartFn(service);
  24. }
  25. requestDidStart(): GraphQLRequestListener | void {
  26. MyApolloServerPlugin.requestDidStartFn();
  27. return {
  28. willSendResponse(requestContext: any): Promise<void> | void {
  29. const data = requestContext.response.data;
  30. MyApolloServerPlugin.willSendResponseFn(data);
  31. },
  32. };
  33. }
  34. }
  35. describe('custom apolloServerPlugins', () => {
  36. const { server, adminClient, shopClient } = createTestEnvironment(
  37. mergeConfig(testConfig(), {
  38. apiOptions: {
  39. apolloServerPlugins: [new MyApolloServerPlugin()],
  40. },
  41. }),
  42. );
  43. beforeAll(async () => {
  44. await server.init({
  45. initialData,
  46. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  47. customerCount: 1,
  48. });
  49. await adminClient.asSuperAdmin();
  50. }, TEST_SETUP_TIMEOUT_MS);
  51. afterAll(async () => {
  52. await server.destroy();
  53. });
  54. it('calls serverWillStart()', () => {
  55. expect(MyApolloServerPlugin.serverWillStartFn).toHaveBeenCalled();
  56. });
  57. it('runs plugin on shop api query', async () => {
  58. MyApolloServerPlugin.reset();
  59. await shopClient.query(gql`
  60. query Q1 {
  61. product(id: "T_1") {
  62. id
  63. name
  64. }
  65. }
  66. `);
  67. expect(MyApolloServerPlugin.requestDidStartFn).toHaveBeenCalledTimes(1);
  68. expect(MyApolloServerPlugin.willSendResponseFn).toHaveBeenCalledTimes(1);
  69. expect(MyApolloServerPlugin.willSendResponseFn.mock.calls[0][0]).toEqual({
  70. product: {
  71. id: 'T_1',
  72. name: 'Laptop',
  73. },
  74. });
  75. });
  76. it('runs plugin on admin api query', async () => {
  77. MyApolloServerPlugin.reset();
  78. await adminClient.query(gql`
  79. query Q2 {
  80. product(id: "T_1") {
  81. id
  82. name
  83. }
  84. }
  85. `);
  86. expect(MyApolloServerPlugin.requestDidStartFn).toHaveBeenCalledTimes(1);
  87. expect(MyApolloServerPlugin.willSendResponseFn).toHaveBeenCalledTimes(1);
  88. expect(MyApolloServerPlugin.willSendResponseFn.mock.calls[0][0]).toEqual({
  89. product: {
  90. id: 'T_1',
  91. name: 'Laptop',
  92. },
  93. });
  94. });
  95. });