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

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