feat: Application types generation from project GQL schema's (#8995)

This commit is contained in:
Adrien de Peretti
2024-09-06 11:45:32 +02:00
committed by GitHub
parent ac30a989f4
commit 2c5e72d141
92 changed files with 5129 additions and 443 deletions

View File

@@ -0,0 +1,46 @@
import { deepMerge } from "../deep-merge"
describe("Deep merge ", function () {
it("should merge 2 objects", () => {
const a = {
x: 1,
y: {
z: 3,
w: 4,
a: {
b: 1,
},
},
}
const aCopy = JSON.parse(JSON.stringify(a))
const b = {
y: {
w: 5,
q: 6,
a: {
c: 14,
},
},
z: 2,
}
const bCopy = JSON.parse(JSON.stringify(b))
const merged = deepMerge(a, b)
expect(merged).toEqual({
x: 1,
y: {
z: 3,
w: 5,
a: {
b: 1,
c: 14,
},
q: 6,
},
z: 2,
})
expect(a).toEqual(aCopy)
expect(b).toEqual(bCopy)
})
})

View File

@@ -45,7 +45,7 @@ describe("remoteQueryObjectFromString", function () {
fields,
})
expect(output).toEqual({
expect(output.__value).toEqual({
product: {
__args: {
q: "name",
@@ -101,9 +101,9 @@ describe("remoteQueryObjectFromString", function () {
service: "product",
variables: {},
fields,
})
} as any)
expect(output).toEqual({
expect(output.__value).toEqual({
product: {
__args: {},
fields: [

View File

@@ -0,0 +1,26 @@
import { isObject } from "./is-object"
export function deepMerge(target: any, source: any) {
const recursive = (a, b) => {
if (!isObject(a)) {
return b
}
if (!isObject(b)) {
return a
}
Object.keys(b).forEach((key) => {
if (isObject(b[key])) {
a[key] ??= {}
a[key] = deepMerge(a[key], b[key])
} else {
a[key] = b[key]
}
})
return a
}
const copy = { ...target }
return recursive(copy, source)
}

View File

@@ -1,17 +0,0 @@
import { MapToConfig } from "./map-object-to"
export function generateLinkableKeysMap(
linkableKeys: Record<string, string>
): MapToConfig {
const entityLinkableKeysMap: MapToConfig = {}
Object.entries(linkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
entityLinkableKeysMap[value].push({
mapTo: key,
valueFrom: key.split("_").pop()!,
})
})
return entityLinkableKeysMap
}

View File

@@ -12,10 +12,12 @@ export * from "./deduplicate"
export * from "./deep-copy"
export * from "./deep-equal-obj"
export * from "./deep-flat-map"
export * from "./deep-merge"
export * from "./define-config"
export * from "./env-editor"
export * from "./errors"
export * from "./file-system"
export * from "./generate-entity-id"
export * from "./generate-linkable-keys-map"
export * from "./get-caller-file-path"
export * from "./get-config-file"
export * from "./get-duplicates"
@@ -26,7 +28,6 @@ export * from "./get-set-difference"
export * from "./graceful-shutdown-server"
export * from "./group-by"
export * from "./handle-postgres-database-error"
export * from "./is-truthy"
export * from "./is-big-number"
export * from "./is-boolean"
export * from "./is-date"
@@ -35,10 +36,12 @@ export * from "./is-email"
export * from "./is-object"
export * from "./is-present"
export * from "./is-string"
export * from "./is-truthy"
export * from "./load-env"
export * from "./lower-case-first"
export * from "./map-object-to"
export * from "./medusa-container"
export * from "./normalize-import-path-with-source"
export * from "./object-from-string-path"
export * from "./object-to-string-path"
export * from "./optional-numeric-serializer"
@@ -68,6 +71,3 @@ export * from "./trim-zeros"
export * from "./upper-case-first"
export * from "./validate-handle"
export * from "./wrap-handler"
export * from "./define-config"
export * from "./env-editor"
export * from "./normalize-import-path-with-source"

View File

@@ -1,3 +1,7 @@
import {
RemoteQueryObjectConfig,
RemoteQueryObjectFromStringResult,
} from "@medusajs/types"
import { isObject } from "./is-object"
/**
@@ -83,20 +87,18 @@ import { isObject } from "./is-object"
* // },
* // }
*/
export function remoteQueryObjectFromString(
config:
| {
entryPoint: string
variables?: any
fields: string[]
}
| {
service: string
variables?: any
fields: string[]
}
): object {
const { entryPoint, service, variables, fields } = {
export function remoteQueryObjectFromString<
const TEntry extends string,
const TConfig extends RemoteQueryObjectConfig<TEntry>
>(
config: TConfig | RemoteQueryObjectConfig<TEntry>
): RemoteQueryObjectFromStringResult<TConfig> {
const {
entryPoint,
service,
variables = {},
fields = [],
} = {
...config,
entryPoint: "entryPoint" in config ? config.entryPoint : undefined,
service: "service" in config ? config.service : undefined,
@@ -114,12 +116,13 @@ export function remoteQueryObjectFromString(
const usedVariables = new Set()
for (const field of fields) {
if (!field.includes(".")) {
const fieldAsString = field as string
if (!fieldAsString.includes(".")) {
remoteJoinerConfig[entryKey]["fields"].push(field)
continue
}
const fieldSegments = field.split(".")
const fieldSegments = fieldAsString.split(".")
const fieldProperty = fieldSegments.pop()
let combinedPath = ""
@@ -151,5 +154,7 @@ export function remoteQueryObjectFromString(
remoteJoinerConfig[entryKey]["__args"] = topLevelArgs ?? {}
return remoteJoinerConfig
return {
__value: remoteJoinerConfig,
} as RemoteQueryObjectFromStringResult<TConfig>
}