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

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