Nexus resolve() 返回 PrismaPromise 但给出错误
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GraphQL 中的 ID 类型作为字符串处理。
可能您使用数字作为待办事项的 ID,因此类型不匹配,从而导致错误。
来自 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.
from https://github.com/graphql-nexus/nexus/blob/11d028277e7fec241246237dbe31707c037407ee/src/typegenPrinter.ts#L44