test-plugins.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { Injectable, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
  2. import { Query, Resolver } from '@nestjs/graphql';
  3. import { LanguageCode } from '@vendure/common/lib/generated-types';
  4. import gql from 'graphql-tag';
  5. import { VendureConfig } from '../../src/config';
  6. import { ConfigModule } from '../../src/config/config.module';
  7. import { ConfigService } from '../../src/config/config.service';
  8. import {
  9. OnVendureBootstrap,
  10. OnVendureClose,
  11. OnVendureWorkerBootstrap,
  12. OnVendureWorkerClose,
  13. VendurePlugin,
  14. } from '../../src/plugin/vendure-plugin';
  15. export class TestPluginWithAllLifecycleHooks
  16. implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
  17. private static onBootstrapFn: any;
  18. private static onWorkerBootstrapFn: any;
  19. private static onCloseFn: any;
  20. private static onWorkerCloseFn: any;
  21. static init(bootstrapFn: any, workerBootstrapFn: any, closeFn: any, workerCloseFn: any) {
  22. this.onBootstrapFn = bootstrapFn;
  23. this.onWorkerBootstrapFn = workerBootstrapFn;
  24. this.onCloseFn = closeFn;
  25. this.onWorkerCloseFn = workerCloseFn;
  26. return this;
  27. }
  28. onVendureBootstrap(): void | Promise<void> {
  29. TestPluginWithAllLifecycleHooks.onBootstrapFn();
  30. }
  31. onVendureWorkerBootstrap(): void | Promise<void> {
  32. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
  33. }
  34. onVendureClose(): void | Promise<void> {
  35. TestPluginWithAllLifecycleHooks.onCloseFn();
  36. }
  37. onVendureWorkerClose(): void | Promise<void> {
  38. TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
  39. }
  40. }
  41. @Resolver()
  42. export class TestAdminPluginResolver {
  43. @Query()
  44. foo() {
  45. return ['bar'];
  46. }
  47. }
  48. @Resolver()
  49. export class TestShopPluginResolver {
  50. @Query()
  51. baz() {
  52. return ['quux'];
  53. }
  54. }
  55. @VendurePlugin({
  56. shopApiExtensions: {
  57. resolvers: [TestShopPluginResolver],
  58. schema: gql`
  59. extend type Query {
  60. baz: [String]!
  61. }
  62. `,
  63. },
  64. adminApiExtensions: {
  65. resolvers: [TestAdminPluginResolver],
  66. schema: gql`
  67. extend type Query {
  68. foo: [String]!
  69. }
  70. `,
  71. },
  72. })
  73. export class TestAPIExtensionPlugin {}
  74. @Injectable()
  75. export class NameService {
  76. getNames(): string[] {
  77. return ['seon', 'linda', 'hong'];
  78. }
  79. }
  80. @Resolver()
  81. export class TestResolverWithInjection {
  82. constructor(private nameService: NameService) {}
  83. @Query()
  84. names() {
  85. return this.nameService.getNames();
  86. }
  87. }
  88. @VendurePlugin({
  89. providers: [NameService],
  90. shopApiExtensions: {
  91. resolvers: [TestResolverWithInjection],
  92. schema: gql`
  93. extend type Query {
  94. names: [String]!
  95. }
  96. `,
  97. },
  98. })
  99. export class TestPluginWithProvider {}
  100. @VendurePlugin({
  101. imports: [ConfigModule],
  102. configuration(config: Required<VendureConfig>): Required<VendureConfig> {
  103. // tslint:disable-next-line:no-non-null-assertion
  104. config.defaultLanguageCode = LanguageCode.zh;
  105. return config;
  106. },
  107. })
  108. export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap {
  109. private static boostrapWasCalled: any;
  110. static setup(boostrapWasCalled: (arg: any) => void) {
  111. TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
  112. return TestPluginWithConfigAndBootstrap;
  113. }
  114. constructor(private configService: ConfigService) {}
  115. onVendureBootstrap() {
  116. TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
  117. }
  118. }