From e03b7f00bb063a9d3a63585ffd7fae5bbd206fde Mon Sep 17 00:00:00 2001 From: "Mitch J." Date: Thu, 31 Oct 2024 05:20:05 -0400 Subject: [PATCH] fix(core): Fix error on internal Administrator customFields (#3159) --- .../graphql-custom-fields.spec.ts.snap | 9 +++++++ .../api/config/graphql-custom-fields.spec.ts | 24 +++++++++++++++++++ .../src/api/config/graphql-custom-fields.ts | 6 +++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/core/src/api/config/__snapshots__/graphql-custom-fields.spec.ts.snap b/packages/core/src/api/config/__snapshots__/graphql-custom-fields.spec.ts.snap index ba1686e508..bb91d908c8 100644 --- a/packages/core/src/api/config/__snapshots__/graphql-custom-fields.spec.ts.snap +++ b/packages/core/src/api/config/__snapshots__/graphql-custom-fields.spec.ts.snap @@ -250,6 +250,15 @@ scalar JSON scalar DateTime" `; +exports[`addGraphQLCustomFields() > uses JSON scalar in UpdateActiveAdministratorInput if only internal custom fields defined on Administrator 1`] = ` +"scalar JSON + +input UpdateActiveAdministratorInput { + placeholder: String + customFields: JSON +}" +`; + exports[`addOrderLineCustomFieldsInput() > Modifies the schema when the addItemToOrder & adjustOrderLine mutation is present 1`] = ` "type Mutation { addItemToOrder(id: ID!, quantity: Int!, customFields: OrderLineCustomFieldsInput = null): Boolean diff --git a/packages/core/src/api/config/graphql-custom-fields.spec.ts b/packages/core/src/api/config/graphql-custom-fields.spec.ts index 08f0122e3c..4dde7a6b79 100644 --- a/packages/core/src/api/config/graphql-custom-fields.spec.ts +++ b/packages/core/src/api/config/graphql-custom-fields.spec.ts @@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest'; import { CustomFieldConfig, CustomFields } from '../../config/custom-field/custom-field-types'; import { + addActiveAdministratorCustomFields, addGraphQLCustomFields, addOrderLineCustomFieldsInput, addRegisterCustomerCustomFieldsInput, @@ -23,6 +24,29 @@ describe('addGraphQLCustomFields()', () => { expect(printSchema(result)).toMatchSnapshot(); }); + // regression test for + // https://github.com/vendure-ecommerce/vendure/issues/3158 + it('uses JSON scalar in UpdateActiveAdministratorInput if only internal custom fields defined on Administrator', () => { + // custom field that is internal but not readonly - should not cause + // `addActiveAdministratorCustomFields` to assume that + // `UpdateAdministratorCustomFieldsInput` exists + const customFieldConfig: Required> = { + Administrator: [{ name: 'testField', type: 'string', internal: true }], + }; + // `addActiveAdministratorCustomFields` should add customFields to + // UpdateActiveAdministratorInput as a JSON scalar. need to provide + // those types for that to work + const input = ` + scalar JSON + + input UpdateActiveAdministratorInput { + placeholder: String + } + `; + const schema = addActiveAdministratorCustomFields(input, customFieldConfig.Administrator); + expect(printSchema(schema)).toMatchSnapshot(); + }); + it('extends a type', () => { const input = ` type Product { diff --git a/packages/core/src/api/config/graphql-custom-fields.ts b/packages/core/src/api/config/graphql-custom-fields.ts index 14f921b5d6..0aa4c5618f 100644 --- a/packages/core/src/api/config/graphql-custom-fields.ts +++ b/packages/core/src/api/config/graphql-custom-fields.ts @@ -256,7 +256,7 @@ export function addServerConfigCustomFields( const customFieldTypeDefs = ` """ This type is deprecated in v2.2 in favor of the EntityCustomFields type, - which allows custom fields to be defined on user-supplies entities. + which allows custom fields to be defined on user-supplied entities. """ type CustomFields { ${Object.keys(customFieldConfig).reduce( @@ -288,7 +288,9 @@ export function addActiveAdministratorCustomFields( administratorCustomFields: CustomFieldConfig[], ) { const schema = typeof typeDefsOrSchema === 'string' ? buildSchema(typeDefsOrSchema) : typeDefsOrSchema; - const writableCustomFields = administratorCustomFields?.filter(field => field.readonly !== true); + const writableCustomFields = administratorCustomFields?.filter( + field => field.readonly !== true && field.internal !== true, + ); const extension = ` extend input UpdateActiveAdministratorInput { customFields: ${