list-query-builder.e2e-spec.ts 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. import { LogicalOperator } from '@vendure/common/lib/generated-types';
  2. import { mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  9. import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
  10. import { LanguageCode, SortOrder } from './graphql/generated-e2e-admin-types';
  11. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  12. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  13. import { sortById } from './utils/test-order-utils';
  14. fixPostgresTimezone();
  15. describe('ListQueryBuilder', () => {
  16. const { server, adminClient, shopClient } = createTestEnvironment(
  17. mergeConfig(testConfig(), {
  18. apiOptions: {
  19. shopListQueryLimit: 10,
  20. adminListQueryLimit: 30,
  21. },
  22. plugins: [ListQueryPlugin],
  23. }),
  24. );
  25. beforeAll(async () => {
  26. await server.init({
  27. initialData,
  28. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  29. customerCount: 3,
  30. });
  31. await adminClient.asSuperAdmin();
  32. }, TEST_SETUP_TIMEOUT_MS);
  33. afterAll(async () => {
  34. await server.destroy();
  35. });
  36. function getItemLabels(items: any[]): string[] {
  37. return items.map((x: any) => x.label).sort();
  38. }
  39. describe('pagination', () => {
  40. it('all en', async () => {
  41. const { testEntities } = await adminClient.query(
  42. GET_LIST,
  43. {
  44. options: {},
  45. },
  46. { languageCode: LanguageCode.en },
  47. );
  48. expect(testEntities.totalItems).toBe(6);
  49. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  50. expect(testEntities.items.map((i: any) => i.name)).toEqual(
  51. expect.arrayContaining([
  52. 'apple',
  53. 'bike',
  54. 'cake',
  55. 'dog',
  56. 'egg',
  57. 'baum', // if default en lang does not exist, use next available lang
  58. ]),
  59. );
  60. });
  61. it('all de', async () => {
  62. const { testEntities } = await adminClient.query(
  63. GET_LIST,
  64. {
  65. options: {},
  66. },
  67. { languageCode: LanguageCode.de },
  68. );
  69. expect(testEntities.totalItems).toBe(6);
  70. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  71. expect(testEntities.items.map((i: any) => i.name)).toEqual(
  72. expect.arrayContaining([
  73. 'apfel',
  74. 'fahrrad',
  75. 'kuchen',
  76. 'hund',
  77. 'egg', // falls back to en translation when de doesn't exist
  78. 'baum',
  79. ]),
  80. );
  81. });
  82. it('take', async () => {
  83. const { testEntities } = await adminClient.query(GET_LIST, {
  84. options: {
  85. take: 2,
  86. },
  87. });
  88. expect(testEntities.totalItems).toBe(6);
  89. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  90. });
  91. it('skip', async () => {
  92. const { testEntities } = await adminClient.query(GET_LIST, {
  93. options: {
  94. skip: 2,
  95. },
  96. });
  97. expect(testEntities.totalItems).toBe(6);
  98. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  99. });
  100. it('skip negative is ignored', async () => {
  101. const { testEntities } = await adminClient.query(GET_LIST, {
  102. options: {
  103. skip: -1,
  104. },
  105. });
  106. expect(testEntities.totalItems).toBe(6);
  107. expect(testEntities.items.length).toBe(6);
  108. });
  109. it('take zero is ignored', async () => {
  110. const { testEntities } = await adminClient.query(GET_LIST, {
  111. options: {
  112. take: 0,
  113. },
  114. });
  115. expect(testEntities.totalItems).toBe(6);
  116. expect(testEntities.items.length).toBe(6);
  117. });
  118. it('take negative is ignored', async () => {
  119. const { testEntities } = await adminClient.query(GET_LIST, {
  120. options: {
  121. take: -1,
  122. },
  123. });
  124. expect(testEntities.totalItems).toBe(6);
  125. expect(testEntities.items.length).toBe(6);
  126. });
  127. it(
  128. 'take beyond adminListQueryLimit',
  129. assertThrowsWithMessage(async () => {
  130. await adminClient.query(GET_LIST, {
  131. options: {
  132. take: 50,
  133. },
  134. });
  135. }, 'Cannot take more than 30 results from a list query'),
  136. );
  137. it(
  138. 'take beyond shopListQueryLimit',
  139. assertThrowsWithMessage(async () => {
  140. await shopClient.query(GET_LIST, {
  141. options: {
  142. take: 50,
  143. },
  144. });
  145. }, 'Cannot take more than 10 results from a list query'),
  146. );
  147. });
  148. describe('string filtering', () => {
  149. it('eq', async () => {
  150. const { testEntities } = await adminClient.query(GET_LIST, {
  151. options: {
  152. filter: {
  153. label: {
  154. eq: 'B',
  155. },
  156. },
  157. },
  158. });
  159. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  160. });
  161. it('notEq', async () => {
  162. const { testEntities } = await adminClient.query(GET_LIST, {
  163. options: {
  164. filter: {
  165. label: {
  166. notEq: 'B',
  167. },
  168. },
  169. },
  170. });
  171. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E', 'F']);
  172. });
  173. it('contains', async () => {
  174. const { testEntities } = await adminClient.query(GET_LIST, {
  175. options: {
  176. filter: {
  177. description: {
  178. contains: 'adip',
  179. },
  180. },
  181. },
  182. });
  183. expect(getItemLabels(testEntities.items)).toEqual(['C']);
  184. });
  185. it('notContains', async () => {
  186. const { testEntities } = await adminClient.query(GET_LIST, {
  187. options: {
  188. filter: {
  189. description: {
  190. notContains: 'te',
  191. },
  192. },
  193. },
  194. });
  195. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
  196. });
  197. it('in', async () => {
  198. const { testEntities } = await adminClient.query(GET_LIST, {
  199. options: {
  200. filter: {
  201. label: {
  202. in: ['A', 'C'],
  203. },
  204. },
  205. },
  206. });
  207. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
  208. });
  209. it('notIn', async () => {
  210. const { testEntities } = await adminClient.query(GET_LIST, {
  211. options: {
  212. filter: {
  213. label: {
  214. notIn: ['A', 'C'],
  215. },
  216. },
  217. },
  218. });
  219. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E', 'F']);
  220. });
  221. it('isNull true', async () => {
  222. const { testEntities } = await adminClient.query(GET_LIST, {
  223. options: {
  224. filter: {
  225. nullableString: {
  226. isNull: true,
  227. },
  228. },
  229. },
  230. });
  231. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  232. });
  233. it('isNull false', async () => {
  234. const { testEntities } = await adminClient.query(GET_LIST, {
  235. options: {
  236. filter: {
  237. nullableString: {
  238. isNull: false,
  239. },
  240. },
  241. },
  242. });
  243. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  244. });
  245. it('filtering on translatable string', async () => {
  246. const { testEntities } = await adminClient.query(GET_LIST_WITH_TRANSLATIONS, {
  247. options: {
  248. filter: {
  249. name: {
  250. contains: 'g',
  251. },
  252. },
  253. },
  254. });
  255. expect(getItemLabels(testEntities.items)).toEqual(['D', 'E']);
  256. });
  257. describe('regex', () => {
  258. it('simple substring', async () => {
  259. const { testEntities } = await adminClient.query(GET_LIST, {
  260. options: {
  261. filter: {
  262. description: {
  263. regex: 'or',
  264. },
  265. },
  266. },
  267. });
  268. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  269. });
  270. it('start of string', async () => {
  271. const { testEntities } = await adminClient.query(GET_LIST, {
  272. options: {
  273. filter: {
  274. description: {
  275. regex: '^in',
  276. },
  277. },
  278. },
  279. });
  280. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  281. });
  282. it('end of string', async () => {
  283. const { testEntities } = await adminClient.query(GET_LIST, {
  284. options: {
  285. filter: {
  286. description: {
  287. regex: 'or$',
  288. },
  289. },
  290. },
  291. });
  292. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  293. });
  294. it('alternation', async () => {
  295. const { testEntities } = await adminClient.query(GET_LIST, {
  296. options: {
  297. filter: {
  298. description: {
  299. regex: 'dolor|tempor',
  300. },
  301. },
  302. },
  303. });
  304. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  305. });
  306. it('complex', async () => {
  307. const { testEntities } = await adminClient.query(GET_LIST, {
  308. options: {
  309. filter: {
  310. description: {
  311. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  312. },
  313. },
  314. },
  315. });
  316. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  317. });
  318. });
  319. });
  320. describe('ID filtering', () => {
  321. it('eq', async () => {
  322. const { testEntities } = await adminClient.query(GET_LIST, {
  323. options: {
  324. filter: {
  325. ownerId: {
  326. eq: 'T_13',
  327. },
  328. },
  329. },
  330. });
  331. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  332. });
  333. it('notEq', async () => {
  334. const { testEntities } = await adminClient.query(GET_LIST, {
  335. options: {
  336. filter: {
  337. ownerId: {
  338. notEq: 'T_13',
  339. },
  340. },
  341. },
  342. });
  343. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E', 'F']);
  344. });
  345. it('in', async () => {
  346. const { testEntities } = await adminClient.query(GET_LIST, {
  347. options: {
  348. filter: {
  349. ownerId: {
  350. in: ['T_10', 'T_15'],
  351. },
  352. },
  353. },
  354. });
  355. expect(getItemLabels(testEntities.items)).toEqual(['A', 'F']);
  356. });
  357. it('in with empty set', async () => {
  358. const { testEntities } = await adminClient.query(GET_LIST, {
  359. options: {
  360. filter: {
  361. ownerId: {
  362. in: [],
  363. },
  364. },
  365. },
  366. });
  367. expect(getItemLabels(testEntities.items)).toEqual([]);
  368. });
  369. it('notIn', async () => {
  370. const { testEntities } = await adminClient.query(GET_LIST, {
  371. options: {
  372. filter: {
  373. ownerId: {
  374. notIn: ['T_10', 'T_15'],
  375. },
  376. },
  377. },
  378. });
  379. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
  380. });
  381. it('notIn with empty set', async () => {
  382. const { testEntities } = await adminClient.query(GET_LIST, {
  383. options: {
  384. filter: {
  385. ownerId: {
  386. notIn: [],
  387. },
  388. },
  389. },
  390. });
  391. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  392. });
  393. it('isNull true', async () => {
  394. const { testEntities } = await adminClient.query(GET_LIST, {
  395. options: {
  396. filter: {
  397. nullableId: {
  398. isNull: true,
  399. },
  400. },
  401. },
  402. });
  403. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  404. });
  405. it('isNull false', async () => {
  406. const { testEntities } = await adminClient.query(GET_LIST, {
  407. options: {
  408. filter: {
  409. nullableId: {
  410. isNull: false,
  411. },
  412. },
  413. },
  414. });
  415. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  416. });
  417. describe('regex', () => {
  418. it('simple substring', async () => {
  419. const { testEntities } = await adminClient.query(GET_LIST, {
  420. options: {
  421. filter: {
  422. description: {
  423. regex: 'or',
  424. },
  425. },
  426. },
  427. });
  428. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  429. });
  430. it('start of string', async () => {
  431. const { testEntities } = await adminClient.query(GET_LIST, {
  432. options: {
  433. filter: {
  434. description: {
  435. regex: '^in',
  436. },
  437. },
  438. },
  439. });
  440. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  441. });
  442. it('end of string', async () => {
  443. const { testEntities } = await adminClient.query(GET_LIST, {
  444. options: {
  445. filter: {
  446. description: {
  447. regex: 'or$',
  448. },
  449. },
  450. },
  451. });
  452. expect(getItemLabels(testEntities.items)).toEqual(['D']);
  453. });
  454. it('alternation', async () => {
  455. const { testEntities } = await adminClient.query(GET_LIST, {
  456. options: {
  457. filter: {
  458. description: {
  459. regex: 'dolor|tempor',
  460. },
  461. },
  462. },
  463. });
  464. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
  465. });
  466. it('complex', async () => {
  467. const { testEntities } = await adminClient.query(GET_LIST, {
  468. options: {
  469. filter: {
  470. description: {
  471. regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
  472. },
  473. },
  474. },
  475. });
  476. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
  477. });
  478. });
  479. });
  480. describe('boolean filtering', () => {
  481. it('eq', async () => {
  482. const { testEntities } = await adminClient.query(GET_LIST, {
  483. options: {
  484. filter: {
  485. active: {
  486. eq: false,
  487. },
  488. },
  489. },
  490. });
  491. expect(getItemLabels(testEntities.items)).toEqual(['C', 'E', 'F']);
  492. });
  493. it('isNull true', async () => {
  494. const { testEntities } = await adminClient.query(GET_LIST, {
  495. options: {
  496. filter: {
  497. nullableBoolean: {
  498. isNull: true,
  499. },
  500. },
  501. },
  502. });
  503. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  504. });
  505. it('isNull false', async () => {
  506. const { testEntities } = await adminClient.query(GET_LIST, {
  507. options: {
  508. filter: {
  509. nullableBoolean: {
  510. isNull: false,
  511. },
  512. },
  513. },
  514. });
  515. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  516. });
  517. });
  518. describe('number filtering', () => {
  519. it('eq', async () => {
  520. const { testEntities } = await adminClient.query(GET_LIST, {
  521. options: {
  522. filter: {
  523. order: {
  524. eq: 1,
  525. },
  526. },
  527. },
  528. });
  529. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  530. });
  531. it('lt', async () => {
  532. const { testEntities } = await adminClient.query(GET_LIST, {
  533. options: {
  534. filter: {
  535. order: {
  536. lt: 1,
  537. },
  538. },
  539. },
  540. });
  541. expect(getItemLabels(testEntities.items)).toEqual(['A']);
  542. });
  543. it('lte', async () => {
  544. const { testEntities } = await adminClient.query(GET_LIST, {
  545. options: {
  546. filter: {
  547. order: {
  548. lte: 1,
  549. },
  550. },
  551. },
  552. });
  553. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  554. });
  555. it('gt', async () => {
  556. const { testEntities } = await adminClient.query(GET_LIST, {
  557. options: {
  558. filter: {
  559. order: {
  560. gt: 1,
  561. },
  562. },
  563. },
  564. });
  565. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  566. });
  567. it('gte', async () => {
  568. const { testEntities } = await adminClient.query(GET_LIST, {
  569. options: {
  570. filter: {
  571. order: {
  572. gte: 1,
  573. },
  574. },
  575. },
  576. });
  577. expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E', 'F']);
  578. });
  579. it('between', async () => {
  580. const { testEntities } = await adminClient.query(GET_LIST, {
  581. options: {
  582. filter: {
  583. order: {
  584. between: {
  585. start: 2,
  586. end: 4,
  587. },
  588. },
  589. },
  590. },
  591. });
  592. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
  593. });
  594. it('isNull true', async () => {
  595. const { testEntities } = await adminClient.query(GET_LIST, {
  596. options: {
  597. filter: {
  598. nullableNumber: {
  599. isNull: true,
  600. },
  601. },
  602. },
  603. });
  604. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  605. });
  606. it('isNull false', async () => {
  607. const { testEntities } = await adminClient.query(GET_LIST, {
  608. options: {
  609. filter: {
  610. nullableNumber: {
  611. isNull: false,
  612. },
  613. },
  614. },
  615. });
  616. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  617. });
  618. });
  619. describe('date filtering', () => {
  620. it('before', async () => {
  621. const { testEntities } = await adminClient.query(GET_LIST, {
  622. options: {
  623. filter: {
  624. date: {
  625. before: '2020-01-20T10:00:00.000Z',
  626. },
  627. },
  628. },
  629. });
  630. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  631. });
  632. it('before on same date', async () => {
  633. const { testEntities } = await adminClient.query(GET_LIST, {
  634. options: {
  635. filter: {
  636. date: {
  637. before: '2020-01-15T17:00:00.000Z',
  638. },
  639. },
  640. },
  641. });
  642. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  643. });
  644. it('after', async () => {
  645. const { testEntities } = await adminClient.query(GET_LIST, {
  646. options: {
  647. filter: {
  648. date: {
  649. after: '2020-01-20T10:00:00.000Z',
  650. },
  651. },
  652. },
  653. });
  654. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  655. });
  656. it('after on same date', async () => {
  657. const { testEntities } = await adminClient.query(GET_LIST, {
  658. options: {
  659. filter: {
  660. date: {
  661. after: '2020-01-25T09:00:00.000Z',
  662. },
  663. },
  664. },
  665. });
  666. expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
  667. });
  668. it('between', async () => {
  669. const { testEntities } = await adminClient.query(GET_LIST, {
  670. options: {
  671. filter: {
  672. date: {
  673. between: {
  674. start: '2020-01-10T10:00:00.000Z',
  675. end: '2020-01-20T10:00:00.000Z',
  676. },
  677. },
  678. },
  679. },
  680. });
  681. expect(getItemLabels(testEntities.items)).toEqual(['B']);
  682. });
  683. it('isNull true', async () => {
  684. const { testEntities } = await adminClient.query(GET_LIST, {
  685. options: {
  686. filter: {
  687. nullableDate: {
  688. isNull: true,
  689. },
  690. },
  691. },
  692. });
  693. expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
  694. });
  695. it('isNull false', async () => {
  696. const { testEntities } = await adminClient.query(GET_LIST, {
  697. options: {
  698. filter: {
  699. nullableDate: {
  700. isNull: false,
  701. },
  702. },
  703. },
  704. });
  705. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
  706. });
  707. });
  708. describe('multiple filters with filterOperator', () => {
  709. it('default AND', async () => {
  710. const { testEntities } = await adminClient.query(GET_LIST, {
  711. options: {
  712. filter: {
  713. description: {
  714. contains: 'Lorem',
  715. },
  716. active: {
  717. eq: false,
  718. },
  719. },
  720. },
  721. });
  722. expect(getItemLabels(testEntities.items)).toEqual([]);
  723. });
  724. it('explicit AND', async () => {
  725. const { testEntities } = await adminClient.query(GET_LIST, {
  726. options: {
  727. filter: {
  728. description: {
  729. contains: 'Lorem',
  730. },
  731. active: {
  732. eq: false,
  733. },
  734. },
  735. filterOperator: LogicalOperator.AND,
  736. },
  737. });
  738. expect(getItemLabels(testEntities.items)).toEqual([]);
  739. });
  740. it('explicit OR', async () => {
  741. const { testEntities } = await adminClient.query(GET_LIST, {
  742. options: {
  743. filter: {
  744. description: {
  745. contains: 'Lorem',
  746. },
  747. active: {
  748. eq: false,
  749. },
  750. },
  751. filterOperator: LogicalOperator.OR,
  752. },
  753. });
  754. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  755. });
  756. it('explicit OR with 3 filters', async () => {
  757. const { testEntities } = await adminClient.query(GET_LIST, {
  758. options: {
  759. filter: {
  760. description: {
  761. contains: 'eiusmod',
  762. },
  763. active: {
  764. eq: false,
  765. },
  766. order: {
  767. lt: 3,
  768. },
  769. },
  770. filterOperator: LogicalOperator.OR,
  771. },
  772. });
  773. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  774. });
  775. it('explicit OR with empty filters object', async () => {
  776. const { testEntities } = await adminClient.query(GET_LIST, {
  777. options: {
  778. filter: {},
  779. filterOperator: LogicalOperator.OR,
  780. },
  781. });
  782. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  783. });
  784. });
  785. describe('complex boolean logic with _and & _or', () => {
  786. it('single _and', async () => {
  787. const { testEntities } = await adminClient.query(GET_LIST, {
  788. options: {
  789. filter: {
  790. _and: [
  791. {
  792. description: {
  793. contains: 'Lorem',
  794. },
  795. active: {
  796. eq: false,
  797. },
  798. },
  799. ],
  800. },
  801. },
  802. });
  803. expect(getItemLabels(testEntities.items)).toEqual([]);
  804. });
  805. it('single _or', async () => {
  806. const { testEntities } = await adminClient.query(GET_LIST, {
  807. options: {
  808. filter: {
  809. _or: [
  810. {
  811. description: {
  812. contains: 'Lorem',
  813. },
  814. active: {
  815. eq: false,
  816. },
  817. },
  818. ],
  819. },
  820. },
  821. });
  822. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
  823. });
  824. it('_or with nested _and', async () => {
  825. const { testEntities } = await adminClient.query(GET_LIST, {
  826. options: {
  827. filter: {
  828. _or: [
  829. {
  830. description: { contains: 'Lorem' },
  831. },
  832. {
  833. _and: [{ order: { gt: 3 } }, { order: { lt: 5 } }],
  834. },
  835. ],
  836. },
  837. },
  838. });
  839. expect(getItemLabels(testEntities.items)).toEqual(['A', 'E']);
  840. });
  841. it('_and with nested _or', async () => {
  842. const { testEntities } = await adminClient.query(GET_LIST, {
  843. options: {
  844. filter: {
  845. _and: [
  846. {
  847. description: { contains: 'e' },
  848. },
  849. {
  850. _or: [{ active: { eq: false } }, { ownerId: { eq: 'T_10' } }],
  851. },
  852. ],
  853. },
  854. },
  855. });
  856. expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'F']);
  857. });
  858. });
  859. describe('sorting', () => {
  860. it('sort by string', async () => {
  861. const { testEntities } = await adminClient.query(GET_LIST, {
  862. options: {
  863. sort: {
  864. label: SortOrder.DESC,
  865. },
  866. },
  867. });
  868. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  869. });
  870. it('sort by number', async () => {
  871. const { testEntities } = await adminClient.query(GET_LIST, {
  872. options: {
  873. sort: {
  874. order: SortOrder.DESC,
  875. },
  876. },
  877. });
  878. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  879. });
  880. it('sort by date', async () => {
  881. const { testEntities } = await adminClient.query(GET_LIST, {
  882. options: {
  883. sort: {
  884. date: SortOrder.DESC,
  885. },
  886. },
  887. });
  888. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  889. });
  890. it('sort by ID', async () => {
  891. const { testEntities } = await adminClient.query(GET_LIST, {
  892. options: {
  893. sort: {
  894. id: SortOrder.DESC,
  895. },
  896. },
  897. });
  898. expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
  899. });
  900. it('sort by translated field en', async () => {
  901. const { testEntities } = await adminClient.query(GET_LIST, {
  902. options: {
  903. sort: {
  904. name: SortOrder.ASC,
  905. },
  906. },
  907. });
  908. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  909. 'apple',
  910. 'baum', // falling back to de here
  911. 'bike',
  912. 'cake',
  913. 'dog',
  914. 'egg',
  915. ]);
  916. });
  917. it('sort by translated field de', async () => {
  918. const { testEntities } = await adminClient.query(
  919. GET_LIST,
  920. {
  921. options: {
  922. sort: {
  923. name: SortOrder.ASC,
  924. },
  925. },
  926. },
  927. { languageCode: LanguageCode.de },
  928. );
  929. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  930. 'apfel',
  931. 'baum',
  932. 'egg',
  933. 'fahrrad',
  934. 'hund',
  935. 'kuchen',
  936. ]);
  937. });
  938. it('sort by translated field en with take', async () => {
  939. const { testEntities } = await adminClient.query(GET_LIST, {
  940. options: {
  941. sort: {
  942. name: SortOrder.ASC,
  943. },
  944. take: 4,
  945. },
  946. });
  947. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'baum', 'bike', 'cake']);
  948. });
  949. it('sort by translated field de with take', async () => {
  950. const { testEntities } = await adminClient.query(
  951. GET_LIST,
  952. {
  953. options: {
  954. sort: {
  955. name: SortOrder.ASC,
  956. },
  957. take: 4,
  958. },
  959. },
  960. { languageCode: LanguageCode.de },
  961. );
  962. expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'baum', 'egg', 'fahrrad']);
  963. });
  964. });
  965. describe('calculated fields', () => {
  966. it('filter by simple calculated property', async () => {
  967. const { testEntities } = await adminClient.query(GET_LIST, {
  968. options: {
  969. filter: {
  970. descriptionLength: {
  971. lt: 12,
  972. },
  973. },
  974. },
  975. });
  976. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  977. });
  978. it('filter by calculated property with boolean expression', async () => {
  979. const { testEntities } = await adminClient.query(GET_LIST, {
  980. options: {
  981. filter: {
  982. _and: [
  983. {
  984. descriptionLength: {
  985. lt: 12,
  986. },
  987. },
  988. ],
  989. },
  990. },
  991. });
  992. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  993. });
  994. it('filter by calculated property with join', async () => {
  995. const { testEntities } = await adminClient.query(GET_LIST, {
  996. options: {
  997. filter: {
  998. price: {
  999. lt: 14,
  1000. },
  1001. },
  1002. },
  1003. });
  1004. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1005. });
  1006. it('filter by calculated property with join and boolean expression', async () => {
  1007. const { testEntities } = await adminClient.query(GET_LIST, {
  1008. options: {
  1009. filter: {
  1010. _and: [
  1011. {
  1012. price: {
  1013. lt: 14,
  1014. },
  1015. },
  1016. ],
  1017. },
  1018. },
  1019. });
  1020. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1021. });
  1022. it('sort by simple calculated property', async () => {
  1023. const { testEntities } = await adminClient.query(GET_LIST, {
  1024. options: {
  1025. sort: {
  1026. descriptionLength: SortOrder.ASC,
  1027. },
  1028. },
  1029. });
  1030. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  1031. });
  1032. it('sort by calculated property with join', async () => {
  1033. const { testEntities } = await adminClient.query(GET_LIST, {
  1034. options: {
  1035. sort: {
  1036. price: SortOrder.ASC,
  1037. },
  1038. },
  1039. });
  1040. expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
  1041. });
  1042. });
  1043. describe('multiple clauses', () => {
  1044. it('sort by translated field en & filter', async () => {
  1045. const { testEntities } = await adminClient.query(GET_LIST, {
  1046. options: {
  1047. sort: {
  1048. name: SortOrder.ASC,
  1049. },
  1050. filter: {
  1051. order: {
  1052. gte: 1,
  1053. },
  1054. },
  1055. },
  1056. });
  1057. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1058. 'baum',
  1059. 'bike',
  1060. 'cake',
  1061. 'dog',
  1062. 'egg',
  1063. ]);
  1064. });
  1065. it('sort by translated field de & filter', async () => {
  1066. const { testEntities } = await adminClient.query(
  1067. GET_LIST,
  1068. {
  1069. options: {
  1070. sort: {
  1071. name: SortOrder.ASC,
  1072. },
  1073. filter: {
  1074. order: {
  1075. gte: 1,
  1076. },
  1077. },
  1078. },
  1079. },
  1080. { languageCode: LanguageCode.de },
  1081. );
  1082. expect(testEntities.items.map((x: any) => x.name)).toEqual([
  1083. 'baum',
  1084. 'egg',
  1085. 'fahrrad',
  1086. 'hund',
  1087. 'kuchen',
  1088. ]);
  1089. });
  1090. it('sort by translated field de & filter & pagination', async () => {
  1091. const { testEntities } = await adminClient.query(
  1092. GET_LIST,
  1093. {
  1094. options: {
  1095. sort: {
  1096. name: SortOrder.ASC,
  1097. },
  1098. filter: {
  1099. order: {
  1100. gte: 1,
  1101. },
  1102. },
  1103. take: 2,
  1104. skip: 1,
  1105. },
  1106. },
  1107. { languageCode: LanguageCode.de },
  1108. );
  1109. expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad']);
  1110. });
  1111. });
  1112. // https://github.com/vendurehq/vendure/issues/1586
  1113. it('using the getMany() of the resulting QueryBuilder', async () => {
  1114. const { testEntitiesGetMany } = await adminClient.query(GET_ARRAY_LIST, {});
  1115. const actualPrices = testEntitiesGetMany
  1116. .sort(sortById)
  1117. .map((x: any) => x.price)
  1118. .sort((a: number, b: number) => a - b);
  1119. const expectedPrices = [11, 9, 22, 14, 13, 33].sort((a, b) => a - b);
  1120. expect(actualPrices).toEqual(expectedPrices);
  1121. });
  1122. // https://github.com/vendurehq/vendure/issues/1611
  1123. describe('translations handling', () => {
  1124. const allTranslations = [
  1125. [
  1126. { languageCode: LanguageCode.en, name: 'apple' },
  1127. { languageCode: LanguageCode.de, name: 'apfel' },
  1128. ],
  1129. [
  1130. { languageCode: LanguageCode.en, name: 'bike' },
  1131. { languageCode: LanguageCode.de, name: 'fahrrad' },
  1132. ],
  1133. ];
  1134. function getTestEntityTranslations(testEntities: { items: any[] }) {
  1135. // Explicitly sort the order of the translations as it was being non-deterministic on
  1136. // the mysql CI tests.
  1137. return testEntities.items.map((e: any) =>
  1138. e.translations.sort((a: any, b: any) => (a.languageCode < b.languageCode ? 1 : -1)),
  1139. );
  1140. }
  1141. it('returns all translations with default languageCode', async () => {
  1142. const { testEntities } = await shopClient.query(GET_LIST_WITH_TRANSLATIONS, {
  1143. options: {
  1144. take: 2,
  1145. sort: {
  1146. order: SortOrder.ASC,
  1147. },
  1148. },
  1149. });
  1150. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1151. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apple', 'bike']);
  1152. expect(testEntityTranslations).toEqual(allTranslations);
  1153. });
  1154. it('returns all translations with non-default languageCode', async () => {
  1155. const { testEntities } = await shopClient.query(
  1156. GET_LIST_WITH_TRANSLATIONS,
  1157. {
  1158. options: {
  1159. take: 2,
  1160. sort: {
  1161. order: SortOrder.ASC,
  1162. },
  1163. },
  1164. },
  1165. { languageCode: LanguageCode.de },
  1166. );
  1167. const testEntityTranslations = getTestEntityTranslations(testEntities);
  1168. expect(testEntities.items.map((e: any) => e.name)).toEqual(['apfel', 'fahrrad']);
  1169. expect(testEntityTranslations).toEqual(allTranslations);
  1170. });
  1171. });
  1172. describe('customPropertyMap', () => {
  1173. it('filter by custom string field', async () => {
  1174. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1175. options: {
  1176. sort: {
  1177. label: SortOrder.ASC,
  1178. },
  1179. filter: {
  1180. customerLastName: { contains: 'zieme' },
  1181. },
  1182. },
  1183. });
  1184. expect(testEntities.items).toEqual([
  1185. {
  1186. id: 'T_1',
  1187. label: 'A',
  1188. name: 'apple',
  1189. parent: {
  1190. id: 'T_2',
  1191. label: 'B',
  1192. name: 'bike',
  1193. },
  1194. orderRelation: {
  1195. customer: {
  1196. firstName: 'Hayden',
  1197. lastName: 'Zieme',
  1198. },
  1199. id: 'T_1',
  1200. },
  1201. },
  1202. {
  1203. id: 'T_4',
  1204. label: 'D',
  1205. name: 'dog',
  1206. parent: {
  1207. id: 'T_2',
  1208. label: 'B',
  1209. name: 'bike',
  1210. },
  1211. orderRelation: {
  1212. customer: {
  1213. firstName: 'Hayden',
  1214. lastName: 'Zieme',
  1215. },
  1216. id: 'T_4',
  1217. },
  1218. },
  1219. ]);
  1220. });
  1221. it('sort by custom string field', async () => {
  1222. const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
  1223. options: {
  1224. sort: {
  1225. customerLastName: SortOrder.ASC,
  1226. },
  1227. },
  1228. });
  1229. expect(testEntities.items.map((i: any) => i.orderRelation.customer)).toEqual([
  1230. { firstName: 'Trevor', lastName: 'Donnelly' },
  1231. { firstName: 'Trevor', lastName: 'Donnelly' },
  1232. { firstName: 'Marques', lastName: 'Sawayn' },
  1233. { firstName: 'Marques', lastName: 'Sawayn' },
  1234. { firstName: 'Hayden', lastName: 'Zieme' },
  1235. { firstName: 'Hayden', lastName: 'Zieme' },
  1236. ]);
  1237. });
  1238. });
  1239. describe('relations in customFields', () => {
  1240. it('should resolve relations in customFields successfully', async () => {
  1241. const { testEntities } = await shopClient.query(GET_LIST_WITH_CUSTOM_FIELD_RELATION, {
  1242. options: {
  1243. filter: {
  1244. label: { eq: 'A' },
  1245. },
  1246. },
  1247. });
  1248. expect(testEntities.items).toEqual([
  1249. {
  1250. id: 'T_1',
  1251. label: 'A',
  1252. customFields: {
  1253. relation: [{ id: 'T_1', data: 'A' }],
  1254. },
  1255. },
  1256. ]);
  1257. });
  1258. it('should resolve multiple relations in customFields successfully', async () => {
  1259. const { testEntities } = await shopClient.query(GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION, {
  1260. options: {
  1261. filter: {
  1262. label: { eq: 'A' },
  1263. },
  1264. },
  1265. });
  1266. expect(testEntities.items).toEqual([
  1267. {
  1268. id: 'T_1',
  1269. label: 'A',
  1270. customFields: {
  1271. relation: [{ id: 'T_1', data: 'A' }],
  1272. otherRelation: [{ id: 'T_1', data: 'A' }],
  1273. },
  1274. },
  1275. ]);
  1276. });
  1277. });
  1278. // https://github.com/vendurehq/vendure/issues/3267
  1279. describe('filtering with duplicate custom property fields in _and blocks', () => {
  1280. it('filters by single tagId', async () => {
  1281. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1282. options: {
  1283. sort: { label: SortOrder.ASC },
  1284. filter: {
  1285. tagId: { eq: 'T_1' }, // tag1
  1286. },
  1287. },
  1288. });
  1289. // Entities with tag1: A, B, C, E
  1290. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E']);
  1291. });
  1292. it('filters by multiple tagIds with _and (same field used twice)', async () => {
  1293. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1294. options: {
  1295. sort: { label: SortOrder.ASC },
  1296. filter: {
  1297. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }], // tag1 AND tag2
  1298. },
  1299. },
  1300. });
  1301. // Entities with BOTH tag1 AND tag2: A, B, E
  1302. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1303. });
  1304. it('filters by three tagIds with _and (same field used three times)', async () => {
  1305. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1306. options: {
  1307. sort: { label: SortOrder.ASC },
  1308. filter: {
  1309. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }, { tagId: { eq: 'T_3' } }], // tag1 AND tag2 AND tag3
  1310. },
  1311. },
  1312. });
  1313. // Only entity E has all three tags
  1314. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1315. });
  1316. it('filters by tagIds with _or (same field used twice)', async () => {
  1317. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1318. options: {
  1319. sort: { label: SortOrder.ASC },
  1320. filter: {
  1321. _or: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }], // tag1 OR tag2
  1322. },
  1323. },
  1324. });
  1325. // Entities with tag1 OR tag2: A, B, C, D, E
  1326. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1327. });
  1328. it('filters by tagId combined with other fields in _and', async () => {
  1329. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1330. options: {
  1331. sort: { label: SortOrder.ASC },
  1332. filter: {
  1333. _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }, { active: { eq: true } }],
  1334. },
  1335. },
  1336. });
  1337. // Entities with tag1 AND tag2 AND active=true: A, B (E is not active)
  1338. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
  1339. });
  1340. // ===== NESTED _and WITHIN _and =====
  1341. it('filters with nested _and within _and (same field)', async () => {
  1342. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1343. options: {
  1344. sort: { label: SortOrder.ASC },
  1345. filter: {
  1346. _and: [
  1347. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1348. { tagId: { eq: 'T_3' } },
  1349. ],
  1350. },
  1351. },
  1352. });
  1353. // (tag1 AND tag2) AND tag3 => Only E has all three
  1354. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1355. });
  1356. it('filters with deeply nested _and (three levels)', async () => {
  1357. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1358. options: {
  1359. sort: { label: SortOrder.ASC },
  1360. filter: {
  1361. _and: [
  1362. {
  1363. _and: [{ tagId: { eq: 'T_1' } }, { _and: [{ tagId: { eq: 'T_2' } }] }],
  1364. },
  1365. ],
  1366. },
  1367. },
  1368. });
  1369. // Deeply nested: tag1 AND tag2 => A, B, E
  1370. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1371. });
  1372. // ===== _and WITHIN _or =====
  1373. it('filters with _and within _or (same field in inner _and)', async () => {
  1374. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1375. options: {
  1376. sort: { label: SortOrder.ASC },
  1377. filter: {
  1378. _or: [
  1379. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1380. { label: { eq: 'F' } },
  1381. ],
  1382. },
  1383. },
  1384. });
  1385. // (tag1 AND tag2) OR label='F' => A, B, E (have both tags) + F (by label)
  1386. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
  1387. });
  1388. it('filters with multiple _and blocks within _or', async () => {
  1389. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1390. options: {
  1391. sort: { label: SortOrder.ASC },
  1392. filter: {
  1393. _or: [
  1394. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_3' } }] },
  1395. { _and: [{ tagId: { eq: 'T_2' } }, { active: { eq: true } }] },
  1396. ],
  1397. },
  1398. },
  1399. });
  1400. // (tag1 AND tag3) => E only
  1401. // OR (tag2 AND active) => A, B, D
  1402. // Combined: A, B, D, E
  1403. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D', 'E']);
  1404. });
  1405. // ===== _or WITHIN _and =====
  1406. it('filters with _or within _and (same field in inner _or)', async () => {
  1407. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1408. options: {
  1409. sort: { label: SortOrder.ASC },
  1410. filter: {
  1411. _and: [
  1412. { _or: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_2' } }] },
  1413. { active: { eq: true } },
  1414. ],
  1415. },
  1416. },
  1417. });
  1418. // (tag1 OR tag2) AND active => A, B, D (C has tag1 but inactive, E has both but inactive)
  1419. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
  1420. });
  1421. it('filters with same field in both _or and _and at different levels', async () => {
  1422. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1423. options: {
  1424. sort: { label: SortOrder.ASC },
  1425. filter: {
  1426. _and: [
  1427. { tagId: { eq: 'T_1' } },
  1428. { _or: [{ tagId: { eq: 'T_2' } }, { tagId: { eq: 'T_3' } }] },
  1429. ],
  1430. },
  1431. },
  1432. });
  1433. // tag1 AND (tag2 OR tag3)
  1434. // tag1: A, B, C, E
  1435. // tag2 OR tag3: A, B, D, E
  1436. // Intersection: A, B, E
  1437. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1438. });
  1439. // ===== COMPLEX MIXED SCENARIOS =====
  1440. it('filters with complex nested structure (_and containing _or containing _and)', async () => {
  1441. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1442. options: {
  1443. sort: { label: SortOrder.ASC },
  1444. filter: {
  1445. _and: [
  1446. {
  1447. _or: [
  1448. { _and: [{ tagId: { eq: 'T_1' } }, { tagId: { eq: 'T_3' } }] },
  1449. { label: { eq: 'D' } },
  1450. ],
  1451. },
  1452. { active: { eq: false } },
  1453. ],
  1454. },
  1455. },
  1456. });
  1457. // ((tag1 AND tag3) OR label='D') AND inactive
  1458. // (tag1 AND tag3): E
  1459. // label='D': D
  1460. // Combined for OR: D, E
  1461. // AND inactive: E only (D is active)
  1462. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1463. });
  1464. it('filters with same field appearing at multiple nesting levels', async () => {
  1465. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1466. options: {
  1467. sort: { label: SortOrder.ASC },
  1468. filter: {
  1469. _and: [
  1470. { tagId: { eq: 'T_1' } },
  1471. {
  1472. _and: [
  1473. { tagId: { eq: 'T_2' } },
  1474. { _or: [{ tagId: { eq: 'T_3' } }, { active: { eq: true } }] },
  1475. ],
  1476. },
  1477. ],
  1478. },
  1479. },
  1480. });
  1481. // tag1 AND (tag2 AND (tag3 OR active))
  1482. // tag1: A, B, C, E
  1483. // tag2: A, B, D, E
  1484. // tag3 OR active: E (tag3) + A, B, D (active) = A, B, D, E
  1485. // tag1 AND tag2: A, B, E
  1486. // tag1 AND tag2 AND (tag3 OR active): A, B, E (E has tag3, A and B are active)
  1487. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1488. });
  1489. // ===== USING "in" OPERATOR WITH SAME FIELD =====
  1490. it('filters with "in" operator combined with "eq" on same field', async () => {
  1491. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1492. options: {
  1493. sort: { label: SortOrder.ASC },
  1494. filter: {
  1495. _and: [{ tagId: { in: ['T_1', 'T_2'] } }, { tagId: { eq: 'T_3' } }],
  1496. },
  1497. },
  1498. });
  1499. // (tag1 or tag2) AND tag3 => Only E has tag3 and also has tag1 or tag2
  1500. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1501. });
  1502. it('filters with multiple "in" operators on same field in _and', async () => {
  1503. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1504. options: {
  1505. sort: { label: SortOrder.ASC },
  1506. filter: {
  1507. _and: [{ tagId: { in: ['T_1'] } }, { tagId: { in: ['T_2'] } }],
  1508. },
  1509. },
  1510. });
  1511. // Must have tag1 AND must have tag2 => A, B, E
  1512. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
  1513. });
  1514. // ===== TOP-LEVEL SAME FIELD (regression test) =====
  1515. it('handles same field at top level (outside _and/_or)', async () => {
  1516. // This tests that a single use of a *-to-Many field still works correctly
  1517. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1518. options: {
  1519. sort: { label: SortOrder.ASC },
  1520. filter: {
  1521. tagId: { in: ['T_1', 'T_2'] },
  1522. },
  1523. },
  1524. });
  1525. // Entities with tag1 OR tag2: A, B, C, D, E
  1526. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1527. });
  1528. it('handles single tagId filter combined with top-level _and', async () => {
  1529. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1530. options: {
  1531. sort: { label: SortOrder.ASC },
  1532. filter: {
  1533. _and: [{ tagId: { eq: 'T_3' } }, { active: { eq: false } }],
  1534. },
  1535. },
  1536. });
  1537. // tag3 AND inactive => E only (only entity with tag3, and it's inactive)
  1538. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1539. });
  1540. // ===== filterOperator: OR at root level =====
  1541. it('respects filterOperator OR with duplicate tagId filters', async () => {
  1542. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1543. options: {
  1544. sort: { label: SortOrder.ASC },
  1545. filterOperator: LogicalOperator.OR,
  1546. filter: {
  1547. tagId: { eq: 'T_3' },
  1548. label: { eq: 'F' },
  1549. },
  1550. },
  1551. });
  1552. // tag3 OR label='F' => E (has tag3) + F (by label)
  1553. expect(getItemLabels(testEntities.items)).toEqual(['E', 'F']);
  1554. });
  1555. // ===== EDGE CASES =====
  1556. it('handles empty _and array gracefully', async () => {
  1557. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1558. options: {
  1559. sort: { label: SortOrder.ASC },
  1560. filter: {
  1561. _and: [],
  1562. },
  1563. },
  1564. });
  1565. // Empty _and should return all entities
  1566. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
  1567. });
  1568. it('handles _and with single condition on *-to-Many field', async () => {
  1569. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1570. options: {
  1571. sort: { label: SortOrder.ASC },
  1572. filter: {
  1573. _and: [{ tagId: { eq: 'T_3' } }],
  1574. },
  1575. },
  1576. });
  1577. // Single condition in _and: tag3 => E only
  1578. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1579. });
  1580. it('handles notEq operator on *-to-Many field', async () => {
  1581. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1582. options: {
  1583. sort: { label: SortOrder.ASC },
  1584. filter: {
  1585. tagId: { notEq: 'T_3' },
  1586. },
  1587. },
  1588. });
  1589. // Entities that have at least one tag that is NOT tag3
  1590. // A (tag1, tag2), B (tag1, tag2), C (tag1), D (tag2), E (tag1, tag2, tag3 - has non-T_3 tags)
  1591. // F has no tags, so no tag satisfies notEq
  1592. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
  1593. });
  1594. it('handles BETWEEN operator on *-to-Many field', async () => {
  1595. // Tag priorities: tag1=10, tag2=20, tag3=30
  1596. // Find entities with tags having priority between 15 and 25 (i.e., tag2 with priority 20)
  1597. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1598. options: {
  1599. sort: { label: SortOrder.ASC },
  1600. filter: {
  1601. tagPriority: { between: { start: 15, end: 25 } },
  1602. },
  1603. },
  1604. });
  1605. // Entities with tag2 (priority 20): A, B, D, E
  1606. expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D', 'E']);
  1607. });
  1608. it('handles BETWEEN with _and on *-to-Many field', async () => {
  1609. // Find entities with tags having priority between 5 and 15 (tag1=10)
  1610. // AND tags having priority between 25 and 35 (tag3=30)
  1611. const { testEntities } = await shopClient.query(GET_LIST_WITH_TAGS, {
  1612. options: {
  1613. sort: { label: SortOrder.ASC },
  1614. filter: {
  1615. _and: [
  1616. { tagPriority: { between: { start: 5, end: 15 } } },
  1617. { tagPriority: { between: { start: 25, end: 35 } } },
  1618. ],
  1619. },
  1620. },
  1621. });
  1622. // Only E has both tag1 (priority 10) and tag3 (priority 30)
  1623. expect(getItemLabels(testEntities.items)).toEqual(['E']);
  1624. });
  1625. });
  1626. });
  1627. const GET_LIST = gql`
  1628. query GetTestEntities($options: TestEntityListOptions) {
  1629. testEntities(options: $options) {
  1630. totalItems
  1631. items {
  1632. id
  1633. label
  1634. name
  1635. date
  1636. }
  1637. }
  1638. }
  1639. `;
  1640. const GET_LIST_WITH_TRANSLATIONS = gql`
  1641. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1642. testEntities(options: $options) {
  1643. totalItems
  1644. items {
  1645. id
  1646. label
  1647. name
  1648. date
  1649. translations {
  1650. languageCode
  1651. name
  1652. }
  1653. }
  1654. }
  1655. }
  1656. `;
  1657. const GET_LIST_WITH_ORDERS = gql`
  1658. query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
  1659. testEntities(options: $options) {
  1660. totalItems
  1661. items {
  1662. id
  1663. label
  1664. name
  1665. parent {
  1666. id
  1667. label
  1668. name
  1669. }
  1670. orderRelation {
  1671. id
  1672. customer {
  1673. firstName
  1674. lastName
  1675. }
  1676. }
  1677. }
  1678. }
  1679. }
  1680. `;
  1681. const GET_ARRAY_LIST = gql`
  1682. query GetTestEntitiesArray($options: TestEntityListOptions) {
  1683. testEntitiesGetMany(options: $options) {
  1684. id
  1685. label
  1686. name
  1687. date
  1688. price
  1689. }
  1690. }
  1691. `;
  1692. const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
  1693. query GetTestWithCustomFieldRelation($options: TestEntityListOptions) {
  1694. testEntities(options: $options) {
  1695. items {
  1696. id
  1697. label
  1698. customFields {
  1699. relation {
  1700. id
  1701. data
  1702. }
  1703. }
  1704. }
  1705. }
  1706. }
  1707. `;
  1708. const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
  1709. query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
  1710. testEntities(options: $options) {
  1711. items {
  1712. id
  1713. label
  1714. customFields {
  1715. relation {
  1716. id
  1717. data
  1718. }
  1719. otherRelation {
  1720. id
  1721. data
  1722. }
  1723. }
  1724. }
  1725. }
  1726. }
  1727. `;
  1728. const GET_LIST_WITH_TAGS = gql`
  1729. query GetTestEntitiesWithTags($options: TestEntityListOptions) {
  1730. testEntities(options: $options) {
  1731. totalItems
  1732. items {
  1733. id
  1734. label
  1735. tags {
  1736. id
  1737. name
  1738. }
  1739. }
  1740. }
  1741. }
  1742. `;