test-plugins.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { Controller, Get, Injectable } from '@nestjs/common';
  2. import { Query, Resolver } from '@nestjs/graphql';
  3. import { Permission } from '@vendure/common/lib/generated-shop-types';
  4. import { LanguageCode } from '@vendure/common/lib/generated-types';
  5. import {
  6. Allow,
  7. ConfigModule,
  8. ConfigService,
  9. InternalServerError,
  10. OnVendureBootstrap,
  11. OnVendureClose,
  12. OnVendureWorkerBootstrap,
  13. OnVendureWorkerClose,
  14. VendurePlugin,
  15. } from '@vendure/core';
  16. import gql from 'graphql-tag';
  17. export class TestPluginWithAllLifecycleHooks
  18. implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
  19. private static onConstructorFn: any;
  20. private static onBootstrapFn: any;
  21. private static onWorkerBootstrapFn: any;
  22. private static onCloseFn: any;
  23. private static onWorkerCloseFn: any;
  24. static init(
  25. constructorFn: any,
  26. bootstrapFn: any,
  27. workerBootstrapFn: any,
  28. closeFn: any,
  29. workerCloseFn: any,
  30. ) {
  31. this.onConstructorFn = constructorFn;
  32. this.onBootstrapFn = bootstrapFn;
  33. this.onWorkerBootstrapFn = workerBootstrapFn;
  34. this.onCloseFn = closeFn;
  35. this.onWorkerCloseFn = workerCloseFn;
  36. return this;
  37. }
  38. constructor() {
  39. TestPluginWithAllLifecycleHooks.onConstructorFn();
  40. }
  41. onVendureBootstrap(): void | Promise<void> {
  42. TestPluginWithAllLifecycleHooks.onBootstrapFn();
  43. }
  44. onVendureWorkerBootstrap(): void | Promise<void> {
  45. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
  46. }
  47. onVendureClose(): void | Promise<void> {
  48. TestPluginWithAllLifecycleHooks.onCloseFn();
  49. this.resetSpies();
  50. }
  51. onVendureWorkerClose(): void | Promise<void> {
  52. TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
  53. this.resetSpies();
  54. }
  55. /**
  56. * This is required because on the first run, the Vendure server will be bootstrapped twice -
  57. * once to populate the database and the second time forthe actual tests. Thus the call counts
  58. * for the plugin lifecycles will be doubled. This method resets them after the initial
  59. * (population) run.
  60. */
  61. private resetSpies() {
  62. TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
  63. TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
  64. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
  65. }
  66. }
  67. @Resolver()
  68. export class TestAdminPluginResolver {
  69. @Query()
  70. foo() {
  71. return ['bar'];
  72. }
  73. @Query()
  74. barList() {
  75. return {
  76. items: [{ id: 1, name: 'Test' }],
  77. totalItems: 1,
  78. };
  79. }
  80. }
  81. @Resolver()
  82. export class TestShopPluginResolver {
  83. @Query()
  84. baz() {
  85. return ['quux'];
  86. }
  87. }
  88. @VendurePlugin({
  89. shopApiExtensions: {
  90. resolvers: [TestShopPluginResolver],
  91. schema: gql`
  92. extend type Query {
  93. baz: [String]!
  94. }
  95. `,
  96. },
  97. adminApiExtensions: {
  98. resolvers: [TestAdminPluginResolver],
  99. schema: gql`
  100. extend type Query {
  101. foo: [String]!
  102. barList(options: BarListOptions): BarList!
  103. }
  104. input BarListOptions
  105. type Bar implements Node {
  106. id: ID!
  107. name: String!
  108. }
  109. type BarList implements PaginatedList {
  110. items: [Bar!]!
  111. totalItems: Int!
  112. }
  113. `,
  114. },
  115. })
  116. export class TestAPIExtensionPlugin {}
  117. @Resolver()
  118. export class TestLazyResolver {
  119. @Query()
  120. lazy() {
  121. return 'sleeping';
  122. }
  123. }
  124. @VendurePlugin({
  125. shopApiExtensions: {
  126. resolvers: () => [TestLazyResolver],
  127. schema: () => gql`
  128. extend type Query {
  129. lazy: String!
  130. }
  131. `,
  132. },
  133. })
  134. export class TestLazyExtensionPlugin {}
  135. @Injectable()
  136. export class NameService {
  137. getNames(): string[] {
  138. return ['seon', 'linda', 'hong'];
  139. }
  140. }
  141. @Resolver()
  142. export class TestResolverWithInjection {
  143. constructor(private nameService: NameService) {}
  144. @Query()
  145. names() {
  146. return this.nameService.getNames();
  147. }
  148. }
  149. @VendurePlugin({
  150. providers: [NameService],
  151. shopApiExtensions: {
  152. resolvers: [TestResolverWithInjection],
  153. schema: gql`
  154. extend type Query {
  155. names: [String]!
  156. }
  157. `,
  158. },
  159. })
  160. export class TestPluginWithProvider {}
  161. @VendurePlugin({
  162. imports: [ConfigModule],
  163. configuration: config => {
  164. // tslint:disable-next-line:no-non-null-assertion
  165. config.defaultLanguageCode = LanguageCode.zh;
  166. return config;
  167. },
  168. })
  169. export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap, OnVendureClose {
  170. private static boostrapWasCalled: any;
  171. static setup(boostrapWasCalled: (arg: any) => void) {
  172. TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
  173. return TestPluginWithConfigAndBootstrap;
  174. }
  175. constructor(private configService: ConfigService) {}
  176. onVendureBootstrap() {
  177. TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
  178. }
  179. onVendureClose() {
  180. TestPluginWithConfigAndBootstrap.boostrapWasCalled.mockClear();
  181. }
  182. }
  183. @Controller('test')
  184. export class TestController {
  185. @Get('public')
  186. publicRoute() {
  187. return 'success';
  188. }
  189. @Allow(Permission.Authenticated)
  190. @Get('restricted')
  191. restrictedRoute() {
  192. return 'success';
  193. }
  194. @Get('bad')
  195. badRoute() {
  196. throw new InternalServerError('uh oh!');
  197. }
  198. }
  199. @VendurePlugin({
  200. controllers: [TestController],
  201. })
  202. export class TestRestPlugin {}