@medusajs/link-modules 0.2.12-snapshot-20240904130551 → 0.2.12-snapshot-20240906075935
Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"generate-schema.d.ts","sourceRoot":"","sources":["../../src/utils/generate-schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;
|
1
|
+
{"version":3,"file":"generate-schema.d.ts","sourceRoot":"","sources":["../../src/utils/generate-schema.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAS9E,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,kBAAkB,EAChC,OAAO,EAAE,wBAAwB,EACjC,OAAO,EAAE,wBAAwB,EACjC,EAAE,MAAM,EAAE,GAAE;IAAE,MAAM,MAAA;CAAwB,UAmM7C"}
|
@@ -20,22 +20,71 @@ function generateGraphQLSchema(joinerConfig, primary, foreign, { logger } = { lo
|
|
20
20
|
if (!extendedModule) {
|
21
21
|
throw new Error(`Module ${extend.serviceName} not found. Please verify that the module is configured and installed, also the module must be loaded before the link modules.`);
|
22
22
|
}
|
23
|
-
const extJoinerConfig =
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
/* const extJoinerConfig = MedusaModule.getJoinerConfig(
|
24
|
+
extend.relationship.serviceName
|
25
|
+
)*/
|
26
|
+
const extendedEntityName = extendedModule[extend.serviceName].__joinerConfig.linkableKeys[extend.relationship.primaryKey];
|
27
|
+
if (!isReadOnlyLink && (!primary || !foreign || !extendedEntityName)) {
|
28
|
+
logger.warn(`Link modules schema: No linkable key found for ${extend.relationship.primaryKey} on module ${extend.serviceName}.`);
|
27
29
|
continue;
|
28
30
|
}
|
29
31
|
const fieldName = (0, utils_1.camelToSnakeCase)((0, utils_1.lowerCaseFirst)(extend.relationship.alias));
|
30
32
|
let type = extend.relationship.isList ? `[${entityName}]` : entityName;
|
31
|
-
if (
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
if (joinerConfig?.isReadOnlyLink) {
|
34
|
+
// TODO: In readonly, the relation ship of the extend should be applied on all entities in the module that have the relationshiop foregin key attribute
|
35
|
+
/*type = extend.relationship.isList
|
36
|
+
? `[${extendedEntityName}]`
|
37
|
+
: extendedEntityName*/
|
38
|
+
continue;
|
35
39
|
}
|
40
|
+
/**
|
41
|
+
* Find the field aliases shortcut to extend the entity with it
|
42
|
+
*/
|
43
|
+
const fieldsAliasesField = Object.entries(extend.fieldAlias || {})
|
44
|
+
.map(([field, config]) => {
|
45
|
+
const path = (0, utils_1.isString)(config) ? config : config.path;
|
46
|
+
const isList = (0, utils_1.isString)(config)
|
47
|
+
? extend.relationship.isList
|
48
|
+
: config.isList ?? extend.relationship.isList;
|
49
|
+
const pathSegments = path.split(",").reverse();
|
50
|
+
/*const relationshipMarkerIndex = pathSegments.findIndex((segment) => {
|
51
|
+
return !!joinerConfig.relationships!.find(
|
52
|
+
(relation) => relation.alias === targetEntityAlias
|
53
|
+
)
|
54
|
+
})
|
55
|
+
|
56
|
+
if (relationshipMarkerIndex === -1) {
|
57
|
+
return
|
58
|
+
}*/
|
59
|
+
/*const relationshipPropertyPath = pathSegments
|
60
|
+
.slice(0, relationshipMarkerIndex + 1)
|
61
|
+
.reverse()*/
|
62
|
+
const targetEntityAlias = path.split(".").pop();
|
63
|
+
const targetEntityRelation = joinerConfig.relationships?.find((relation) => relation.alias === targetEntityAlias);
|
64
|
+
if (!targetEntityRelation) {
|
65
|
+
return;
|
66
|
+
}
|
67
|
+
const targetEntityName = modules_sdk_1.MedusaModule.getJoinerConfig(targetEntityRelation.serviceName).linkableKeys?.[targetEntityRelation.foreignKey];
|
68
|
+
if (!targetEntityName) {
|
69
|
+
logger.warn(`Link modules schema: No linkable key found for ${targetEntityRelation.foreignKey} on module ${targetEntityRelation.serviceName}.`);
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
// TODO: Re visit field aliases that access properties from a type
|
73
|
+
/*const targetEntityType = `${targetEntityName}${
|
74
|
+
relationshipPropertyPath.length
|
75
|
+
? relationshipPropertyPath.reduce((acc, value) => {
|
76
|
+
return `${acc}[${value}]`
|
77
|
+
}, targetEntityName)
|
78
|
+
: ""
|
79
|
+
}`*/
|
80
|
+
return `${field}: ${isList ? `[${targetEntityName}]` : targetEntityName}`;
|
81
|
+
})
|
82
|
+
.filter(Boolean);
|
36
83
|
typeDef += `
|
37
|
-
extend type ${
|
84
|
+
extend type ${extendedEntityName} {
|
38
85
|
${fieldName}: ${type}
|
86
|
+
|
87
|
+
${fieldsAliasesField.join("\n")}
|
39
88
|
}
|
40
89
|
`;
|
41
90
|
}
|
@@ -57,9 +106,16 @@ function generateGraphQLSchema(joinerConfig, primary, foreign, { logger } = { lo
|
|
57
106
|
nullable: !!extraFields[column].nullable,
|
58
107
|
};
|
59
108
|
}
|
109
|
+
// TODO: temporary, every module might always expose their schema
|
110
|
+
const doesPrimaryExportSchema = !!modules_sdk_1.MedusaModule.getJoinerConfig(primary.serviceName)?.schema;
|
111
|
+
const doesForeignExportSchema = !!modules_sdk_1.MedusaModule.getJoinerConfig(foreign.serviceName)?.schema;
|
60
112
|
// Link table relationships
|
61
|
-
const primaryField =
|
62
|
-
|
113
|
+
const primaryField = doesPrimaryExportSchema
|
114
|
+
? `${(0, utils_1.camelToSnakeCase)(primary.alias)}: ${(0, utils_1.toPascalCase)((0, compose_link_name_1.composeTableName)(primary.serviceName))}`
|
115
|
+
: "";
|
116
|
+
const foreignField = doesForeignExportSchema
|
117
|
+
? `${(0, utils_1.camelToSnakeCase)(foreign.alias)}: ${(0, utils_1.toPascalCase)((0, compose_link_name_1.composeTableName)(foreign.serviceName))}`
|
118
|
+
: "";
|
63
119
|
typeDef += `
|
64
120
|
type ${entityName} {
|
65
121
|
${Object.entries(fields)
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@medusajs/link-modules",
|
3
|
-
"version": "0.2.12-snapshot-
|
3
|
+
"version": "0.2.12-snapshot-20240906075935",
|
4
4
|
"description": "Medusa Default Link Modules Package and Definitions",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -29,10 +29,10 @@
|
|
29
29
|
"test:integration": "jest --forceExit -- integration-tests/**/__tests__/**/*.ts"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
|
-
"@medusajs/types": "1.12.0-snapshot-
|
32
|
+
"@medusajs/types": "1.12.0-snapshot-20240906075935",
|
33
33
|
"cross-env": "^5.2.1",
|
34
34
|
"jest": "^29.7.0",
|
35
|
-
"medusa-test-utils": "1.1.45-snapshot-
|
35
|
+
"medusa-test-utils": "1.1.45-snapshot-20240906075935",
|
36
36
|
"pg-god": "^1.0.12",
|
37
37
|
"rimraf": "^5.0.1",
|
38
38
|
"ts-node": "^10.9.1",
|
@@ -40,8 +40,8 @@
|
|
40
40
|
"typescript": "^5.5.0"
|
41
41
|
},
|
42
42
|
"dependencies": {
|
43
|
-
"@medusajs/modules-sdk": "1.13.0-snapshot-
|
44
|
-
"@medusajs/utils": "1.12.0-snapshot-
|
43
|
+
"@medusajs/modules-sdk": "1.13.0-snapshot-20240906075935",
|
44
|
+
"@medusajs/utils": "1.12.0-snapshot-20240906075935"
|
45
45
|
},
|
46
46
|
"peerDependencies": {
|
47
47
|
"@mikro-orm/core": "5.9.7",
|