Browse Source

fix(core): Improve message for custom field schema errors

Michael Bromley 1 year ago
parent
commit
7ac4ac925e
1 changed files with 19 additions and 8 deletions
  1. 19 8
      packages/core/src/api/config/graphql-custom-fields.ts

+ 19 - 8
packages/core/src/api/config/graphql-custom-fields.ts

@@ -5,11 +5,11 @@ import {
     GraphQLInputObjectType,
     GraphQLList,
     GraphQLSchema,
-    isInterfaceType,
     parse,
 } from 'graphql';
 
 import { CustomFieldConfig, CustomFields } from '../../config/custom-field/custom-field-types';
+import { Logger } from '../../config/logger/vendure-logger';
 
 import { getCustomFieldsConfigWithoutInterfaces } from './get-custom-fields-config-without-interfaces';
 
@@ -47,12 +47,23 @@ export function addGraphQLCustomFields(
 
         for (const fieldDef of customEntityFields) {
             if (fieldDef.type === 'relation') {
-                if (!schema.getType(fieldDef.graphQLType || fieldDef.entity.name)) {
-                    throw new Error(
-                        `The GraphQL type "${
-                            fieldDef?.graphQLType ?? '(unknown)'
-                        }" specified by the ${entityName}.${fieldDef.name} custom field does not exist`,
-                    );
+                const graphQlTypeName = fieldDef.graphQLType || fieldDef.entity.name;
+                if (!schema.getType(graphQlTypeName)) {
+                    const customFieldPath = `${entityName}.${fieldDef.name}`;
+                    const errorMessage = `The GraphQL type "${
+                        graphQlTypeName ?? '(unknown)'
+                    }" specified by the ${customFieldPath} custom field does not exist in the ${publicOnly ? 'Shop API' : 'Admin API'} schema.`;
+                    Logger.warn(errorMessage);
+                    if (publicOnly) {
+                        Logger.warn(
+                            [
+                                `This can be resolved by either:`,
+                                `  - setting \`public: false\` in the ${customFieldPath} custom field config`,
+                                `  - defining the "${graphQlTypeName}" type in the Shop API schema`,
+                            ].join('\n'),
+                        );
+                    }
+                    throw new Error(errorMessage);
                 }
             }
         }
@@ -245,7 +256,7 @@ export function addServerConfigCustomFields(
                     '',
                 )}
             }
-            
+
             type EntityCustomFields {
                 entityName: String!
                 customFields: [CustomFieldConfig!]!