chore(modules-sdk): Move Query and cleanup (#9054)
* chore(modules-sdk): Move Query and cleanup * cleanup * fix memoization
This commit is contained in:
1 parent
4031da35b0
commit
423bae1d73
4 files changed
+108
-70
No files matched your search
@@ -14,11 +14,7 @@ import {
|
|||||||
ModuleExports,
|
ModuleExports,
|
||||||
ModuleJoinerConfig,
|
ModuleJoinerConfig,
|
||||||
ModuleServiceInitializeOptions,
|
ModuleServiceInitializeOptions,
|
||||||
RemoteJoinerOptions,
|
|
||||||
RemoteJoinerQuery,
|
|
||||||
RemoteQueryFunction,
|
RemoteQueryFunction,
|
||||||
RemoteQueryObjectConfig,
|
|
||||||
RemoteQueryObjectFromStringResult,
|
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
@@ -30,7 +26,6 @@ import {
|
|||||||
Modules,
|
Modules,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
promiseAll,
|
promiseAll,
|
||||||
remoteQueryObjectFromString,
|
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import type { Knex } from "@mikro-orm/knex"
|
import type { Knex } from "@mikro-orm/knex"
|
||||||
import { asValue } from "awilix"
|
import { asValue } from "awilix"
|
||||||
@@ -42,7 +37,7 @@ import {
|
|||||||
RegisterModuleJoinerConfig,
|
RegisterModuleJoinerConfig,
|
||||||
} from "./medusa-module"
|
} from "./medusa-module"
|
||||||
import { RemoteLink } from "./remote-link"
|
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 { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"
|
||||||
import { cleanGraphQLSchema } from "./utils"
|
import { cleanGraphQLSchema } from "./utils"
|
||||||
|
|
||||||
@@ -421,68 +416,6 @@ async function MedusaApp_({
|
|||||||
customRemoteFetchData: remoteFetchData,
|
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 ({
|
const applyMigration = async ({
|
||||||
modulesNames,
|
modulesNames,
|
||||||
action = "run",
|
action = "run",
|
||||||
@@ -584,7 +517,7 @@ async function MedusaApp_({
|
|||||||
onApplicationStart,
|
onApplicationStart,
|
||||||
modules: allModules,
|
modules: allModules,
|
||||||
link: remoteLink,
|
link: remoteLink,
|
||||||
query,
|
query: createQuery(remoteQuery),
|
||||||
entitiesMap: schema.getTypeMap(),
|
entitiesMap: schema.getTypeMap(),
|
||||||
gqlSchema: schema,
|
gqlSchema: schema,
|
||||||
notFound,
|
notFound,
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from "./query"
|
||||||
|
export * from "./remote-query"
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+1
-1
@@ -15,7 +15,7 @@ import {
|
|||||||
RemoteNestedExpands,
|
RemoteNestedExpands,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { isString, toPascalCase } from "@medusajs/utils"
|
import { isString, toPascalCase } from "@medusajs/utils"
|
||||||
import { MedusaModule } from "./medusa-module"
|
import { MedusaModule } from "../medusa-module"
|
||||||
|
|
||||||
export class RemoteQuery {
|
export class RemoteQuery {
|
||||||
private remoteJoiner: RemoteJoiner
|
private remoteJoiner: RemoteJoiner
|
||||||
Reference in new issue
Block a user