微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 NextJS 时在 NexusJS 中定义 contextType 的正确方法

如何解决使用 NextJS 时在 NexusJS 中定义 contextType 的正确方法

我试图让 prisma 和 Nexus 与 NextJS 一起工作,但我在定义 GraphQL 架构中的 contextType 时遇到了一些问题。

我这样定义架构:

export const schema = makeSchema({
  types: [Query,User,Mutation],contextType: {
    module: require.resolve("./graphql/context"),export: "Context",},plugins: [nexusprisma({ experimentalCRUD: true })],outputs: {
    typegen: path.join(process.cwd(),"generated","nexus-typegen.ts"),schema: path.join(process.cwd(),"schema.graphql"),});

当我通过运行 npm run dev 启动开发服务器时发生错误错误如下:

Module not found: Can't resolve './graphql/context'
  46 |   types: [Query,47 |   contextType: {
> 48 |     module: require.resolve("./graphql/context"),|            ^
  49 |     export: "Context",50 |   },51 |   plugins: [nexusprisma({ experimentalCRUD: true })],

这是 context.ts 文件

import { prismaClient } from "@prisma/client";

const prisma = new prismaClient();

export interface Context {
  prisma: prismaClient;
}

export function createContext(): Context {
  return { prisma };
}

我的依赖是这些:

  "dependencies": {
    "@prisma/client": "^2.13.1","apollo-server-micro": "^2.19.1","next": "latest","nexus": "^1.0.0","nexus-plugin-prisma": "^0.27.0","react": "^16.12.0","react-dom": "^16.12.0"
  },"devDependencies": {
    "@prisma/cli": "^2.13.1","@types/node": "^12.12.21","@types/react": "^16.9.16","@types/react-dom": "^16.9.4","typescript": "^4.1.3"
  },

我一直在寻找解决方案,但现在我被困住了。我认为这可能与 NextJS 及其 Webpack 配置有关,但我完全不了解这些,所以我真的不知道该尝试什么。

感谢任何帮助。

项目树:

root
├─ generated
│  ├─ nexus-typegen.ts
│  └─ schema.graphql
├─ graphql
│  ├─ context.ts
│  └─ schema.ts
├─ interfaces
│  └─ index.ts
├─ next-env.d.ts
├─ package-lock.json
├─ package.json
├─ pages
│  ├─ api
│  │  ├─ graphql.ts
├─ prisma
│  └─ schema.prisma
├─ tsconfig.json

解决方法

我认为错误出在您为模块提供的路径中。假设您已在 ./schema.ts 中定义架构,您的上下文类型应如下所示,因为它是您需要指定的相对路径

contextType: {
  module: require.resolve("./context"),export: "Context",}
,

这种方式对我有用:


Eigen::Vector2<float> outpointa = min_element(points.begin(),points.end(),isLeftOf<float>);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。