fua*_*z98 3 nexus typescript reactjs graphql next.js
我正在尝试在 Next.js API 路由中运行 graphql。
我正在使用 Nexus 来编写 graphql 模式。这是两个文件context.ts和schema.ts用于设置nexus开发模式的文件。
// context.ts
import { database } from "../loaders/database";
import { PrismaClient } from "@prisma/client";
export interface Context {
database: PrismaClient;
}
export const context: Context = {
database,
};
Run Code Online (Sandbox Code Playgroud)
// schema.ts
import { makeSchema } from "nexus";
import { nexusPrisma } from "nexus-plugin-prisma";
import { join } from "path";
import * as types from "./types";
export const schema = makeSchema({
types,
plugins: [
nexusPrisma({
prismaClient: (ctx) => ctx.database,
experimentalCRUD: true,
}),
],
outputs: {
schema: join(__dirname, "generated", "schema.graphql"),
typegen: join(__dirname, "generated", "nexus-typegen.ts"),
},
contextType: {
module: join(__dirname, "context.ts"),
export: "Context",
}
});
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了一下,我找到的衣柜就是他们用来解决问题的地方。sourceTypes我尝试过,但错误并没有消失。
我使用以下脚本为 graphql 生成schema和types。
{
"scripts": {
"generate:nexus": "ts-node --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts",
}
}
Run Code Online (Sandbox Code Playgroud)
尽管代码编译成功,但仍出现以下错误。
event - build page: /api/graphql
event - compiled successfully
Error: Root typing path "/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/.next/server/pages/api/context.ts" for the type "context" does not exist
at Object.resolveImportPath (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/utils.js:411:15)
at TypegenPrinter.printDynamicImport (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:132:40)
at TypegenPrinter.printHeaders (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:80:18)
at TypegenPrinter.print (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenPrinter.js:69:22)
at TypegenMetadata.<anonymous> (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/nexus/dist/typegenMetadata.js:109:128)
at Generator.next (<anonymous>)
at fulfilled (/mnt/B49635D3963596B8/Web/Web/react/next/nextjs-starter/node_modules/tslib/tslib.js:114:62)
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决我做错的地方?
谢谢!
经过一番调试,我终于找到了解决方案。
首先,nexus 文档有一个供next.js用户阅读的部分,这是所有人都必须阅读的部分。关联。
我必须将 替换__dirname为process.cwd()。问题终于解决了。
// schema.ts
import { makeSchema } from "nexus";
import { nexusPrisma } from "nexus-plugin-prisma";
import { join } from "path";
import * as types from "./types";
export const schema = makeSchema({
types,
plugins: [
nexusPrisma({
prismaClient: (ctx) => ctx.database,
experimentalCRUD: true,
}),
],
outputs: {
schema: join(process.cwd(), "src/server/graphql/generated/schema.graphql"),
typegen: join(process.cwd(), "src/server/graphql/generated/nexus-typegen.ts"),
},
contextType: {
module: join(process.cwd(), "src/server/graphql/context.ts"),
export: "Context",
},
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1657 次 |
| 最近记录: |