feat(modules-sdk): remote query context filter (#7153)

What:

- Remote query now handles `context` keywork in the arguments to forward it as a filter to the method `list`
- Pricing module `list` method returning `calculated_price` if requested as a field
This commit is contained in:
Carlos R. L. Rodrigues
2024-04-26 07:20:42 -03:00
committed by GitHub
parent aef222278b
commit 4b57c5d286
10 changed files with 202 additions and 21 deletions
@@ -33,13 +33,23 @@ describe("remoteQueryObjectFromString", function () {
it("should return a remote query object", function () {
const output = remoteQueryObjectFromString({
entryPoint: "product",
variables: {},
variables: {
q: "name",
options: {
name: "option_name",
},
"options.values": {
value: 123,
},
},
fields,
})
expect(output).toEqual({
product: {
__args: {},
__args: {
q: "name",
},
fields: [
"id",
"created_at",
@@ -54,6 +64,9 @@ describe("remoteQueryObjectFromString", function () {
},
options: {
__args: {
name: "option_name",
},
fields: [
"id",
"created_at",
@@ -64,6 +77,9 @@ describe("remoteQueryObjectFromString", function () {
"metadata",
],
values: {
__args: {
value: 123,
},
fields: [
"id",
"created_at",
@@ -1,3 +1,5 @@
import { isObject } from "./is-object"
/**
* Convert a string fields array to a remote query object
* @param config - The configuration object
@@ -109,9 +111,7 @@ export function remoteQueryObjectFromString(
},
}
if (variables) {
remoteJoinerConfig[entryKey]["__args"] = variables
}
const usedVariables = new Set()
for (const field of fields) {
if (!field.includes(".")) {
@@ -122,8 +122,19 @@ export function remoteQueryObjectFromString(
const fieldSegments = field.split(".")
const fieldProperty = fieldSegments.pop()
let combinedPath = ""
const deepConfigRef = fieldSegments.reduce((acc, curr) => {
acc[curr] ??= {}
combinedPath = combinedPath ? combinedPath + "." + curr : curr
if (isObject(variables) && combinedPath in variables) {
acc[curr] ??= {}
acc[curr]["__args"] = variables[combinedPath]
usedVariables.add(combinedPath)
} else {
acc[curr] ??= {}
}
return acc[curr]
}, remoteJoinerConfig[entryKey])
@@ -131,5 +142,14 @@ export function remoteQueryObjectFromString(
deepConfigRef["fields"].push(fieldProperty)
}
const topLevelArgs = {}
for (const key of Object.keys(variables ?? {})) {
if (!usedVariables.has(key)) {
topLevelArgs[key] = variables[key]
}
}
remoteJoinerConfig[entryKey]["__args"] = topLevelArgs ?? {}
return remoteJoinerConfig
}