test-plugins.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. @Query()
  48. barList() {
  49. return {
  50. items: [{ id: 1, name: 'Test' }],
  51. totalItems: 1,
  52. };
  53. }
  54. }
  55. @Resolver()
  56. export class TestShopPluginResolver {
  57. @Query()
  58. baz() {
  59. return ['quux'];
  60. }
  61. }
  62. @VendurePlugin({
  63. shopApiExtensions: {
  64. resolvers: [TestShopPluginResolver],
  65. schema: gql`
  66. extend type Query {
  67. baz: [String]!
  68. }
  69. `,
  70. },
  71. adminApiExtensions: {
  72. resolvers: [TestAdminPluginResolver],
  73. schema: gql`
  74. extend type Query {
  75. foo: [String]!
  76. barList(options: BarListOptions): BarList!
  77. }
  78. input BarListOptions
  79. type Bar implements Node {
  80. id: ID!
  81. name: String!
  82. }
  83. type BarList implements PaginatedList {
  84. items: [Bar!]!
  85. totalItems: Int!
  86. }
  87. `,
  88. },
  89. })
  90. export class TestAPIExtensionPlugin {}
  91. @Resolver()
  92. export class TestLazyResolver {
  93. @Query()
  94. lazy() {
  95. return 'sleeping';
  96. }
  97. }
  98. @VendurePlugin({
  99. shopApiExtensions: {
  100. resolvers: () => [TestLazyResolver],
  101. schema: () => gql`
  102. extend type Query {
  103. lazy: String!
  104. }
  105. `,
  106. },
  107. })
  108. export class TestLazyExtensionPlugin {}
  109. @Injectable()
  110. export class NameService {
  111. getNames(): string[] {
  112. return ['seon', 'linda', 'hong'];
  113. }
  114. }
  115. @Resolver()
  116. export class TestResolverWithInjection {
  117. constructor(private nameService: NameService) {}
  118. @Query()
  119. names() {
  120. return this.nameService.getNames();
  121. }
  122. }
  123. @VendurePlugin({
  124. providers: [NameService],
  125. shopApiExtensions: {
  126. resolvers: [TestResolverWithInjection],
  127. schema: gql`
  128. extend type Query {
  129. names: [String]!
  130. }
  131. `,
  132. },
  133. })
  134. export class TestPluginWithProvider {}
  135. @VendurePlugin({
  136. imports: [ConfigModule],
  137. configuration: config => {
  138. // tslint:disable-next-line:no-non-null-assertion
  139. config.defaultLanguageCode = LanguageCode.zh;
  140. return config;
  141. },
  142. })
  143. export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap {
  144. private static boostrapWasCalled: any;
  145. static setup(boostrapWasCalled: (arg: any) => void) {
  146. TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
  147. return TestPluginWithConfigAndBootstrap;
  148. }
  149. constructor(private configService: ConfigService) {}
  150. onVendureBootstrap() {
  151. TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
  152. }
  153. }