| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import { Injectable, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
- import { Query, Resolver } from '@nestjs/graphql';
- import { LanguageCode } from '@vendure/common/lib/generated-types';
- import gql from 'graphql-tag';
- import { VendureConfig } from '../../src/config';
- import { ConfigModule } from '../../src/config/config.module';
- import { ConfigService } from '../../src/config/config.service';
- import {
- OnVendureBootstrap,
- OnVendureClose,
- OnVendureWorkerBootstrap,
- OnVendureWorkerClose,
- VendurePlugin,
- } from '../../src/plugin/vendure-plugin';
- export class TestPluginWithAllLifecycleHooks
- implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
- private static onConstructorFn: any;
- private static onBootstrapFn: any;
- private static onWorkerBootstrapFn: any;
- private static onCloseFn: any;
- private static onWorkerCloseFn: any;
- static init(
- constructorFn: any,
- bootstrapFn: any,
- workerBootstrapFn: any,
- closeFn: any,
- workerCloseFn: any,
- ) {
- this.onConstructorFn = constructorFn;
- this.onBootstrapFn = bootstrapFn;
- this.onWorkerBootstrapFn = workerBootstrapFn;
- this.onCloseFn = closeFn;
- this.onWorkerCloseFn = workerCloseFn;
- return this;
- }
- constructor() {
- TestPluginWithAllLifecycleHooks.onConstructorFn();
- }
- onVendureBootstrap(): void | Promise<void> {
- TestPluginWithAllLifecycleHooks.onBootstrapFn();
- }
- onVendureWorkerBootstrap(): void | Promise<void> {
- TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
- }
- onVendureClose(): void | Promise<void> {
- TestPluginWithAllLifecycleHooks.onCloseFn();
- this.resetSpies();
- }
- onVendureWorkerClose(): void | Promise<void> {
- TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
- this.resetSpies();
- }
- /**
- * This is required because on the first run, the Vendure server will be bootstrapped twice -
- * once to populate the database and the second time forthe actual tests. Thus the call counts
- * for the plugin lifecycles will be doubled. This method resets them after the initial
- * (population) run.
- */
- private resetSpies() {
- TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
- TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
- TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
- }
- }
- @Resolver()
- export class TestAdminPluginResolver {
- @Query()
- foo() {
- return ['bar'];
- }
- @Query()
- barList() {
- return {
- items: [{ id: 1, name: 'Test' }],
- totalItems: 1,
- };
- }
- }
- @Resolver()
- export class TestShopPluginResolver {
- @Query()
- baz() {
- return ['quux'];
- }
- }
- @VendurePlugin({
- shopApiExtensions: {
- resolvers: [TestShopPluginResolver],
- schema: gql`
- extend type Query {
- baz: [String]!
- }
- `,
- },
- adminApiExtensions: {
- resolvers: [TestAdminPluginResolver],
- schema: gql`
- extend type Query {
- foo: [String]!
- barList(options: BarListOptions): BarList!
- }
- input BarListOptions
- type Bar implements Node {
- id: ID!
- name: String!
- }
- type BarList implements PaginatedList {
- items: [Bar!]!
- totalItems: Int!
- }
- `,
- },
- })
- export class TestAPIExtensionPlugin {}
- @Resolver()
- export class TestLazyResolver {
- @Query()
- lazy() {
- return 'sleeping';
- }
- }
- @VendurePlugin({
- shopApiExtensions: {
- resolvers: () => [TestLazyResolver],
- schema: () => gql`
- extend type Query {
- lazy: String!
- }
- `,
- },
- })
- export class TestLazyExtensionPlugin {}
- @Injectable()
- export class NameService {
- getNames(): string[] {
- return ['seon', 'linda', 'hong'];
- }
- }
- @Resolver()
- export class TestResolverWithInjection {
- constructor(private nameService: NameService) {}
- @Query()
- names() {
- return this.nameService.getNames();
- }
- }
- @VendurePlugin({
- providers: [NameService],
- shopApiExtensions: {
- resolvers: [TestResolverWithInjection],
- schema: gql`
- extend type Query {
- names: [String]!
- }
- `,
- },
- })
- export class TestPluginWithProvider {}
- @VendurePlugin({
- imports: [ConfigModule],
- configuration: config => {
- // tslint:disable-next-line:no-non-null-assertion
- config.defaultLanguageCode = LanguageCode.zh;
- return config;
- },
- })
- export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap, OnVendureClose {
- private static boostrapWasCalled: any;
- static setup(boostrapWasCalled: (arg: any) => void) {
- TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
- return TestPluginWithConfigAndBootstrap;
- }
- constructor(private configService: ConfigService) {}
- onVendureBootstrap() {
- TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
- }
- onVendureClose() {
- TestPluginWithConfigAndBootstrap.boostrapWasCalled.mockClear();
- }
- }
|