Skip to content

Commit

Permalink
fix(core): Fix error on internal Administrator customFields (#3159)
Browse files Browse the repository at this point in the history
  • Loading branch information
toBeOfUse authored Oct 31, 2024
1 parent a21f129 commit e03b7f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/api/config/graphql-custom-fields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Pick<CustomFields, 'Administrator'>> = {
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 {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/api/config/graphql-custom-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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: ${
Expand Down

0 comments on commit e03b7f0

Please sign in to comment.