|
|
@@ -1,38 +1,104 @@
|
|
|
+import { buildSchema, extendSchema, parse, printSchema } from 'graphql';
|
|
|
+
|
|
|
+import { CustomFieldConfig, CustomFields, CustomFieldType } from '../../../shared/shared-types';
|
|
|
import { assertNever } from '../../../shared/shared-utils';
|
|
|
-import { CustomFields, CustomFieldType } from '../config/vendure-config';
|
|
|
|
|
|
/**
|
|
|
* Given a CustomFields config object, generates an SDL string extending the built-in
|
|
|
- * types with a customFields property.
|
|
|
+ * types with a customFields property for all entities, translations and inputs for which
|
|
|
+ * custom fields are defined.
|
|
|
*/
|
|
|
-export function generateGraphQlCustomFieldsTypes(customFieldConfig?: CustomFields): string {
|
|
|
+export function addGraphQLCustomFields(typeDefs: string, customFieldConfig?: CustomFields): string {
|
|
|
+ const schema = buildSchema(typeDefs);
|
|
|
+
|
|
|
if (!customFieldConfig) {
|
|
|
- return '';
|
|
|
+ return typeDefs;
|
|
|
}
|
|
|
|
|
|
let customFieldTypeDefs = '';
|
|
|
+
|
|
|
for (const entityName of Object.keys(customFieldConfig)) {
|
|
|
const customEntityFields = customFieldConfig[entityName as keyof CustomFields];
|
|
|
|
|
|
if (customEntityFields) {
|
|
|
- customFieldTypeDefs += `
|
|
|
- type ${entityName}CustomFields {
|
|
|
- ${customEntityFields.map(field => `${field.name}: ${getGraphQlType(field.type)}`).join('\n')}
|
|
|
+ const localeStringFields = customEntityFields.filter(field => field.type === 'localeString');
|
|
|
+ const nonLocaleStringFields = customEntityFields.filter(field => field.type !== 'localeString');
|
|
|
+
|
|
|
+ if (schema.getType(entityName)) {
|
|
|
+ customFieldTypeDefs += `
|
|
|
+ type ${entityName}CustomFields {
|
|
|
+ ${mapToFields(customEntityFields)}
|
|
|
+ }
|
|
|
+
|
|
|
+ extend type ${entityName} {
|
|
|
+ customFields: ${entityName}CustomFields
|
|
|
+ }
|
|
|
+ `;
|
|
|
}
|
|
|
|
|
|
- extend type ${entityName} {
|
|
|
- customFields: ${entityName}CustomFields
|
|
|
+ if (localeStringFields.length && schema.getType(`${entityName}Translation`)) {
|
|
|
+ customFieldTypeDefs += `
|
|
|
+ type ${entityName}TranslationCustomFields {
|
|
|
+ ${mapToFields(localeStringFields)}
|
|
|
+ }
|
|
|
+
|
|
|
+ extend type ${entityName}Translation {
|
|
|
+ customFields: ${entityName}TranslationCustomFields
|
|
|
+ }
|
|
|
+ `;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (schema.getType(`Create${entityName}Input`)) {
|
|
|
+ customFieldTypeDefs += `
|
|
|
+ input Create${entityName}CustomFieldsInput {
|
|
|
+ ${mapToFields(nonLocaleStringFields)}
|
|
|
+ }
|
|
|
+
|
|
|
+ extend input Create${entityName}Input {
|
|
|
+ customFields: Create${entityName}CustomFieldsInput
|
|
|
+ }
|
|
|
+ `;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (schema.getType(`Update${entityName}Input`)) {
|
|
|
+ customFieldTypeDefs += `
|
|
|
+ input Update${entityName}CustomFieldsInput {
|
|
|
+ ${mapToFields(nonLocaleStringFields)}
|
|
|
+ }
|
|
|
+
|
|
|
+ extend input Update${entityName}Input {
|
|
|
+ customFields: Update${entityName}CustomFieldsInput
|
|
|
+ }
|
|
|
+ `;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (localeStringFields && schema.getType(`${entityName}TranslationInput`)) {
|
|
|
+ customFieldTypeDefs += `
|
|
|
+ input ${entityName}TranslationCustomFieldsInput {
|
|
|
+ ${mapToFields(localeStringFields)}
|
|
|
+ }
|
|
|
+
|
|
|
+ extend input ${entityName}TranslationInput {
|
|
|
+ customFields: ${entityName}TranslationCustomFieldsInput
|
|
|
+ }
|
|
|
+ `;
|
|
|
}
|
|
|
- `;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return customFieldTypeDefs;
|
|
|
+ return printSchema(extendSchema(schema, parse(customFieldTypeDefs)));
|
|
|
}
|
|
|
|
|
|
-type GraphQLSDLType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID';
|
|
|
+type GraphQLFieldType = 'String' | 'Int' | 'Float' | 'Boolean' | 'ID';
|
|
|
+
|
|
|
+/**
|
|
|
+ * Maps an array of CustomFieldConfig objects into a string of SDL fields.
|
|
|
+ */
|
|
|
+function mapToFields(fieldDefs: CustomFieldConfig[]): string {
|
|
|
+ return fieldDefs.map(field => `${field.name}: ${getGraphQlType(field.type)}`).join('\n');
|
|
|
+}
|
|
|
|
|
|
-function getGraphQlType(type: CustomFieldType): GraphQLSDLType {
|
|
|
+function getGraphQlType(type: CustomFieldType): GraphQLFieldType {
|
|
|
switch (type) {
|
|
|
case 'string':
|
|
|
case 'datetime':
|