Nexus resolve() 返回 PrismaPromise 但给出错误

发布于 2025-01-16 14:00:22 字数 2463 浏览 1 评论 0原文

我正在尝试使用 Nexus.js、Prisma 和 Typescript 为待办事项应用程序构建一个 apollo 服务器。

我创建了 Todo nexus 类型,并且想创建查询,但遇到错误。
定义:

import { extendType, objectType } from "nexus";

export const Todo = objectType({
  name: "Todo",
  definition(t) {
    t.id("id");
    t.nonNull.string("title");
    t.string("detail");
  },
});

export const TodoQuery = extendType({
  type: "Query",
  definition(t) {
    t.list.field("todos", {
      type: "Todo",
      resolve(_root, _args, ctx) {
        return ctx.db.todo.findMany(); // db is an instance of PrismaClient()
      },
    });
  },
});

错误:

Type '(_root: {}, _args: {}, ctx: Context) => PrismaPromise<Todo[]>' is not assignable to type 'FieldResolver<"Query", "todos">'.
  Type 'PrismaPromise<Todo[]>' is not assignable to type 'MaybePromise<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
    Type 'PrismaPromise<Todo[]>' is not assignable to type 'PromiseLike<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
      Types of property 'then' are incompatible.
        Type '<TResult1 = Todo[], TResult2 = never>(onfulfilled?: ((value: Todo[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null, TResult2 = never>(onfulfilled?: ((value: ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null) => TResult1 | PromiseLike<...>) | null | undefined, ...'.
          Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
            Types of parameters 'value' and 'value' are incompatible.
              Type 'Todo[]' is not assignable to type '({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[]'.
                Type 'Todo' is not assignable to type '{ detail?: string | null | undefined; id?: string | null | undefined; title: string; }'.
                  Types of property 'id' are incompatible.
                    Type 'number' is not assignable to type 'string'.

I am trying to build an apollo server with Nexus.js, Prisma and Typescript for a to-do app.

I created the Todo nexus type, and I wanted to create the query, but I'm having an error.
The definition:

import { extendType, objectType } from "nexus";

export const Todo = objectType({
  name: "Todo",
  definition(t) {
    t.id("id");
    t.nonNull.string("title");
    t.string("detail");
  },
});

export const TodoQuery = extendType({
  type: "Query",
  definition(t) {
    t.list.field("todos", {
      type: "Todo",
      resolve(_root, _args, ctx) {
        return ctx.db.todo.findMany(); // db is an instance of PrismaClient()
      },
    });
  },
});

The error:

Type '(_root: {}, _args: {}, ctx: Context) => PrismaPromise<Todo[]>' is not assignable to type 'FieldResolver<"Query", "todos">'.
  Type 'PrismaPromise<Todo[]>' is not assignable to type 'MaybePromise<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
    Type 'PrismaPromise<Todo[]>' is not assignable to type 'PromiseLike<({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null>'.
      Types of property 'then' are incompatible.
        Type '<TResult1 = Todo[], TResult2 = never>(onfulfilled?: ((value: Todo[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null, TResult2 = never>(onfulfilled?: ((value: ({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[] | null) => TResult1 | PromiseLike<...>) | null | undefined, ...'.
          Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
            Types of parameters 'value' and 'value' are incompatible.
              Type 'Todo[]' is not assignable to type '({ detail?: string | null | undefined; id?: string | null | undefined; title: string; } | null)[]'.
                Type 'Todo' is not assignable to type '{ detail?: string | null | undefined; id?: string | null | undefined; title: string; }'.
                  Types of property 'id' are incompatible.
                    Type 'number' is not assignable to type 'string'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言月老 2025-01-23 14:00:22

GraphQL 中的 ID 类型作为字符串处理。
可能您使用数字作为待办事项的 ID,因此类型不匹配,从而导致错误。

const SpecifiedScalars = {
  ID: 'string',
  String: 'string',
  Float: 'number',
  Int: 'number',
  Boolean: 'boolean',
}

来自 https://github.com/graphql-nexus/nexus/blob/11d028277e7fec241246237dbe31707c037407ee/src/typegenPrinter.ts#L44

ID types in GraphQL are handled as String.
Probably, you are using number as the ID of the todo, so the type does not match, resulting in an error.

const SpecifiedScalars = {
  ID: 'string',
  String: 'string',
  Float: 'number',
  Int: 'number',
  Boolean: 'boolean',
}

from https://github.com/graphql-nexus/nexus/blob/11d028277e7fec241246237dbe31707c037407ee/src/typegenPrinter.ts#L44

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文