Nexus 和 GraphQL:“上下文”类型的根输入路径不存在
Posted
技术标签:
【中文标题】Nexus 和 GraphQL:“上下文”类型的根输入路径不存在【英文标题】:Nexus & GraphQL: Root typing path for the type "context" does not exist 【发布时间】:2021-10-09 23:08:37 【问题描述】:我正在尝试在 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,
;
// 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)
谁能帮我看看我哪里做错了?
谢谢!
【问题讨论】:
【参考方案1】:经过一番调试,终于找到了解决办法。
首先,nexus 文档有一部分供next.js
用户使用,这是所有人必读的。 link.
我必须将__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",
,
);
【讨论】:
以上是关于Nexus 和 GraphQL:“上下文”类型的根输入路径不存在的主要内容,如果未能解决你的问题,请参考以下文章
GraphQL:每种类型的子字段与可以使用 args 过滤的根查询字段?
使用 GraphQL、Nexus 和 Prisma 优化 SQL 查询