collection.e2e-spec.ts 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /* tslint:disable:no-non-null-assertion */
  2. import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
  3. import {
  4. DefaultJobQueuePlugin,
  5. facetValueCollectionFilter,
  6. variantNameCollectionFilter,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import gql from 'graphql-tag';
  10. import path from 'path';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { pick } from '../../common/lib/pick';
  14. import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
  15. import {
  16. Collection,
  17. CreateCollection,
  18. CreateCollectionInput,
  19. CreateCollectionSelectVariants,
  20. DeleteCollection,
  21. DeleteProduct,
  22. DeletionResult,
  23. FacetValueFragment,
  24. GetAssetList,
  25. GetCollection,
  26. GetCollectionBreadcrumbs,
  27. GetCollectionProducts,
  28. GetCollections,
  29. GetCollectionsForProducts,
  30. GetCollectionsWithAssets,
  31. GetFacetValues,
  32. GetProductCollections,
  33. GetProductCollectionsWithParent,
  34. GetProductsWithVariantIds,
  35. LanguageCode,
  36. MoveCollection,
  37. SortOrder,
  38. UpdateCollection,
  39. UpdateProduct,
  40. UpdateProductVariants,
  41. } from './graphql/generated-e2e-admin-types';
  42. import {
  43. CREATE_COLLECTION,
  44. DELETE_PRODUCT,
  45. GET_ASSET_LIST,
  46. UPDATE_COLLECTION,
  47. UPDATE_PRODUCT,
  48. UPDATE_PRODUCT_VARIANTS,
  49. } from './graphql/shared-definitions';
  50. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  51. import { awaitRunningJobs } from './utils/await-running-jobs';
  52. import { sortById } from './utils/test-order-utils';
  53. describe('Collection resolver', () => {
  54. const { server, adminClient } = createTestEnvironment({
  55. ...testConfig,
  56. plugins: [DefaultJobQueuePlugin],
  57. });
  58. let assets: GetAssetList.Items[];
  59. let facetValues: FacetValueFragment[];
  60. let electronicsCollection: Collection.Fragment;
  61. let computersCollection: Collection.Fragment;
  62. let pearCollection: Collection.Fragment;
  63. beforeAll(async () => {
  64. await server.init({
  65. initialData,
  66. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-collections.csv'),
  67. customerCount: 1,
  68. });
  69. await adminClient.asSuperAdmin();
  70. const assetsResult = await adminClient.query<GetAssetList.Query, GetAssetList.Variables>(
  71. GET_ASSET_LIST,
  72. {
  73. options: {
  74. sort: {
  75. name: SortOrder.ASC,
  76. },
  77. },
  78. },
  79. );
  80. assets = assetsResult.assets.items;
  81. const facetValuesResult = await adminClient.query<GetFacetValues.Query>(GET_FACET_VALUES);
  82. facetValues = facetValuesResult.facets.items.reduce(
  83. (values, facet) => [...values, ...facet.values],
  84. [] as FacetValueFragment[],
  85. );
  86. }, TEST_SETUP_TIMEOUT_MS);
  87. afterAll(async () => {
  88. await server.destroy();
  89. });
  90. /**
  91. * Test case for https://github.com/vendure-ecommerce/vendure/issues/97
  92. */
  93. it('collection breadcrumbs works after bootstrap', async () => {
  94. const result = await adminClient.query<GetCollectionBreadcrumbs.Query>(GET_COLLECTION_BREADCRUMBS, {
  95. id: 'T_1',
  96. });
  97. expect(result.collection!.breadcrumbs[0].name).toBe(ROOT_COLLECTION_NAME);
  98. });
  99. describe('createCollection', () => {
  100. it('creates a root collection', async () => {
  101. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  102. CREATE_COLLECTION,
  103. {
  104. input: {
  105. assetIds: [assets[0].id, assets[1].id],
  106. featuredAssetId: assets[1].id,
  107. filters: [
  108. {
  109. code: facetValueCollectionFilter.code,
  110. arguments: [
  111. {
  112. name: 'facetValueIds',
  113. value: `["${getFacetValueId('electronics')}"]`,
  114. },
  115. {
  116. name: 'containsAny',
  117. value: `false`,
  118. },
  119. ],
  120. },
  121. ],
  122. translations: [
  123. {
  124. languageCode: LanguageCode.en,
  125. name: 'Electronics',
  126. description: '',
  127. slug: 'electronics',
  128. },
  129. ],
  130. },
  131. },
  132. );
  133. electronicsCollection = result.createCollection;
  134. expect(electronicsCollection).toMatchSnapshot();
  135. expect(electronicsCollection.parent!.name).toBe(ROOT_COLLECTION_NAME);
  136. });
  137. it('creates a nested collection', async () => {
  138. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  139. CREATE_COLLECTION,
  140. {
  141. input: {
  142. parentId: electronicsCollection.id,
  143. translations: [
  144. {
  145. languageCode: LanguageCode.en,
  146. name: 'Computers',
  147. description: '',
  148. slug: 'computers',
  149. },
  150. ],
  151. filters: [
  152. {
  153. code: facetValueCollectionFilter.code,
  154. arguments: [
  155. {
  156. name: 'facetValueIds',
  157. value: `["${getFacetValueId('computers')}"]`,
  158. },
  159. {
  160. name: 'containsAny',
  161. value: `false`,
  162. },
  163. ],
  164. },
  165. ],
  166. },
  167. },
  168. );
  169. computersCollection = result.createCollection;
  170. expect(computersCollection.parent!.name).toBe(electronicsCollection.name);
  171. });
  172. it('creates a 2nd level nested collection', async () => {
  173. const result = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  174. CREATE_COLLECTION,
  175. {
  176. input: {
  177. parentId: computersCollection.id,
  178. translations: [
  179. { languageCode: LanguageCode.en, name: 'Pear', description: '', slug: 'pear' },
  180. ],
  181. filters: [
  182. {
  183. code: facetValueCollectionFilter.code,
  184. arguments: [
  185. {
  186. name: 'facetValueIds',
  187. value: `["${getFacetValueId('pear')}"]`,
  188. },
  189. {
  190. name: 'containsAny',
  191. value: `false`,
  192. },
  193. ],
  194. },
  195. ],
  196. },
  197. },
  198. );
  199. pearCollection = result.createCollection;
  200. expect(pearCollection.parent!.name).toBe(computersCollection.name);
  201. });
  202. it('slug is normalized to be url-safe', async () => {
  203. const { createCollection } = await adminClient.query<
  204. CreateCollection.Mutation,
  205. CreateCollection.Variables
  206. >(CREATE_COLLECTION, {
  207. input: {
  208. translations: [
  209. {
  210. languageCode: LanguageCode.en,
  211. name: 'Accessories',
  212. description: '',
  213. slug: 'Accessories!',
  214. },
  215. {
  216. languageCode: LanguageCode.de,
  217. name: 'Zubehör',
  218. description: '',
  219. slug: 'Zubehör!',
  220. },
  221. ],
  222. filters: [],
  223. },
  224. });
  225. expect(createCollection.slug).toBe('accessories');
  226. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  227. 'accessories',
  228. );
  229. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  230. 'zubehor',
  231. );
  232. });
  233. it('create with duplicate slug is renamed to be unique', async () => {
  234. const { createCollection } = await adminClient.query<
  235. CreateCollection.Mutation,
  236. CreateCollection.Variables
  237. >(CREATE_COLLECTION, {
  238. input: {
  239. translations: [
  240. {
  241. languageCode: LanguageCode.en,
  242. name: 'Accessories',
  243. description: '',
  244. slug: 'Accessories',
  245. },
  246. {
  247. languageCode: LanguageCode.de,
  248. name: 'Zubehör',
  249. description: '',
  250. slug: 'Zubehör',
  251. },
  252. ],
  253. filters: [],
  254. },
  255. });
  256. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.en)?.slug).toBe(
  257. 'accessories-2',
  258. );
  259. expect(createCollection.translations.find(t => t.languageCode === LanguageCode.de)?.slug).toBe(
  260. 'zubehor-2',
  261. );
  262. });
  263. });
  264. describe('updateCollection', () => {
  265. it('updates with assets', async () => {
  266. const { updateCollection } = await adminClient.query<
  267. UpdateCollection.Mutation,
  268. UpdateCollection.Variables
  269. >(UPDATE_COLLECTION, {
  270. input: {
  271. id: pearCollection.id,
  272. assetIds: [assets[1].id, assets[2].id],
  273. featuredAssetId: assets[1].id,
  274. translations: [
  275. { languageCode: LanguageCode.en, description: 'Apple stuff ', slug: 'apple-stuff' },
  276. ],
  277. },
  278. });
  279. expect(updateCollection).toMatchSnapshot();
  280. pearCollection = updateCollection;
  281. });
  282. it('updating existing assets', async () => {
  283. const { updateCollection } = await adminClient.query<
  284. UpdateCollection.Mutation,
  285. UpdateCollection.Variables
  286. >(UPDATE_COLLECTION, {
  287. input: {
  288. id: pearCollection.id,
  289. assetIds: [assets[3].id, assets[0].id],
  290. featuredAssetId: assets[3].id,
  291. },
  292. });
  293. expect(updateCollection.assets.map(a => a.id)).toEqual([assets[3].id, assets[0].id]);
  294. });
  295. it('removes all assets', async () => {
  296. const { updateCollection } = await adminClient.query<
  297. UpdateCollection.Mutation,
  298. UpdateCollection.Variables
  299. >(UPDATE_COLLECTION, {
  300. input: {
  301. id: pearCollection.id,
  302. assetIds: [],
  303. },
  304. });
  305. expect(updateCollection.assets).toEqual([]);
  306. expect(updateCollection.featuredAsset).toBeNull();
  307. });
  308. });
  309. it('collection by id', async () => {
  310. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  311. id: computersCollection.id,
  312. });
  313. if (!result.collection) {
  314. fail(`did not return the collection`);
  315. return;
  316. }
  317. expect(result.collection.id).toBe(computersCollection.id);
  318. });
  319. it('collection by slug', async () => {
  320. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  321. slug: computersCollection.slug,
  322. });
  323. if (!result.collection) {
  324. fail(`did not return the collection`);
  325. return;
  326. }
  327. expect(result.collection.id).toBe(computersCollection.id);
  328. });
  329. it(
  330. 'throws if neither id nor slug provided',
  331. assertThrowsWithMessage(async () => {
  332. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {});
  333. }, 'Either the Collection id or slug must be provided'),
  334. );
  335. it(
  336. 'throws if id and slug do not refer to the same Product',
  337. assertThrowsWithMessage(async () => {
  338. await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  339. id: computersCollection.id,
  340. slug: pearCollection.slug,
  341. });
  342. }, 'The provided id and slug refer to different Collections'),
  343. );
  344. it('parent field', async () => {
  345. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  346. id: computersCollection.id,
  347. });
  348. if (!result.collection) {
  349. fail(`did not return the collection`);
  350. return;
  351. }
  352. expect(result.collection.parent!.name).toBe('Electronics');
  353. });
  354. // Tests fix for https://github.com/vendure-ecommerce/vendure/issues/361
  355. it('parent field resolved by CollectionEntityResolver', async () => {
  356. const { product } = await adminClient.query<
  357. GetProductCollectionsWithParent.Query,
  358. GetProductCollectionsWithParent.Variables
  359. >(GET_PRODUCT_COLLECTIONS_WITH_PARENT, {
  360. id: 'T_1',
  361. });
  362. expect(product?.collections.length).toBe(3);
  363. expect(product?.collections.sort(sortById)).toEqual([
  364. {
  365. id: 'T_3',
  366. name: 'Electronics',
  367. parent: {
  368. id: 'T_1',
  369. name: '__root_collection__',
  370. },
  371. },
  372. {
  373. id: 'T_4',
  374. name: 'Computers',
  375. parent: {
  376. id: 'T_3',
  377. name: 'Electronics',
  378. },
  379. },
  380. {
  381. id: 'T_5',
  382. name: 'Pear',
  383. parent: {
  384. id: 'T_4',
  385. name: 'Computers',
  386. },
  387. },
  388. ]);
  389. });
  390. it('children field', async () => {
  391. const result = await adminClient.query<GetCollection.Query, GetCollection.Variables>(GET_COLLECTION, {
  392. id: electronicsCollection.id,
  393. });
  394. if (!result.collection) {
  395. fail(`did not return the collection`);
  396. return;
  397. }
  398. expect(result.collection.children!.length).toBe(1);
  399. expect(result.collection.children![0].name).toBe('Computers');
  400. });
  401. it('breadcrumbs', async () => {
  402. const result = await adminClient.query<
  403. GetCollectionBreadcrumbs.Query,
  404. GetCollectionBreadcrumbs.Variables
  405. >(GET_COLLECTION_BREADCRUMBS, {
  406. id: pearCollection.id,
  407. });
  408. if (!result.collection) {
  409. fail(`did not return the collection`);
  410. return;
  411. }
  412. expect(result.collection.breadcrumbs).toEqual([
  413. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  414. {
  415. id: electronicsCollection.id,
  416. name: electronicsCollection.name,
  417. slug: electronicsCollection.slug,
  418. },
  419. { id: computersCollection.id, name: computersCollection.name, slug: computersCollection.slug },
  420. { id: pearCollection.id, name: pearCollection.name, slug: pearCollection.slug },
  421. ]);
  422. });
  423. it('breadcrumbs for root collection', async () => {
  424. const result = await adminClient.query<
  425. GetCollectionBreadcrumbs.Query,
  426. GetCollectionBreadcrumbs.Variables
  427. >(GET_COLLECTION_BREADCRUMBS, {
  428. id: 'T_1',
  429. });
  430. if (!result.collection) {
  431. fail(`did not return the collection`);
  432. return;
  433. }
  434. expect(result.collection.breadcrumbs).toEqual([
  435. { id: 'T_1', name: ROOT_COLLECTION_NAME, slug: ROOT_COLLECTION_NAME },
  436. ]);
  437. });
  438. it('collections.assets', async () => {
  439. const { collections } = await adminClient.query<GetCollectionsWithAssets.Query>(gql`
  440. query GetCollectionsWithAssets {
  441. collections {
  442. items {
  443. assets {
  444. name
  445. }
  446. }
  447. }
  448. }
  449. `);
  450. expect(collections.items[0].assets).toBeDefined();
  451. });
  452. describe('moveCollection', () => {
  453. it('moves a collection to a new parent', async () => {
  454. const result = await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(
  455. MOVE_COLLECTION,
  456. {
  457. input: {
  458. collectionId: pearCollection.id,
  459. parentId: electronicsCollection.id,
  460. index: 0,
  461. },
  462. },
  463. );
  464. expect(result.moveCollection.parent!.id).toBe(electronicsCollection.id);
  465. const positions = await getChildrenOf(electronicsCollection.id);
  466. expect(positions.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  467. });
  468. it('re-evaluates Collection contents on move', async () => {
  469. await awaitRunningJobs(adminClient);
  470. const result = await adminClient.query<
  471. GetCollectionProducts.Query,
  472. GetCollectionProducts.Variables
  473. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  474. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  475. 'Laptop 13 inch 8GB',
  476. 'Laptop 15 inch 8GB',
  477. 'Laptop 13 inch 16GB',
  478. 'Laptop 15 inch 16GB',
  479. 'Instant Camera',
  480. ]);
  481. });
  482. it('alters the position in the current parent 1', async () => {
  483. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  484. input: {
  485. collectionId: computersCollection.id,
  486. parentId: electronicsCollection.id,
  487. index: 0,
  488. },
  489. });
  490. const afterResult = await getChildrenOf(electronicsCollection.id);
  491. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  492. });
  493. it('alters the position in the current parent 2', async () => {
  494. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  495. input: {
  496. collectionId: pearCollection.id,
  497. parentId: electronicsCollection.id,
  498. index: 0,
  499. },
  500. });
  501. const afterResult = await getChildrenOf(electronicsCollection.id);
  502. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  503. });
  504. it('corrects an out-of-bounds negative index value', async () => {
  505. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  506. input: {
  507. collectionId: pearCollection.id,
  508. parentId: electronicsCollection.id,
  509. index: -3,
  510. },
  511. });
  512. const afterResult = await getChildrenOf(electronicsCollection.id);
  513. expect(afterResult.map(i => i.id)).toEqual([pearCollection.id, computersCollection.id]);
  514. });
  515. it('corrects an out-of-bounds positive index value', async () => {
  516. await adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  517. input: {
  518. collectionId: pearCollection.id,
  519. parentId: electronicsCollection.id,
  520. index: 10,
  521. },
  522. });
  523. const afterResult = await getChildrenOf(electronicsCollection.id);
  524. expect(afterResult.map(i => i.id)).toEqual([computersCollection.id, pearCollection.id]);
  525. });
  526. it(
  527. 'throws if attempting to move into self',
  528. assertThrowsWithMessage(
  529. () =>
  530. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  531. input: {
  532. collectionId: pearCollection.id,
  533. parentId: pearCollection.id,
  534. index: 0,
  535. },
  536. }),
  537. `Cannot move a Collection into itself`,
  538. ),
  539. );
  540. it(
  541. 'throws if attempting to move into a decendant of self',
  542. assertThrowsWithMessage(
  543. () =>
  544. adminClient.query<MoveCollection.Mutation, MoveCollection.Variables>(MOVE_COLLECTION, {
  545. input: {
  546. collectionId: pearCollection.id,
  547. parentId: pearCollection.id,
  548. index: 0,
  549. },
  550. }),
  551. `Cannot move a Collection into itself`,
  552. ),
  553. );
  554. async function getChildrenOf(parentId: string): Promise<Array<{ name: string; id: string }>> {
  555. const result = await adminClient.query<GetCollections.Query>(GET_COLLECTIONS);
  556. return result.collections.items.filter(i => i.parent!.id === parentId);
  557. }
  558. });
  559. describe('deleteCollection', () => {
  560. let collectionToDeleteParent: CreateCollection.CreateCollection;
  561. let collectionToDeleteChild: CreateCollection.CreateCollection;
  562. let laptopProductId: string;
  563. beforeAll(async () => {
  564. const result1 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  565. CREATE_COLLECTION,
  566. {
  567. input: {
  568. filters: [
  569. {
  570. code: variantNameCollectionFilter.code,
  571. arguments: [
  572. {
  573. name: 'operator',
  574. value: 'contains',
  575. },
  576. {
  577. name: 'term',
  578. value: 'laptop',
  579. },
  580. ],
  581. },
  582. ],
  583. translations: [
  584. {
  585. languageCode: LanguageCode.en,
  586. name: 'Delete Me Parent',
  587. description: '',
  588. slug: 'delete-me-parent',
  589. },
  590. ],
  591. assetIds: ['T_1'],
  592. },
  593. },
  594. );
  595. collectionToDeleteParent = result1.createCollection;
  596. const result2 = await adminClient.query<CreateCollection.Mutation, CreateCollection.Variables>(
  597. CREATE_COLLECTION,
  598. {
  599. input: {
  600. filters: [],
  601. translations: [
  602. {
  603. languageCode: LanguageCode.en,
  604. name: 'Delete Me Child',
  605. description: '',
  606. slug: 'delete-me-child',
  607. },
  608. ],
  609. parentId: collectionToDeleteParent.id,
  610. assetIds: ['T_2'],
  611. },
  612. },
  613. );
  614. collectionToDeleteChild = result2.createCollection;
  615. await awaitRunningJobs(adminClient);
  616. });
  617. it(
  618. 'throws for invalid collection id',
  619. assertThrowsWithMessage(async () => {
  620. await adminClient.query<DeleteCollection.Mutation, DeleteCollection.Variables>(
  621. DELETE_COLLECTION,
  622. {
  623. id: 'T_999',
  624. },
  625. );
  626. }, "No Collection with the id '999' could be found"),
  627. );
  628. it('collection and product related prior to deletion', async () => {
  629. const { collection } = await adminClient.query<
  630. GetCollectionProducts.Query,
  631. GetCollectionProducts.Variables
  632. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  633. id: collectionToDeleteParent.id,
  634. });
  635. expect(collection!.productVariants.items.map(pick(['name']))).toEqual([
  636. { name: 'Laptop 13 inch 8GB' },
  637. { name: 'Laptop 15 inch 8GB' },
  638. { name: 'Laptop 13 inch 16GB' },
  639. { name: 'Laptop 15 inch 16GB' },
  640. ]);
  641. laptopProductId = collection!.productVariants.items[0].productId;
  642. const { product } = await adminClient.query<
  643. GetProductCollections.Query,
  644. GetProductCollections.Variables
  645. >(GET_PRODUCT_COLLECTIONS, {
  646. id: laptopProductId,
  647. });
  648. expect(product!.collections).toEqual([
  649. { id: 'T_3', name: 'Electronics' },
  650. { id: 'T_4', name: 'Computers' },
  651. { id: 'T_5', name: 'Pear' },
  652. { id: 'T_8', name: 'Delete Me Parent' },
  653. { id: 'T_9', name: 'Delete Me Child' },
  654. ]);
  655. });
  656. it('deleteCollection works', async () => {
  657. const { deleteCollection } = await adminClient.query<
  658. DeleteCollection.Mutation,
  659. DeleteCollection.Variables
  660. >(DELETE_COLLECTION, {
  661. id: collectionToDeleteParent.id,
  662. });
  663. expect(deleteCollection.result).toBe(DeletionResult.DELETED);
  664. });
  665. it('deleted parent collection is null', async () => {
  666. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  667. GET_COLLECTION,
  668. {
  669. id: collectionToDeleteParent.id,
  670. },
  671. );
  672. expect(collection).toBeNull();
  673. });
  674. it('deleted child collection is null', async () => {
  675. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  676. GET_COLLECTION,
  677. {
  678. id: collectionToDeleteChild.id,
  679. },
  680. );
  681. expect(collection).toBeNull();
  682. });
  683. it('product no longer lists collection', async () => {
  684. const { product } = await adminClient.query<
  685. GetProductCollections.Query,
  686. GetProductCollections.Variables
  687. >(GET_PRODUCT_COLLECTIONS, {
  688. id: laptopProductId,
  689. });
  690. expect(product!.collections).toEqual([
  691. { id: 'T_3', name: 'Electronics' },
  692. { id: 'T_4', name: 'Computers' },
  693. { id: 'T_5', name: 'Pear' },
  694. ]);
  695. });
  696. });
  697. describe('filters', () => {
  698. it('Collection with no filters has no productVariants', async () => {
  699. const result = await adminClient.query<
  700. CreateCollectionSelectVariants.Mutation,
  701. CreateCollectionSelectVariants.Variables
  702. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  703. input: {
  704. translations: [
  705. { languageCode: LanguageCode.en, name: 'Empty', description: '', slug: 'empty' },
  706. ],
  707. filters: [],
  708. } as CreateCollectionInput,
  709. });
  710. expect(result.createCollection.productVariants.totalItems).toBe(0);
  711. });
  712. describe('facetValue filter', () => {
  713. it('electronics', async () => {
  714. const result = await adminClient.query<
  715. GetCollectionProducts.Query,
  716. GetCollectionProducts.Variables
  717. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  718. id: electronicsCollection.id,
  719. });
  720. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  721. 'Laptop 13 inch 8GB',
  722. 'Laptop 15 inch 8GB',
  723. 'Laptop 13 inch 16GB',
  724. 'Laptop 15 inch 16GB',
  725. 'Curvy Monitor 24 inch',
  726. 'Curvy Monitor 27 inch',
  727. 'Gaming PC i7-8700 240GB SSD',
  728. 'Gaming PC R7-2700 240GB SSD',
  729. 'Gaming PC i7-8700 120GB SSD',
  730. 'Gaming PC R7-2700 120GB SSD',
  731. 'Hard Drive 1TB',
  732. 'Hard Drive 2TB',
  733. 'Hard Drive 3TB',
  734. 'Hard Drive 4TB',
  735. 'Hard Drive 6TB',
  736. 'Clacky Keyboard',
  737. 'USB Cable',
  738. 'Instant Camera',
  739. 'Camera Lens',
  740. 'Tripod',
  741. 'SLR Camera',
  742. ]);
  743. });
  744. it('computers', async () => {
  745. const result = await adminClient.query<
  746. GetCollectionProducts.Query,
  747. GetCollectionProducts.Variables
  748. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  749. id: computersCollection.id,
  750. });
  751. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  752. 'Laptop 13 inch 8GB',
  753. 'Laptop 15 inch 8GB',
  754. 'Laptop 13 inch 16GB',
  755. 'Laptop 15 inch 16GB',
  756. 'Curvy Monitor 24 inch',
  757. 'Curvy Monitor 27 inch',
  758. 'Gaming PC i7-8700 240GB SSD',
  759. 'Gaming PC R7-2700 240GB SSD',
  760. 'Gaming PC i7-8700 120GB SSD',
  761. 'Gaming PC R7-2700 120GB SSD',
  762. 'Hard Drive 1TB',
  763. 'Hard Drive 2TB',
  764. 'Hard Drive 3TB',
  765. 'Hard Drive 4TB',
  766. 'Hard Drive 6TB',
  767. 'Clacky Keyboard',
  768. 'USB Cable',
  769. ]);
  770. });
  771. it('photo AND pear', async () => {
  772. const result = await adminClient.query<
  773. CreateCollectionSelectVariants.Mutation,
  774. CreateCollectionSelectVariants.Variables
  775. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  776. input: {
  777. translations: [
  778. {
  779. languageCode: LanguageCode.en,
  780. name: 'Photo AND Pear',
  781. description: '',
  782. slug: 'photo-and-pear',
  783. },
  784. ],
  785. filters: [
  786. {
  787. code: facetValueCollectionFilter.code,
  788. arguments: [
  789. {
  790. name: 'facetValueIds',
  791. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  792. 'photo',
  793. )}"]`,
  794. },
  795. {
  796. name: 'containsAny',
  797. value: `false`,
  798. },
  799. ],
  800. },
  801. ],
  802. } as CreateCollectionInput,
  803. });
  804. await awaitRunningJobs(adminClient);
  805. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  806. GET_COLLECTION,
  807. {
  808. id: result.createCollection.id,
  809. },
  810. );
  811. expect(collection!.productVariants.items.map(i => i.name)).toEqual(['Instant Camera']);
  812. });
  813. it('photo OR pear', async () => {
  814. const result = await adminClient.query<
  815. CreateCollectionSelectVariants.Mutation,
  816. CreateCollectionSelectVariants.Variables
  817. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  818. input: {
  819. translations: [
  820. {
  821. languageCode: LanguageCode.en,
  822. name: 'Photo OR Pear',
  823. description: '',
  824. slug: 'photo-or-pear',
  825. },
  826. ],
  827. filters: [
  828. {
  829. code: facetValueCollectionFilter.code,
  830. arguments: [
  831. {
  832. name: 'facetValueIds',
  833. value: `["${getFacetValueId('pear')}", "${getFacetValueId(
  834. 'photo',
  835. )}"]`,
  836. },
  837. {
  838. name: 'containsAny',
  839. value: `true`,
  840. },
  841. ],
  842. },
  843. ],
  844. } as CreateCollectionInput,
  845. });
  846. await awaitRunningJobs(adminClient);
  847. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  848. GET_COLLECTION,
  849. {
  850. id: result.createCollection.id,
  851. },
  852. );
  853. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  854. 'Laptop 13 inch 8GB',
  855. 'Laptop 15 inch 8GB',
  856. 'Laptop 13 inch 16GB',
  857. 'Laptop 15 inch 16GB',
  858. 'Instant Camera',
  859. 'Camera Lens',
  860. 'Tripod',
  861. 'SLR Camera',
  862. 'Hat',
  863. ]);
  864. });
  865. it('bell OR pear in computers', async () => {
  866. const result = await adminClient.query<
  867. CreateCollectionSelectVariants.Mutation,
  868. CreateCollectionSelectVariants.Variables
  869. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  870. input: {
  871. parentId: computersCollection.id,
  872. translations: [
  873. {
  874. languageCode: LanguageCode.en,
  875. name: 'Bell OR Pear Computers',
  876. description: '',
  877. slug: 'bell-or-pear',
  878. },
  879. ],
  880. filters: [
  881. {
  882. code: facetValueCollectionFilter.code,
  883. arguments: [
  884. {
  885. name: 'facetValueIds',
  886. value: `["${getFacetValueId('pear')}", "${getFacetValueId('bell')}"]`,
  887. },
  888. {
  889. name: 'containsAny',
  890. value: `true`,
  891. },
  892. ],
  893. },
  894. ],
  895. } as CreateCollectionInput,
  896. });
  897. await awaitRunningJobs(adminClient);
  898. const { collection } = await adminClient.query<GetCollection.Query, GetCollection.Variables>(
  899. GET_COLLECTION,
  900. {
  901. id: result.createCollection.id,
  902. },
  903. );
  904. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  905. 'Laptop 13 inch 8GB',
  906. 'Laptop 15 inch 8GB',
  907. 'Laptop 13 inch 16GB',
  908. 'Laptop 15 inch 16GB',
  909. 'Curvy Monitor 24 inch',
  910. 'Curvy Monitor 27 inch',
  911. ]);
  912. });
  913. });
  914. describe('variantName filter', () => {
  915. async function createVariantNameFilteredCollection(
  916. operator: string,
  917. term: string,
  918. ): Promise<Collection.Fragment> {
  919. const { createCollection } = await adminClient.query<
  920. CreateCollection.Mutation,
  921. CreateCollection.Variables
  922. >(CREATE_COLLECTION, {
  923. input: {
  924. translations: [
  925. {
  926. languageCode: LanguageCode.en,
  927. name: `${operator} ${term}`,
  928. description: '',
  929. slug: `${operator} ${term}`,
  930. },
  931. ],
  932. filters: [
  933. {
  934. code: variantNameCollectionFilter.code,
  935. arguments: [
  936. {
  937. name: 'operator',
  938. value: operator,
  939. },
  940. {
  941. name: 'term',
  942. value: term,
  943. },
  944. ],
  945. },
  946. ],
  947. },
  948. });
  949. await awaitRunningJobs(adminClient);
  950. return createCollection;
  951. }
  952. it('contains operator', async () => {
  953. const collection = await createVariantNameFilteredCollection('contains', 'camera');
  954. const result = await adminClient.query<
  955. GetCollectionProducts.Query,
  956. GetCollectionProducts.Variables
  957. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  958. id: collection.id,
  959. });
  960. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  961. 'Instant Camera',
  962. 'Camera Lens',
  963. 'SLR Camera',
  964. ]);
  965. });
  966. it('startsWith operator', async () => {
  967. const collection = await createVariantNameFilteredCollection('startsWith', 'camera');
  968. const result = await adminClient.query<
  969. GetCollectionProducts.Query,
  970. GetCollectionProducts.Variables
  971. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  972. id: collection.id,
  973. });
  974. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual(['Camera Lens']);
  975. });
  976. it('endsWith operator', async () => {
  977. const collection = await createVariantNameFilteredCollection('endsWith', 'camera');
  978. const result = await adminClient.query<
  979. GetCollectionProducts.Query,
  980. GetCollectionProducts.Variables
  981. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  982. id: collection.id,
  983. });
  984. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  985. 'Instant Camera',
  986. 'SLR Camera',
  987. ]);
  988. });
  989. it('doesNotContain operator', async () => {
  990. const collection = await createVariantNameFilteredCollection('doesNotContain', 'camera');
  991. const result = await adminClient.query<
  992. GetCollectionProducts.Query,
  993. GetCollectionProducts.Variables
  994. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  995. id: collection.id,
  996. });
  997. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  998. 'Laptop 13 inch 8GB',
  999. 'Laptop 15 inch 8GB',
  1000. 'Laptop 13 inch 16GB',
  1001. 'Laptop 15 inch 16GB',
  1002. 'Curvy Monitor 24 inch',
  1003. 'Curvy Monitor 27 inch',
  1004. 'Gaming PC i7-8700 240GB SSD',
  1005. 'Gaming PC R7-2700 240GB SSD',
  1006. 'Gaming PC i7-8700 120GB SSD',
  1007. 'Gaming PC R7-2700 120GB SSD',
  1008. 'Hard Drive 1TB',
  1009. 'Hard Drive 2TB',
  1010. 'Hard Drive 3TB',
  1011. 'Hard Drive 4TB',
  1012. 'Hard Drive 6TB',
  1013. 'Clacky Keyboard',
  1014. 'USB Cable',
  1015. 'Tripod',
  1016. 'Hat',
  1017. ]);
  1018. });
  1019. });
  1020. describe('re-evaluation of contents on changes', () => {
  1021. let products: GetProductsWithVariantIds.Items[];
  1022. beforeAll(async () => {
  1023. const result = await adminClient.query<GetProductsWithVariantIds.Query>(gql`
  1024. query GetProductsWithVariantIds {
  1025. products(options: { sort: { id: ASC } }) {
  1026. items {
  1027. id
  1028. name
  1029. variants {
  1030. id
  1031. name
  1032. }
  1033. }
  1034. }
  1035. }
  1036. `);
  1037. products = result.products.items;
  1038. });
  1039. it('updates contents when Product is updated', async () => {
  1040. await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(UPDATE_PRODUCT, {
  1041. input: {
  1042. id: products[1].id,
  1043. facetValueIds: [
  1044. getFacetValueId('electronics'),
  1045. getFacetValueId('computers'),
  1046. getFacetValueId('pear'),
  1047. ],
  1048. },
  1049. });
  1050. await awaitRunningJobs(adminClient);
  1051. const result = await adminClient.query<
  1052. GetCollectionProducts.Query,
  1053. GetCollectionProducts.Variables
  1054. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1055. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1056. 'Laptop 13 inch 8GB',
  1057. 'Laptop 15 inch 8GB',
  1058. 'Laptop 13 inch 16GB',
  1059. 'Laptop 15 inch 16GB',
  1060. 'Curvy Monitor 24 inch',
  1061. 'Curvy Monitor 27 inch',
  1062. 'Instant Camera',
  1063. ]);
  1064. });
  1065. it('updates contents when ProductVariant is updated', async () => {
  1066. const gamingPc240GB = products
  1067. .find(p => p.name === 'Gaming PC')!
  1068. .variants.find(v => v.name.includes('240GB'))!;
  1069. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1070. UPDATE_PRODUCT_VARIANTS,
  1071. {
  1072. input: [
  1073. {
  1074. id: gamingPc240GB.id,
  1075. facetValueIds: [getFacetValueId('pear')],
  1076. },
  1077. ],
  1078. },
  1079. );
  1080. await awaitRunningJobs(adminClient);
  1081. const result = await adminClient.query<
  1082. GetCollectionProducts.Query,
  1083. GetCollectionProducts.Variables
  1084. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1085. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1086. 'Laptop 13 inch 8GB',
  1087. 'Laptop 15 inch 8GB',
  1088. 'Laptop 13 inch 16GB',
  1089. 'Laptop 15 inch 16GB',
  1090. 'Curvy Monitor 24 inch',
  1091. 'Curvy Monitor 27 inch',
  1092. 'Gaming PC i7-8700 240GB SSD',
  1093. 'Instant Camera',
  1094. ]);
  1095. });
  1096. it('correctly filters when ProductVariant and Product both have matching FacetValue', async () => {
  1097. const gamingPc240GB = products
  1098. .find(p => p.name === 'Gaming PC')!
  1099. .variants.find(v => v.name.includes('240GB'))!;
  1100. await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  1101. UPDATE_PRODUCT_VARIANTS,
  1102. {
  1103. input: [
  1104. {
  1105. id: gamingPc240GB.id,
  1106. facetValueIds: [getFacetValueId('electronics'), getFacetValueId('pear')],
  1107. },
  1108. ],
  1109. },
  1110. );
  1111. await awaitRunningJobs(adminClient);
  1112. const result = await adminClient.query<
  1113. GetCollectionProducts.Query,
  1114. GetCollectionProducts.Variables
  1115. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearCollection.id });
  1116. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1117. 'Laptop 13 inch 8GB',
  1118. 'Laptop 15 inch 8GB',
  1119. 'Laptop 13 inch 16GB',
  1120. 'Laptop 15 inch 16GB',
  1121. 'Curvy Monitor 24 inch',
  1122. 'Curvy Monitor 27 inch',
  1123. 'Gaming PC i7-8700 240GB SSD',
  1124. 'Instant Camera',
  1125. ]);
  1126. });
  1127. });
  1128. it('filter inheritance of nested collections (issue #158)', async () => {
  1129. const a = 1;
  1130. const { createCollection: pearElectronics } = await adminClient.query<
  1131. CreateCollectionSelectVariants.Mutation,
  1132. CreateCollectionSelectVariants.Variables
  1133. >(CREATE_COLLECTION_SELECT_VARIANTS, {
  1134. input: {
  1135. parentId: electronicsCollection.id,
  1136. translations: [
  1137. {
  1138. languageCode: LanguageCode.en,
  1139. name: 'pear electronics',
  1140. description: '',
  1141. slug: 'pear-electronics',
  1142. },
  1143. ],
  1144. filters: [
  1145. {
  1146. code: facetValueCollectionFilter.code,
  1147. arguments: [
  1148. {
  1149. name: 'facetValueIds',
  1150. value: `["${getFacetValueId('pear')}"]`,
  1151. },
  1152. {
  1153. name: 'containsAny',
  1154. value: `false`,
  1155. },
  1156. ],
  1157. },
  1158. ],
  1159. } as CreateCollectionInput,
  1160. });
  1161. await awaitRunningJobs(adminClient);
  1162. const result = await adminClient.query<
  1163. GetCollectionProducts.Query,
  1164. GetCollectionProducts.Variables
  1165. >(GET_COLLECTION_PRODUCT_VARIANTS, { id: pearElectronics.id });
  1166. expect(result.collection!.productVariants.items.map(i => i.name)).toEqual([
  1167. 'Laptop 13 inch 8GB',
  1168. 'Laptop 15 inch 8GB',
  1169. 'Laptop 13 inch 16GB',
  1170. 'Laptop 15 inch 16GB',
  1171. 'Curvy Monitor 24 inch',
  1172. 'Curvy Monitor 27 inch',
  1173. 'Gaming PC i7-8700 240GB SSD',
  1174. 'Instant Camera',
  1175. // no "Hat"
  1176. ]);
  1177. });
  1178. });
  1179. describe('Product collections property', () => {
  1180. it('returns all collections to which the Product belongs', async () => {
  1181. const result = await adminClient.query<
  1182. GetCollectionsForProducts.Query,
  1183. GetCollectionsForProducts.Variables
  1184. >(GET_COLLECTIONS_FOR_PRODUCTS, { term: 'camera' });
  1185. expect(result.products.items[0].collections).toEqual([
  1186. { id: 'T_3', name: 'Electronics' },
  1187. { id: 'T_5', name: 'Pear' },
  1188. { id: 'T_11', name: 'Photo AND Pear' },
  1189. { id: 'T_12', name: 'Photo OR Pear' },
  1190. { id: 'T_14', name: 'contains camera' },
  1191. { id: 'T_16', name: 'endsWith camera' },
  1192. { id: 'T_18', name: 'pear electronics' },
  1193. ]);
  1194. });
  1195. });
  1196. it('collection does not list deleted products', async () => {
  1197. await adminClient.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
  1198. id: 'T_2', // curvy monitor
  1199. });
  1200. const { collection } = await adminClient.query<
  1201. GetCollectionProducts.Query,
  1202. GetCollectionProducts.Variables
  1203. >(GET_COLLECTION_PRODUCT_VARIANTS, {
  1204. id: pearCollection.id,
  1205. });
  1206. expect(collection!.productVariants.items.map(i => i.name)).toEqual([
  1207. 'Laptop 13 inch 8GB',
  1208. 'Laptop 15 inch 8GB',
  1209. 'Laptop 13 inch 16GB',
  1210. 'Laptop 15 inch 16GB',
  1211. 'Gaming PC i7-8700 240GB SSD',
  1212. 'Instant Camera',
  1213. ]);
  1214. });
  1215. function getFacetValueId(code: string): string {
  1216. const match = facetValues.find(fv => fv.code === code);
  1217. if (!match) {
  1218. throw new Error(`Could not find a FacetValue with the code "${code}"`);
  1219. }
  1220. return match.id;
  1221. }
  1222. });
  1223. export const GET_COLLECTION = gql`
  1224. query GetCollection($id: ID, $slug: String) {
  1225. collection(id: $id, slug: $slug) {
  1226. ...Collection
  1227. productVariants {
  1228. items {
  1229. id
  1230. name
  1231. }
  1232. }
  1233. }
  1234. }
  1235. ${COLLECTION_FRAGMENT}
  1236. `;
  1237. export const MOVE_COLLECTION = gql`
  1238. mutation MoveCollection($input: MoveCollectionInput!) {
  1239. moveCollection(input: $input) {
  1240. ...Collection
  1241. }
  1242. }
  1243. ${COLLECTION_FRAGMENT}
  1244. `;
  1245. const GET_FACET_VALUES = gql`
  1246. query GetFacetValues {
  1247. facets {
  1248. items {
  1249. values {
  1250. ...FacetValue
  1251. }
  1252. }
  1253. }
  1254. }
  1255. ${FACET_VALUE_FRAGMENT}
  1256. `;
  1257. const GET_COLLECTIONS = gql`
  1258. query GetCollections {
  1259. collections {
  1260. items {
  1261. id
  1262. name
  1263. position
  1264. parent {
  1265. id
  1266. name
  1267. }
  1268. }
  1269. }
  1270. }
  1271. `;
  1272. const GET_COLLECTION_PRODUCT_VARIANTS = gql`
  1273. query GetCollectionProducts($id: ID!) {
  1274. collection(id: $id) {
  1275. productVariants(options: { sort: { id: ASC } }) {
  1276. items {
  1277. id
  1278. name
  1279. facetValues {
  1280. code
  1281. }
  1282. productId
  1283. }
  1284. }
  1285. }
  1286. }
  1287. `;
  1288. const CREATE_COLLECTION_SELECT_VARIANTS = gql`
  1289. mutation CreateCollectionSelectVariants($input: CreateCollectionInput!) {
  1290. createCollection(input: $input) {
  1291. id
  1292. productVariants {
  1293. items {
  1294. name
  1295. }
  1296. totalItems
  1297. }
  1298. }
  1299. }
  1300. `;
  1301. const GET_COLLECTION_BREADCRUMBS = gql`
  1302. query GetCollectionBreadcrumbs($id: ID!) {
  1303. collection(id: $id) {
  1304. breadcrumbs {
  1305. id
  1306. name
  1307. slug
  1308. }
  1309. }
  1310. }
  1311. `;
  1312. const GET_COLLECTIONS_FOR_PRODUCTS = gql`
  1313. query GetCollectionsForProducts($term: String!) {
  1314. products(options: { filter: { name: { contains: $term } } }) {
  1315. items {
  1316. id
  1317. name
  1318. collections {
  1319. id
  1320. name
  1321. }
  1322. }
  1323. }
  1324. }
  1325. `;
  1326. const DELETE_COLLECTION = gql`
  1327. mutation DeleteCollection($id: ID!) {
  1328. deleteCollection(id: $id) {
  1329. result
  1330. message
  1331. }
  1332. }
  1333. `;
  1334. const GET_PRODUCT_COLLECTIONS = gql`
  1335. query GetProductCollections($id: ID!) {
  1336. product(id: $id) {
  1337. id
  1338. collections {
  1339. id
  1340. name
  1341. }
  1342. }
  1343. }
  1344. `;
  1345. const GET_PRODUCT_COLLECTIONS_WITH_PARENT = gql`
  1346. query GetProductCollectionsWithParent($id: ID!) {
  1347. product(id: $id) {
  1348. id
  1349. collections {
  1350. id
  1351. name
  1352. parent {
  1353. id
  1354. name
  1355. }
  1356. }
  1357. }
  1358. }
  1359. `;