Explorar el Código

fix(core): Fix struct custom field support on GlobalSettings

Fixes #3381
Michael Bromley hace 10 meses
padre
commit
50a90e793a

+ 13 - 0
packages/core/e2e/custom-field-struct.e2e-spec.ts

@@ -85,6 +85,19 @@ const customConfig = mergeConfig(testConfig(), {
                 ],
             },
         ],
+        // https://github.com/vendure-ecommerce/vendure/issues/3381
+        GlobalSettings: [
+            {
+                name: 'tipsPercentage',
+                type: 'struct',
+                list: true,
+                fields: [
+                    { name: 'percentage', type: 'float' },
+                    { name: 'name', type: 'string' },
+                    { name: 'isDefault', type: 'boolean' },
+                ],
+            },
+        ],
     },
 });
 

+ 13 - 6
packages/core/src/api/config/graphql-custom-fields.ts

@@ -140,16 +140,23 @@ export function addGraphQLCustomFields(
                 `;
         }
 
-        if (schema.getType(`Create${entityName}Input`)) {
-            if (writeableNonLocalizedFields.length) {
-                for (const structCustomField of structCustomFields) {
-                    customFieldTypeDefs += `
+        const hasCreateInputType = schema.getType(`Create${entityName}Input`);
+        const hasUpdateInputType = schema.getType(`Update${entityName}Input`);
+
+        if ((hasCreateInputType || hasUpdateInputType) && writeableNonLocalizedFields.length) {
+            // Define any Struct input types that are required by
+            // the create and/or update input types.
+            for (const structCustomField of structCustomFields) {
+                customFieldTypeDefs += `
                         input ${getStructInputName(entityName, structCustomField)} {
                             ${mapToStructFields(structCustomField.fields, wrapListType(getGraphQlInputType(entityName)))}
                         }
                     `;
-                }
+            }
+        }
 
+        if (hasCreateInputType) {
+            if (writeableNonLocalizedFields.length) {
                 customFieldTypeDefs += `
                     input Create${entityName}CustomFieldsInput {
                        ${mapToFields(
@@ -172,7 +179,7 @@ export function addGraphQLCustomFields(
             }
         }
 
-        if (schema.getType(`Update${entityName}Input`)) {
+        if (hasUpdateInputType) {
             if (writeableNonLocalizedFields.length) {
                 customFieldTypeDefs += `
                     input Update${entityName}CustomFieldsInput {