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

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