feat(index): Provide a similar API to Query (#9193)
**What**
Align the index engine API to be similar to the Query API
## Example
```ts
// Benefit from the same level of typing like the remote query
const { data, metadata } = await indexEngine.query<'product'>({
fields: [
"product.*",
"product.variants.*",
"product.variants.prices.*",
],
filters: {
product: {
variants: {
prices: {
amount: { $gt: 50 },
},
},
},
},
pagination: {
order: {
product: {
variants: {
prices: {
amount: "DESC",
},
},
},
},
},
})
```
This commit is contained in:
committed by
GitHub
parent
1215a7c094
commit
3084008fc9
@@ -1,5 +1,5 @@
|
||||
import { MedusaModule } from "../medusa-module"
|
||||
import { FileSystem } from "@medusajs/utils"
|
||||
import { FileSystem, toCamelCase } from "@medusajs/utils"
|
||||
import { GraphQLSchema } from "graphql/type"
|
||||
import { parse, printSchema } from "graphql"
|
||||
import { codegen } from "@graphql-codegen/core"
|
||||
@@ -21,13 +21,13 @@ function buildEntryPointsTypeMap(
|
||||
|
||||
return aliases.flatMap((alias) => {
|
||||
const names = Array.isArray(alias.name) ? alias.name : [alias.name]
|
||||
const entity = alias.args?.["entity"]
|
||||
const entity = alias?.["entity"]
|
||||
return names.map((aliasItem) => {
|
||||
return {
|
||||
entryPoint: aliasItem,
|
||||
entityType: entity
|
||||
? schema.includes(`export type ${entity} `)
|
||||
? alias.args?.["entity"]
|
||||
? alias?.["entity"]
|
||||
: "any"
|
||||
: "any",
|
||||
}
|
||||
@@ -39,9 +39,11 @@ function buildEntryPointsTypeMap(
|
||||
|
||||
async function generateTypes({
|
||||
outputDir,
|
||||
filename,
|
||||
config,
|
||||
}: {
|
||||
outputDir: string
|
||||
filename: string
|
||||
config: Parameters<typeof codegen>[0]
|
||||
}) {
|
||||
const fileSystem = new FileSystem(outputDir)
|
||||
@@ -49,9 +51,11 @@ async function generateTypes({
|
||||
let output = await codegen(config)
|
||||
const entryPoints = buildEntryPointsTypeMap(output)
|
||||
|
||||
const interfaceName = toCamelCase(filename)
|
||||
|
||||
const remoteQueryEntryPoints = `
|
||||
declare module '@medusajs/types' {
|
||||
interface RemoteQueryEntryPoints {
|
||||
interface ${interfaceName} {
|
||||
${entryPoints
|
||||
.map((entry) => ` ${entry.entryPoint}: ${entry.entityType}`)
|
||||
.join("\n")}
|
||||
@@ -60,19 +64,31 @@ ${entryPoints
|
||||
|
||||
output += remoteQueryEntryPoints
|
||||
|
||||
await fileSystem.create("remote-query-types.d.ts", output)
|
||||
await fileSystem.create(
|
||||
"index.d.ts",
|
||||
"export * as RemoteQueryTypes from './remote-query-types'"
|
||||
)
|
||||
await fileSystem.create(filename + ".d.ts", output)
|
||||
|
||||
const doesBarrelExists = await fileSystem.exists("index.d.ts")
|
||||
if (!doesBarrelExists) {
|
||||
await fileSystem.create(
|
||||
"index.d.ts",
|
||||
`export * as ${interfaceName}Types from './${filename}'`
|
||||
)
|
||||
} else {
|
||||
const content = await fileSystem.contents("index.d.ts")
|
||||
if (!content.includes(`${interfaceName}Types`)) {
|
||||
const newContent = `export * as ${interfaceName}Types from './${filename}'\n${content}`
|
||||
await fileSystem.create("index.d.ts", newContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function gqlSchemaToTypes({
|
||||
schema,
|
||||
outputDir,
|
||||
filename,
|
||||
}: {
|
||||
schema: GraphQLSchema
|
||||
outputDir: string
|
||||
filename: string
|
||||
}) {
|
||||
const config = {
|
||||
documents: [],
|
||||
@@ -98,5 +114,5 @@ export async function gqlSchemaToTypes({
|
||||
},
|
||||
}
|
||||
|
||||
await generateTypes({ outputDir, config })
|
||||
await generateTypes({ outputDir, filename, config })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user