| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- import { printSchema } from 'graphql';
- import { describe, expect, it } from 'vitest';
- import { CustomFieldConfig, CustomFields } from '../../config/custom-field/custom-field-types';
- import {
- addActiveAdministratorCustomFields,
- addGraphQLCustomFields,
- addOrderLineCustomFieldsInput,
- addRegisterCustomerCustomFieldsInput,
- } from './graphql-custom-fields';
- describe('addGraphQLCustomFields()', () => {
- it('uses JSON scalar if no custom fields defined', () => {
- const input = `
- type Product {
- id: ID
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- 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 {
- id: ID
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [{ name: 'available', type: 'boolean' }],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with a translation', () => {
- const input = `
- type Product {
- id: ID
- translations: [ProductTranslation!]!
- }
- type ProductTranslation {
- id: ID
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with a Create input', () => {
- const input = `
- type Product {
- id: ID
- }
- input CreateProductInput {
- image: String
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with an Update input', () => {
- const input = `
- type Product {
- id: ID
- }
- input UpdateProductInput {
- image: String
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with a Create input and a translation', () => {
- const input = `
- type Product {
- id: ID
- }
- type ProductTranslation {
- id: ID
- }
- input ProductTranslationInput {
- id: ID
- }
- input CreateProductInput {
- image: String
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with SortParameters', () => {
- const input = `
- type Product {
- id: ID
- }
- input ProductSortParameter {
- id: SortOrder
- }
- enum SortOrder {
- ASC
- DESC
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends a type with FilterParameters', () => {
- const input = `
- type Product {
- name: String
- }
- input ProductFilterParameter {
- id: StringOperators
- }
- input StringOperators {
- eq: String
- }
- input NumberOperators {
- eq: Float
- }
- input DateOperators {
- eq: String
- }
- input BooleanOperators {
- eq: Boolean
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean' },
- { name: 'shortName', type: 'localeString' },
- { name: 'rating', type: 'float' },
- { name: 'published', type: 'datetime' },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, false);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('publicOnly = true', () => {
- const input = `
- type Product {
- id: ID
- }
- `;
- const customFieldConfig: CustomFields = {
- Product: [
- { name: 'available', type: 'boolean', public: true },
- { name: 'profitMargin', type: 'float', public: false },
- ],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, true);
- expect(printSchema(result)).toMatchSnapshot();
- });
- it('extends OrderAddress if Address custom fields defined', () => {
- const input = `
- type Address {
- id: ID
- streetLine1: String
- }
- type OrderAddress {
- streetLine1: String
- }
- `;
- const customFieldConfig: CustomFields = {
- Address: [{ name: 'instructions', type: 'string' }],
- };
- const result = addGraphQLCustomFields(input, customFieldConfig, true);
- expect(printSchema(result)).toMatchSnapshot();
- });
- });
- describe('addOrderLineCustomFieldsInput()', () => {
- it('Modifies the schema when the addItemToOrder & adjustOrderLine mutation is present', () => {
- const input = `
- type Mutation {
- addItemToOrder(id: ID!, quantity: Int!): Boolean
- adjustOrderLine(id: ID!, quantity: Int): Boolean
- }
- `;
- const customFieldConfig: CustomFieldConfig[] = [
- { name: 'giftWrap', type: 'boolean' },
- { name: 'message', type: 'string' },
- ];
- const result = addOrderLineCustomFieldsInput(input, customFieldConfig);
- expect(printSchema(result)).toMatchSnapshot();
- });
- });
- describe('addRegisterCustomerCustomFieldsInput()', () => {
- it('add public writable custom fields to RegisterCustomerInput', () => {
- const input = `
- input RegisterCustomerInput {
- emailAddress: String!
- title: String
- firstName: String
- lastName: String
- phoneNumber: String
- password: String
- }
- type Mutation {
- registerCustomerAccount(input: RegisterCustomerInput!): Boolean!
- }
- `;
- const customFieldConfig: CustomFieldConfig[] = [
- { name: 'isB2B', type: 'boolean', nullable: false },
- { name: 'message', type: 'string' },
- { name: 'rating', type: 'int', public: false },
- { name: 'dbRef', type: 'int', internal: true },
- ];
- const result = addRegisterCustomerCustomFieldsInput(input, customFieldConfig);
- expect(printSchema(result)).toMatchSnapshot();
- });
- });
|