custom-fields.e2e-spec.ts 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. import { CurrencyCode, LanguageCode } from '@vendure/common/lib/generated-types';
  2. import {
  3. Asset,
  4. CustomFields,
  5. Logger,
  6. mergeConfig,
  7. OrderService,
  8. ProductService,
  9. RequestContextService,
  10. TransactionalConnection,
  11. } from '@vendure/core';
  12. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
  13. import { fail } from 'node:assert';
  14. import path from 'node:path';
  15. import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
  16. import { initialData } from '../../../e2e-common/e2e-initial-data';
  17. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  18. import { graphql, ResultOf } from './graphql/graphql-admin';
  19. import { graphql as graphqlShop, ResultOf as ResultOfShop } from './graphql/graphql-shop';
  20. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  21. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  22. fixPostgresTimezone();
  23. const validateInjectorSpy = vi.fn();
  24. const customConfig = mergeConfig(testConfig(), {
  25. dbConnectionOptions: {
  26. timezone: 'Z',
  27. },
  28. customFields: {
  29. Product: [
  30. { name: 'nullable', type: 'string' },
  31. { name: 'notNullable', type: 'string', nullable: false, defaultValue: '' },
  32. { name: 'stringWithDefault', type: 'string', defaultValue: 'hello' },
  33. { name: 'localeStringWithDefault', type: 'localeString', defaultValue: 'hola' },
  34. { name: 'intWithDefault', type: 'int', defaultValue: 5 },
  35. { name: 'floatWithDefault', type: 'float', defaultValue: 5.5678 },
  36. { name: 'booleanWithDefault', type: 'boolean', defaultValue: true },
  37. {
  38. name: 'dateTimeWithDefault',
  39. type: 'datetime',
  40. defaultValue: new Date('2019-04-30T12:59:16.4158386Z'),
  41. },
  42. { name: 'validateString', type: 'string', pattern: '^[0-9][a-z]+$' },
  43. { name: 'validateLocaleString', type: 'localeString', pattern: '^[0-9][a-z]+$' },
  44. { name: 'validateInt', type: 'int', min: 0, max: 10 },
  45. { name: 'validateFloat', type: 'float', min: 0.5, max: 10.5 },
  46. {
  47. name: 'validateDateTime',
  48. type: 'datetime',
  49. min: '2019-01-01T08:30',
  50. max: '2019-06-01T08:30',
  51. },
  52. {
  53. name: 'validateFn1',
  54. type: 'string',
  55. validate: value => {
  56. if (value !== 'valid') {
  57. return `The value ['${value as string}'] is not valid`;
  58. }
  59. },
  60. },
  61. {
  62. name: 'validateFn2',
  63. type: 'string',
  64. validate: value => {
  65. if (value !== 'valid') {
  66. return [
  67. {
  68. languageCode: LanguageCode.en,
  69. value: `The value ['${value as string}'] is not valid`,
  70. },
  71. ];
  72. }
  73. },
  74. },
  75. {
  76. name: 'validateFn3',
  77. type: 'string',
  78. validate: (value, injector) => {
  79. const connection = injector.get(TransactionalConnection);
  80. validateInjectorSpy(connection);
  81. },
  82. },
  83. {
  84. name: 'validateFn4',
  85. type: 'string',
  86. validate: async (value, injector) => {
  87. await new Promise(resolve => setTimeout(resolve, 1));
  88. return 'async error';
  89. },
  90. },
  91. {
  92. name: 'validateRelation',
  93. type: 'relation',
  94. entity: Asset,
  95. validate: async value => {
  96. await new Promise(resolve => setTimeout(resolve, 1));
  97. return 'relation error';
  98. },
  99. },
  100. {
  101. name: 'stringWithOptions',
  102. type: 'string',
  103. options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
  104. },
  105. {
  106. name: 'nullableStringWithOptions',
  107. type: 'string',
  108. nullable: true,
  109. options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
  110. },
  111. {
  112. name: 'nonPublic',
  113. type: 'string',
  114. defaultValue: 'hi!',
  115. public: false,
  116. },
  117. {
  118. name: 'public',
  119. type: 'string',
  120. defaultValue: 'ho!',
  121. public: true,
  122. },
  123. {
  124. name: 'longString',
  125. type: 'string',
  126. length: 10000,
  127. },
  128. {
  129. name: 'longLocaleString',
  130. type: 'localeString',
  131. length: 10000,
  132. },
  133. {
  134. name: 'readonlyString',
  135. type: 'string',
  136. readonly: true,
  137. },
  138. {
  139. name: 'internalString',
  140. type: 'string',
  141. internal: true,
  142. },
  143. {
  144. name: 'stringList',
  145. type: 'string',
  146. list: true,
  147. },
  148. {
  149. name: 'localeStringList',
  150. type: 'localeString',
  151. list: true,
  152. },
  153. {
  154. name: 'stringListWithDefault',
  155. type: 'string',
  156. list: true,
  157. defaultValue: ['cat'],
  158. },
  159. {
  160. name: 'intListWithValidation',
  161. type: 'int',
  162. list: true,
  163. validate: value => {
  164. if (!value.includes(42)) {
  165. return 'Must include the number 42!';
  166. }
  167. },
  168. },
  169. {
  170. name: 'uniqueString',
  171. type: 'string',
  172. unique: true,
  173. },
  174. ],
  175. Facet: [
  176. {
  177. name: 'translated',
  178. type: 'localeString',
  179. },
  180. ],
  181. Customer: [
  182. {
  183. name: 'score',
  184. type: 'int',
  185. readonly: true,
  186. },
  187. ],
  188. Collection: [
  189. { name: 'secretKey1', type: 'string', defaultValue: '', public: false, internal: true },
  190. { name: 'secretKey2', type: 'string', defaultValue: '', public: false, internal: false },
  191. ],
  192. OrderLine: [{ name: 'validateInt', type: 'int', min: 0, max: 10 }],
  193. ProductVariantPrice: [
  194. {
  195. name: 'costPrice',
  196. type: 'int',
  197. },
  198. ],
  199. // Single readonly Address custom field to test
  200. // https://github.com/vendurehq/vendure/issues/3326
  201. Address: [
  202. {
  203. name: 'hereId',
  204. type: 'string',
  205. readonly: true,
  206. nullable: true,
  207. },
  208. ],
  209. } as CustomFields,
  210. });
  211. describe('Custom fields', () => {
  212. const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
  213. // Product guard for nullable results with customFields
  214. type ProductWithCustomFields = NonNullable<ResultOf<typeof getProductNullableDocument>['product']>;
  215. const productGuard: ErrorResultGuard<ProductWithCustomFields> = createErrorResultGuard(
  216. input => input !== null && 'customFields' in input,
  217. );
  218. // Collection guard for shop API
  219. type CollectionResult = NonNullable<ResultOfShop<typeof getCollectionCustomFieldsDocument>['collection']>;
  220. const collectionGuard: ErrorResultGuard<CollectionResult> = createErrorResultGuard(
  221. input => input !== null,
  222. );
  223. // ProductVariant guard (custom document with price customFields)
  224. type ProductVariantResult = ResultOf<
  225. typeof updateProductVariantsWithPriceCustomFieldsDocument
  226. >['updateProductVariants'][number];
  227. const productVariantGuard: ErrorResultGuard<ProductVariantResult> = createErrorResultGuard(
  228. input => input !== null && 'prices' in input,
  229. );
  230. // Order guard for shop API validation tests
  231. type OrderWithLines = { id: string; lines: any[] };
  232. const orderGuard: ErrorResultGuard<OrderWithLines> = createErrorResultGuard(input => 'lines' in input);
  233. beforeAll(async () => {
  234. await server.init({
  235. initialData,
  236. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  237. customerCount: 1,
  238. });
  239. await adminClient.asSuperAdmin();
  240. }, TEST_SETUP_TIMEOUT_MS);
  241. afterAll(async () => {
  242. await server.destroy();
  243. });
  244. it('globalSettings.serverConfig.customFieldConfig', async () => {
  245. const { globalSettings } = await adminClient.query(getServerConfigCustomFieldsDocument);
  246. expect(globalSettings.serverConfig.customFieldConfig).toEqual({
  247. Product: [
  248. { name: 'nullable', type: 'string', list: false },
  249. { name: 'notNullable', type: 'string', list: false },
  250. { name: 'stringWithDefault', type: 'string', list: false },
  251. { name: 'localeStringWithDefault', type: 'localeString', list: false },
  252. { name: 'intWithDefault', type: 'int', list: false },
  253. { name: 'floatWithDefault', type: 'float', list: false },
  254. { name: 'booleanWithDefault', type: 'boolean', list: false },
  255. { name: 'dateTimeWithDefault', type: 'datetime', list: false },
  256. { name: 'validateString', type: 'string', list: false },
  257. { name: 'validateLocaleString', type: 'localeString', list: false },
  258. { name: 'validateInt', type: 'int', list: false },
  259. { name: 'validateFloat', type: 'float', list: false },
  260. { name: 'validateDateTime', type: 'datetime', list: false },
  261. { name: 'validateFn1', type: 'string', list: false },
  262. { name: 'validateFn2', type: 'string', list: false },
  263. { name: 'validateFn3', type: 'string', list: false },
  264. { name: 'validateFn4', type: 'string', list: false },
  265. {
  266. name: 'validateRelation',
  267. type: 'relation',
  268. list: false,
  269. scalarFields: [
  270. 'id',
  271. 'createdAt',
  272. 'updatedAt',
  273. 'languageCode',
  274. 'name',
  275. 'type',
  276. 'fileSize',
  277. 'mimeType',
  278. 'width',
  279. 'height',
  280. 'source',
  281. 'preview',
  282. 'customFields',
  283. ],
  284. },
  285. { name: 'stringWithOptions', type: 'string', list: false },
  286. { name: 'nullableStringWithOptions', type: 'string', list: false },
  287. { name: 'nonPublic', type: 'string', list: false },
  288. { name: 'public', type: 'string', list: false },
  289. { name: 'longString', type: 'string', list: false },
  290. { name: 'longLocaleString', type: 'localeString', list: false },
  291. { name: 'readonlyString', type: 'string', list: false },
  292. { name: 'stringList', type: 'string', list: true },
  293. { name: 'localeStringList', type: 'localeString', list: true },
  294. { name: 'stringListWithDefault', type: 'string', list: true },
  295. { name: 'intListWithValidation', type: 'int', list: true },
  296. { name: 'uniqueString', type: 'string', list: false },
  297. // The internal type should not be exposed at all
  298. // { name: 'internalString', type: 'string' },
  299. ],
  300. });
  301. });
  302. it('globalSettings.serverConfig.entityCustomFields', async () => {
  303. const { globalSettings } = await adminClient.query(getServerConfigEntityCustomFieldsDocument);
  304. const productCustomFields = globalSettings.serverConfig.entityCustomFields.find(
  305. e => e.entityName === 'Product',
  306. );
  307. expect(productCustomFields).toEqual({
  308. entityName: 'Product',
  309. customFields: [
  310. { name: 'nullable', type: 'string', list: false },
  311. { name: 'notNullable', type: 'string', list: false },
  312. { name: 'stringWithDefault', type: 'string', list: false },
  313. { name: 'localeStringWithDefault', type: 'localeString', list: false },
  314. { name: 'intWithDefault', type: 'int', list: false },
  315. { name: 'floatWithDefault', type: 'float', list: false },
  316. { name: 'booleanWithDefault', type: 'boolean', list: false },
  317. { name: 'dateTimeWithDefault', type: 'datetime', list: false },
  318. { name: 'validateString', type: 'string', list: false },
  319. { name: 'validateLocaleString', type: 'localeString', list: false },
  320. { name: 'validateInt', type: 'int', list: false },
  321. { name: 'validateFloat', type: 'float', list: false },
  322. { name: 'validateDateTime', type: 'datetime', list: false },
  323. { name: 'validateFn1', type: 'string', list: false },
  324. { name: 'validateFn2', type: 'string', list: false },
  325. { name: 'validateFn3', type: 'string', list: false },
  326. { name: 'validateFn4', type: 'string', list: false },
  327. {
  328. name: 'validateRelation',
  329. type: 'relation',
  330. list: false,
  331. scalarFields: [
  332. 'id',
  333. 'createdAt',
  334. 'updatedAt',
  335. 'languageCode',
  336. 'name',
  337. 'type',
  338. 'fileSize',
  339. 'mimeType',
  340. 'width',
  341. 'height',
  342. 'source',
  343. 'preview',
  344. 'customFields',
  345. ],
  346. },
  347. { name: 'stringWithOptions', type: 'string', list: false },
  348. { name: 'nullableStringWithOptions', type: 'string', list: false },
  349. { name: 'nonPublic', type: 'string', list: false },
  350. { name: 'public', type: 'string', list: false },
  351. { name: 'longString', type: 'string', list: false },
  352. { name: 'longLocaleString', type: 'localeString', list: false },
  353. { name: 'readonlyString', type: 'string', list: false },
  354. { name: 'stringList', type: 'string', list: true },
  355. { name: 'localeStringList', type: 'localeString', list: true },
  356. { name: 'stringListWithDefault', type: 'string', list: true },
  357. { name: 'intListWithValidation', type: 'int', list: true },
  358. { name: 'uniqueString', type: 'string', list: false },
  359. // The internal type should not be exposed at all
  360. // { name: 'internalString', type: 'string' },
  361. ],
  362. });
  363. });
  364. it('get nullable with no default', async () => {
  365. const { product } = await adminClient.query(getProductNullableDocument, { id: 'T_1' });
  366. expect(product).toEqual({
  367. id: 'T_1',
  368. name: 'Laptop',
  369. customFields: {
  370. nullable: null,
  371. },
  372. });
  373. });
  374. it('get entity with localeString only', async () => {
  375. const { facet } = await adminClient.query(getFacetCustomFieldsDocument, { id: 'T_1' });
  376. expect(facet).toEqual({
  377. id: 'T_1',
  378. name: 'category',
  379. customFields: {
  380. translated: null,
  381. },
  382. });
  383. });
  384. it('get fields with default values', async () => {
  385. const { product } = await adminClient.query(getProductWithDefaultsDocument, { id: 'T_1' });
  386. const customFields = {
  387. stringWithDefault: 'hello',
  388. localeStringWithDefault: 'hola',
  389. intWithDefault: 5,
  390. floatWithDefault: 5.5678,
  391. booleanWithDefault: true,
  392. dateTimeWithDefault: '2019-04-30T12:59:16.415Z',
  393. // MySQL does not support defaults on TEXT fields, which is what "simple-json" uses
  394. // internally. See https://stackoverflow.com/q/3466872/772859
  395. stringListWithDefault: customConfig.dbConnectionOptions.type === 'mysql' ? null : ['cat'],
  396. };
  397. expect(product).toEqual({
  398. id: 'T_1',
  399. name: 'Laptop',
  400. customFields,
  401. });
  402. });
  403. it(
  404. 'update non-nullable field',
  405. assertThrowsWithMessage(async () => {
  406. await adminClient.query(
  407. graphql(`
  408. mutation {
  409. updateProduct(input: { id: "T_1", customFields: { notNullable: null } }) {
  410. id
  411. }
  412. }
  413. `),
  414. );
  415. }, 'The custom field "notNullable" value cannot be set to null'),
  416. );
  417. it(
  418. 'throws on attempt to update readonly field',
  419. assertThrowsWithMessage(async () => {
  420. await adminClient.query(
  421. graphql(`
  422. mutation {
  423. updateProduct(input: { id: "T_1", customFields: { readonlyString: "hello" } }) {
  424. id
  425. }
  426. }
  427. `),
  428. );
  429. }, 'Field "readonlyString" is not defined by type "UpdateProductCustomFieldsInput"'),
  430. );
  431. it(
  432. 'throws on attempt to update readonly field when no other custom fields defined',
  433. assertThrowsWithMessage(async () => {
  434. await adminClient.query(
  435. graphql(`
  436. mutation {
  437. updateCustomer(input: { id: "T_1", customFields: { score: 5 } }) {
  438. ... on Customer {
  439. id
  440. }
  441. }
  442. }
  443. `),
  444. );
  445. }, 'The custom field "score" is readonly'),
  446. );
  447. it(
  448. 'throws on attempt to create readonly field',
  449. assertThrowsWithMessage(async () => {
  450. await adminClient.query(
  451. graphql(`
  452. mutation {
  453. createProduct(
  454. input: {
  455. translations: [{ languageCode: en, name: "test" }]
  456. customFields: { readonlyString: "hello" }
  457. }
  458. ) {
  459. id
  460. }
  461. }
  462. `),
  463. );
  464. }, 'Field "readonlyString" is not defined by type "CreateProductCustomFieldsInput"'),
  465. );
  466. it('string length allows long strings', async () => {
  467. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  468. const result = await adminClient.query(updateProductLongStringDocument, {
  469. id: 'T_1',
  470. stringValue: longString,
  471. });
  472. expect(result.updateProduct.customFields.longString).toBe(longString);
  473. });
  474. it('string length allows long localeStrings', async () => {
  475. const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
  476. const result = await adminClient.query(updateProductLongLocaleStringDocument, {
  477. id: 'T_1',
  478. stringValue: longString,
  479. });
  480. expect(result.updateProduct.customFields.longLocaleString).toBe(longString);
  481. });
  482. describe('validation', () => {
  483. it(
  484. 'invalid string',
  485. assertThrowsWithMessage(async () => {
  486. await adminClient.query(
  487. graphql(`
  488. mutation {
  489. updateProduct(input: { id: "T_1", customFields: { validateString: "hello" } }) {
  490. id
  491. }
  492. }
  493. `),
  494. );
  495. }, 'The custom field "validateString" value ["hello"] does not match the pattern [^[0-9][a-z]+$]'),
  496. );
  497. it(
  498. 'invalid string option',
  499. assertThrowsWithMessage(async () => {
  500. await adminClient.query(
  501. graphql(`
  502. mutation {
  503. updateProduct(input: { id: "T_1", customFields: { stringWithOptions: "tiny" } }) {
  504. id
  505. }
  506. }
  507. `),
  508. );
  509. }, "The custom field \"stringWithOptions\" value [\"tiny\"] is invalid. Valid options are ['small', 'medium', 'large']"),
  510. );
  511. it('valid string option', async () => {
  512. const { updateProduct } = await adminClient.query(updateProductStringOptionsDocument, {
  513. id: 'T_1',
  514. value: 'medium',
  515. });
  516. expect(updateProduct.customFields.stringWithOptions).toBe('medium');
  517. });
  518. it('nullable string option with null', async () => {
  519. const { updateProduct } = await adminClient.query(updateProductNullableStringOptionsDocument, {
  520. id: 'T_1',
  521. value: null,
  522. });
  523. expect(updateProduct.customFields.nullableStringWithOptions).toBeNull();
  524. });
  525. it(
  526. 'invalid localeString',
  527. assertThrowsWithMessage(async () => {
  528. await adminClient.query(
  529. graphql(`
  530. mutation {
  531. updateProduct(
  532. input: {
  533. id: "T_1"
  534. translations: [
  535. {
  536. id: "T_1"
  537. languageCode: en
  538. customFields: { validateLocaleString: "servus" }
  539. }
  540. ]
  541. }
  542. ) {
  543. id
  544. }
  545. }
  546. `),
  547. );
  548. }, 'The custom field "validateLocaleString" value ["servus"] does not match the pattern [^[0-9][a-z]+$]'),
  549. );
  550. it(
  551. 'invalid int',
  552. assertThrowsWithMessage(async () => {
  553. await adminClient.query(
  554. graphql(`
  555. mutation {
  556. updateProduct(input: { id: "T_1", customFields: { validateInt: 12 } }) {
  557. id
  558. }
  559. }
  560. `),
  561. );
  562. }, 'The custom field "validateInt" value [12] is greater than the maximum [10]'),
  563. );
  564. it(
  565. 'invalid float',
  566. assertThrowsWithMessage(async () => {
  567. await adminClient.query(
  568. graphql(`
  569. mutation {
  570. updateProduct(input: { id: "T_1", customFields: { validateFloat: 10.6 } }) {
  571. id
  572. }
  573. }
  574. `),
  575. );
  576. }, 'The custom field "validateFloat" value [10.6] is greater than the maximum [10.5]'),
  577. );
  578. it(
  579. 'invalid datetime',
  580. assertThrowsWithMessage(async () => {
  581. await adminClient.query(
  582. graphql(`
  583. mutation {
  584. updateProduct(
  585. input: {
  586. id: "T_1"
  587. customFields: { validateDateTime: "2019-01-01T05:25:00.000Z" }
  588. }
  589. ) {
  590. id
  591. }
  592. }
  593. `),
  594. );
  595. }, 'The custom field "validateDateTime" value [2019-01-01T05:25:00.000Z] is less than the minimum [2019-01-01T08:30]'),
  596. );
  597. it(
  598. 'invalid validate function with string',
  599. assertThrowsWithMessage(async () => {
  600. await adminClient.query(
  601. graphql(`
  602. mutation {
  603. updateProduct(input: { id: "T_1", customFields: { validateFn1: "invalid" } }) {
  604. id
  605. }
  606. }
  607. `),
  608. );
  609. }, "The value ['invalid'] is not valid"),
  610. );
  611. it(
  612. 'invalid validate function with localized string',
  613. assertThrowsWithMessage(async () => {
  614. await adminClient.query(
  615. graphql(`
  616. mutation {
  617. updateProduct(input: { id: "T_1", customFields: { validateFn2: "invalid" } }) {
  618. id
  619. }
  620. }
  621. `),
  622. );
  623. }, "The value ['invalid'] is not valid"),
  624. );
  625. it(
  626. 'invalid list field',
  627. assertThrowsWithMessage(async () => {
  628. await adminClient.query(
  629. graphql(`
  630. mutation {
  631. updateProduct(
  632. input: { id: "T_1", customFields: { intListWithValidation: [1, 2, 3] } }
  633. ) {
  634. id
  635. }
  636. }
  637. `),
  638. );
  639. }, 'Must include the number 42!'),
  640. );
  641. it('valid list field', async () => {
  642. const { updateProduct } = await adminClient.query(updateProductIntListDocument, {
  643. id: 'T_1',
  644. values: [1, 42, 3],
  645. });
  646. expect(updateProduct.customFields.intListWithValidation).toEqual([1, 42, 3]);
  647. });
  648. it('can inject providers into validation fn', async () => {
  649. const { updateProduct } = await adminClient.query(updateProductValidateFn3Document, {
  650. id: 'T_1',
  651. value: 'some value',
  652. });
  653. expect(updateProduct.customFields.validateFn3).toBe('some value');
  654. expect(validateInjectorSpy).toHaveBeenCalledTimes(1);
  655. expect(validateInjectorSpy.mock.calls[0][0] instanceof TransactionalConnection).toBe(true);
  656. });
  657. it(
  658. 'supports async validation fn',
  659. assertThrowsWithMessage(async () => {
  660. await adminClient.query(
  661. graphql(`
  662. mutation {
  663. updateProduct(input: { id: "T_1", customFields: { validateFn4: "some value" } }) {
  664. id
  665. customFields {
  666. validateFn4
  667. }
  668. }
  669. }
  670. `),
  671. );
  672. }, 'async error'),
  673. );
  674. // https://github.com/vendurehq/vendure/issues/1000
  675. it(
  676. 'supports validation of relation types',
  677. assertThrowsWithMessage(async () => {
  678. await adminClient.query(
  679. graphql(`
  680. mutation {
  681. updateProduct(input: { id: "T_1", customFields: { validateRelationId: "T_1" } }) {
  682. id
  683. customFields {
  684. validateFn4
  685. }
  686. }
  687. }
  688. `),
  689. );
  690. }, 'relation error'),
  691. );
  692. // https://github.com/vendurehq/vendure/issues/1091
  693. it('handles well graphql internal fields', async () => {
  694. // throws "Cannot read property 'args' of undefined" if broken
  695. await adminClient.query(
  696. graphql(`
  697. mutation {
  698. __typename
  699. updateProduct(input: { id: "T_1", customFields: { nullable: "some value" } }) {
  700. __typename
  701. id
  702. customFields {
  703. __typename
  704. nullable
  705. }
  706. }
  707. }
  708. `),
  709. );
  710. });
  711. // https://github.com/vendurehq/vendure/issues/1953
  712. describe('validation of OrderLine custom fields', () => {
  713. it('addItemToOrder', async () => {
  714. try {
  715. const { addItemToOrder } = await shopClient.query(
  716. graphqlShop(`
  717. mutation {
  718. addItemToOrder(
  719. productVariantId: 1
  720. quantity: 1
  721. customFields: { validateInt: 11 }
  722. ) {
  723. ... on Order {
  724. id
  725. }
  726. }
  727. }
  728. `),
  729. );
  730. fail('Should have thrown');
  731. } catch (e) {
  732. expect(e.message).toContain(
  733. 'The custom field "validateInt" value [11] is greater than the maximum [10]',
  734. );
  735. }
  736. const { addItemToOrder: result } = await shopClient.query(
  737. addItemToOrderWithCustomFieldsDocument,
  738. {
  739. productVariantId: '1',
  740. quantity: 1,
  741. validateInt: 9,
  742. },
  743. );
  744. orderGuard.assertSuccess(result);
  745. expect(result.lines[0].customFields).toEqual({ validateInt: 9 });
  746. });
  747. it('adjustOrderLine', async () => {
  748. try {
  749. const { adjustOrderLine } = await shopClient.query(
  750. graphqlShop(`
  751. mutation {
  752. adjustOrderLine(
  753. orderLineId: "T_1"
  754. quantity: 1
  755. customFields: { validateInt: 11 }
  756. ) {
  757. ... on Order {
  758. id
  759. }
  760. }
  761. }
  762. `),
  763. );
  764. fail('Should have thrown');
  765. } catch (e) {
  766. expect(e.message).toContain(
  767. 'The custom field "validateInt" value [11] is greater than the maximum [10]',
  768. );
  769. }
  770. const { adjustOrderLine: result } = await shopClient.query(
  771. adjustOrderLineWithCustomFieldsDocument,
  772. {
  773. orderLineId: 'T_1',
  774. quantity: 1,
  775. validateInt: 2,
  776. },
  777. );
  778. orderGuard.assertSuccess(result);
  779. expect(result.lines[0].customFields).toEqual({ validateInt: 2 });
  780. });
  781. });
  782. });
  783. describe('public access', () => {
  784. it(
  785. 'non-public throws for Shop API',
  786. assertThrowsWithMessage(async () => {
  787. await shopClient.query(
  788. graphqlShop(`
  789. query {
  790. product(id: "T_1") {
  791. id
  792. customFields {
  793. nonPublic
  794. }
  795. }
  796. }
  797. `),
  798. );
  799. }, 'Cannot query field "nonPublic" on type "ProductCustomFields"'),
  800. );
  801. it('publicly accessible via Shop API', async () => {
  802. const { product } = await shopClient.query(getShopProductPublicCustomFieldDocument, {
  803. id: 'T_1',
  804. });
  805. if (!product) {
  806. throw new Error('Product not found');
  807. }
  808. productGuard.assertSuccess(product);
  809. expect(product.customFields.public).toBe('ho!');
  810. });
  811. it(
  812. 'internal throws for Shop API',
  813. assertThrowsWithMessage(async () => {
  814. await shopClient.query(
  815. graphqlShop(`
  816. query {
  817. product(id: "T_1") {
  818. id
  819. customFields {
  820. internalString
  821. }
  822. }
  823. }
  824. `),
  825. );
  826. }, 'Cannot query field "internalString" on type "ProductCustomFields"'),
  827. );
  828. it(
  829. 'internal throws for Admin API',
  830. assertThrowsWithMessage(async () => {
  831. await adminClient.query(
  832. graphql(`
  833. query {
  834. product(id: "T_1") {
  835. id
  836. customFields {
  837. internalString
  838. }
  839. }
  840. }
  841. `),
  842. );
  843. }, 'Cannot query field "internalString" on type "ProductCustomFields"'),
  844. );
  845. // https://github.com/vendurehq/vendure/issues/3049
  846. it('does not leak private fields via JSON type', async () => {
  847. const { collection } = await shopClient.query(getCollectionCustomFieldsDocument, { id: 'T_1' });
  848. collectionGuard.assertSuccess(collection);
  849. expect(collection.customFields).toBe(null);
  850. });
  851. });
  852. describe('sort & filter', () => {
  853. it('can sort by custom fields', async () => {
  854. const { products } = await adminClient.query(getProductsSortByNullableDocument);
  855. expect(products.totalItems).toBe(1);
  856. });
  857. // https://github.com/vendurehq/vendure/issues/1581
  858. it('can sort by localeString custom fields', async () => {
  859. const { products } = await adminClient.query(getProductsSortByLocaleStringDocument);
  860. expect(products.totalItems).toBe(1);
  861. });
  862. it('can filter by custom fields', async () => {
  863. const { products } = await adminClient.query(getProductsFilterByStringDocument);
  864. expect(products.totalItems).toBe(1);
  865. });
  866. it('can filter by localeString custom fields', async () => {
  867. const { products } = await adminClient.query(getProductsFilterByLocaleStringDocument);
  868. expect(products.totalItems).toBe(1);
  869. });
  870. it('can filter by custom list fields', async () => {
  871. const { products: result1 } = await adminClient.query(getProductsFilterByIntListDocument, {
  872. value: 42,
  873. });
  874. expect(result1.totalItems).toBe(1);
  875. const { products: result2 } = await adminClient.query(getProductsFilterByIntListDocument, {
  876. value: 43,
  877. });
  878. expect(result2.totalItems).toBe(0);
  879. });
  880. it(
  881. 'cannot sort by custom list fields',
  882. assertThrowsWithMessage(async () => {
  883. await adminClient.query(
  884. graphql(`
  885. query {
  886. products(options: { sort: { intListWithValidation: ASC } }) {
  887. totalItems
  888. }
  889. }
  890. `),
  891. );
  892. }, 'Field "intListWithValidation" is not defined by type "ProductSortParameter".'),
  893. );
  894. it(
  895. 'cannot filter by internal field in Admin API',
  896. assertThrowsWithMessage(async () => {
  897. await adminClient.query(
  898. graphql(`
  899. query {
  900. products(options: { filter: { internalString: { contains: "hello" } } }) {
  901. totalItems
  902. }
  903. }
  904. `),
  905. );
  906. }, 'Field "internalString" is not defined by type "ProductFilterParameter"'),
  907. );
  908. it(
  909. 'cannot filter by internal field in Shop API',
  910. assertThrowsWithMessage(async () => {
  911. await shopClient.query(
  912. graphqlShop(`
  913. query {
  914. products(options: { filter: { internalString: { contains: "hello" } } }) {
  915. totalItems
  916. }
  917. }
  918. `),
  919. );
  920. }, 'Field "internalString" is not defined by type "ProductFilterParameter"'),
  921. );
  922. });
  923. describe('product on productVariant entity', () => {
  924. it('is translated', async () => {
  925. const { productVariants } = await adminClient.query(getProductVariantsWithCustomFieldsDocument, {
  926. productId: 'T_1',
  927. });
  928. expect(productVariants.items[0].product).toEqual({
  929. id: 'T_1',
  930. name: 'Laptop',
  931. customFields: {
  932. localeStringWithDefault: 'hola',
  933. stringWithDefault: 'hello',
  934. },
  935. });
  936. });
  937. });
  938. describe('unique constraint', () => {
  939. it('setting unique value works', async () => {
  940. const result = await adminClient.query(updateProductUniqueStringDocument, {
  941. id: 'T_1',
  942. value: 'foo',
  943. });
  944. expect(result.updateProduct.customFields.uniqueString).toBe('foo');
  945. });
  946. it('setting conflicting value fails', async () => {
  947. try {
  948. await adminClient.query(createProductUniqueStringDocument, { uniqueString: 'foo' });
  949. fail('Should have thrown');
  950. } catch (e: any) {
  951. let duplicateKeyErrMessage = 'unassigned';
  952. switch (customConfig.dbConnectionOptions.type) {
  953. case 'mariadb':
  954. case 'mysql':
  955. duplicateKeyErrMessage = "Duplicate entry 'foo' for key";
  956. break;
  957. case 'postgres':
  958. duplicateKeyErrMessage = 'duplicate key value violates unique constraint';
  959. break;
  960. case 'sqlite':
  961. case 'sqljs':
  962. duplicateKeyErrMessage = 'UNIQUE constraint failed: product.customFieldsUniquestring';
  963. break;
  964. }
  965. expect(e.message).toContain(duplicateKeyErrMessage);
  966. }
  967. });
  968. });
  969. it('on ProductVariantPrice', async () => {
  970. const { updateProductVariants } = await adminClient.query(
  971. updateProductVariantsWithPriceCustomFieldsDocument,
  972. {
  973. input: [
  974. {
  975. id: 'T_1',
  976. prices: [
  977. {
  978. price: 129900,
  979. currencyCode: 'USD',
  980. customFields: {
  981. costPrice: 100,
  982. },
  983. },
  984. ],
  985. },
  986. ],
  987. },
  988. );
  989. productVariantGuard.assertSuccess(updateProductVariants[0]);
  990. if (!updateProductVariants[0]) {
  991. throw new Error('Update product variants failed');
  992. }
  993. expect(updateProductVariants[0].prices).toEqual([
  994. {
  995. currencyCode: 'USD',
  996. price: 129900,
  997. customFields: {
  998. costPrice: 100,
  999. },
  1000. },
  1001. ]);
  1002. });
  1003. // https://github.com/vendurehq/vendure/issues/3909
  1004. it('persists custom fields when creating a new ProductVariantPrice', async () => {
  1005. // First, add EUR to the channel's available currencies
  1006. await adminClient.query(
  1007. graphql(`
  1008. mutation UpdateChannel($input: UpdateChannelInput!) {
  1009. updateChannel(input: $input) {
  1010. ... on Channel {
  1011. id
  1012. availableCurrencyCodes
  1013. }
  1014. }
  1015. }
  1016. `),
  1017. {
  1018. input: {
  1019. id: 'T_1',
  1020. availableCurrencyCodes: [CurrencyCode.USD, CurrencyCode.EUR],
  1021. },
  1022. },
  1023. );
  1024. // Now create a new price in EUR with custom fields
  1025. const { updateProductVariants } = await adminClient.query(
  1026. graphql(`
  1027. mutation UpdateProductVariants($input: [UpdateProductVariantInput!]!) {
  1028. updateProductVariants(input: $input) {
  1029. id
  1030. prices {
  1031. currencyCode
  1032. price
  1033. customFields {
  1034. costPrice
  1035. }
  1036. }
  1037. }
  1038. }
  1039. `),
  1040. {
  1041. input: [
  1042. {
  1043. id: 'T_1',
  1044. prices: [
  1045. {
  1046. price: 129900,
  1047. currencyCode: 'USD',
  1048. customFields: {
  1049. costPrice: 100,
  1050. },
  1051. },
  1052. {
  1053. price: 99900,
  1054. currencyCode: 'EUR',
  1055. customFields: {
  1056. costPrice: 200,
  1057. },
  1058. },
  1059. ],
  1060. },
  1061. ],
  1062. },
  1063. );
  1064. expect(updateProductVariants[0].prices).toContainEqual({
  1065. currencyCode: 'EUR',
  1066. price: 99900,
  1067. customFields: {
  1068. costPrice: 200,
  1069. },
  1070. });
  1071. });
  1072. describe('setting custom fields directly via a service method', () => {
  1073. it('OrderService.addItemToOrder warns on unknown custom field', async () => {
  1074. const orderService = server.app.get(OrderService);
  1075. const requestContextService = server.app.get(RequestContextService);
  1076. const ctx = await requestContextService.create({
  1077. apiType: 'admin',
  1078. });
  1079. const order = await orderService.create(ctx);
  1080. const warnSpy = vi.spyOn(Logger, 'warn');
  1081. await orderService.addItemToOrder(ctx, order.id, 1, 1, {
  1082. customFieldWhichDoesNotExist: 'test value',
  1083. });
  1084. expect(warnSpy).toHaveBeenCalledWith(
  1085. 'Custom field customFieldWhichDoesNotExist not found for entity OrderLine',
  1086. );
  1087. });
  1088. it('OrderService.addItemToOrder does not warn on known custom field', async () => {
  1089. const orderService = server.app.get(OrderService);
  1090. const requestContextService = server.app.get(RequestContextService);
  1091. const ctx = await requestContextService.create({
  1092. apiType: 'admin',
  1093. });
  1094. const order = await orderService.create(ctx);
  1095. const warnSpy = vi.spyOn(Logger, 'warn');
  1096. await orderService.addItemToOrder(ctx, order.id, 1, 1, {
  1097. validateInt: 1,
  1098. });
  1099. expect(warnSpy).not.toHaveBeenCalled();
  1100. });
  1101. it('OrderService.addItemToOrder warns on multiple unknown custom fields', async () => {
  1102. const orderService = server.app.get(OrderService);
  1103. const requestContextService = server.app.get(RequestContextService);
  1104. const ctx = await requestContextService.create({
  1105. apiType: 'admin',
  1106. });
  1107. const order = await orderService.create(ctx);
  1108. const warnSpy = vi.spyOn(Logger, 'warn');
  1109. await orderService.addItemToOrder(ctx, order.id, 1, 1, {
  1110. unknownField1: 'foo',
  1111. unknownField2: 'bar',
  1112. });
  1113. expect(warnSpy).toHaveBeenCalledWith('Custom field unknownField1 not found for entity OrderLine');
  1114. expect(warnSpy).toHaveBeenCalledWith('Custom field unknownField2 not found for entity OrderLine');
  1115. });
  1116. it('OrderService.addItemToOrder does not warn when no custom fields are provided', async () => {
  1117. const orderService = server.app.get(OrderService);
  1118. const requestContextService = server.app.get(RequestContextService);
  1119. const ctx = await requestContextService.create({
  1120. apiType: 'admin',
  1121. });
  1122. const order = await orderService.create(ctx);
  1123. const warnSpy = vi.spyOn(Logger, 'warn');
  1124. await orderService.addItemToOrder(ctx, order.id, 1, 1);
  1125. expect(warnSpy).not.toHaveBeenCalled();
  1126. });
  1127. it('warns on unknown custom field in ProductTranslation entity', async () => {
  1128. const productService = server.app.get(ProductService);
  1129. const requestContextService = server.app.get(RequestContextService);
  1130. const ctx = await requestContextService.create({
  1131. apiType: 'admin',
  1132. });
  1133. const warnSpy = vi.spyOn(Logger, 'warn');
  1134. await productService.create(ctx, {
  1135. translations: [
  1136. {
  1137. languageCode: LanguageCode.en,
  1138. name: 'test',
  1139. slug: 'test',
  1140. description: '',
  1141. customFields: { customFieldWhichDoesNotExist: 'foo' },
  1142. },
  1143. ],
  1144. });
  1145. expect(warnSpy).toHaveBeenCalledWith(
  1146. 'Custom field customFieldWhichDoesNotExist not found for entity ProductTranslation',
  1147. );
  1148. });
  1149. it('does not warn when Translation has a valid custom field', async () => {
  1150. const productService = server.app.get(ProductService);
  1151. const requestContextService = server.app.get(RequestContextService);
  1152. const ctx = await requestContextService.create({
  1153. apiType: 'admin',
  1154. });
  1155. const warnSpy = vi.spyOn(Logger, 'warn');
  1156. await productService.create(ctx, {
  1157. translations: [
  1158. {
  1159. languageCode: LanguageCode.en,
  1160. name: 'test',
  1161. slug: 'test',
  1162. description: '',
  1163. customFields: { localeStringWithDefault: 'foo' },
  1164. },
  1165. ],
  1166. });
  1167. expect(warnSpy).not.toHaveBeenCalled();
  1168. });
  1169. });
  1170. });
  1171. const getServerConfigCustomFieldsDocument = graphql(`
  1172. query GetServerConfigCustomFields {
  1173. globalSettings {
  1174. serverConfig {
  1175. customFieldConfig {
  1176. Product {
  1177. ... on CustomField {
  1178. name
  1179. type
  1180. list
  1181. }
  1182. ... on RelationCustomFieldConfig {
  1183. scalarFields
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. `);
  1191. const getServerConfigEntityCustomFieldsDocument = graphql(`
  1192. query GetServerConfigEntityCustomFields {
  1193. globalSettings {
  1194. serverConfig {
  1195. entityCustomFields {
  1196. entityName
  1197. customFields {
  1198. ... on CustomField {
  1199. name
  1200. type
  1201. list
  1202. }
  1203. ... on RelationCustomFieldConfig {
  1204. scalarFields
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. `);
  1212. const getProductNullableDocument = graphql(`
  1213. query GetProductNullable($id: ID!) {
  1214. product(id: $id) {
  1215. id
  1216. name
  1217. customFields {
  1218. nullable
  1219. }
  1220. }
  1221. }
  1222. `);
  1223. const getProductWithDefaultsDocument = graphql(`
  1224. query GetProductWithDefaults($id: ID!) {
  1225. product(id: $id) {
  1226. id
  1227. name
  1228. customFields {
  1229. stringWithDefault
  1230. localeStringWithDefault
  1231. intWithDefault
  1232. floatWithDefault
  1233. booleanWithDefault
  1234. dateTimeWithDefault
  1235. stringListWithDefault
  1236. }
  1237. }
  1238. }
  1239. `);
  1240. const updateProductLongStringDocument = graphql(`
  1241. mutation UpdateProductLongString($id: ID!, $stringValue: String!) {
  1242. updateProduct(input: { id: $id, customFields: { longString: $stringValue } }) {
  1243. id
  1244. customFields {
  1245. longString
  1246. }
  1247. }
  1248. }
  1249. `);
  1250. const updateProductLongLocaleStringDocument = graphql(`
  1251. mutation UpdateProductLongLocaleString($id: ID!, $stringValue: String!) {
  1252. updateProduct(
  1253. input: {
  1254. id: $id
  1255. translations: [{ languageCode: en, customFields: { longLocaleString: $stringValue } }]
  1256. }
  1257. ) {
  1258. id
  1259. customFields {
  1260. longLocaleString
  1261. }
  1262. }
  1263. }
  1264. `);
  1265. const updateProductStringOptionsDocument = graphql(`
  1266. mutation UpdateProductStringOptions($id: ID!, $value: String!) {
  1267. updateProduct(input: { id: $id, customFields: { stringWithOptions: $value } }) {
  1268. id
  1269. customFields {
  1270. stringWithOptions
  1271. }
  1272. }
  1273. }
  1274. `);
  1275. const updateProductNullableStringOptionsDocument = graphql(`
  1276. mutation UpdateProductNullableStringOptions($id: ID!, $value: String) {
  1277. updateProduct(input: { id: $id, customFields: { nullableStringWithOptions: $value } }) {
  1278. id
  1279. customFields {
  1280. nullableStringWithOptions
  1281. }
  1282. }
  1283. }
  1284. `);
  1285. const updateProductIntListDocument = graphql(`
  1286. mutation UpdateProductIntList($id: ID!, $values: [Int!]!) {
  1287. updateProduct(input: { id: $id, customFields: { intListWithValidation: $values } }) {
  1288. id
  1289. customFields {
  1290. intListWithValidation
  1291. }
  1292. }
  1293. }
  1294. `);
  1295. const updateProductValidateFn3Document = graphql(`
  1296. mutation UpdateProductValidateFn3($id: ID!, $value: String!) {
  1297. updateProduct(input: { id: $id, customFields: { validateFn3: $value } }) {
  1298. id
  1299. customFields {
  1300. validateFn3
  1301. }
  1302. }
  1303. }
  1304. `);
  1305. const updateProductUniqueStringDocument = graphql(`
  1306. mutation UpdateProductUniqueString($id: ID!, $value: String!) {
  1307. updateProduct(input: { id: $id, customFields: { uniqueString: $value } }) {
  1308. id
  1309. customFields {
  1310. uniqueString
  1311. }
  1312. }
  1313. }
  1314. `);
  1315. const createProductUniqueStringDocument = graphql(`
  1316. mutation CreateProductUniqueString($uniqueString: String!) {
  1317. createProduct(
  1318. input: {
  1319. translations: [{ languageCode: en, name: "test 2", slug: "test-2", description: "" }]
  1320. customFields: { uniqueString: $uniqueString }
  1321. }
  1322. ) {
  1323. id
  1324. }
  1325. }
  1326. `);
  1327. const getProductVariantsWithCustomFieldsDocument = graphql(`
  1328. query GetProductVariantsWithCustomFields($productId: ID!) {
  1329. productVariants(productId: $productId) {
  1330. items {
  1331. product {
  1332. name
  1333. id
  1334. customFields {
  1335. localeStringWithDefault
  1336. stringWithDefault
  1337. }
  1338. }
  1339. }
  1340. }
  1341. }
  1342. `);
  1343. const getFacetCustomFieldsDocument = graphql(`
  1344. query GetFacetCustomFields($id: ID!) {
  1345. facet(id: $id) {
  1346. id
  1347. name
  1348. customFields {
  1349. translated
  1350. }
  1351. }
  1352. }
  1353. `);
  1354. const getCollectionCustomFieldsDocument = graphqlShop(`
  1355. query GetCollectionCustomFields($id: ID!) {
  1356. collection(id: $id) {
  1357. id
  1358. customFields
  1359. }
  1360. }
  1361. `);
  1362. const getProductsSortByNullableDocument = graphql(`
  1363. query GetProductsSortByNullable {
  1364. products(options: { sort: { nullable: ASC } }) {
  1365. totalItems
  1366. }
  1367. }
  1368. `);
  1369. const getProductsSortByLocaleStringDocument = graphql(`
  1370. query GetProductsSortByLocaleString {
  1371. products(options: { sort: { localeStringWithDefault: ASC } }) {
  1372. totalItems
  1373. }
  1374. }
  1375. `);
  1376. const getProductsFilterByStringDocument = graphql(`
  1377. query GetProductsFilterByString {
  1378. products(options: { filter: { stringWithDefault: { contains: "hello" } } }) {
  1379. totalItems
  1380. }
  1381. }
  1382. `);
  1383. const getProductsFilterByLocaleStringDocument = graphql(`
  1384. query GetProductsFilterByLocaleString {
  1385. products(options: { filter: { localeStringWithDefault: { contains: "hola" } } }) {
  1386. totalItems
  1387. }
  1388. }
  1389. `);
  1390. const getProductsFilterByIntListDocument = graphql(`
  1391. query GetProductsFilterByIntList($value: Float!) {
  1392. products(options: { filter: { intListWithValidation: { inList: $value } } }) {
  1393. totalItems
  1394. }
  1395. }
  1396. `);
  1397. const addItemToOrderWithCustomFieldsDocument = graphqlShop(`
  1398. mutation AddItemToOrderWithCustomFields($productVariantId: ID!, $quantity: Int!, $validateInt: Int!) {
  1399. addItemToOrder(productVariantId: $productVariantId, quantity: $quantity, customFields: { validateInt: $validateInt }) {
  1400. ... on Order {
  1401. id
  1402. lines {
  1403. customFields {
  1404. validateInt
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. `);
  1411. const adjustOrderLineWithCustomFieldsDocument = graphqlShop(`
  1412. mutation AdjustOrderLineWithCustomFields($orderLineId: ID!, $quantity: Int!, $validateInt: Int!) {
  1413. adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, customFields: { validateInt: $validateInt }) {
  1414. ... on Order {
  1415. id
  1416. lines {
  1417. customFields {
  1418. validateInt
  1419. }
  1420. }
  1421. }
  1422. }
  1423. }
  1424. `);
  1425. const updateProductVariantsWithPriceCustomFieldsDocument = graphql(`
  1426. mutation UpdateProductVariantsWithPriceCustomFields($input: [UpdateProductVariantInput!]!) {
  1427. updateProductVariants(input: $input) {
  1428. id
  1429. prices {
  1430. currencyCode
  1431. price
  1432. customFields {
  1433. costPrice
  1434. }
  1435. }
  1436. }
  1437. }
  1438. `);
  1439. const getShopProductPublicCustomFieldDocument = graphqlShop(`
  1440. query GetShopProductPublicCustomField($id: ID!) {
  1441. product(id: $id) {
  1442. id
  1443. customFields {
  1444. public
  1445. }
  1446. }
  1447. }
  1448. `);