| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582 |
- import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types';
- import {
- Asset,
- CustomFields,
- Logger,
- mergeConfig,
- OrderService,
- ProductService,
- RequestContextService,
- TransactionalConnection,
- } from '@vendure/core';
- import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
- import { fail } from 'node:assert';
- import path from 'node:path';
- import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import { graphql, ResultOf } from './graphql/graphql-admin';
- import { graphql as graphqlShop, ResultOf as ResultOfShop } from './graphql/graphql-shop';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- import { fixPostgresTimezone } from './utils/fix-pg-timezone';
- fixPostgresTimezone();
- const validateInjectorSpy = vi.fn();
- const customConfig = mergeConfig(testConfig(), {
- dbConnectionOptions: {
- timezone: 'Z',
- },
- customFields: {
- Product: [
- { name: 'nullable', type: 'string' },
- { name: 'notNullable', type: 'string', nullable: false, defaultValue: '' },
- { name: 'stringWithDefault', type: 'string', defaultValue: 'hello' },
- { name: 'localeStringWithDefault', type: 'localeString', defaultValue: 'hola' },
- { name: 'intWithDefault', type: 'int', defaultValue: 5 },
- { name: 'floatWithDefault', type: 'float', defaultValue: 5.5678 },
- { name: 'booleanWithDefault', type: 'boolean', defaultValue: true },
- {
- name: 'dateTimeWithDefault',
- type: 'datetime',
- defaultValue: new Date('2019-04-30T12:59:16.4158386Z'),
- },
- { name: 'validateString', type: 'string', pattern: '^[0-9][a-z]+$' },
- { name: 'validateLocaleString', type: 'localeString', pattern: '^[0-9][a-z]+$' },
- { name: 'validateInt', type: 'int', min: 0, max: 10 },
- { name: 'validateFloat', type: 'float', min: 0.5, max: 10.5 },
- {
- name: 'validateDateTime',
- type: 'datetime',
- min: '2019-01-01T08:30',
- max: '2019-06-01T08:30',
- },
- {
- name: 'validateFn1',
- type: 'string',
- validate: value => {
- if (value !== 'valid') {
- return `The value ['${value as string}'] is not valid`;
- }
- },
- },
- {
- name: 'validateFn2',
- type: 'string',
- validate: value => {
- if (value !== 'valid') {
- return [
- {
- languageCode: LanguageCode.en,
- value: `The value ['${value as string}'] is not valid`,
- },
- ];
- }
- },
- },
- {
- name: 'validateFn3',
- type: 'string',
- validate: (value, injector) => {
- const connection = injector.get(TransactionalConnection);
- validateInjectorSpy(connection);
- },
- },
- {
- name: 'validateFn4',
- type: 'string',
- validate: async (value, injector) => {
- await new Promise(resolve => setTimeout(resolve, 1));
- return 'async error';
- },
- },
- {
- name: 'validateRelation',
- type: 'relation',
- entity: Asset,
- validate: async value => {
- await new Promise(resolve => setTimeout(resolve, 1));
- return 'relation error';
- },
- },
- {
- name: 'stringWithOptions',
- type: 'string',
- options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
- },
- {
- name: 'nullableStringWithOptions',
- type: 'string',
- nullable: true,
- options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
- },
- {
- name: 'nonPublic',
- type: 'string',
- defaultValue: 'hi!',
- public: false,
- },
- {
- name: 'public',
- type: 'string',
- defaultValue: 'ho!',
- public: true,
- },
- {
- name: 'longString',
- type: 'string',
- length: 10000,
- },
- {
- name: 'longLocaleString',
- type: 'localeString',
- length: 10000,
- },
- {
- name: 'readonlyString',
- type: 'string',
- readonly: true,
- },
- {
- name: 'internalString',
- type: 'string',
- internal: true,
- },
- {
- name: 'stringList',
- type: 'string',
- list: true,
- },
- {
- name: 'localeStringList',
- type: 'localeString',
- list: true,
- },
- {
- name: 'stringListWithDefault',
- type: 'string',
- list: true,
- defaultValue: ['cat'],
- },
- {
- name: 'intListWithValidation',
- type: 'int',
- list: true,
- validate: value => {
- if (!value.includes(42)) {
- return 'Must include the number 42!';
- }
- },
- },
- {
- name: 'uniqueString',
- type: 'string',
- unique: true,
- },
- ],
- Facet: [
- {
- name: 'translated',
- type: 'localeString',
- },
- ],
- Customer: [
- {
- name: 'score',
- type: 'int',
- readonly: true,
- },
- ],
- Collection: [
- { name: 'secretKey1', type: 'string', defaultValue: '', public: false, internal: true },
- { name: 'secretKey2', type: 'string', defaultValue: '', public: false, internal: false },
- ],
- OrderLine: [{ name: 'validateInt', type: 'int', min: 0, max: 10 }],
- ProductVariantPrice: [
- {
- name: 'costPrice',
- type: 'int',
- },
- ],
- // Single readonly Address custom field to test
- // https://github.com/vendurehq/vendure/issues/3326
- Address: [
- {
- name: 'hereId',
- type: 'string',
- readonly: true,
- nullable: true,
- },
- ],
- } as CustomFields,
- });
- describe('Custom fields', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
- // Product guard for nullable results with customFields
- type ProductWithCustomFields = NonNullable<ResultOf<typeof getProductNullableDocument>['product']>;
- const productGuard: ErrorResultGuard<ProductWithCustomFields> = createErrorResultGuard(
- input => input !== null && 'customFields' in input,
- );
- // Collection guard for shop API
- type CollectionResult = NonNullable<ResultOfShop<typeof getCollectionCustomFieldsDocument>['collection']>;
- const collectionGuard: ErrorResultGuard<CollectionResult> = createErrorResultGuard(
- input => input !== null,
- );
- // ProductVariant guard (custom document with price customFields)
- type ProductVariantResult = ResultOf<
- typeof updateProductVariantsWithPriceCustomFieldsDocument
- >['updateProductVariants'][number];
- const productVariantGuard: ErrorResultGuard<ProductVariantResult> = createErrorResultGuard(
- input => input !== null && 'prices' in input,
- );
- // Order guard for shop API validation tests
- type OrderWithLines = { id: string; lines: any[] };
- const orderGuard: ErrorResultGuard<OrderWithLines> = createErrorResultGuard(input => 'lines' in input);
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('globalSettings.serverConfig.customFieldConfig', async () => {
- const { globalSettings } = await adminClient.query(getServerConfigCustomFieldsDocument);
- expect(globalSettings.serverConfig.customFieldConfig).toEqual({
- Product: [
- { name: 'nullable', type: 'string', list: false },
- { name: 'notNullable', type: 'string', list: false },
- { name: 'stringWithDefault', type: 'string', list: false },
- { name: 'localeStringWithDefault', type: 'localeString', list: false },
- { name: 'intWithDefault', type: 'int', list: false },
- { name: 'floatWithDefault', type: 'float', list: false },
- { name: 'booleanWithDefault', type: 'boolean', list: false },
- { name: 'dateTimeWithDefault', type: 'datetime', list: false },
- { name: 'validateString', type: 'string', list: false },
- { name: 'validateLocaleString', type: 'localeString', list: false },
- { name: 'validateInt', type: 'int', list: false },
- { name: 'validateFloat', type: 'float', list: false },
- { name: 'validateDateTime', type: 'datetime', list: false },
- { name: 'validateFn1', type: 'string', list: false },
- { name: 'validateFn2', type: 'string', list: false },
- { name: 'validateFn3', type: 'string', list: false },
- { name: 'validateFn4', type: 'string', list: false },
- {
- name: 'validateRelation',
- type: 'relation',
- list: false,
- scalarFields: [
- 'id',
- 'createdAt',
- 'updatedAt',
- 'languageCode',
- 'name',
- 'type',
- 'fileSize',
- 'mimeType',
- 'width',
- 'height',
- 'source',
- 'preview',
- 'customFields',
- ],
- },
- { name: 'stringWithOptions', type: 'string', list: false },
- { name: 'nullableStringWithOptions', type: 'string', list: false },
- { name: 'nonPublic', type: 'string', list: false },
- { name: 'public', type: 'string', list: false },
- { name: 'longString', type: 'string', list: false },
- { name: 'longLocaleString', type: 'localeString', list: false },
- { name: 'readonlyString', type: 'string', list: false },
- { name: 'stringList', type: 'string', list: true },
- { name: 'localeStringList', type: 'localeString', list: true },
- { name: 'stringListWithDefault', type: 'string', list: true },
- { name: 'intListWithValidation', type: 'int', list: true },
- { name: 'uniqueString', type: 'string', list: false },
- // The internal type should not be exposed at all
- // { name: 'internalString', type: 'string' },
- ],
- });
- });
- it('globalSettings.serverConfig.entityCustomFields', async () => {
- const { globalSettings } = await adminClient.query(getServerConfigEntityCustomFieldsDocument);
- const productCustomFields = globalSettings.serverConfig.entityCustomFields.find(
- e => e.entityName === 'Product',
- );
- expect(productCustomFields).toEqual({
- entityName: 'Product',
- customFields: [
- { name: 'nullable', type: 'string', list: false },
- { name: 'notNullable', type: 'string', list: false },
- { name: 'stringWithDefault', type: 'string', list: false },
- { name: 'localeStringWithDefault', type: 'localeString', list: false },
- { name: 'intWithDefault', type: 'int', list: false },
- { name: 'floatWithDefault', type: 'float', list: false },
- { name: 'booleanWithDefault', type: 'boolean', list: false },
- { name: 'dateTimeWithDefault', type: 'datetime', list: false },
- { name: 'validateString', type: 'string', list: false },
- { name: 'validateLocaleString', type: 'localeString', list: false },
- { name: 'validateInt', type: 'int', list: false },
- { name: 'validateFloat', type: 'float', list: false },
- { name: 'validateDateTime', type: 'datetime', list: false },
- { name: 'validateFn1', type: 'string', list: false },
- { name: 'validateFn2', type: 'string', list: false },
- { name: 'validateFn3', type: 'string', list: false },
- { name: 'validateFn4', type: 'string', list: false },
- {
- name: 'validateRelation',
- type: 'relation',
- list: false,
- scalarFields: [
- 'id',
- 'createdAt',
- 'updatedAt',
- 'languageCode',
- 'name',
- 'type',
- 'fileSize',
- 'mimeType',
- 'width',
- 'height',
- 'source',
- 'preview',
- 'customFields',
- ],
- },
- { name: 'stringWithOptions', type: 'string', list: false },
- { name: 'nullableStringWithOptions', type: 'string', list: false },
- { name: 'nonPublic', type: 'string', list: false },
- { name: 'public', type: 'string', list: false },
- { name: 'longString', type: 'string', list: false },
- { name: 'longLocaleString', type: 'localeString', list: false },
- { name: 'readonlyString', type: 'string', list: false },
- { name: 'stringList', type: 'string', list: true },
- { name: 'localeStringList', type: 'localeString', list: true },
- { name: 'stringListWithDefault', type: 'string', list: true },
- { name: 'intListWithValidation', type: 'int', list: true },
- { name: 'uniqueString', type: 'string', list: false },
- // The internal type should not be exposed at all
- // { name: 'internalString', type: 'string' },
- ],
- });
- });
- it('get nullable with no default', async () => {
- const { product } = await adminClient.query(getProductNullableDocument, { id: 'T_1' });
- expect(product).toEqual({
- id: 'T_1',
- name: 'Laptop',
- customFields: {
- nullable: null,
- },
- });
- });
- it('get entity with localeString only', async () => {
- const { facet } = await adminClient.query(getFacetCustomFieldsDocument, { id: 'T_1' });
- expect(facet).toEqual({
- id: 'T_1',
- name: 'category',
- customFields: {
- translated: null,
- },
- });
- });
- it('get fields with default values', async () => {
- const { product } = await adminClient.query(getProductWithDefaultsDocument, { id: 'T_1' });
- const customFields = {
- stringWithDefault: 'hello',
- localeStringWithDefault: 'hola',
- intWithDefault: 5,
- floatWithDefault: 5.5678,
- booleanWithDefault: true,
- dateTimeWithDefault: '2019-04-30T12:59:16.415Z',
- // MySQL does not support defaults on TEXT fields, which is what "simple-json" uses
- // internally. See https://stackoverflow.com/q/3466872/772859
- stringListWithDefault: customConfig.dbConnectionOptions.type === 'mysql' ? null : ['cat'],
- };
- expect(product).toEqual({
- id: 'T_1',
- name: 'Laptop',
- customFields,
- });
- });
- it(
- 'update non-nullable field',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { notNullable: null } }) {
- id
- }
- }
- `),
- );
- }, 'The custom field "notNullable" value cannot be set to null'),
- );
- it(
- 'throws on attempt to update readonly field',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { readonlyString: "hello" } }) {
- id
- }
- }
- `),
- );
- }, 'Field "readonlyString" is not defined by type "UpdateProductCustomFieldsInput"'),
- );
- it(
- 'throws on attempt to update readonly field when no other custom fields defined',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateCustomer(input: { id: "T_1", customFields: { score: 5 } }) {
- ... on Customer {
- id
- }
- }
- }
- `),
- );
- }, 'The custom field "score" is readonly'),
- );
- it(
- 'throws on attempt to create readonly field',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- createProduct(
- input: {
- translations: [{ languageCode: en, name: "test" }]
- customFields: { readonlyString: "hello" }
- }
- ) {
- id
- }
- }
- `),
- );
- }, 'Field "readonlyString" is not defined by type "CreateProductCustomFieldsInput"'),
- );
- it('string length allows long strings', async () => {
- const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
- const result = await adminClient.query(updateProductLongStringDocument, {
- id: 'T_1',
- stringValue: longString,
- });
- expect(result.updateProduct.customFields.longString).toBe(longString);
- });
- it('string length allows long localeStrings', async () => {
- const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
- const result = await adminClient.query(updateProductLongLocaleStringDocument, {
- id: 'T_1',
- stringValue: longString,
- });
- expect(result.updateProduct.customFields.longLocaleString).toBe(longString);
- });
- describe('validation', () => {
- it(
- 'invalid string',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateString: "hello" } }) {
- id
- }
- }
- `),
- );
- }, 'The custom field "validateString" value ["hello"] does not match the pattern [^[0-9][a-z]+$]'),
- );
- it(
- 'invalid string option',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "tiny" } }) {
- id
- }
- }
- `),
- );
- }, "The custom field \"stringWithOptions\" value [\"tiny\"] is invalid. Valid options are ['small', 'medium', 'large']"),
- );
- it('valid string option', async () => {
- const { updateProduct } = await adminClient.query(updateProductStringOptionsDocument, {
- id: 'T_1',
- value: 'medium',
- });
- expect(updateProduct.customFields.stringWithOptions).toBe('medium');
- });
- it('nullable string option with null', async () => {
- const { updateProduct } = await adminClient.query(updateProductNullableStringOptionsDocument, {
- id: 'T_1',
- value: null,
- });
- expect(updateProduct.customFields.nullableStringWithOptions).toBeNull();
- });
- it(
- 'invalid localeString',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(
- input: {
- id: "T_1"
- translations: [
- {
- id: "T_1"
- languageCode: en
- customFields: { validateLocaleString: "servus" }
- }
- ]
- }
- ) {
- id
- }
- }
- `),
- );
- }, 'The custom field "validateLocaleString" value ["servus"] does not match the pattern [^[0-9][a-z]+$]'),
- );
- it(
- 'invalid int',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateInt: 12 } }) {
- id
- }
- }
- `),
- );
- }, 'The custom field "validateInt" value [12] is greater than the maximum [10]'),
- );
- it(
- 'invalid float',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateFloat: 10.6 } }) {
- id
- }
- }
- `),
- );
- }, 'The custom field "validateFloat" value [10.6] is greater than the maximum [10.5]'),
- );
- it(
- 'invalid datetime',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(
- input: {
- id: "T_1"
- customFields: { validateDateTime: "2019-01-01T05:25:00.000Z" }
- }
- ) {
- id
- }
- }
- `),
- );
- }, 'The custom field "validateDateTime" value [2019-01-01T05:25:00.000Z] is less than the minimum [2019-01-01T08:30]'),
- );
- it(
- 'invalid validate function with string',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateFn1: "invalid" } }) {
- id
- }
- }
- `),
- );
- }, "The value ['invalid'] is not valid"),
- );
- it(
- 'invalid validate function with localized string',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateFn2: "invalid" } }) {
- id
- }
- }
- `),
- );
- }, "The value ['invalid'] is not valid"),
- );
- it(
- 'invalid list field',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(
- input: { id: "T_1", customFields: { intListWithValidation: [1, 2, 3] } }
- ) {
- id
- }
- }
- `),
- );
- }, 'Must include the number 42!'),
- );
- it('valid list field', async () => {
- const { updateProduct } = await adminClient.query(updateProductIntListDocument, {
- id: 'T_1',
- values: [1, 42, 3],
- });
- expect(updateProduct.customFields.intListWithValidation).toEqual([1, 42, 3]);
- });
- it('can inject providers into validation fn', async () => {
- const { updateProduct } = await adminClient.query(updateProductValidateFn3Document, {
- id: 'T_1',
- value: 'some value',
- });
- expect(updateProduct.customFields.validateFn3).toBe('some value');
- expect(validateInjectorSpy).toHaveBeenCalledTimes(1);
- expect(validateInjectorSpy.mock.calls[0][0] instanceof TransactionalConnection).toBe(true);
- });
- it(
- 'supports async validation fn',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateFn4: "some value" } }) {
- id
- customFields {
- validateFn4
- }
- }
- }
- `),
- );
- }, 'async error'),
- );
- // https://github.com/vendurehq/vendure/issues/1000
- it(
- 'supports validation of relation types',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- mutation {
- updateProduct(input: { id: "T_1", customFields: { validateRelationId: "T_1" } }) {
- id
- customFields {
- validateFn4
- }
- }
- }
- `),
- );
- }, 'relation error'),
- );
- // https://github.com/vendurehq/vendure/issues/1091
- it('handles well graphql internal fields', async () => {
- // throws "Cannot read property 'args' of undefined" if broken
- await adminClient.query(
- graphql(`
- mutation {
- __typename
- updateProduct(input: { id: "T_1", customFields: { nullable: "some value" } }) {
- __typename
- id
- customFields {
- __typename
- nullable
- }
- }
- }
- `),
- );
- });
- // https://github.com/vendurehq/vendure/issues/1953
- describe('validation of OrderLine custom fields', () => {
- it('addItemToOrder', async () => {
- try {
- const { addItemToOrder } = await shopClient.query(
- graphqlShop(`
- mutation {
- addItemToOrder(
- productVariantId: 1
- quantity: 1
- customFields: { validateInt: 11 }
- ) {
- ... on Order {
- id
- }
- }
- }
- `),
- );
- fail('Should have thrown');
- } catch (e) {
- expect(e.message).toContain(
- 'The custom field "validateInt" value [11] is greater than the maximum [10]',
- );
- }
- const { addItemToOrder: result } = await shopClient.query(
- addItemToOrderWithCustomFieldsDocument,
- {
- productVariantId: '1',
- quantity: 1,
- validateInt: 9,
- },
- );
- orderGuard.assertSuccess(result);
- expect(result.lines[0].customFields).toEqual({ validateInt: 9 });
- });
- it('adjustOrderLine', async () => {
- try {
- const { adjustOrderLine } = await shopClient.query(
- graphqlShop(`
- mutation {
- adjustOrderLine(
- orderLineId: "T_1"
- quantity: 1
- customFields: { validateInt: 11 }
- ) {
- ... on Order {
- id
- }
- }
- }
- `),
- );
- fail('Should have thrown');
- } catch (e) {
- expect(e.message).toContain(
- 'The custom field "validateInt" value [11] is greater than the maximum [10]',
- );
- }
- const { adjustOrderLine: result } = await shopClient.query(
- adjustOrderLineWithCustomFieldsDocument,
- {
- orderLineId: 'T_1',
- quantity: 1,
- validateInt: 2,
- },
- );
- orderGuard.assertSuccess(result);
- expect(result.lines[0].customFields).toEqual({ validateInt: 2 });
- });
- });
- });
- describe('public access', () => {
- it(
- 'non-public throws for Shop API',
- assertThrowsWithMessage(async () => {
- await shopClient.query(
- graphqlShop(`
- query {
- product(id: "T_1") {
- id
- customFields {
- nonPublic
- }
- }
- }
- `),
- );
- }, 'Cannot query field "nonPublic" on type "ProductCustomFields"'),
- );
- it('publicly accessible via Shop API', async () => {
- const { product } = await shopClient.query(getShopProductPublicCustomFieldDocument, {
- id: 'T_1',
- });
- if (!product) {
- throw new Error('Product not found');
- }
- productGuard.assertSuccess(product);
- expect(product.customFields.public).toBe('ho!');
- });
- it(
- 'internal throws for Shop API',
- assertThrowsWithMessage(async () => {
- await shopClient.query(
- graphqlShop(`
- query {
- product(id: "T_1") {
- id
- customFields {
- internalString
- }
- }
- }
- `),
- );
- }, 'Cannot query field "internalString" on type "ProductCustomFields"'),
- );
- it(
- 'internal throws for Admin API',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- query {
- product(id: "T_1") {
- id
- customFields {
- internalString
- }
- }
- }
- `),
- );
- }, 'Cannot query field "internalString" on type "ProductCustomFields"'),
- );
- // https://github.com/vendurehq/vendure/issues/3049
- it('does not leak private fields via JSON type', async () => {
- const { collection } = await shopClient.query(getCollectionCustomFieldsDocument, { id: 'T_1' });
- collectionGuard.assertSuccess(collection);
- expect(collection.customFields).toBe(null);
- });
- });
- describe('sort & filter', () => {
- it('can sort by custom fields', async () => {
- const { products } = await adminClient.query(getProductsSortByNullableDocument);
- expect(products.totalItems).toBe(1);
- });
- // https://github.com/vendurehq/vendure/issues/1581
- it('can sort by localeString custom fields', async () => {
- const { products } = await adminClient.query(getProductsSortByLocaleStringDocument);
- expect(products.totalItems).toBe(1);
- });
- it('can filter by custom fields', async () => {
- const { products } = await adminClient.query(getProductsFilterByStringDocument);
- expect(products.totalItems).toBe(1);
- });
- it('can filter by localeString custom fields', async () => {
- const { products } = await adminClient.query(getProductsFilterByLocaleStringDocument);
- expect(products.totalItems).toBe(1);
- });
- it('can filter by custom list fields', async () => {
- const { products: result1 } = await adminClient.query(getProductsFilterByIntListDocument, {
- value: 42,
- });
- expect(result1.totalItems).toBe(1);
- const { products: result2 } = await adminClient.query(getProductsFilterByIntListDocument, {
- value: 43,
- });
- expect(result2.totalItems).toBe(0);
- });
- it(
- 'cannot sort by custom list fields',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- query {
- products(options: { sort: { intListWithValidation: ASC } }) {
- totalItems
- }
- }
- `),
- );
- }, 'Field "intListWithValidation" is not defined by type "ProductSortParameter".'),
- );
- it(
- 'cannot filter by internal field in Admin API',
- assertThrowsWithMessage(async () => {
- await adminClient.query(
- graphql(`
- query {
- products(options: { filter: { internalString: { contains: "hello" } } }) {
- totalItems
- }
- }
- `),
- );
- }, 'Field "internalString" is not defined by type "ProductFilterParameter"'),
- );
- it(
- 'cannot filter by internal field in Shop API',
- assertThrowsWithMessage(async () => {
- await shopClient.query(
- graphqlShop(`
- query {
- products(options: { filter: { internalString: { contains: "hello" } } }) {
- totalItems
- }
- }
- `),
- );
- }, 'Field "internalString" is not defined by type "ProductFilterParameter"'),
- );
- });
- describe('product on productVariant entity', () => {
- it('is translated', async () => {
- const { productVariants } = await adminClient.query(getProductVariantsWithCustomFieldsDocument, {
- productId: 'T_1',
- });
- expect(productVariants.items[0].product).toEqual({
- id: 'T_1',
- name: 'Laptop',
- customFields: {
- localeStringWithDefault: 'hola',
- stringWithDefault: 'hello',
- },
- });
- });
- });
- describe('unique constraint', () => {
- it('setting unique value works', async () => {
- const result = await adminClient.query(updateProductUniqueStringDocument, {
- id: 'T_1',
- value: 'foo',
- });
- expect(result.updateProduct.customFields.uniqueString).toBe('foo');
- });
- it('setting conflicting value fails', async () => {
- try {
- await adminClient.query(createProductUniqueStringDocument, { uniqueString: 'foo' });
- fail('Should have thrown');
- } catch (e: any) {
- let duplicateKeyErrMessage = 'unassigned';
- switch (customConfig.dbConnectionOptions.type) {
- case 'mariadb':
- case 'mysql':
- duplicateKeyErrMessage = "Duplicate entry 'foo' for key";
- break;
- case 'postgres':
- duplicateKeyErrMessage = 'duplicate key value violates unique constraint';
- break;
- case 'sqlite':
- case 'sqljs':
- duplicateKeyErrMessage = 'UNIQUE constraint failed: product.customFieldsUniquestring';
- break;
- }
- expect(e.message).toContain(duplicateKeyErrMessage);
- }
- });
- });
- it('on ProductVariantPrice', async () => {
- const { updateProductVariants } = await adminClient.query(
- updateProductVariantsWithPriceCustomFieldsDocument,
- {
- input: [
- {
- id: 'T_1',
- prices: [
- {
- price: 129900,
- currencyCode: 'USD',
- customFields: {
- costPrice: 100,
- },
- },
- ],
- },
- ],
- },
- );
- productVariantGuard.assertSuccess(updateProductVariants[0]);
- if (!updateProductVariants[0]) {
- throw new Error('Update product variants failed');
- }
- expect(updateProductVariants[0].prices).toEqual([
- {
- currencyCode: 'USD',
- price: 129900,
- customFields: {
- costPrice: 100,
- },
- },
- ]);
- });
- // https://github.com/vendurehq/vendure/issues/3909
- it('persists custom fields when creating a new ProductVariantPrice', async () => {
- // First, add EUR to the channel's available currencies
- await adminClient.query(
- graphql(`
- mutation UpdateChannel($input: UpdateChannelInput!) {
- updateChannel(input: $input) {
- ... on Channel {
- id
- availableCurrencyCodes
- }
- }
- }
- `),
- {
- input: {
- id: 'T_1',
- availableCurrencyCodes: [CurrencyCode.USD, CurrencyCode.EUR],
- },
- },
- );
- // Now create a new price in EUR with custom fields
- const { updateProductVariants } = await adminClient.query(
- graphql(`
- mutation UpdateProductVariants($input: [UpdateProductVariantInput!]!) {
- updateProductVariants(input: $input) {
- id
- prices {
- currencyCode
- price
- customFields {
- costPrice
- }
- }
- }
- }
- `),
- {
- input: [
- {
- id: 'T_1',
- prices: [
- {
- price: 129900,
- currencyCode: 'USD',
- customFields: {
- costPrice: 100,
- },
- },
- {
- price: 99900,
- currencyCode: 'EUR',
- customFields: {
- costPrice: 200,
- },
- },
- ],
- },
- ],
- },
- );
- expect(updateProductVariants[0].prices).toContainEqual({
- currencyCode: 'EUR',
- price: 99900,
- customFields: {
- costPrice: 200,
- },
- });
- });
- describe('setting custom fields directly via a service method', () => {
- it('OrderService.addItemToOrder warns on unknown custom field', async () => {
- const orderService = server.app.get(OrderService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const order = await orderService.create(ctx);
- const warnSpy = vi.spyOn(Logger, 'warn');
- await orderService.addItemToOrder(ctx, order.id, 1, 1, {
- customFieldWhichDoesNotExist: 'test value',
- });
- expect(warnSpy).toHaveBeenCalledWith(
- 'Custom field customFieldWhichDoesNotExist not found for entity OrderLine',
- );
- });
- it('OrderService.addItemToOrder does not warn on known custom field', async () => {
- const orderService = server.app.get(OrderService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const order = await orderService.create(ctx);
- const warnSpy = vi.spyOn(Logger, 'warn');
- await orderService.addItemToOrder(ctx, order.id, 1, 1, {
- validateInt: 1,
- });
- expect(warnSpy).not.toHaveBeenCalled();
- });
- it('OrderService.addItemToOrder warns on multiple unknown custom fields', async () => {
- const orderService = server.app.get(OrderService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const order = await orderService.create(ctx);
- const warnSpy = vi.spyOn(Logger, 'warn');
- await orderService.addItemToOrder(ctx, order.id, 1, 1, {
- unknownField1: 'foo',
- unknownField2: 'bar',
- });
- expect(warnSpy).toHaveBeenCalledWith('Custom field unknownField1 not found for entity OrderLine');
- expect(warnSpy).toHaveBeenCalledWith('Custom field unknownField2 not found for entity OrderLine');
- });
- it('OrderService.addItemToOrder does not warn when no custom fields are provided', async () => {
- const orderService = server.app.get(OrderService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const order = await orderService.create(ctx);
- const warnSpy = vi.spyOn(Logger, 'warn');
- await orderService.addItemToOrder(ctx, order.id, 1, 1);
- expect(warnSpy).not.toHaveBeenCalled();
- });
- it('warns on unknown custom field in ProductTranslation entity', async () => {
- const productService = server.app.get(ProductService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const warnSpy = vi.spyOn(Logger, 'warn');
- await productService.create(ctx, {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'test',
- slug: 'test',
- description: '',
- customFields: { customFieldWhichDoesNotExist: 'foo' },
- },
- ],
- });
- expect(warnSpy).toHaveBeenCalledWith(
- 'Custom field customFieldWhichDoesNotExist not found for entity ProductTranslation',
- );
- });
- it('does not warn when Translation has a valid custom field', async () => {
- const productService = server.app.get(ProductService);
- const requestContextService = server.app.get(RequestContextService);
- const ctx = await requestContextService.create({
- apiType: 'admin',
- });
- const warnSpy = vi.spyOn(Logger, 'warn');
- await productService.create(ctx, {
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'test',
- slug: 'test',
- description: '',
- customFields: { localeStringWithDefault: 'foo' },
- },
- ],
- });
- expect(warnSpy).not.toHaveBeenCalled();
- });
- });
- });
- const getServerConfigCustomFieldsDocument = graphql(`
- query GetServerConfigCustomFields {
- globalSettings {
- serverConfig {
- customFieldConfig {
- Product {
- ... on CustomField {
- name
- type
- list
- }
- ... on RelationCustomFieldConfig {
- scalarFields
- }
- }
- }
- }
- }
- }
- `);
- const getServerConfigEntityCustomFieldsDocument = graphql(`
- query GetServerConfigEntityCustomFields {
- globalSettings {
- serverConfig {
- entityCustomFields {
- entityName
- customFields {
- ... on CustomField {
- name
- type
- list
- }
- ... on RelationCustomFieldConfig {
- scalarFields
- }
- }
- }
- }
- }
- }
- `);
- const getProductNullableDocument = graphql(`
- query GetProductNullable($id: ID!) {
- product(id: $id) {
- id
- name
- customFields {
- nullable
- }
- }
- }
- `);
- const getProductWithDefaultsDocument = graphql(`
- query GetProductWithDefaults($id: ID!) {
- product(id: $id) {
- id
- name
- customFields {
- stringWithDefault
- localeStringWithDefault
- intWithDefault
- floatWithDefault
- booleanWithDefault
- dateTimeWithDefault
- stringListWithDefault
- }
- }
- }
- `);
- const updateProductLongStringDocument = graphql(`
- mutation UpdateProductLongString($id: ID!, $stringValue: String!) {
- updateProduct(input: { id: $id, customFields: { longString: $stringValue } }) {
- id
- customFields {
- longString
- }
- }
- }
- `);
- const updateProductLongLocaleStringDocument = graphql(`
- mutation UpdateProductLongLocaleString($id: ID!, $stringValue: String!) {
- updateProduct(
- input: {
- id: $id
- translations: [{ languageCode: en, customFields: { longLocaleString: $stringValue } }]
- }
- ) {
- id
- customFields {
- longLocaleString
- }
- }
- }
- `);
- const updateProductStringOptionsDocument = graphql(`
- mutation UpdateProductStringOptions($id: ID!, $value: String!) {
- updateProduct(input: { id: $id, customFields: { stringWithOptions: $value } }) {
- id
- customFields {
- stringWithOptions
- }
- }
- }
- `);
- const updateProductNullableStringOptionsDocument = graphql(`
- mutation UpdateProductNullableStringOptions($id: ID!, $value: String) {
- updateProduct(input: { id: $id, customFields: { nullableStringWithOptions: $value } }) {
- id
- customFields {
- nullableStringWithOptions
- }
- }
- }
- `);
- const updateProductIntListDocument = graphql(`
- mutation UpdateProductIntList($id: ID!, $values: [Int!]!) {
- updateProduct(input: { id: $id, customFields: { intListWithValidation: $values } }) {
- id
- customFields {
- intListWithValidation
- }
- }
- }
- `);
- const updateProductValidateFn3Document = graphql(`
- mutation UpdateProductValidateFn3($id: ID!, $value: String!) {
- updateProduct(input: { id: $id, customFields: { validateFn3: $value } }) {
- id
- customFields {
- validateFn3
- }
- }
- }
- `);
- const updateProductUniqueStringDocument = graphql(`
- mutation UpdateProductUniqueString($id: ID!, $value: String!) {
- updateProduct(input: { id: $id, customFields: { uniqueString: $value } }) {
- id
- customFields {
- uniqueString
- }
- }
- }
- `);
- const createProductUniqueStringDocument = graphql(`
- mutation CreateProductUniqueString($uniqueString: String!) {
- createProduct(
- input: {
- translations: [{ languageCode: en, name: "test 2", slug: "test-2", description: "" }]
- customFields: { uniqueString: $uniqueString }
- }
- ) {
- id
- }
- }
- `);
- const getProductVariantsWithCustomFieldsDocument = graphql(`
- query GetProductVariantsWithCustomFields($productId: ID!) {
- productVariants(productId: $productId) {
- items {
- product {
- name
- id
- customFields {
- localeStringWithDefault
- stringWithDefault
- }
- }
- }
- }
- }
- `);
- const getFacetCustomFieldsDocument = graphql(`
- query GetFacetCustomFields($id: ID!) {
- facet(id: $id) {
- id
- name
- customFields {
- translated
- }
- }
- }
- `);
- const getCollectionCustomFieldsDocument = graphqlShop(`
- query GetCollectionCustomFields($id: ID!) {
- collection(id: $id) {
- id
- customFields
- }
- }
- `);
- const getProductsSortByNullableDocument = graphql(`
- query GetProductsSortByNullable {
- products(options: { sort: { nullable: ASC } }) {
- totalItems
- }
- }
- `);
- const getProductsSortByLocaleStringDocument = graphql(`
- query GetProductsSortByLocaleString {
- products(options: { sort: { localeStringWithDefault: ASC } }) {
- totalItems
- }
- }
- `);
- const getProductsFilterByStringDocument = graphql(`
- query GetProductsFilterByString {
- products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
- totalItems
- }
- }
- `);
- const getProductsFilterByLocaleStringDocument = graphql(`
- query GetProductsFilterByLocaleString {
- products(options: { filter: { localeStringWithDefault: { contains: "hola" } } }) {
- totalItems
- }
- }
- `);
- const getProductsFilterByIntListDocument = graphql(`
- query GetProductsFilterByIntList($value: Float!) {
- products(options: { filter: { intListWithValidation: { inList: $value } } }) {
- totalItems
- }
- }
- `);
- const addItemToOrderWithCustomFieldsDocument = graphqlShop(`
- mutation AddItemToOrderWithCustomFields($productVariantId: ID!, $quantity: Int!, $validateInt: Int!) {
- addItemToOrder(productVariantId: $productVariantId, quantity: $quantity, customFields: { validateInt: $validateInt }) {
- ... on Order {
- id
- lines {
- customFields {
- validateInt
- }
- }
- }
- }
- }
- `);
- const adjustOrderLineWithCustomFieldsDocument = graphqlShop(`
- mutation AdjustOrderLineWithCustomFields($orderLineId: ID!, $quantity: Int!, $validateInt: Int!) {
- adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, customFields: { validateInt: $validateInt }) {
- ... on Order {
- id
- lines {
- customFields {
- validateInt
- }
- }
- }
- }
- }
- `);
- const updateProductVariantsWithPriceCustomFieldsDocument = graphql(`
- mutation UpdateProductVariantsWithPriceCustomFields($input: [UpdateProductVariantInput!]!) {
- updateProductVariants(input: $input) {
- id
- prices {
- currencyCode
- price
- customFields {
- costPrice
- }
- }
- }
- }
- `);
- const getShopProductPublicCustomFieldDocument = graphqlShop(`
- query GetShopProductPublicCustomField($id: ID!) {
- product(id: $id) {
- id
- customFields {
- public
- }
- }
- }
- `);
|