|
|
@@ -1,56 +1,22 @@
|
|
|
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
|
|
|
import { DynamicModule } from '@nestjs/common';
|
|
|
import { GraphQLModule, GraphQLTypesLoader } from '@nestjs/graphql';
|
|
|
-import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
|
|
|
-import {
|
|
|
- buildClientSchema,
|
|
|
- buildSchema,
|
|
|
- extendSchema,
|
|
|
- GraphQLSchema,
|
|
|
- IntrospectionQuery,
|
|
|
- IntrospectionSchema,
|
|
|
- printIntrospectionSchema,
|
|
|
- printSchema,
|
|
|
- ValidationContext,
|
|
|
-} from 'graphql';
|
|
|
-import path from 'path';
|
|
|
+import { GraphQLSchema, printSchema, ValidationContext } from 'graphql';
|
|
|
|
|
|
import { ConfigModule } from '../../config/config.module';
|
|
|
import { ConfigService } from '../../config/config.service';
|
|
|
-import {
|
|
|
- AutoIncrementIdStrategy,
|
|
|
- EntityIdStrategy,
|
|
|
- RuntimeVendureConfig,
|
|
|
- UuidIdStrategy,
|
|
|
-} from '../../config/index';
|
|
|
import { I18nModule } from '../../i18n/i18n.module';
|
|
|
import { I18nService } from '../../i18n/i18n.service';
|
|
|
-import { getPluginAPIExtensions } from '../../plugin/plugin-metadata';
|
|
|
import { ServiceModule } from '../../service/service.module';
|
|
|
import { ApiSharedModule } from '../api-internal-modules';
|
|
|
import { CustomFieldRelationResolverService } from '../common/custom-field-relation-resolver.service';
|
|
|
-import { ApiType } from '../common/get-api-type';
|
|
|
import { IdCodecService } from '../common/id-codec.service';
|
|
|
import { AssetInterceptorPlugin } from '../middleware/asset-interceptor-plugin';
|
|
|
import { IdCodecPlugin } from '../middleware/id-codec-plugin';
|
|
|
import { TranslateErrorsPlugin } from '../middleware/translate-errors-plugin';
|
|
|
|
|
|
-import { generateActiveOrderTypes } from './generate-active-order-types';
|
|
|
-import { generateAuthenticationTypes } from './generate-auth-types';
|
|
|
-import { generateErrorCodeEnum } from './generate-error-code-enum';
|
|
|
-import { generateListOptions } from './generate-list-options';
|
|
|
-import { generatePermissionEnum } from './generate-permissions';
|
|
|
import { generateResolvers } from './generate-resolvers';
|
|
|
-import {
|
|
|
- addActiveAdministratorCustomFields,
|
|
|
- addGraphQLCustomFields,
|
|
|
- addModifyOrderCustomFields,
|
|
|
- addOrderLineCustomFieldsInput,
|
|
|
- addPaymentMethodQuoteCustomFields,
|
|
|
- addRegisterCustomerCustomFieldsInput,
|
|
|
- addServerConfigCustomFields,
|
|
|
- addShippingMethodQuoteCustomFields,
|
|
|
-} from './graphql-custom-fields';
|
|
|
+import { getFinalVendureSchema, isUsingDefaultEntityIdStrategy } from './get-final-vendure-schema';
|
|
|
|
|
|
export interface GraphQLApiOptions {
|
|
|
apiType: 'shop' | 'admin';
|
|
|
@@ -167,83 +133,3 @@ async function createGraphQLOptions(
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-type GetSchemaOptions = {
|
|
|
- config: RuntimeVendureConfig;
|
|
|
- typePaths: string[];
|
|
|
- typesLoader: GraphQLTypesLoader;
|
|
|
- apiType: 'shop' | 'admin';
|
|
|
-};
|
|
|
-type GetSchemaAsSDLOptions = GetSchemaOptions & { output: 'sdl' };
|
|
|
-
|
|
|
-export async function getFinalVendureSchema(options: GetSchemaOptions): Promise<GraphQLSchema>;
|
|
|
-export async function getFinalVendureSchema(options: GetSchemaAsSDLOptions): Promise<string>;
|
|
|
-export async function getFinalVendureSchema(
|
|
|
- options: GetSchemaOptions & { output?: 'sdl' },
|
|
|
-): Promise<GraphQLSchema | string> {
|
|
|
- const { config, typePaths, typesLoader, apiType } = options;
|
|
|
- // Paths must be normalized to use forward-slash separators.
|
|
|
- // See https://github.com/nestjs/graphql/issues/336
|
|
|
- const normalizedPaths = typePaths.map(p => p.split(path.sep).join('/'));
|
|
|
- const typeDefs = await typesLoader.mergeTypesByPaths(normalizedPaths);
|
|
|
- let schema = buildSchema(typeDefs);
|
|
|
- schema = buildSchemaFromVendureConfig(schema, config, apiType);
|
|
|
- if (options.output === 'sdl') {
|
|
|
- return printSchema(schema);
|
|
|
- } else {
|
|
|
- return schema;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function buildSchemaFromVendureConfig(
|
|
|
- schema: GraphQLSchema,
|
|
|
- config: RuntimeVendureConfig,
|
|
|
- apiType: 'shop' | 'admin',
|
|
|
-): GraphQLSchema {
|
|
|
- const authStrategies =
|
|
|
- apiType === 'shop'
|
|
|
- ? config.authOptions.shopAuthenticationStrategy
|
|
|
- : config.authOptions.adminAuthenticationStrategy;
|
|
|
-
|
|
|
- const customFields = config.customFields;
|
|
|
-
|
|
|
- schema = extendSchemaWithPluginApiExtensions(schema, config.plugins, apiType);
|
|
|
- schema = generateListOptions(schema);
|
|
|
- schema = addGraphQLCustomFields(schema, customFields, apiType === 'shop');
|
|
|
- schema = addOrderLineCustomFieldsInput(schema, customFields.OrderLine || [], apiType === 'shop');
|
|
|
- schema = addModifyOrderCustomFields(schema, customFields.Order || []);
|
|
|
- schema = addShippingMethodQuoteCustomFields(schema, customFields.ShippingMethod || []);
|
|
|
- schema = addPaymentMethodQuoteCustomFields(schema, customFields.PaymentMethod || []);
|
|
|
- schema = generateAuthenticationTypes(schema, authStrategies);
|
|
|
- schema = generateErrorCodeEnum(schema);
|
|
|
- if (apiType === 'admin') {
|
|
|
- schema = addServerConfigCustomFields(schema, customFields);
|
|
|
- schema = addActiveAdministratorCustomFields(schema, customFields.Administrator);
|
|
|
- }
|
|
|
- if (apiType === 'shop') {
|
|
|
- schema = addRegisterCustomerCustomFieldsInput(schema, customFields.Customer || []);
|
|
|
- schema = generateActiveOrderTypes(schema, config.orderOptions.activeOrderStrategy);
|
|
|
- }
|
|
|
- schema = generatePermissionEnum(schema, config.authOptions.customPermissions);
|
|
|
-
|
|
|
- return schema;
|
|
|
-}
|
|
|
-
|
|
|
-function extendSchemaWithPluginApiExtensions(
|
|
|
- schema: GraphQLSchema,
|
|
|
- plugins: RuntimeVendureConfig['plugins'],
|
|
|
- apiType: 'admin' | 'shop',
|
|
|
-) {
|
|
|
- getPluginAPIExtensions(plugins, apiType)
|
|
|
- .map(e => (typeof e.schema === 'function' ? e.schema() : e.schema))
|
|
|
- .filter(notNullOrUndefined)
|
|
|
- .forEach(documentNode => (schema = extendSchema(schema, documentNode)));
|
|
|
- return schema;
|
|
|
-}
|
|
|
-
|
|
|
-function isUsingDefaultEntityIdStrategy(entityIdStrategy: EntityIdStrategy<any>): boolean {
|
|
|
- return (
|
|
|
- entityIdStrategy.constructor === AutoIncrementIdStrategy ||
|
|
|
- entityIdStrategy.constructor === UuidIdStrategy
|
|
|
- );
|
|
|
-}
|