Browse Source

feat(core): Added playground and debug config for graphql apis

BREAKING CHANGE: The graphql-playground for the Shop and Admin APIs are now turned off by default, and the Apollo server debug option is also set to false by default (it was formerly true). You can manually configure these values using the VendureConfig.apiOptions object.

Co-authored-by: Kai Chu <kai.chu@inoviagroup.se>
Kai Chu 5 years ago
parent
commit
1fb5fb475d

+ 6 - 2
packages/core/src/api/api.module.ts

@@ -28,7 +28,9 @@ import { ValidateCustomFieldsInterceptor } from './middleware/validate-custom-fi
         configureGraphQLModule(configService => ({
         configureGraphQLModule(configService => ({
             apiType: 'shop',
             apiType: 'shop',
             apiPath: configService.apiOptions.shopApiPath,
             apiPath: configService.apiOptions.shopApiPath,
-            typePaths: ['type', 'shop-api', 'common'].map(p =>
+            playground: configService.apiOptions.shopApiPlayground,
+            debug: configService.apiOptions.shopApiDebug,
+            typePaths: ['type', 'shop-api', 'common'].map((p) =>
                 path.join(__dirname, 'schema', p, '*.graphql'),
                 path.join(__dirname, 'schema', p, '*.graphql'),
             ),
             ),
             resolverModule: ShopApiModule,
             resolverModule: ShopApiModule,
@@ -36,7 +38,9 @@ import { ValidateCustomFieldsInterceptor } from './middleware/validate-custom-fi
         configureGraphQLModule(configService => ({
         configureGraphQLModule(configService => ({
             apiType: 'admin',
             apiType: 'admin',
             apiPath: configService.apiOptions.adminApiPath,
             apiPath: configService.apiOptions.adminApiPath,
-            typePaths: ['type', 'admin-api', 'common'].map(p =>
+            playground: configService.apiOptions.adminApiPlayground,
+            debug: configService.apiOptions.adminApiDebug,
+            typePaths: ['type', 'admin-api', 'common'].map((p) =>
                 path.join(__dirname, 'schema', p, '*.graphql'),
                 path.join(__dirname, 'schema', p, '*.graphql'),
             ),
             ),
             resolverModule: AdminApiModule,
             resolverModule: AdminApiModule,

+ 4 - 6
packages/core/src/api/config/configure-graphql-module.ts

@@ -31,6 +31,8 @@ export interface GraphQLApiOptions {
     apiType: 'shop' | 'admin';
     apiType: 'shop' | 'admin';
     typePaths: string[];
     typePaths: string[];
     apiPath: string;
     apiPath: string;
+    debug: boolean;
+    playground: boolean | any;
     // tslint:disable-next-line:ban-types
     // tslint:disable-next-line:ban-types
     resolverModule: Function;
     resolverModule: Function;
 }
 }
@@ -133,12 +135,8 @@ async function createGraphQLOptions(
         uploads: {
         uploads: {
             maxFileSize: configService.assetOptions.uploadMaxFileSize,
             maxFileSize: configService.assetOptions.uploadMaxFileSize,
         },
         },
-        playground: {
-            settings: {
-                'request.credentials': 'include',
-            } as any,
-        },
-        debug: true,
+        playground: options.playground || false,
+        debug: options.debug || false,
         context: (req: any) => req,
         context: (req: any) => req,
         // This is handled by the Express cors plugin
         // This is handled by the Express cors plugin
         cors: false,
         cors: false,

+ 4 - 1
packages/core/src/config/config.service.mock.ts

@@ -8,7 +8,11 @@ export class MockConfigService implements MockClass<ConfigService> {
     apiOptions = {
     apiOptions = {
         channelTokenKey: 'vendure-token',
         channelTokenKey: 'vendure-token',
         adminApiPath: 'admin-api',
         adminApiPath: 'admin-api',
+        adminApiPlayground: false,
+        adminApiDebug: true,
         shopApiPath: 'shop-api',
         shopApiPath: 'shop-api',
+        shopApiPlayground: false,
+        shopApiDebug: true,
         port: 3000,
         port: 3000,
         cors: false,
         cors: false,
         middleware: [],
         middleware: [],
@@ -16,7 +20,6 @@ export class MockConfigService implements MockClass<ConfigService> {
     };
     };
     authOptions: {};
     authOptions: {};
     defaultChannelToken: 'channel-token';
     defaultChannelToken: 'channel-token';
-
     defaultLanguageCode: jest.Mock<any>;
     defaultLanguageCode: jest.Mock<any>;
     roundingStrategy: {};
     roundingStrategy: {};
     entityIdStrategy = new MockIdStrategy();
     entityIdStrategy = new MockIdStrategy();

+ 1 - 1
packages/core/src/config/config.service.ts

@@ -57,7 +57,7 @@ export class ConfigService implements VendureConfig {
     get defaultLanguageCode(): LanguageCode {
     get defaultLanguageCode(): LanguageCode {
         return this.activeConfig.defaultLanguageCode;
         return this.activeConfig.defaultLanguageCode;
     }
     }
-
+    
     get entityIdStrategy(): EntityIdStrategy {
     get entityIdStrategy(): EntityIdStrategy {
         return this.activeConfig.entityIdStrategy;
         return this.activeConfig.entityIdStrategy;
     }
     }

+ 4 - 1
packages/core/src/config/default-config.ts

@@ -36,7 +36,11 @@ export const defaultConfig: RuntimeVendureConfig = {
         hostname: '',
         hostname: '',
         port: 3000,
         port: 3000,
         adminApiPath: 'admin-api',
         adminApiPath: 'admin-api',
+        adminApiPlayground: false,
+        adminApiDebug: false,
         shopApiPath: 'shop-api',
         shopApiPath: 'shop-api',
+        shopApiPlayground: false,
+        shopApiDebug: false,
         channelTokenKey: 'vendure-token',
         channelTokenKey: 'vendure-token',
         cors: {
         cors: {
             origin: true,
             origin: true,
@@ -57,7 +61,6 @@ export const defaultConfig: RuntimeVendureConfig = {
     catalogOptions: {
     catalogOptions: {
         collectionFilters: defaultCollectionFilters,
         collectionFilters: defaultCollectionFilters,
     },
     },
-
     entityIdStrategy: new AutoIncrementIdStrategy(),
     entityIdStrategy: new AutoIncrementIdStrategy(),
     assetOptions: {
     assetOptions: {
         assetNamingStrategy: new DefaultAssetNamingStrategy(),
         assetNamingStrategy: new DefaultAssetNamingStrategy(),

+ 32 - 0
packages/core/src/config/vendure-config.ts

@@ -65,6 +65,38 @@ export interface ApiOptions {
      * @default 'shop-api'
      * @default 'shop-api'
      */
      */
     shopApiPath?: string;
     shopApiPath?: string;
+    /**
+     * @description
+     * The playground config to the admin GraphQL API
+     * [ApolloServer playground](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructoroptions-apolloserver).
+     *
+     * @default false
+     */
+    adminApiPlayground?: boolean | any;
+    /**
+     * @description
+     * The playground config to the shop GraphQL API
+     * [ApolloServer playground](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructoroptions-apolloserver).
+     *
+     * @default false
+     */
+    shopApiPlayground?: boolean | any;
+    /**
+     * @description
+     * The debug config to the admin GraphQL API
+     * [ApolloServer playground](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructoroptions-apolloserver).
+     *
+     * @default false
+     */
+    adminApiDebug?: boolean;
+    /**
+     * @description
+     * The debug config to the admin GraphQL API
+     * [ApolloServer playground](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructoroptions-apolloserver).
+     *
+     * @default false
+     */
+    shopApiDebug?: boolean;
     /**
     /**
      * @description
      * @description
      * The name of the property which contains the token of the
      * The name of the property which contains the token of the

+ 12 - 0
packages/create/templates/vendure-config.hbs

@@ -29,7 +29,19 @@ const path = require('path');
     apiOptions: {
     apiOptions: {
         port: 3000,
         port: 3000,
         adminApiPath: 'admin-api',
         adminApiPath: 'admin-api',
+        adminApiPlayground: {
+            settings: {
+                'request.credentials': 'include',
+            }{{#if isTs}} as any{{/if}},
+        },// turn this off for production
+        adminApiDebug: true, // turn this off for production
         shopApiPath: 'shop-api',
         shopApiPath: 'shop-api',
+        shopApiPlayground: { 
+            settings: {
+                'request.credentials': 'include',
+            }{{#if isTs}} as any{{/if}},
+        },// turn this off for production
+        shopApiDebug: true,// turn this off for production
     },
     },
     authOptions: {
     authOptions: {
         sessionSecret: '{{ sessionSecret }}',
         sessionSecret: '{{ sessionSecret }}',

+ 13 - 0
packages/dev-server/dev-config.ts

@@ -22,6 +22,19 @@ export const devConfig: VendureConfig = {
     apiOptions: {
     apiOptions: {
         port: API_PORT,
         port: API_PORT,
         adminApiPath: ADMIN_API_PATH,
         adminApiPath: ADMIN_API_PATH,
+        adminApiPlayground: {
+            settings: {
+                'request.credentials': 'include',
+            } as any,
+        },
+        adminApiDebug: true,
+        shopApiPath: SHOP_API_PATH,
+        shopApiPlayground: {
+            settings: {
+                'request.credentials': 'include',
+            } as any,
+        },
+        shopApiDebug: true,
     },
     },
     authOptions: {
     authOptions: {
         disableAuth: false,
         disableAuth: false,