collection.e2e-spec.ts 55 KB

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