feat(utils,modules-sdk): Remote query object to string array (#5216)

**What**
- transform remote query object back to string array
- get all fields and fields from given relations from a GraphQL schema

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2023-10-03 19:54:41 +02:00
committed by GitHub
parent cb569c2dfe
commit eeceec791c
13 changed files with 664 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
import { stringToRemoteQueryObject } from "../string-to-remote-query-object"
import { remoteQueryObjectFromString } from "../remote-query-object-from-string"
const fields = [
"id",
@@ -29,9 +29,9 @@ const fields = [
"options.values.metadata",
]
describe("stringToRemoteQueryObject", function () {
describe("remoteQueryObjectFromString", function () {
it("should return a remote query object", function () {
const output = stringToRemoteQueryObject({
const output = remoteQueryObjectFromString({
entryPoint: "product",
variables: {},
fields,

View File

@@ -0,0 +1,219 @@
import { remoteQueryObjectToString } from "../remote-query-object-to-string"
const remoteQueryObject = {
fields: [
"id",
"title",
"subtitle",
"status",
"external_id",
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"collection_id",
"type_id",
"weight",
"length",
"height",
"width",
"hs_code",
"origin_country",
"mid_code",
"material",
"created_at",
"updated_at",
"deleted_at",
"metadata",
],
images: {
fields: ["id", "created_at", "updated_at", "deleted_at", "url", "metadata"],
},
tags: {
fields: ["id", "created_at", "updated_at", "deleted_at", "value"],
},
type: {
fields: ["id", "created_at", "updated_at", "deleted_at", "value"],
},
collection: {
fields: ["title", "handle", "id", "created_at", "updated_at", "deleted_at"],
},
options: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"product_id",
"metadata",
],
values: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"value",
"option_id",
"variant_id",
"metadata",
],
},
},
variants: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"product_id",
"sku",
"barcode",
"ean",
"upc",
"variant_rank",
"inventory_quantity",
"allow_backorder",
"manage_inventory",
"hs_code",
"origin_country",
"mid_code",
"material",
"weight",
"length",
"height",
"width",
"metadata",
],
options: {
fields: [
"id",
"created_at",
"updated_at",
"deleted_at",
"value",
"option_id",
"variant_id",
"metadata",
],
},
},
profile: {
fields: ["id", "created_at", "updated_at", "deleted_at", "name", "type"],
},
}
describe("remoteQueryObjectToString", function () {
it("should return a string array of fields/relations", function () {
const output = remoteQueryObjectToString(remoteQueryObject)
expect(output).toEqual([
"id",
"title",
"subtitle",
"status",
"external_id",
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"collection_id",
"type_id",
"weight",
"length",
"height",
"width",
"hs_code",
"origin_country",
"mid_code",
"material",
"created_at",
"updated_at",
"deleted_at",
"metadata",
"images.id",
"images.created_at",
"images.updated_at",
"images.deleted_at",
"images.url",
"images.metadata",
"tags.id",
"tags.created_at",
"tags.updated_at",
"tags.deleted_at",
"tags.value",
"type.id",
"type.created_at",
"type.updated_at",
"type.deleted_at",
"type.value",
"collection.title",
"collection.handle",
"collection.id",
"collection.created_at",
"collection.updated_at",
"collection.deleted_at",
"options.id",
"options.created_at",
"options.updated_at",
"options.deleted_at",
"options.title",
"options.product_id",
"options.metadata",
"options.values.id",
"options.values.created_at",
"options.values.updated_at",
"options.values.deleted_at",
"options.values.value",
"options.values.option_id",
"options.values.variant_id",
"options.values.metadata",
"variants.id",
"variants.created_at",
"variants.updated_at",
"variants.deleted_at",
"variants.title",
"variants.product_id",
"variants.sku",
"variants.barcode",
"variants.ean",
"variants.upc",
"variants.variant_rank",
"variants.inventory_quantity",
"variants.allow_backorder",
"variants.manage_inventory",
"variants.hs_code",
"variants.origin_country",
"variants.mid_code",
"variants.material",
"variants.weight",
"variants.length",
"variants.height",
"variants.width",
"variants.metadata",
"variants.options.id",
"variants.options.created_at",
"variants.options.updated_at",
"variants.options.deleted_at",
"variants.options.value",
"variants.options.option_id",
"variants.options.variant_id",
"variants.options.metadata",
"profile.id",
"profile.created_at",
"profile.updated_at",
"profile.deleted_at",
"profile.name",
"profile.type",
])
})
})

View File

@@ -16,8 +16,11 @@ export * from "./map-object-to"
export * from "./medusa-container"
export * from "./object-from-string-path"
export * from "./object-to-string-path"
export * from "./remote-query-object-from-string"
export * from "./remote-query-object-to-string"
export * from "./set-metadata"
export * from "./simple-hash"
export * from "./string-to-select-relation-object"
export * from "./stringify-circular"
export * from "./to-camel-case"
export * from "./to-kebab-case"
@@ -25,5 +28,3 @@ export * from "./to-pascal-case"
export * from "./transaction"
export * from "./upper-case-first"
export * from "./wrap-handler"
export * from "./string-to-remote-query-object"
export * from "./string-to-select-relation-object"

View File

@@ -34,7 +34,7 @@
* "options.values.metadata",
* ]
*
* const remoteQueryObject = stringToRemoteQueryObject({
* const remoteQueryObject = remoteQueryObjectFromString({
* entryPoint: "product",
* variables: {},
* fields,
@@ -83,7 +83,7 @@
* // },
* // }
*/
export function stringToRemoteQueryObject({
export function remoteQueryObjectFromString({
entryPoint,
variables,
fields,

View File

@@ -0,0 +1,51 @@
/**
* Transform a remote query object to a string array containing the chain of fields and relations
*
* @param fields
* @param parent
*
* @example
*
* const remoteQueryObject = {
* fields: [
* "id",
* "title",
* ],
* images: {
* fields: ["id", "created_at", "updated_at", "deleted_at", "url", "metadata"],
* },
* }
*
* const fields = remoteQueryObjectToString(remoteQueryObject)
*
* console.log(fields)
* // ["id", "title", "images.id", "images.created_at", "images.updated_at", "images.deleted_at", "images.url", "images.metadata"]
*/
export function remoteQueryObjectToString(
fields: object,
parent?: string
): string[] {
return Object.keys(fields).reduce((acc, key) => {
if (key === "fields") {
if (parent) {
fields[key].map((fieldKey) => acc.push(`${parent}.${fieldKey}`))
} else {
fields[key].map((fieldKey) => acc.push(fieldKey))
}
return acc
}
if (typeof fields[key] === "object") {
acc = acc.concat(
remoteQueryObjectToString(
fields[key],
parent ? `${parent}.${key}` : key
)
)
return acc
}
return acc
}, [] as string[])
}