chore(modules-sdk): Move Query and cleanup (#9054)

* chore(modules-sdk): Move Query and cleanup

* cleanup

* fix memoization
This commit is contained in:
Adrien de Peretti
2024-09-09 12:42:17 +02:00
committed by GitHub
parent 4031da35b0
commit 423bae1d73
4 changed files with 108 additions and 70 deletions

View File

@@ -14,11 +14,7 @@ import {
ModuleExports,
ModuleJoinerConfig,
ModuleServiceInitializeOptions,
RemoteJoinerOptions,
RemoteJoinerQuery,
RemoteQueryFunction,
RemoteQueryObjectConfig,
RemoteQueryObjectFromStringResult,
} from "@medusajs/types"
import {
ContainerRegistrationKeys,
@@ -30,7 +26,6 @@ import {
Modules,
ModulesSdkUtils,
promiseAll,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import type { Knex } from "@mikro-orm/knex"
import { asValue } from "awilix"
@@ -42,7 +37,7 @@ import {
RegisterModuleJoinerConfig,
} from "./medusa-module"
import { RemoteLink } from "./remote-link"
import { RemoteQuery } from "./remote-query"
import { createQuery, RemoteQuery } from "./remote-query"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
import { cleanGraphQLSchema } from "./utils"
@@ -421,68 +416,6 @@ async function MedusaApp_({
customRemoteFetchData: remoteFetchData,
})
const query: RemoteQueryFunction = async (
queryOptions:
| RemoteQueryObjectConfig<any>
| RemoteQueryObjectFromStringResult<any>
| RemoteJoinerQuery,
options?: RemoteJoinerOptions
) => {
if (!isObject(queryOptions)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Invalid query, expected object and received something else."
)
}
let normalizedQuery: any = queryOptions
if ("__value" in queryOptions) {
normalizedQuery = queryOptions.__value
} else if (
"entryPoint" in normalizedQuery ||
"service" in normalizedQuery
) {
normalizedQuery = remoteQueryObjectFromString(
normalizedQuery as Parameters<typeof remoteQueryObjectFromString>[0]
).__value
}
return await remoteQuery.query(normalizedQuery, undefined, options)
}
/**
* Query wrapper to provide specific GraphQL like API around remoteQuery.query
* @param query
* @param variables
* @param options
*/
query.gql = async function (query, variables?, options?) {
return await remoteQuery.query(query, variables, options)
}
/**
* Graph function uses the remoteQuery under the hood and
* returns a result set
*/
query.graph = async function (queryOptions, options) {
const normalizedQuery = remoteQueryObjectFromString(queryOptions).__value
const response = await remoteQuery.query(
normalizedQuery,
undefined,
options
)
if (Array.isArray(response)) {
return { data: response, metadata: undefined }
}
return {
data: response.rows,
metadata: response.metadata,
}
}
const applyMigration = async ({
modulesNames,
action = "run",
@@ -584,7 +517,7 @@ async function MedusaApp_({
onApplicationStart,
modules: allModules,
link: remoteLink,
query,
query: createQuery(remoteQuery),
entitiesMap: schema.getTypeMap(),
gqlSchema: schema,
notFound,

View File

@@ -0,0 +1,2 @@
export * from "./query"
export * from "./remote-query"

View File

@@ -0,0 +1,103 @@
import { RemoteQuery } from "./remote-query"
import {
GraphResultSet,
RemoteJoinerOptions,
RemoteJoinerQuery,
RemoteQueryFunction,
RemoteQueryFunctionReturnPagination,
RemoteQueryObjectConfig,
RemoteQueryObjectFromStringResult,
} from "@medusajs/types"
import {
isObject,
MedusaError,
remoteQueryObjectFromString,
} from "@medusajs/utils"
function unwrapQueryConfig(
config:
| RemoteQueryObjectConfig<any>
| RemoteQueryObjectFromStringResult<any>
| RemoteJoinerQuery
): object {
let normalizedQuery: any = config
if ("__value" in config) {
normalizedQuery = config.__value
} else if ("entryPoint" in normalizedQuery || "service" in normalizedQuery) {
normalizedQuery = remoteQueryObjectFromString(
normalizedQuery as Parameters<typeof remoteQueryObjectFromString>[0]
).__value
}
return normalizedQuery
}
function unwrapRemoteQueryResponse(
response:
| any[]
| { rows: any[]; metadata: RemoteQueryFunctionReturnPagination }
): GraphResultSet<any> {
if (Array.isArray(response)) {
return { data: response, metadata: undefined }
}
return {
data: response.rows,
metadata: response.metadata,
}
}
/**
* Wrap the remote query into a dedicated and more user friendly API than the low level API
* @param remoteQuery
*/
export function createQuery(remoteQuery: RemoteQuery): RemoteQueryFunction {
const query: RemoteQueryFunction = async (
queryOptions:
| RemoteQueryObjectConfig<any>
| RemoteQueryObjectFromStringResult<any>
| RemoteJoinerQuery,
options?: RemoteJoinerOptions
) => {
if (!isObject(queryOptions)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Invalid query, expected object and received something else."
)
}
const config = unwrapQueryConfig(queryOptions)
return await remoteQuery.query(config, undefined, options)
}
/**
* Query wrapper to provide specific GraphQL like API around remoteQuery.query
* @param query
* @param variables
* @param options
*/
query.gql = async function (query, variables?, options?) {
return await remoteQuery.query(query, variables, options)
}
/**
* Graph function uses the remoteQuery under the hood and
* returns a result set
*/
query.graph = async function <const TEntry extends string>(
queryOptions: RemoteQueryObjectConfig<TEntry>,
options?: RemoteJoinerOptions
): Promise<GraphResultSet<TEntry>> {
const normalizedQuery = remoteQueryObjectFromString(queryOptions).__value
const response = await remoteQuery.query(
normalizedQuery,
undefined,
options
)
return unwrapRemoteQueryResponse(response)
}
return query
}

View File

@@ -15,7 +15,7 @@ import {
RemoteNestedExpands,
} from "@medusajs/types"
import { isString, toPascalCase } from "@medusajs/utils"
import { MedusaModule } from "./medusa-module"
import { MedusaModule } from "../medusa-module"
export class RemoteQuery {
private remoteJoiner: RemoteJoiner