chore: Register Query in container (#9103)
* chore(framework): Register the query * chore(framework): Register the query
This commit is contained in:
committed by
GitHub
parent
a729fb3fbb
commit
a01e7e4ffe
@@ -14,52 +14,60 @@ import {
|
||||
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
|
||||
* API wrapper around the remoteQuery
|
||||
*/
|
||||
export function createQuery(remoteQuery: RemoteQuery): RemoteQueryFunction {
|
||||
const query: RemoteQueryFunction = async (
|
||||
export class Query {
|
||||
#remoteQuery: RemoteQuery
|
||||
|
||||
constructor(remoteQuery: RemoteQuery) {
|
||||
this.#remoteQuery = remoteQuery
|
||||
}
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
#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,
|
||||
}
|
||||
}
|
||||
|
||||
async query(
|
||||
queryOptions:
|
||||
| RemoteQueryObjectConfig<any>
|
||||
| RemoteQueryObjectFromStringResult<any>
|
||||
| RemoteJoinerQuery,
|
||||
options?: RemoteJoinerOptions
|
||||
) => {
|
||||
) {
|
||||
if (!isObject(queryOptions)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
@@ -67,8 +75,8 @@ export function createQuery(remoteQuery: RemoteQuery): RemoteQueryFunction {
|
||||
)
|
||||
}
|
||||
|
||||
const config = unwrapQueryConfig(queryOptions)
|
||||
return await remoteQuery.query(config, undefined, options)
|
||||
const config = this.#unwrapQueryConfig(queryOptions)
|
||||
return await this.#remoteQuery.query(config, undefined, options)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,27 +85,42 @@ export function createQuery(remoteQuery: RemoteQuery): RemoteQueryFunction {
|
||||
* @param variables
|
||||
* @param options
|
||||
*/
|
||||
query.gql = async function (query, variables?, options?) {
|
||||
return await remoteQuery.query(query, variables, options)
|
||||
async gql(query, variables?, options?) {
|
||||
return await this.#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>(
|
||||
async graph<const TEntry extends string>(
|
||||
queryOptions: RemoteQueryObjectConfig<TEntry>,
|
||||
options?: RemoteJoinerOptions
|
||||
): Promise<GraphResultSet<TEntry>> {
|
||||
const normalizedQuery = remoteQueryObjectFromString(queryOptions).__value
|
||||
const response = await remoteQuery.query(
|
||||
const response = await this.#remoteQuery.query(
|
||||
normalizedQuery,
|
||||
undefined,
|
||||
options
|
||||
)
|
||||
|
||||
return unwrapRemoteQueryResponse(response)
|
||||
return this.#unwrapRemoteQueryResponse(response)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API wrapper around the remoteQuery with backward compatibility support
|
||||
* @param remoteQuery
|
||||
*/
|
||||
export function createQuery(remoteQuery: RemoteQuery): RemoteQueryFunction {
|
||||
const query = new Query(remoteQuery)
|
||||
|
||||
function backwardCompatibleQuery(...args: any[]) {
|
||||
return query.query.apply(query, args)
|
||||
}
|
||||
|
||||
return query
|
||||
backwardCompatibleQuery.graph = query.graph
|
||||
backwardCompatibleQuery.gql = query.gql
|
||||
|
||||
return backwardCompatibleQuery
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user