chore(modules-sdk,orchestration): to remote joiner query (#4974)
Helper function to transform js/json object into RemoteJoinerQuery format.
```typescript
toRemoteJoinerQuery({
product: {
fields: ["id", "title"],
__args: {
skip: 0,
},
variants: {
fields: ["id", "title", "handle", "sku"],
shipping_profile: {
profile: {
fields: ["id", "name"],
},
},
},
collections: {
fields: ["id", "title"],
},
},
})
```
outputs:
```
{
alias: "product",
fields: ["id", "title"],
expands: [
{
property: "product.variants",
fields: ["id", "title", "handle", "sku"],
},
{
property: "product.variants.shipping_profile",
},
{
property: "product.variants.shipping_profile.profile",
fields: ["id", "name"],
},
{
property: "product.collections",
fields: ["id", "title"],
},
],
args: [
{
name: "skip",
value: 0,
},
],
};
```
This commit is contained in:
committed by
GitHub
parent
7a12aa04ac
commit
d8649bacaa
58
packages/orchestration/src/joiner/helpers.ts
Normal file
58
packages/orchestration/src/joiner/helpers.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { RemoteJoinerQuery } from "@medusajs/types"
|
||||
|
||||
export function toRemoteJoinerQuery(obj: any): RemoteJoinerQuery {
|
||||
const remoteJoinerQuery: RemoteJoinerQuery = {
|
||||
alias: "",
|
||||
fields: [],
|
||||
expands: [],
|
||||
}
|
||||
|
||||
function extractRecursive(obj, parentName = "") {
|
||||
for (const key in obj) {
|
||||
const value = obj[key]
|
||||
|
||||
const canExpand =
|
||||
typeof value === "object" &&
|
||||
!["fields", "__args", "__directives"].includes(key)
|
||||
|
||||
if (canExpand) {
|
||||
const entityName = parentName ? `${parentName}.${key}` : key
|
||||
const expandObj: any = {
|
||||
property: entityName,
|
||||
}
|
||||
|
||||
const isEntryPoint = parentName === ""
|
||||
const reference = isEntryPoint ? remoteJoinerQuery : expandObj
|
||||
|
||||
if (value.__args) {
|
||||
reference.args = Object.entries(value.__args).map(
|
||||
([name, value]) => ({
|
||||
name,
|
||||
value,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
if (value.__directives) {
|
||||
reference.directives = Object.entries(value.__directives).map(
|
||||
([name, value]) => ({ name, value })
|
||||
)
|
||||
}
|
||||
|
||||
reference.fields = value.fields
|
||||
|
||||
if (isEntryPoint) {
|
||||
remoteJoinerQuery.alias = key
|
||||
} else {
|
||||
remoteJoinerQuery.expands!.push(expandObj)
|
||||
}
|
||||
|
||||
extractRecursive(value, entityName)
|
||||
}
|
||||
}
|
||||
|
||||
return remoteJoinerQuery
|
||||
}
|
||||
|
||||
return extractRecursive(obj)
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./helpers"
|
||||
export * from "./remote-joiner"
|
||||
|
||||
Reference in New Issue
Block a user