diff --git a/packages/core/modules-sdk/src/medusa-app.ts b/packages/core/modules-sdk/src/medusa-app.ts index b0dc7716cd..9da67b7783 100644 --- a/packages/core/modules-sdk/src/medusa-app.ts +++ b/packages/core/modules-sdk/src/medusa-app.ts @@ -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 - | RemoteQueryObjectFromStringResult - | 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[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, diff --git a/packages/core/modules-sdk/src/remote-query/index.ts b/packages/core/modules-sdk/src/remote-query/index.ts new file mode 100644 index 0000000000..ba40fcf9b0 --- /dev/null +++ b/packages/core/modules-sdk/src/remote-query/index.ts @@ -0,0 +1,2 @@ +export * from "./query" +export * from "./remote-query" diff --git a/packages/core/modules-sdk/src/remote-query/query.ts b/packages/core/modules-sdk/src/remote-query/query.ts new file mode 100644 index 0000000000..4188c677f4 --- /dev/null +++ b/packages/core/modules-sdk/src/remote-query/query.ts @@ -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 + | RemoteQueryObjectFromStringResult + | 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[0] + ).__value + } + + return normalizedQuery +} + +function unwrapRemoteQueryResponse( + response: + | any[] + | { rows: any[]; metadata: RemoteQueryFunctionReturnPagination } +): GraphResultSet { + 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 + | RemoteQueryObjectFromStringResult + | 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 ( + queryOptions: RemoteQueryObjectConfig, + options?: RemoteJoinerOptions + ): Promise> { + const normalizedQuery = remoteQueryObjectFromString(queryOptions).__value + const response = await remoteQuery.query( + normalizedQuery, + undefined, + options + ) + + return unwrapRemoteQueryResponse(response) + } + + return query +} diff --git a/packages/core/modules-sdk/src/remote-query.ts b/packages/core/modules-sdk/src/remote-query/remote-query.ts similarity index 99% rename from packages/core/modules-sdk/src/remote-query.ts rename to packages/core/modules-sdk/src/remote-query/remote-query.ts index 8a277e5c34..3f561f6999 100644 --- a/packages/core/modules-sdk/src/remote-query.ts +++ b/packages/core/modules-sdk/src/remote-query/remote-query.ts @@ -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