Browse Source

feat(core): Add Address custom fields to OrderAddress

Relates to #409
Michael Bromley 5 years ago
parent
commit
6f35493fa8

+ 22 - 0
packages/core/src/api/config/__snapshots__/graphql-custom-fields.spec.ts.snap

@@ -1,5 +1,27 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`addGraphQLCustomFields() extends OrderAddress if Address custom fields defined 1`] = `
+"type Address {
+  id: ID
+  streetLine1: String
+  customFields: AddressCustomFields
+}
+
+type AddressCustomFields {
+  instructions: String
+}
+
+scalar DateTime
+
+scalar JSON
+
+type OrderAddress {
+  streetLine1: String
+  customFields: AddressCustomFields
+}
+"
+`;
+
 exports[`addGraphQLCustomFields() extends a type 1`] = `
 "scalar DateTime
 

+ 18 - 0
packages/core/src/api/config/graphql-custom-fields.spec.ts

@@ -203,6 +203,24 @@ describe('addGraphQLCustomFields()', () => {
         const result = addGraphQLCustomFields(input, customFieldConfig, true);
         expect(printSchema(result)).toMatchSnapshot();
     });
+
+    it('extends OrderAddress if Address custom fields defined', () => {
+        const input = `
+             type Address {
+                 id: ID
+                 streetLine1: String
+             }
+
+             type OrderAddress {
+                 streetLine1: String
+             }
+        `;
+        const customFieldConfig: CustomFields = {
+            Address: [{ name: 'instructions', type: 'string' }],
+        };
+        const result = addGraphQLCustomFields(input, customFieldConfig, true);
+        expect(printSchema(result)).toMatchSnapshot();
+    });
 });
 
 describe('addOrderLineCustomFieldsInput()', () => {

+ 17 - 7
packages/core/src/api/config/graphql-custom-fields.ts

@@ -32,15 +32,15 @@ export function addGraphQLCustomFields(
 
     for (const entityName of Object.keys(customFieldConfig)) {
         const customEntityFields = (customFieldConfig[entityName as keyof CustomFields] || []).filter(
-            (config) => {
+            config => {
                 return !config.internal && (publicOnly === true ? config.public !== false : true);
             },
         );
 
-        const localeStringFields = customEntityFields.filter((field) => field.type === 'localeString');
-        const nonLocaleStringFields = customEntityFields.filter((field) => field.type !== 'localeString');
-        const writeableLocaleStringFields = localeStringFields.filter((field) => !field.readonly);
-        const writeableNonLocaleStringFields = nonLocaleStringFields.filter((field) => !field.readonly);
+        const localeStringFields = customEntityFields.filter(field => field.type === 'localeString');
+        const nonLocaleStringFields = customEntityFields.filter(field => field.type !== 'localeString');
+        const writeableLocaleStringFields = localeStringFields.filter(field => !field.readonly);
+        const writeableNonLocaleStringFields = nonLocaleStringFields.filter(field => !field.readonly);
 
         if (schema.getType(entityName)) {
             if (customEntityFields.length) {
@@ -53,6 +53,16 @@ export function addGraphQLCustomFields(
                         customFields: ${entityName}CustomFields
                     }
                 `;
+
+                // For custom fields on the Address entity, we also extend the OrderAddress
+                // type (which is used to store address snapshots on Orders)
+                if (entityName === 'Address' && schema.getType('OrderAddress')) {
+                    customFieldTypeDefs += `
+                        extend type OrderAddress {
+                            customFields: ${entityName}CustomFields
+                        }
+                    `;
+                }
             } else {
                 customFieldTypeDefs += `
                     extend type ${entityName} {
@@ -197,7 +207,7 @@ export function addRegisterCustomerCustomFieldsInput(
     if (!customerCustomFields || customerCustomFields.length === 0) {
         return schema;
     }
-    const publicWritableCustomFields = customerCustomFields.filter((fieldDef) => {
+    const publicWritableCustomFields = customerCustomFields.filter(fieldDef => {
         return fieldDef.public !== false && !fieldDef.readonly && !fieldDef.internal;
     });
     if (publicWritableCustomFields.length < 1) {
@@ -271,7 +281,7 @@ type GraphQLFieldType = 'DateTime' | 'String' | 'Int' | 'Float' | 'Boolean' | 'I
  * Maps an array of CustomFieldConfig objects into a string of SDL fields.
  */
 function mapToFields(fieldDefs: CustomFieldConfig[], typeFn: (fieldType: CustomFieldType) => string): string {
-    return fieldDefs.map((field) => `${field.name}: ${typeFn(field.type)}`).join('\n');
+    return fieldDefs.map(field => `${field.name}: ${typeFn(field.type)}`).join('\n');
 }
 
 function getFilterOperator(type: CustomFieldType): string {