فهرست منبع

fix(core): Include custom fields on `activePaymentMethods` and `activeShippingMethods` queries (#3513)

David Höck 7 ماه پیش
والد
کامیت
bb4723d7ac

+ 32 - 4
packages/core/src/api/config/graphql-custom-fields.ts

@@ -13,7 +13,6 @@ import {
     CustomFieldConfig,
     CustomFields,
     StructCustomFieldConfig,
-    RelationCustomFieldConfig,
     StructFieldConfig,
 } from '../../config/custom-field/custom-field-types';
 import { Logger } from '../../config/logger/vendure-logger';
@@ -47,6 +46,8 @@ export function addGraphQLCustomFields(
     }
 
     const customFieldsConfig = getCustomFieldsConfigWithoutInterfaces(customFieldConfig, schema);
+    const entitiesWithPublicTypes = [`ShippingMethod`, `PaymentMethod`];
+
     for (const [entityName, customFields] of customFieldsConfig) {
         const gqlType = schema.getType(entityName);
         if (isObjectType(gqlType) && gqlType.getFields().customFields) {
@@ -249,6 +250,36 @@ export function addGraphQLCustomFields(
                 }
             }
         }
+
+        const publicEntityName = `Public${entityName}`;
+
+        if (schema.getType(publicEntityName) && entitiesWithPublicTypes.includes(entityName)) {
+            if (customEntityFields.length) {
+                for (const structCustomField of structCustomFields) {
+                    customFieldTypeDefs += `
+                        type ${getStructTypeName(publicEntityName, structCustomField)} {
+                            ${mapToStructFields(structCustomField.fields, wrapListType(getGraphQlTypeForStructField))}
+                        }
+                    `;
+                }
+
+                customFieldTypeDefs += `
+                    type ${publicEntityName}CustomFields {
+                        ${mapToFields(customEntityFields, wrapListType(getGraphQlType(entityName)))}
+                    }
+    
+                    extend type ${publicEntityName} {
+                        customFields: ${publicEntityName}CustomFields
+                    }
+                `;
+            } else {
+                customFieldTypeDefs += `
+                    extend type ${publicEntityName} {
+                        customFields: JSON
+                    }
+                `;
+            }
+        }
     }
 
     const publicAddressFields = customFieldConfig.Address?.filter(
@@ -634,7 +665,6 @@ function getFilterOperator(config: CustomFieldConfig): string | undefined {
         default:
             assertNever(config);
     }
-    return 'String';
 }
 
 function getGraphQlInputType(entityName: string) {
@@ -685,7 +715,6 @@ function getGraphQlType(entityName: string) {
             default:
                 assertNever(config);
         }
-        return 'String';
     };
 }
 
@@ -705,7 +734,6 @@ function getGraphQlTypeForStructField(config: StructFieldConfig): string {
         default:
             assertNever(config);
     }
-    return 'String';
 }
 
 function getStructTypeName(entityName: string, fieldDef: StructCustomFieldConfig): string {

+ 4 - 3
packages/core/src/service/services/payment-method.service.ts

@@ -322,9 +322,10 @@ export class PaymentMethodService {
     }
 
     async getActivePaymentMethods(ctx: RequestContext): Promise<PaymentMethod[]> {
-        const paymentMethods = await this.connection
-            .getRepository(ctx, PaymentMethod)
-            .find({ where: { enabled: true, channels: { id: ctx.channelId } }, relations: ['channels'] });
+        const paymentMethods = await this.connection.getRepository(ctx, PaymentMethod).find({
+            where: { enabled: true, channels: { id: ctx.channelId } },
+            relations: ['channels', 'customFields'],
+        });
         return paymentMethods.map(p => this.translator.translate(p, ctx));
     }
 }

+ 1 - 1
packages/core/src/service/services/shipping-method.service.ts

@@ -269,7 +269,7 @@ export class ShippingMethodService {
 
     async getActiveShippingMethods(ctx: RequestContext): Promise<ShippingMethod[]> {
         const shippingMethods = await this.connection.getRepository(ctx, ShippingMethod).find({
-            relations: ['channels'],
+            relations: ['channels', 'customFields'],
             where: { deletedAt: IsNull() },
         });
         return shippingMethods