Raw
1 import { MedusaModule } from "@medusajs/framework/modules-sdk"
2 import { buildSchemaObjectRepresentation } from "../build-config"
3
4 // Mock MedusaModule only
5 jest.mock("@medusajs/framework/modules-sdk", () => ({
6 MedusaModule: {
7 getAllJoinerConfigs: jest.fn(),
8 },
9 }))
10
11 // No need to mock @medusajs/framework/utils since we're using the actual implementations
12
13 describe("buildSchemaObjectRepresentation", () => {
14 // Setup mocks before each test
15 beforeEach(() => {
16 jest.clearAllMocks()
17
18 // Mock MedusaModule.getAllJoinerConfigs
19 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([])
20 })
21
22 afterEach(() => {
23 jest.resetAllMocks()
24 })
25
26 it("should a simple object representation for a single entity", () => {
27 const schema = "type Product { id: ID! }"
28
29 const output = buildSchemaObjectRepresentation(schema)
30
31 delete (output as any).executableSchema
32
33 expect(output).toEqual({
34 objectRepresentation: {
35 _serviceNameModuleConfigMap: {},
36 Product: {
37 entity: "Product",
38 parents: [],
39 alias: "",
40 listeners: [],
41 moduleConfig: null,
42 fields: ["id"],
43 },
44 _schemaPropertiesMap: {
45 "": {
46 isInverse: false,
47 isList: undefined,
48 ref: {
49 entity: "Product",
50 parents: [],
51 alias: "",
52 listeners: [],
53 moduleConfig: null,
54 fields: ["id"],
55 },
56 },
57 },
58 },
59 entitiesMap: expect.objectContaining({
60 Product: expect.objectContaining({
61 name: "Product",
62 }),
63 }),
64 })
65 })
66
67 it("should process entities", () => {
68 const moduleSchema = `
69 type Product {
70 id: ID!
71 title: String!
72 }
73 `
74
75 const productModuleJoinerConfig = {
76 serviceName: "ProductService",
77 schema: moduleSchema,
78 alias: [
79 {
80 name: "product",
81 entity: "Product",
82 },
83 ],
84 }
85
86 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
87 productModuleJoinerConfig,
88 ])
89
90 const indexSchema = `
91 type Product @Listeners(values: ["product.created"]) {
92 id: ID!
93 title: String!
94 }
95 `
96
97 const { objectRepresentation, entitiesMap } =
98 buildSchemaObjectRepresentation(indexSchema)
99
100 expect(entitiesMap).toEqual(
101 expect.objectContaining({
102 Product: expect.objectContaining({
103 name: "Product",
104 _fields: {
105 id: expect.objectContaining({
106 name: "id",
107 }),
108 title: expect.objectContaining({
109 name: "title",
110 }),
111 },
112 }),
113 })
114 )
115
116 expect(objectRepresentation["Product"]).toBeDefined()
117 expect(objectRepresentation["Product"].entity).toBe("Product")
118 expect(objectRepresentation["Product"].listeners).toEqual([
119 "product.created",
120 ])
121 expect(objectRepresentation["Product"].alias).toBe("product")
122
123 expect(objectRepresentation["Product"].fields).toBeDefined()
124 expect(objectRepresentation["Product"].fields).toEqual(["id", "title"])
125
126 expect(objectRepresentation._schemaPropertiesMap["product"]).toBeDefined()
127 expect(objectRepresentation._schemaPropertiesMap["product"].ref).toEqual({
128 entity: "Product",
129 parents: [],
130 alias: "product",
131 listeners: ["product.created"],
132 moduleConfig: productModuleJoinerConfig,
133 fields: ["id", "title"],
134 })
135 })
136
137 it("should handle parent-child relationships between entities", () => {
138 const schema = `
139 type Product {
140 id: ID!
141 title: String!
142 variants: [ProductVariant!]
143 }
144
145 type ProductVariant {
146 id: ID!
147 title: String!
148 product: Product!
149 }
150 `
151
152 const productModuleJoinerConfig = {
153 serviceName: "ProductService",
154 schema: schema,
155 alias: [
156 {
157 name: "product",
158 entity: "Product",
159 },
160 {
161 name: "variant",
162 entity: "ProductVariant",
163 },
164 ],
165 }
166
167 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
168 productModuleJoinerConfig,
169 ])
170
171 const indexSchema = `
172 type Product @Listeners(values: ["product.created"]) {
173 id: ID!
174 title: String!
175 variants: [ProductVariant!]
176 }
177
178 type ProductVariant @Listeners(values: ["variant.created"]) {
179 id: ID!
180 title: String!
181 }
182 `
183
184 const { objectRepresentation, entitiesMap } =
185 buildSchemaObjectRepresentation(indexSchema)
186
187 expect(entitiesMap).toEqual(
188 expect.objectContaining({
189 Product: expect.objectContaining({
190 name: "Product",
191 _fields: {
192 id: expect.objectContaining({
193 name: "id",
194 }),
195 title: expect.objectContaining({
196 name: "title",
197 }),
198 variants: expect.objectContaining({
199 name: "variants",
200 }),
201 },
202 }),
203 })
204 )
205
206 expect(objectRepresentation["Product"]).toBeDefined()
207 expect(objectRepresentation["Product"].entity).toBe("Product")
208 expect(objectRepresentation["Product"].parents).toEqual([])
209 expect(objectRepresentation["Product"].listeners).toEqual([
210 "product.created",
211 ])
212 expect(objectRepresentation["Product"].alias).toBe("product")
213
214 expect(objectRepresentation["Product"].fields).toBeDefined()
215 expect(objectRepresentation["Product"].fields).toEqual(["id", "title"])
216
217 expect(objectRepresentation["ProductVariant"]).toBeDefined()
218 expect(objectRepresentation["ProductVariant"].entity).toBe("ProductVariant")
219 expect(objectRepresentation["ProductVariant"].parents).toEqual([
220 expect.objectContaining({
221 ref: objectRepresentation["Product"],
222 targetProp: "variants",
223 }),
224 ])
225
226 const productRefExpectation = {
227 entity: "Product",
228 parents: [],
229 alias: "product",
230 listeners: ["product.created"],
231 moduleConfig: productModuleJoinerConfig,
232 fields: ["id", "title"],
233 }
234
235 expect(objectRepresentation._schemaPropertiesMap["product"].ref).toEqual(
236 productRefExpectation
237 )
238
239 const variantRefExpectation = {
240 entity: "ProductVariant",
241 parents: [
242 {
243 ref: productRefExpectation,
244 targetProp: "variants",
245 inverseSideProp: "product",
246 isList: true,
247 },
248 ],
249 alias: "variant",
250 listeners: ["variant.created"],
251 moduleConfig: productModuleJoinerConfig,
252 fields: ["id", "title", "product.id"],
253 }
254
255 expect(objectRepresentation._schemaPropertiesMap["variant"]).toBeDefined()
256 expect(objectRepresentation._schemaPropertiesMap["variant"].ref).toEqual(
257 variantRefExpectation
258 )
259
260 expect(
261 objectRepresentation._schemaPropertiesMap["product.variants"]
262 ).toBeDefined()
263 expect(
264 objectRepresentation._schemaPropertiesMap["product.variants"].ref
265 ).toEqual(variantRefExpectation)
266 })
267
268 it("should handle deep nested parent-child relationships between entities", () => {
269 const schema = `
270 type Category {
271 id: ID!
272 name: String!
273 products: [Product!]
274 }
275
276 type Product {
277 id: ID!
278 title: String!
279 variants: [ProductVariant!]
280 category: Category!
281 }
282
283 type ProductVariant {
284 id: ID!
285 title: String!
286 options: [VariantOption!]
287 product: Product!
288 }
289
290 type VariantOption {
291 id: ID!
292 value: String!
293 product_variant: ProductVariant!
294 }
295 `
296
297 const moduleJoinerConfig = {
298 serviceName: "ProductService",
299 schema: schema,
300 alias: [
301 {
302 name: "category",
303 entity: "Category",
304 },
305 {
306 name: "product",
307 entity: "Product",
308 },
309 {
310 name: "variant",
311 entity: "ProductVariant",
312 },
313 {
314 name: "option",
315 entity: "VariantOption",
316 },
317 ],
318 }
319
320 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
321 moduleJoinerConfig,
322 ])
323
324 const indexSchema = `
325 type Category @Listeners(values: ["category.created"]) {
326 id: ID!
327 name: String!
328 products: [Product!]
329 }
330
331 type Product @Listeners(values: ["product.created"]) {
332 id: ID!
333 title: String!
334 variants: [ProductVariant!]
335 }
336
337 type ProductVariant @Listeners(values: ["variant.created"]) {
338 id: ID!
339 title: String!
340 options: [VariantOption!]
341 }
342
343 type VariantOption @Listeners(values: ["option.created"]) {
344 id: ID!
345 value: String!
346 }
347 `
348
349 const { objectRepresentation, entitiesMap } =
350 buildSchemaObjectRepresentation(indexSchema)
351
352 // Verify entitiesMap structure
353 expect(entitiesMap).toEqual(
354 expect.objectContaining({
355 Category: expect.objectContaining({
356 name: "Category",
357 _fields: {
358 id: expect.objectContaining({
359 name: "id",
360 }),
361 name: expect.objectContaining({
362 name: "name",
363 }),
364 products: expect.objectContaining({
365 name: "products",
366 }),
367 },
368 }),
369 Product: expect.objectContaining({
370 name: "Product",
371 _fields: {
372 id: expect.objectContaining({
373 name: "id",
374 }),
375 title: expect.objectContaining({
376 name: "title",
377 }),
378 variants: expect.objectContaining({
379 name: "variants",
380 }),
381 },
382 }),
383 ProductVariant: expect.objectContaining({
384 name: "ProductVariant",
385 _fields: {
386 id: expect.objectContaining({
387 name: "id",
388 }),
389 title: expect.objectContaining({
390 name: "title",
391 }),
392 options: expect.objectContaining({
393 name: "options",
394 }),
395 },
396 }),
397 VariantOption: expect.objectContaining({
398 name: "VariantOption",
399 _fields: {
400 id: expect.objectContaining({
401 name: "id",
402 }),
403 value: expect.objectContaining({
404 name: "value",
405 }),
406 },
407 }),
408 })
409 )
410
411 // Verify that all entities exist in the objectRepresentation
412 expect(objectRepresentation["Category"]).toBeDefined()
413 expect(objectRepresentation["Category"].entity).toBe("Category")
414 expect(objectRepresentation["Category"].parents).toEqual([])
415 expect(objectRepresentation["Category"].listeners).toEqual([
416 "category.created",
417 ])
418 expect(objectRepresentation["Category"].alias).toBe("category")
419 expect(objectRepresentation["Category"].fields).toEqual(["id", "name"])
420
421 expect(objectRepresentation["Product"]).toBeDefined()
422 expect(objectRepresentation["Product"].entity).toBe("Product")
423 expect(objectRepresentation["Product"].listeners).toEqual([
424 "product.created",
425 ])
426 expect(objectRepresentation["Product"].alias).toBe("product")
427 expect(objectRepresentation["Product"].fields).toEqual([
428 "id",
429 "title",
430 "category.id",
431 ])
432
433 expect(objectRepresentation["ProductVariant"]).toBeDefined()
434 expect(objectRepresentation["ProductVariant"].entity).toBe("ProductVariant")
435 expect(objectRepresentation["ProductVariant"].listeners).toEqual([
436 "variant.created",
437 ])
438 expect(objectRepresentation["ProductVariant"].alias).toBe("variant")
439 expect(objectRepresentation["ProductVariant"].fields).toEqual([
440 "id",
441 "title",
442 "product.id",
443 ])
444
445 expect(objectRepresentation["VariantOption"]).toBeDefined()
446 expect(objectRepresentation["VariantOption"].entity).toBe("VariantOption")
447 expect(objectRepresentation["VariantOption"].listeners).toEqual([
448 "option.created",
449 ])
450 expect(objectRepresentation["VariantOption"].alias).toBe("option")
451 expect(objectRepresentation["VariantOption"].fields).toEqual([
452 "id",
453 "value",
454 "product_variant.id",
455 ])
456
457 // Check parent-child relationships
458 expect(objectRepresentation["Product"].parents).toEqual([
459 expect.objectContaining({
460 ref: objectRepresentation["Category"],
461 targetProp: "products",
462 }),
463 ])
464
465 expect(objectRepresentation["ProductVariant"].parents).toEqual([
466 expect.objectContaining({
467 ref: objectRepresentation["Product"],
468 targetProp: "variants",
469 }),
470 ])
471
472 expect(objectRepresentation["VariantOption"].parents).toEqual([
473 expect.objectContaining({
474 ref: objectRepresentation["ProductVariant"],
475 targetProp: "options",
476 }),
477 ])
478
479 // Create reference expectation objects for each entity
480 const categoryRefExpectation = {
481 entity: "Category",
482 parents: [],
483 alias: "category",
484 listeners: ["category.created"],
485 moduleConfig: moduleJoinerConfig,
486 fields: ["id", "name"],
487 }
488
489 const productRefExpectation = {
490 entity: "Product",
491 parents: [
492 {
493 ref: categoryRefExpectation,
494 targetProp: "products",
495 inverseSideProp: "category",
496 isList: true,
497 },
498 ],
499 alias: "product",
500 listeners: ["product.created"],
501 moduleConfig: moduleJoinerConfig,
502 fields: ["id", "title", "category.id"],
503 }
504
505 const variantRefExpectation = {
506 entity: "ProductVariant",
507 parents: [
508 {
509 ref: productRefExpectation,
510 targetProp: "variants",
511 inverseSideProp: "product",
512 isList: true,
513 },
514 ],
515 alias: "variant",
516 listeners: ["variant.created"],
517 moduleConfig: moduleJoinerConfig,
518 fields: ["id", "title", "product.id"],
519 }
520
521 const optionRefExpectation = {
522 entity: "VariantOption",
523 parents: [
524 {
525 ref: variantRefExpectation,
526 targetProp: "options",
527 inverseSideProp: "product_variant",
528 isList: true,
529 },
530 ],
531 alias: "option",
532 listeners: ["option.created"],
533 moduleConfig: moduleJoinerConfig,
534 fields: ["id", "value", "product_variant.id"],
535 }
536
537 // Check that aliases are correctly set in the schema properties map
538 expect(objectRepresentation._schemaPropertiesMap["category"]).toBeDefined()
539 expect(objectRepresentation._schemaPropertiesMap["category"].ref).toEqual(
540 categoryRefExpectation
541 )
542
543 expect(objectRepresentation._schemaPropertiesMap["product"]).toBeDefined()
544 expect(objectRepresentation._schemaPropertiesMap["product"].ref).toEqual(
545 productRefExpectation
546 )
547
548 expect(objectRepresentation._schemaPropertiesMap["variant"]).toBeDefined()
549 expect(objectRepresentation._schemaPropertiesMap["variant"].ref).toEqual(
550 variantRefExpectation
551 )
552
553 expect(objectRepresentation._schemaPropertiesMap["option"]).toBeDefined()
554 expect(objectRepresentation._schemaPropertiesMap["option"].ref).toEqual(
555 optionRefExpectation
556 )
557
558 // Check nested paths
559 expect(
560 objectRepresentation._schemaPropertiesMap["category.products"]
561 ).toBeDefined()
562 expect(
563 objectRepresentation._schemaPropertiesMap["category.products"].ref
564 ).toEqual(productRefExpectation)
565
566 expect(
567 objectRepresentation._schemaPropertiesMap["product.variants"]
568 ).toBeDefined()
569 expect(
570 objectRepresentation._schemaPropertiesMap["product.variants"].ref
571 ).toEqual(variantRefExpectation)
572
573 expect(
574 objectRepresentation._schemaPropertiesMap["variant.options"]
575 ).toBeDefined()
576 expect(
577 objectRepresentation._schemaPropertiesMap["variant.options"].ref
578 ).toEqual(optionRefExpectation)
579 })
580
581 it("should handle entities with various field types", () => {
582 const schema = `
583 type Product {
584 id: ID!
585 title: String!
586 price: Float!
587 inStock: Boolean!
588 inventory: Int!
589 metadata: JSON
590 createdAt: DateTime
591 tags: [String!]
592 }
593 `
594
595 const productModuleJoinerConfig = {
596 serviceName: "ProductService",
597 schema: schema,
598 alias: [
599 {
600 name: "product",
601 entity: "Product",
602 },
603 ],
604 }
605
606 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
607 productModuleJoinerConfig,
608 ])
609
610 const indexSchema = `
611 type Product @Listeners(values: ["product.created"]) {
612 id: ID!
613 title: String!
614 price: Float!
615 inStock: Boolean!
616 inventory: Int!
617 metadata: JSON
618 createdAt: DateTime
619 tags: [String!]
620 }
621
622 scalar JSON
623 scalar DateTime
624 `
625
626 const { objectRepresentation, entitiesMap } =
627 buildSchemaObjectRepresentation(indexSchema)
628
629 // Verify entitiesMap structure
630 expect(entitiesMap).toEqual(
631 expect.objectContaining({
632 Product: expect.objectContaining({
633 name: "Product",
634 _fields: {
635 id: expect.objectContaining({
636 name: "id",
637 }),
638 title: expect.objectContaining({
639 name: "title",
640 }),
641 price: expect.objectContaining({
642 name: "price",
643 }),
644 inStock: expect.objectContaining({
645 name: "inStock",
646 }),
647 inventory: expect.objectContaining({
648 name: "inventory",
649 }),
650 metadata: expect.objectContaining({
651 name: "metadata",
652 }),
653 createdAt: expect.objectContaining({
654 name: "createdAt",
655 }),
656 tags: expect.objectContaining({
657 name: "tags",
658 }),
659 },
660 }),
661 })
662 )
663
664 expect(objectRepresentation["Product"]).toBeDefined()
665 expect(objectRepresentation["Product"].entity).toBe("Product")
666 expect(objectRepresentation["Product"].parents).toEqual([])
667 expect(objectRepresentation["Product"].listeners).toEqual([
668 "product.created",
669 ])
670 expect(objectRepresentation["Product"].alias).toBe("product")
671 expect(objectRepresentation["Product"].moduleConfig).toBe(
672 productModuleJoinerConfig
673 )
674
675 // Check that all fields are included
676 expect(objectRepresentation["Product"].fields).toEqual([
677 "id",
678 "title",
679 "price",
680 "inStock",
681 "inventory",
682 "metadata",
683 "createdAt",
684 "tags",
685 ])
686
687 // Create reference expectation object for the entity
688 const productRefExpectation = {
689 entity: "Product",
690 parents: [],
691 alias: "product",
692 listeners: ["product.created"],
693 moduleConfig: productModuleJoinerConfig,
694 fields: [
695 "id",
696 "title",
697 "price",
698 "inStock",
699 "inventory",
700 "metadata",
701 "createdAt",
702 "tags",
703 ],
704 }
705
706 // Check that alias is correctly set in the schema properties map
707 expect(objectRepresentation._schemaPropertiesMap["product"]).toBeDefined()
708 expect(objectRepresentation._schemaPropertiesMap["product"].ref).toEqual(
709 productRefExpectation
710 )
711
712 // Check that module config is set correctly in the service map
713 expect(objectRepresentation._serviceNameModuleConfigMap).toEqual(
714 expect.objectContaining({
715 ProductService: productModuleJoinerConfig,
716 })
717 )
718 })
719
720 it("should handle entities with multiple listeners", () => {
721 const schema = `
722 type Product {
723 id: ID!
724 title: String!
725 }
726 `
727
728 const productModuleJoinerConfig = {
729 serviceName: "ProductService",
730 schema: schema,
731 alias: [
732 {
733 name: "product",
734 entity: "Product",
735 },
736 ],
737 }
738
739 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
740 productModuleJoinerConfig,
741 ])
742
743 const indexSchema = `
744 type Product @Listeners(values: ["product.created", "product.updated", "product.deleted"]) {
745 id: ID!
746 title: String!
747 }
748 `
749
750 const { objectRepresentation, entitiesMap } =
751 buildSchemaObjectRepresentation(indexSchema)
752
753 // Verify entitiesMap structure
754 expect(entitiesMap).toEqual(
755 expect.objectContaining({
756 Product: expect.objectContaining({
757 name: "Product",
758 _fields: {
759 id: expect.objectContaining({
760 name: "id",
761 }),
762 title: expect.objectContaining({
763 name: "title",
764 }),
765 },
766 }),
767 })
768 )
769
770 expect(objectRepresentation["Product"]).toBeDefined()
771 expect(objectRepresentation["Product"].entity).toBe("Product")
772 expect(objectRepresentation["Product"].parents).toEqual([])
773 expect(objectRepresentation["Product"].listeners).toEqual([
774 "product.created",
775 "product.updated",
776 "product.deleted",
777 ])
778 expect(objectRepresentation["Product"].alias).toBe("product")
779 expect(objectRepresentation["Product"].moduleConfig).toBe(
780 productModuleJoinerConfig
781 )
782 expect(objectRepresentation["Product"].fields).toEqual(["id", "title"])
783
784 // Create reference expectation object for the entity
785 const productRefExpectation = {
786 entity: "Product",
787 parents: [],
788 alias: "product",
789 listeners: ["product.created", "product.updated", "product.deleted"],
790 moduleConfig: productModuleJoinerConfig,
791 fields: ["id", "title"],
792 }
793
794 // Check that alias is correctly set in the schema properties map
795 expect(objectRepresentation._schemaPropertiesMap["product"]).toBeDefined()
796 expect(objectRepresentation._schemaPropertiesMap["product"].ref).toEqual(
797 productRefExpectation
798 )
799
800 // Check that module config is set correctly in the service map
801 expect(objectRepresentation._serviceNameModuleConfigMap).toEqual(
802 expect.objectContaining({
803 ProductService: productModuleJoinerConfig,
804 })
805 )
806 })
807
808 it("should handle link modules between entities from different services specifying link relationships", () => {
809 const productSchema = `
810 type Product {
811 id: ID!
812 title: String!
813 }
814 `
815
816 const orderSchema = `
817 type Order {
818 id: ID!
819 code: String!
820 }
821 `
822
823 const orderItemSchema = `
824 type OrderItem {
825 id: ID!
826 quantity: Int!
827 product_id: ID!
828 order_id: ID!
829 }
830 `
831
832 const productModuleJoinerConfig = {
833 serviceName: "ProductService",
834 schema: productSchema,
835 alias: [
836 {
837 name: "product",
838 entity: "Product",
839 },
840 ],
841 linkableKeys: {
842 product_id: "Product",
843 },
844 }
845
846 const orderModuleJoinerConfig = {
847 serviceName: "OrderService",
848 schema: orderSchema,
849 alias: [
850 {
851 name: "order",
852 entity: "Order",
853 },
854 ],
855 linkableKeys: {
856 order_id: "Order",
857 },
858 }
859
860 const orderItemLinkModuleJoinerConfig = {
861 serviceName: "OrderItemService",
862 isLink: true,
863 schema: orderItemSchema,
864 alias: [
865 {
866 name: "order_item",
867 entity: "OrderItem",
868 },
869 ],
870 relationships: [
871 {
872 serviceName: "ProductService",
873 foreignKey: "product_id",
874 },
875 {
876 serviceName: "OrderService",
877 foreignKey: "order_id",
878 },
879 ],
880 extends: [
881 {
882 serviceName: "OrderService",
883 relationship: {
884 serviceName: "OrderItemService",
885 primaryKey: "order_id",
886 },
887 },
888 {
889 serviceName: "ProductService",
890 relationship: {
891 serviceName: "OrderItemService",
892 primaryKey: "product_id",
893 },
894 },
895 ],
896 }
897
898 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
899 productModuleJoinerConfig,
900 orderModuleJoinerConfig,
901 orderItemLinkModuleJoinerConfig,
902 ])
903
904 const indexSchema = `
905 type Product @Listeners(values: ["product.created"]) {
906 id: ID!
907 title: String!
908 order_items: [OrderItem!]
909 }
910
911 type Order @Listeners(values: ["order.created"]) {
912 id: ID!
913 code: String!
914 product_items: [OrderItem!]
915 }
916
917 type OrderItem @Listeners(values: ["order_item.created"]) {
918 id: ID!
919 quantity: Int!
920 product_id: ID!
921 product: Product!
922 order_id: ID!
923 order: Order!
924 }
925 `
926
927 const { objectRepresentation, entitiesMap } =
928 buildSchemaObjectRepresentation(indexSchema)
929
930 // Verify entitiesMap structure
931 expect(entitiesMap).toEqual(
932 expect.objectContaining({
933 Product: expect.objectContaining({
934 name: "Product",
935 _fields: {
936 id: expect.objectContaining({
937 name: "id",
938 }),
939 title: expect.objectContaining({
940 name: "title",
941 }),
942 order_items: expect.objectContaining({
943 name: "order_items",
944 }),
945 },
946 }),
947 Order: expect.objectContaining({
948 name: "Order",
949 _fields: {
950 id: expect.objectContaining({
951 name: "id",
952 }),
953 code: expect.objectContaining({
954 name: "code",
955 }),
956 product_items: expect.objectContaining({
957 name: "product_items",
958 }),
959 },
960 }),
961 OrderItem: expect.objectContaining({
962 name: "OrderItem",
963 _fields: {
964 id: expect.objectContaining({
965 name: "id",
966 }),
967 quantity: expect.objectContaining({
968 name: "quantity",
969 }),
970 product: expect.objectContaining({
971 name: "product",
972 }),
973 order: expect.objectContaining({
974 name: "order",
975 }),
976 product_id: expect.objectContaining({
977 name: "product_id",
978 }),
979 order_id: expect.objectContaining({
980 name: "order_id",
981 }),
982 },
983 }),
984 })
985 )
986
987 // Verify that all entities exist in the objectRepresentation
988 expect(objectRepresentation["Product"]).toBeDefined()
989 expect(objectRepresentation["Product"].entity).toBe("Product")
990 expect(objectRepresentation["Product"].parents).toEqual([
991 {
992 ref: expect.objectContaining({
993 entity: "OrderItem",
994 }),
995 targetProp: "product",
996 inverseSideProp: "order_items",
997 isList: false,
998 },
999 ])
1000 expect(objectRepresentation["Product"].listeners).toEqual([
1001 "product.created",
1002 ])
1003 expect(objectRepresentation["Product"].alias).toBe("product")
1004 expect(objectRepresentation["Product"].moduleConfig).toBe(
1005 productModuleJoinerConfig
1006 )
1007 expect(objectRepresentation["Product"].fields).toEqual([
1008 "id",
1009 "title",
1010 "order_items.id",
1011 ])
1012
1013 expect(objectRepresentation["Order"]).toBeDefined()
1014 expect(objectRepresentation["Order"].entity).toBe("Order")
1015 expect(objectRepresentation["Order"].parents).toEqual([
1016 {
1017 ref: expect.objectContaining({
1018 entity: "OrderItem",
1019 }),
1020 targetProp: "order",
1021 inverseSideProp: "product_items",
1022 isList: false,
1023 },
1024 ])
1025 expect(objectRepresentation["Order"].listeners).toEqual(["order.created"])
1026 expect(objectRepresentation["Order"].alias).toBe("order")
1027 expect(objectRepresentation["Order"].moduleConfig).toBe(
1028 orderModuleJoinerConfig
1029 )
1030 expect(objectRepresentation["Order"].fields).toEqual([
1031 "id",
1032 "code",
1033 "product_items.id",
1034 ])
1035
1036 expect(objectRepresentation["OrderItem"]).toBeDefined()
1037 expect(objectRepresentation["OrderItem"].entity).toBe("OrderItem")
1038 expect(objectRepresentation["OrderItem"].parents).toEqual(
1039 expect.arrayContaining([
1040 {
1041 ref: expect.objectContaining({
1042 entity: "Order",
1043 }),
1044 targetProp: "product_items",
1045 inverseSideProp: "order",
1046 isList: true,
1047 },
1048 {
1049 ref: expect.objectContaining({
1050 entity: "Product",
1051 }),
1052 targetProp: "order_items",
1053 inverseSideProp: "product",
1054 isList: true,
1055 },
1056 ])
1057 )
1058 expect(objectRepresentation["OrderItem"].listeners).toEqual([
1059 "order_item.created",
1060 ])
1061 expect(objectRepresentation["OrderItem"].alias).toBe("order_item")
1062 expect(objectRepresentation["OrderItem"].moduleConfig).toBe(
1063 orderItemLinkModuleJoinerConfig
1064 )
1065 expect(objectRepresentation["OrderItem"].fields).toEqual([
1066 "id",
1067 "quantity",
1068 "product_id",
1069 "order_id",
1070 "product.id",
1071 "order.id",
1072 ])
1073
1074 // Check that links between services are properly set up
1075 expect(objectRepresentation._serviceNameModuleConfigMap).toEqual(
1076 expect.objectContaining({
1077 ProductService: productModuleJoinerConfig,
1078 OrderService: orderModuleJoinerConfig,
1079 OrderItemService: orderItemLinkModuleJoinerConfig,
1080 })
1081 )
1082 })
1083
1084 it("should handle link modules between entities from different services without specifying link relationships", () => {
1085 const productSchema = `
1086 type Product {
1087 id: ID!
1088 title: String!
1089 variants: [ProductVariant!]
1090 }
1091
1092 type ProductVariant {
1093 id: ID!
1094 title: String!
1095 product_id: ID!
1096 product: Product!
1097 }
1098 `
1099
1100 const priceSchema = `
1101 type PriceSet {
1102 id: ID!
1103 prices: [Price!]
1104 }
1105
1106 type Price {
1107 id: ID!
1108 amount: Float!
1109 currency_code: String!
1110 price_set_id: ID!
1111 price_set: PriceSet!
1112 }
1113 `
1114
1115 const productModuleJoinerConfig = {
1116 serviceName: "ProductService",
1117 schema: productSchema,
1118 alias: [
1119 {
1120 name: "product",
1121 entity: "Product",
1122 },
1123 {
1124 name: "product_variant",
1125 entity: "ProductVariant",
1126 },
1127 ],
1128 linkableKeys: {
1129 product_id: "Product",
1130 variant_id: "ProductVariant",
1131 },
1132 }
1133
1134 const priceModuleJoinerConfig = {
1135 serviceName: "PriceService",
1136 schema: priceSchema,
1137 alias: [
1138 {
1139 name: "price_set",
1140 entity: "PriceSet",
1141 },
1142 {
1143 name: "price",
1144 entity: "Price",
1145 },
1146 ],
1147 linkableKeys: {
1148 price_set_id: "PriceSet",
1149 },
1150 }
1151
1152 const productVariantPriceSetLinkModuleJoinerConfig = {
1153 serviceName: "ProductVariantPriceSetService",
1154 isLink: true,
1155 schema: `
1156 type ProductVariantPriceSetLink {
1157 id: ID!
1158 product_variant_id: ID!
1159 price_set_id: ID!
1160 product_variant: ProductVariant!
1161 price_set: [PriceSet!]
1162 }
1163
1164 extend type ProductVariant {
1165 product_variant_price_set_link: ProductVariantPriceSetLink!
1166 }
1167
1168 extend type PriceSet {
1169 product_variant_price_set_link: ProductVariantPriceSetLink!
1170 }
1171 `,
1172 alias: [
1173 {
1174 name: "product_variant_price_set_link",
1175 entity: "ProductVariantPriceSetLink",
1176 },
1177 ],
1178 relationships: [
1179 {
1180 serviceName: "ProductService",
1181 foreignKey: "product_variant_id",
1182 entity: "ProductVariant",
1183 },
1184 {
1185 serviceName: "PriceService",
1186 foreignKey: "price_set_id",
1187 entity: "PriceSet",
1188 },
1189 ],
1190 extends: [
1191 {
1192 serviceName: "ProductService",
1193 relationship: {
1194 fieldAlias: {
1195 prices: "product_variant_price_set_link.price_set.prices",
1196 isList: true,
1197 },
1198 serviceName: "ProductVariantPriceSetService",
1199 primaryKey: "product_variant_id",
1200 isList: true,
1201 },
1202 },
1203 {
1204 serviceName: "PriceService",
1205 relationship: {
1206 fieldAlias: {
1207 product_variant: "product_variant_price_set_link.product_variant",
1208 },
1209 serviceName: "ProductVariantPriceSetService",
1210 primaryKey: "price_set_id",
1211 },
1212 },
1213 ],
1214 }
1215
1216 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
1217 productModuleJoinerConfig,
1218 priceModuleJoinerConfig,
1219 productVariantPriceSetLinkModuleJoinerConfig,
1220 ])
1221
1222 const indexSchema = `
1223 type Product @Listeners(values: ["product.created"]) {
1224 id: ID
1225 title: String
1226
1227 variants: [ProductVariant]
1228 }
1229
1230 type ProductVariant @Listeners(values: ["product_variant.created"]) {
1231 id: ID
1232 product_id: String
1233 title: String
1234
1235 prices: [Price]
1236 }
1237
1238 type Price @Listeners(values: ["price.created"]) {
1239 id: ID
1240 amount: Float
1241 currency_code: String
1242 }
1243 `
1244
1245 const { objectRepresentation, entitiesMap } =
1246 buildSchemaObjectRepresentation(indexSchema)
1247
1248 // Verify entitiesMap structure
1249 expect(entitiesMap).toEqual(
1250 expect.objectContaining({
1251 Product: expect.objectContaining({
1252 name: "Product",
1253 _fields: {
1254 id: expect.objectContaining({
1255 name: "id",
1256 }),
1257 title: expect.objectContaining({
1258 name: "title",
1259 }),
1260 variants: expect.objectContaining({
1261 name: "variants",
1262 }),
1263 },
1264 }),
1265 ProductVariant: expect.objectContaining({
1266 name: "ProductVariant",
1267 _fields: {
1268 id: expect.objectContaining({
1269 name: "id",
1270 }),
1271 product_id: expect.objectContaining({
1272 name: "product_id",
1273 }),
1274 title: expect.objectContaining({
1275 name: "title",
1276 }),
1277 prices: expect.objectContaining({
1278 name: "prices",
1279 }),
1280 },
1281 }),
1282 Price: expect.objectContaining({
1283 name: "Price",
1284 _fields: {
1285 id: expect.objectContaining({
1286 name: "id",
1287 }),
1288 amount: expect.objectContaining({
1289 name: "amount",
1290 }),
1291 currency_code: expect.objectContaining({
1292 name: "currency_code",
1293 }),
1294 },
1295 }),
1296 })
1297 )
1298
1299 // Verify that all entities exist in the objectRepresentation
1300 expect(objectRepresentation["Product"]).toBeDefined()
1301 expect(objectRepresentation["Product"].entity).toBe("Product")
1302 expect(objectRepresentation["Product"].parents).toEqual([])
1303 expect(objectRepresentation["Product"].listeners).toEqual([
1304 "product.created",
1305 ])
1306 expect(objectRepresentation["Product"].alias).toBe("product")
1307 expect(objectRepresentation["Product"].moduleConfig).toBe(
1308 productModuleJoinerConfig
1309 )
1310 expect(objectRepresentation["Product"].fields).toEqual(["id", "title"])
1311
1312 expect(objectRepresentation["ProductVariant"]).toBeDefined()
1313 expect(objectRepresentation["ProductVariant"].entity).toBe("ProductVariant")
1314 expect(objectRepresentation["ProductVariant"].parents).toEqual([
1315 {
1316 ref: objectRepresentation["Product"],
1317 targetProp: "variants",
1318 inverseSideProp: "product",
1319 isList: true,
1320 },
1321 ])
1322 expect(objectRepresentation["ProductVariant"].listeners).toEqual([
1323 "product_variant.created",
1324 ])
1325 expect(objectRepresentation["ProductVariant"].alias).toBe("product_variant")
1326 expect(objectRepresentation["ProductVariant"].moduleConfig).toBe(
1327 productModuleJoinerConfig
1328 )
1329 expect(objectRepresentation["ProductVariant"].fields).toEqual([
1330 "id",
1331 "product_id",
1332 "title",
1333 "product.id",
1334 ])
1335
1336 expect(objectRepresentation["ProductVariantPriceSetLink"]).toBeDefined()
1337 expect(objectRepresentation["ProductVariantPriceSetLink"].entity).toBe(
1338 "ProductVariantPriceSetLink"
1339 )
1340 expect(objectRepresentation["ProductVariantPriceSetLink"].parents).toEqual([
1341 {
1342 ref: objectRepresentation["ProductVariant"],
1343 inverseSideProp: "product_variant",
1344 targetProp: "product_variant_price_set_link",
1345 isList: false,
1346 isInverse: false,
1347 },
1348 ])
1349 expect(objectRepresentation["ProductVariantPriceSetLink"].fields).toEqual([
1350 "id",
1351 "product_variant_id",
1352 "price_set_id",
1353 ])
1354
1355 expect(objectRepresentation["PriceSet"]).toBeDefined()
1356 expect(objectRepresentation["PriceSet"].entity).toBe("PriceSet")
1357 expect(objectRepresentation["PriceSet"].listeners).toEqual([
1358 "price-service.price-set.created",
1359 "price-service.price-set.updated",
1360 "price-service.price-set.deleted",
1361 "price-service.price-set.restored",
1362 ])
1363 expect(objectRepresentation["PriceSet"].parents).toEqual([
1364 {
1365 ref: objectRepresentation["ProductVariantPriceSetLink"],
1366 targetProp: "price_set",
1367 inverseSideProp: "product_variant_price_set_link",
1368 isList: true,
1369 },
1370 ])
1371 expect(objectRepresentation["PriceSet"].fields).toEqual(["id"])
1372 expect(objectRepresentation["PriceSet"].moduleConfig).toBe(
1373 priceModuleJoinerConfig
1374 )
1375
1376 expect(objectRepresentation["Price"]).toBeDefined()
1377 expect(objectRepresentation["Price"].entity).toBe("Price")
1378 expect(objectRepresentation["Price"].parents).toEqual([
1379 {
1380 inSchemaRef: objectRepresentation["ProductVariant"],
1381 ref: objectRepresentation["PriceSet"],
1382 targetProp: "prices",
1383 inverseSideProp: "price_set",
1384 isList: true,
1385 },
1386 ])
1387 expect(objectRepresentation["Price"].fields).toEqual([
1388 "id",
1389 "amount",
1390 "currency_code",
1391 "price_set.id",
1392 ])
1393 expect(objectRepresentation["Price"].moduleConfig).toBe(
1394 priceModuleJoinerConfig
1395 )
1396
1397 // Check that links between services are properly set up
1398 expect(objectRepresentation._serviceNameModuleConfigMap).toEqual(
1399 expect.objectContaining({
1400 ProductService: productModuleJoinerConfig,
1401 PriceService: priceModuleJoinerConfig,
1402 ProductVariantPriceSetService:
1403 productVariantPriceSetLinkModuleJoinerConfig,
1404 })
1405 )
1406 })
1407
1408 it("should handle link modules between entities from different services without specifying link relationships and multiple field aliasees for the same entity type with cyclic relationships", () => {
1409 const productSchema = `
1410 type Product {
1411 id: ID!
1412 title: String!
1413 variants: [ProductVariant!]
1414 }
1415
1416 type ProductVariant {
1417 id: ID!
1418 title: String!
1419 product_id: ID!
1420 product: Product!
1421 }
1422 `
1423
1424 const priceSchema = `
1425 type Price {
1426 id: ID!
1427 amount: Float!
1428 currency_code: String!
1429 }
1430 `
1431
1432 const productModuleJoinerConfig = {
1433 serviceName: "ProductService",
1434 schema: productSchema,
1435 alias: [
1436 {
1437 name: "product",
1438 entity: "Product",
1439 },
1440 {
1441 name: "product_variant",
1442 entity: "ProductVariant",
1443 },
1444 ],
1445 linkableKeys: {
1446 product_id: "Product",
1447 variant_id: "ProductVariant",
1448 },
1449 }
1450
1451 const priceModuleJoinerConfig = {
1452 serviceName: "PriceService",
1453 schema: priceSchema,
1454 alias: [
1455 {
1456 name: "price",
1457 entity: "Price",
1458 },
1459 ],
1460 linkableKeys: {
1461 price_id: "Price",
1462 },
1463 }
1464
1465 const productPriceLinkModuleJoinerConfig = {
1466 serviceName: "ProductPriceService",
1467 isLink: true,
1468 schema: `
1469 type ProductPriceLink {
1470 id: ID!
1471 product_id: ID!
1472 price_id: ID!
1473 product: Product!
1474 price: Price!
1475 }
1476
1477 extend type Product {
1478 product_price_link: ProductPriceLink!
1479 }
1480
1481 extend type Price {
1482 product_price_link: ProductPriceLink!
1483 }
1484 `,
1485 alias: [
1486 {
1487 name: "product_price_link",
1488 entity: "ProductPriceLink",
1489 },
1490 ],
1491 relationships: [
1492 {
1493 serviceName: "ProductService",
1494 foreignKey: "product_id",
1495 entity: "Product",
1496 },
1497 {
1498 serviceName: "PriceService",
1499 foreignKey: "price_id",
1500 entity: "Price",
1501 },
1502 ],
1503 extends: [
1504 {
1505 serviceName: "ProductService",
1506 relationship: {
1507 fieldAlias: {
1508 prices: "product_price_link.price",
1509 isList: true,
1510 },
1511 serviceName: "ProductPriceService",
1512 primaryKey: "product_id",
1513 isList: true,
1514 },
1515 },
1516 {
1517 serviceName: "PriceService",
1518 relationship: {
1519 fieldAlias: {
1520 product: "product_price_link.product",
1521 },
1522 serviceName: "ProductPriceService",
1523 primaryKey: "price_id",
1524 },
1525 },
1526 ],
1527 }
1528
1529 const productVariantPriceLinkModuleJoinerConfig = {
1530 serviceName: "ProductVariantPriceService",
1531 isLink: true,
1532 schema: `
1533 type ProductVariantPriceLink {
1534 id: ID!
1535 product_variant_id: ID!
1536 price_id: ID!
1537 product_variant: ProductVariant!
1538 price: Price!
1539 }
1540
1541 extend type ProductVariant {
1542 product_variant_price_link: ProductVariantPriceLink!
1543 }
1544
1545 extend type Price {
1546 product_variant_price_link: ProductVariantPriceLink!
1547 }
1548 `,
1549 alias: [
1550 {
1551 name: "product_variant_price_link",
1552 entity: "ProductVariantPriceLink",
1553 },
1554 ],
1555 relationships: [
1556 {
1557 serviceName: "ProductService",
1558 foreignKey: "variant_id",
1559 entity: "ProductVariant",
1560 },
1561 {
1562 serviceName: "PriceService",
1563 foreignKey: "price_id",
1564 entity: "Price",
1565 },
1566 ],
1567 extends: [
1568 {
1569 serviceName: "ProductService",
1570 relationship: {
1571 fieldAlias: {
1572 product_variant: "product_variant_price_link.product_variant",
1573 },
1574 serviceName: "ProductVariantPriceService",
1575 primaryKey: "variant_id",
1576 isList: true,
1577 },
1578 },
1579 {
1580 serviceName: "PriceService",
1581 relationship: {
1582 fieldAlias: {
1583 price: "product_variant_price_link.price",
1584 },
1585 serviceName: "ProductVariantPriceService",
1586 primaryKey: "price_id",
1587 },
1588 },
1589 ],
1590 }
1591
1592 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
1593 productModuleJoinerConfig,
1594 priceModuleJoinerConfig,
1595 productPriceLinkModuleJoinerConfig,
1596 productVariantPriceLinkModuleJoinerConfig,
1597 ])
1598
1599 const indexSchema = `
1600 type Product @Listeners(values: ["product.created"]) {
1601 id: ID
1602 title: String
1603
1604 prices: [Price]
1605 }
1606
1607 type ProductVariant @Listeners(values: ["product_variant.created"]) {
1608 id: ID
1609 title: String
1610
1611 prices: [Price]
1612 }
1613
1614 type Price @Listeners(values: ["price.created"]) {
1615 id: ID
1616 amount: Float
1617 currency_code: String
1618 product_variant: ProductVariant
1619 product: Product
1620 }
1621 `
1622
1623 const { objectRepresentation, entitiesMap } =
1624 buildSchemaObjectRepresentation(indexSchema)
1625
1626 // Verify entitiesMap structure
1627 expect(entitiesMap).toEqual(
1628 expect.objectContaining({
1629 Product: expect.objectContaining({
1630 name: "Product",
1631 _fields: {
1632 id: expect.objectContaining({
1633 name: "id",
1634 }),
1635 title: expect.objectContaining({
1636 name: "title",
1637 }),
1638 prices: expect.objectContaining({
1639 name: "prices",
1640 }),
1641 },
1642 }),
1643 ProductVariant: expect.objectContaining({
1644 name: "ProductVariant",
1645 _fields: {
1646 id: expect.objectContaining({
1647 name: "id",
1648 }),
1649 title: expect.objectContaining({
1650 name: "title",
1651 }),
1652 prices: expect.objectContaining({
1653 name: "prices",
1654 }),
1655 },
1656 }),
1657 Price: expect.objectContaining({
1658 name: "Price",
1659 _fields: {
1660 id: expect.objectContaining({
1661 name: "id",
1662 }),
1663 amount: expect.objectContaining({
1664 name: "amount",
1665 }),
1666 currency_code: expect.objectContaining({
1667 name: "currency_code",
1668 }),
1669 product_variant: expect.objectContaining({
1670 name: "product_variant",
1671 }),
1672 product: expect.objectContaining({
1673 name: "product",
1674 }),
1675 },
1676 }),
1677 })
1678 )
1679
1680 // Verify that all entities exist in the objectRepresentation
1681 expect(objectRepresentation["Product"]).toBeDefined()
1682 expect(objectRepresentation["Product"].entity).toBe("Product")
1683 expect(objectRepresentation["Product"].parents).toEqual([
1684 expect.objectContaining({
1685 inSchemaRef: expect.objectContaining({
1686 entity: "Price",
1687 }),
1688 ref: expect.objectContaining({
1689 entity: "ProductPriceLink",
1690 }),
1691 targetProp: "product",
1692 inverseSideProp: "product_price_link",
1693 isList: false,
1694 }),
1695 ])
1696 expect(objectRepresentation["Product"].listeners).toEqual([
1697 "product.created",
1698 ])
1699 expect(objectRepresentation["Product"].alias).toBe("product")
1700 expect(objectRepresentation["Product"].moduleConfig).toBe(
1701 productModuleJoinerConfig
1702 )
1703 expect(objectRepresentation["Product"].fields).toEqual(["id", "title"])
1704
1705 expect(objectRepresentation["ProductVariant"]).toBeDefined()
1706 expect(objectRepresentation["ProductVariant"].entity).toBe("ProductVariant")
1707 expect(objectRepresentation["ProductVariant"].parents).toEqual([
1708 expect.objectContaining({
1709 inSchemaRef: expect.objectContaining({
1710 entity: "Price",
1711 }),
1712 ref: expect.objectContaining({
1713 entity: "ProductVariantPriceLink",
1714 }),
1715 targetProp: "product_variant",
1716 inverseSideProp: "product_variant_price_link",
1717 isList: false,
1718 }),
1719 ])
1720 expect(objectRepresentation["ProductVariant"].listeners).toEqual([
1721 "product_variant.created",
1722 ])
1723 expect(objectRepresentation["ProductVariant"].alias).toBe("product_variant")
1724 expect(objectRepresentation["ProductVariant"].moduleConfig).toBe(
1725 productModuleJoinerConfig
1726 )
1727 expect(objectRepresentation["ProductVariant"].fields).toEqual([
1728 "id",
1729 "title",
1730 ])
1731
1732 expect(objectRepresentation["ProductVariantPriceLink"]).toBeDefined()
1733 expect(objectRepresentation["ProductVariantPriceLink"].entity).toBe(
1734 "ProductVariantPriceLink"
1735 )
1736
1737 expect(objectRepresentation["ProductVariantPriceLink"].parents).toEqual([
1738 expect.objectContaining({
1739 ref: expect.objectContaining({
1740 entity: "Price",
1741 }),
1742 targetProp: "product_variant_price_link",
1743 inverseSideProp: "price",
1744 isList: false,
1745 isInverse: true,
1746 }),
1747 expect.objectContaining({
1748 ref: expect.objectContaining({
1749 entity: "ProductVariant",
1750 }),
1751 targetProp: "product_variant_price_link",
1752 inverseSideProp: "product_variant",
1753 isList: false,
1754 isInverse: false,
1755 }),
1756 ])
1757 expect(objectRepresentation["ProductVariantPriceLink"].fields).toEqual([
1758 "id",
1759 "variant_id",
1760 "price_id",
1761 ])
1762
1763 expect(objectRepresentation["ProductPriceLink"]).toBeDefined()
1764 expect(objectRepresentation["ProductPriceLink"].entity).toBe(
1765 "ProductPriceLink"
1766 )
1767
1768 expect(objectRepresentation["ProductPriceLink"].parents).toEqual([
1769 expect.objectContaining({
1770 ref: expect.objectContaining({
1771 entity: "Product",
1772 }),
1773 inverseSideProp: "product",
1774 targetProp: "product_price_link",
1775 isList: false,
1776 isInverse: false,
1777 }),
1778 expect.objectContaining({
1779 ref: expect.objectContaining({
1780 entity: "Price",
1781 }),
1782 inverseSideProp: "price",
1783 targetProp: "product_price_link",
1784 isList: false,
1785 isInverse: true,
1786 }),
1787 ])
1788 expect(objectRepresentation["ProductPriceLink"].fields).toEqual([
1789 "id",
1790 "product_id",
1791 "price_id",
1792 ])
1793
1794 expect(objectRepresentation["Price"]).toBeDefined()
1795 expect(objectRepresentation["Price"].entity).toBe("Price")
1796 expect(objectRepresentation["Price"].parents).toEqual([
1797 expect.objectContaining({
1798 inSchemaRef: expect.objectContaining({
1799 entity: "Product",
1800 }),
1801 ref: expect.objectContaining({
1802 entity: "ProductPriceLink",
1803 }),
1804 targetProp: "prices",
1805 inverseSideProp: "product_price_link",
1806 isList: false,
1807 }),
1808 expect.objectContaining({
1809 inSchemaRef: expect.objectContaining({
1810 entity: "ProductVariant",
1811 }),
1812 ref: expect.objectContaining({
1813 entity: "ProductVariantPriceLink",
1814 }),
1815 targetProp: "prices",
1816 inverseSideProp: "product_variant_price_link",
1817 isList: false,
1818 }),
1819 ])
1820 expect(objectRepresentation["Price"].fields).toEqual([
1821 "id",
1822 "amount",
1823 "currency_code",
1824 ])
1825 expect(objectRepresentation["Price"].moduleConfig).toBe(
1826 priceModuleJoinerConfig
1827 )
1828
1829 // Check that links between services are properly set up
1830 expect(objectRepresentation._serviceNameModuleConfigMap).toEqual(
1831 expect.objectContaining({
1832 ProductService: productModuleJoinerConfig,
1833 PriceService: priceModuleJoinerConfig,
1834 ProductPriceService: productPriceLinkModuleJoinerConfig,
1835 ProductVariantPriceService: productVariantPriceLinkModuleJoinerConfig,
1836 })
1837 )
1838 })
1839
1840 it("should set inSchemaRef for two entities (Alpha and Beta) when both are linked to Product", () => {
1841 const productSchema = `
1842 type Product {
1843 id: ID!
1844 title: String!
1845 }
1846 `
1847
1848 const customSchema = `
1849 type Alpha {
1850 id: ID!
1851 product_id: ID!
1852 product: Product!
1853 }
1854
1855 type Beta {
1856 id: ID!
1857 product_id: ID!
1858 product: Product!
1859 }
1860 `
1861
1862 const productModuleJoinerConfig = {
1863 serviceName: "ProductService",
1864 schema: productSchema,
1865 alias: [
1866 {
1867 name: "product",
1868 entity: "Product",
1869 },
1870 ],
1871 linkableKeys: {
1872 product_id: "Product",
1873 },
1874 }
1875
1876 const customModuleJoinerConfig = {
1877 serviceName: "CustomService",
1878 schema: customSchema,
1879 alias: [
1880 {
1881 name: "alpha",
1882 entity: "Alpha",
1883 },
1884 {
1885 name: "beta",
1886 entity: "Beta",
1887 },
1888 ],
1889 linkableKeys: {
1890 alpha_id: "Alpha",
1891 beta_id: "Beta",
1892 },
1893 }
1894
1895 const alphaProductLinkModuleJoinerConfig = {
1896 serviceName: "AlphaProductLinkService",
1897 isLink: true,
1898 schema: `
1899 type AlphaProductLink {
1900 id: ID!
1901 alpha_id: ID!
1902 product_id: ID!
1903 alpha: Alpha!
1904 product: Product!
1905 }
1906
1907 extend type Alpha {
1908 alpha_product_link: AlphaProductLink!
1909 }
1910
1911 extend type Product {
1912 alpha_product_link: AlphaProductLink!
1913 }
1914 `,
1915 alias: [
1916 {
1917 name: "alpha_product_link",
1918 entity: "AlphaProductLink",
1919 },
1920 ],
1921 relationships: [
1922 {
1923 serviceName: "CustomService",
1924 foreignKey: "alpha_id",
1925 entity: "Alpha",
1926 },
1927 {
1928 serviceName: "ProductService",
1929 foreignKey: "product_id",
1930 entity: "Product",
1931 },
1932 ],
1933 extends: [
1934 {
1935 serviceName: "CustomService",
1936 relationship: {
1937 fieldAlias: {
1938 product: "alpha_product_link.product",
1939 },
1940 serviceName: "AlphaProductLinkService",
1941 primaryKey: "alpha_id",
1942 },
1943 },
1944 {
1945 serviceName: "ProductService",
1946 relationship: {
1947 fieldAlias: {
1948 alphas: "alpha_product_link.alpha",
1949 isList: true,
1950 },
1951 serviceName: "AlphaProductLinkService",
1952 primaryKey: "product_id",
1953 isList: true,
1954 },
1955 },
1956 ],
1957 }
1958
1959 const betaProductLinkModuleJoinerConfig = {
1960 serviceName: "BetaProductLinkService",
1961 isLink: true,
1962 schema: `
1963 type BetaProductLink {
1964 id: ID!
1965 beta_id: ID!
1966 product_id: ID!
1967 beta: Beta!
1968 product: Product!
1969 }
1970
1971 extend type Beta {
1972 beta_product_link: BetaProductLink!
1973 }
1974
1975 extend type Product {
1976 beta_product_link: BetaProductLink!
1977 }
1978 `,
1979 alias: [
1980 {
1981 name: "beta_product_link",
1982 entity: "BetaProductLink",
1983 },
1984 ],
1985 relationships: [
1986 {
1987 serviceName: "CustomService",
1988 foreignKey: "beta_id",
1989 entity: "Beta",
1990 },
1991 {
1992 serviceName: "ProductService",
1993 foreignKey: "product_id",
1994 entity: "Product",
1995 },
1996 ],
1997 extends: [
1998 {
1999 serviceName: "CustomService",
2000 relationship: {
2001 fieldAlias: {
2002 product: "beta_product_link.product",
2003 },
2004 serviceName: "BetaProductLinkService",
2005 primaryKey: "beta_id",
2006 },
2007 },
2008 {
2009 serviceName: "ProductService",
2010 relationship: {
2011 fieldAlias: {
2012 betas: "beta_product_link.beta",
2013 isList: true,
2014 },
2015 serviceName: "BetaProductLinkService",
2016 primaryKey: "product_id",
2017 isList: true,
2018 },
2019 },
2020 ],
2021 }
2022
2023 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([
2024 productModuleJoinerConfig,
2025 customModuleJoinerConfig,
2026 alphaProductLinkModuleJoinerConfig,
2027 betaProductLinkModuleJoinerConfig,
2028 ])
2029
2030 const indexSchema = `
2031 type Product @Listeners(values: ["product.created"]) {
2032 id: ID
2033 title: String
2034 alphas: [Alpha]
2035 betas: [Beta]
2036 }
2037
2038 type Alpha @Listeners(values: ["custom.alpha.created"]) {
2039 id: ID
2040 product: Product
2041 }
2042
2043 type Beta @Listeners(values: ["custom.beta.created"]) {
2044 id: ID
2045 product: Product
2046 }
2047 `
2048
2049 const { objectRepresentation } =
2050 buildSchemaObjectRepresentation(indexSchema)
2051
2052 const alphaParentWithSchemaRef = objectRepresentation["Alpha"].parents.find(
2053 (parent) => parent.inSchemaRef?.entity === "Product"
2054 )
2055 const betaParentWithSchemaRef = objectRepresentation["Beta"].parents.find(
2056 (parent) => parent.inSchemaRef?.entity === "Product"
2057 )
2058
2059 expect(alphaParentWithSchemaRef).toBeDefined()
2060 expect(alphaParentWithSchemaRef).toEqual(
2061 expect.objectContaining({
2062 inSchemaRef: objectRepresentation["Product"],
2063 })
2064 )
2065
2066 expect(betaParentWithSchemaRef).toBeDefined()
2067 expect(betaParentWithSchemaRef).toEqual(
2068 expect.objectContaining({
2069 inSchemaRef: objectRepresentation["Product"],
2070 })
2071 )
2072 })
2073
2074 it("should throw an error when an entity with listeners doesn't have a corresponding module", () => {
2075 const indexSchema = `
2076 type Product @Listeners(values: ["product.created"]) {
2077 id: ID!
2078 title: String!
2079 }
2080 `
2081
2082 // Return empty array for getAllJoinerConfigs so there's no module for the Product entity
2083 ;(MedusaModule.getAllJoinerConfigs as jest.Mock).mockReturnValue([])
2084
2085 // The function should throw an error because the entity has listeners but no module
2086 expect(() => {
2087 buildSchemaObjectRepresentation(indexSchema)
2088 }).toThrow(
2089 /unable to retrieve the module that corresponds to the entity Product/
2090 )
2091 })
2092 })