Browse Source

refactor(server): Simplify the way plugin providers are exported

Michael Bromley 7 years ago
parent
commit
74338d00fb

+ 4 - 13
server/src/api/api.module.ts

@@ -2,11 +2,10 @@ import { Module } from '@nestjs/common';
 import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
 import { GraphQLModule } from '@nestjs/graphql';
 
-import { notNullOrUndefined } from '../../../shared/shared-utils';
-import { getConfig } from '../config/config-helpers';
 import { ConfigModule } from '../config/config.module';
 import { DataImportModule } from '../data-import/data-import.module';
 import { I18nModule } from '../i18n/i18n.module';
+import { PluginModule } from '../plugin/plugin.module';
 import { ServiceModule } from '../service/service.module';
 
 import { IdCodecService } from './common/id-codec.service';
@@ -38,7 +37,7 @@ import { TaxCategoryResolver } from './resolvers/tax-category.resolver';
 import { TaxRateResolver } from './resolvers/tax-rate.resolver';
 import { ZoneResolver } from './resolvers/zone.resolver';
 
-const exportedProviders = [
+const resolvers = [
     PromotionResolver,
     AdministratorResolver,
     AuthResolver,
@@ -63,13 +62,6 @@ const exportedProviders = [
     ZoneResolver,
 ];
 
-// Plugins may define resolver classes which must also be registered with this module
-// alongside the build-in resolvers.
-const pluginResolvers = getConfig()
-    .plugins.map(p => (p.defineResolvers ? p.defineResolvers() : undefined))
-    .filter(notNullOrUndefined)
-    .reduce((flattened, resolvers) => flattened.concat(resolvers), []);
-
 /**
  * The ApiModule is responsible for the public API of the application. This is where requests
  * come in, are parsed and then handed over to the ServiceModule classes which take care
@@ -83,10 +75,10 @@ const pluginResolvers = getConfig()
             useClass: GraphqlConfigService,
             imports: [ConfigModule, I18nModule],
         }),
+        PluginModule,
     ],
     providers: [
-        ...exportedProviders,
-        ...pluginResolvers,
+        ...resolvers,
         RequestContextService,
         IdCodecService,
         {
@@ -102,6 +94,5 @@ const pluginResolvers = getConfig()
             useClass: IdInterceptor,
         },
     ],
-    exports: exportedProviders,
 })
 export class ApiModule {}

+ 2 - 2
server/src/config/vendure-plugin/vendure-plugin.ts

@@ -31,9 +31,9 @@ export interface VendurePlugin {
     defineGraphQlTypes?(): DocumentNode;
 
     /**
-     * The plugin may define custom GraphQL resolvers.
+     * The plugin may define custom providers (including GraphQL resolvers) which can then be injected via the Nest DI container.
      */
-    defineResolvers?(): Array<Type<any>>;
+    defineProviders?(): Array<Type<any>>;
 
     /**
      * The plugin may define custom database entities, which should be defined as classes annotated as per the

+ 2 - 1
server/src/i18n/messages/en.json

@@ -11,7 +11,8 @@
     "entity-with-id-not-found": "No { entityName } with the id '{ id }' could be found",
     "forbidden": "You are not currently authorized to perform this action",
     "invalid-sort-field": "The sort field '{ fieldName }' is invalid. Valid fields are: { validFields }",
-    "no-valid-channel-specified": "No valid channel was specified",
+    "no-search-plugin-configured": "No search plugin has been configured",
+    "no-valid-channel-specified": "No valid channel was specified (ensure the 'vendure-token' header was specified in the request)",
     "order-contents-may-only-be-modified-in-addingitems-state": "Order contents may only be modified when in the \"AddingItems\" state",
     "order-does-not-contain-line-with-id": "This order does not contain an OrderLine with the id { id }",
     "order-item-quantity-must-be-positive": "{ quantity } is not a valid quantity for an OrderItem",

+ 23 - 0
server/src/plugin/plugin.module.ts

@@ -0,0 +1,23 @@
+import { Module } from '@nestjs/common';
+
+import { notNullOrUndefined } from '../../../shared/shared-utils';
+import { getConfig } from '../config/config-helpers';
+import { ConfigModule } from '../config/config.module';
+import { EventBusModule } from '../event-bus/event-bus.module';
+import { ServiceModule } from '../service/service.module';
+
+const pluginProviders = getConfig()
+    .plugins.map(p => (p.defineProviders ? p.defineProviders() : undefined))
+    .filter(notNullOrUndefined)
+    .reduce((flattened, providers) => flattened.concat(providers), []);
+
+/**
+ * This module collects and re-exports all providers defined in plugins so that they can be used in other
+ * modules (e.g. providing customer resolvers to the ApiModule)
+ */
+@Module({
+    imports: [ServiceModule, EventBusModule, ConfigModule],
+    providers: pluginProviders,
+    exports: pluginProviders,
+})
+export class PluginModule {}