Nexus & GraphQL:类型 "context" 的根键入路径不存在
Nexus & GraphQL: Root typing path for the type "context" does not exist
我正尝试在 Next.js API 路由中 运行 graphql。
我正在使用 nexus 编写 graphql 模式。这是设置nexus开发模式的context.ts
和schema.ts
两个文件
// context.ts
import { database } from "../loaders/database";
import { PrismaClient } from "@prisma/client";
export interface Context {
database: PrismaClient;
}
export const context: Context = {
database,
};
// 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",
}
});
我在网上搜索了一下,我找到的壁橱是here,他们用sourceTypes
解决了这个问题。我试过了,但错误并没有消失。
我使用以下脚本为 graphql 生成 schema
和 types
。
{
"scripts": {
"generate:nexus": "ts-node --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts",
}
}
虽然代码编译成功,但出现以下错误。
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)
任何人都可以帮助我哪里做错了吗?
谢谢!
经过一番调试,终于找到了解决办法
首先,nexus 文档有一个面向 next.js
用户的部分,这是所有人必读的部分。 link.
我必须用 process.cwd()
替换 __dirname
。问题终于解决了
// 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",
},
});
我正尝试在 Next.js API 路由中 运行 graphql。
我正在使用 nexus 编写 graphql 模式。这是设置nexus开发模式的context.ts
和schema.ts
两个文件
// context.ts
import { database } from "../loaders/database";
import { PrismaClient } from "@prisma/client";
export interface Context {
database: PrismaClient;
}
export const context: Context = {
database,
};
// 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",
}
});
我在网上搜索了一下,我找到的壁橱是here,他们用sourceTypes
解决了这个问题。我试过了,但错误并没有消失。
我使用以下脚本为 graphql 生成 schema
和 types
。
{
"scripts": {
"generate:nexus": "ts-node --transpile-only -P nexus.tsconfig.json src/server/graphql/schema.ts",
}
}
虽然代码编译成功,但出现以下错误。
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)
任何人都可以帮助我哪里做错了吗?
谢谢!
经过一番调试,终于找到了解决办法
首先,nexus 文档有一个面向 next.js
用户的部分,这是所有人必读的部分。 link.
我必须用 process.cwd()
替换 __dirname
。问题终于解决了
// 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",
},
});