使用 GraphQL Nexus 删除在 Prisma 中不起作用的突变
Delete Mutations not working in Prisma with GraphQL Nexus
我正在尝试编写删除变更,但出现了一些奇怪的行为。如果我转到应用程序的前端并单击删除按钮两次然后刷新页面,记录将从数据库中删除。但是如果我点击它一次,然后刷新,它仍然在数据库中。
这是打印到控制台的错误:
Uncaught (in promise) Error:
Invalid `context.db.user.delete()` invocation in
/home/eric/DEV/Junk/nexus-prisma-app/api/graphql/User.ts:84:44`
An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
所以在前端我必须点击我的删除按钮两次才能工作,但是打印出相同的错误,并且该函数不会重新获取查询。如果我进入 Apollo 沙箱,我只能通过 ID 运行 删除一次记录,但它仍然打印出相同的错误,表明它不起作用,但如果我再次获取用户,用户已被删除,突变生效。
/api/graphql/User.ts:
import { extendType, nonNull, objectType, stringArg } from "nexus";
export const User = objectType({
name: "User",
definition(t) {
t.string("id");
t.string("name");
t.string("username");
t.string("email");
},
});
export const GetUsersQuery = extendType({
type: "Query",
definition(t) {
t.nonNull.list.field("users", {
type: "User",
resolve(_root, _args, context) {
return context.db.user.findMany();
},
});
},
});
export const UserMutations = extendType({
type: "Mutation",
definition(t) {
t.field("createUser", {
type: "User",
args: {
name: nonNull(stringArg()),
username: nonNull(stringArg()),
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
const newUser = {
name: args.name,
username: args.username,
email: args.email,
};
return context.db.user.create({ data: newUser });
},
});
t.nonNull.list.field("findUser", {
type: "User",
args: {
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
console.log(args);
return context.db.user.findMany({
where: {
email: args.email,
},
});
},
});
},
});
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.list.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
async resolve(_root, args, context) {
console.log(args.id);
return await context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});
我在 VS Code 中遇到了这个问题:
Type '(_root: {}, args: { id: string; }, context: Context) => Promise<User>' is not assignable to type 'FieldResolver<"Mutation", "deleteUser">'.
Type 'Promise<User>' is not assignable to type 'MaybePromise<({ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null)[]> | MaybePromise<...>'.
Type 'Promise<User>' is not assignable to type 'PromiseLike<MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]>'.
Types of property 'then' are incompatible.
Type '<TResult1 = User, TResult2 = never>(onfulfilled?: ((value: User) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[], TResult2 = never>(onfulfilled?: ((value: MaybePromise<...>[]) => TResult1 | PromiseLike<...>) | ... 1 more ... | undefined, onrejected?: (...'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'User' is missing the following properties from type 'MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]': length, pop, push, concat, and 29 more.
有谁知道如何解决这个问题?
从 `t.nonNull.list.field("deleteUser"{/***/}) 中删除 list
可以解决问题。
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
resolve(_root, args, context) {
return context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});
我正在尝试编写删除变更,但出现了一些奇怪的行为。如果我转到应用程序的前端并单击删除按钮两次然后刷新页面,记录将从数据库中删除。但是如果我点击它一次,然后刷新,它仍然在数据库中。
这是打印到控制台的错误:
Uncaught (in promise) Error:
Invalid `context.db.user.delete()` invocation in
/home/eric/DEV/Junk/nexus-prisma-app/api/graphql/User.ts:84:44`
An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
所以在前端我必须点击我的删除按钮两次才能工作,但是打印出相同的错误,并且该函数不会重新获取查询。如果我进入 Apollo 沙箱,我只能通过 ID 运行 删除一次记录,但它仍然打印出相同的错误,表明它不起作用,但如果我再次获取用户,用户已被删除,突变生效。
/api/graphql/User.ts:
import { extendType, nonNull, objectType, stringArg } from "nexus";
export const User = objectType({
name: "User",
definition(t) {
t.string("id");
t.string("name");
t.string("username");
t.string("email");
},
});
export const GetUsersQuery = extendType({
type: "Query",
definition(t) {
t.nonNull.list.field("users", {
type: "User",
resolve(_root, _args, context) {
return context.db.user.findMany();
},
});
},
});
export const UserMutations = extendType({
type: "Mutation",
definition(t) {
t.field("createUser", {
type: "User",
args: {
name: nonNull(stringArg()),
username: nonNull(stringArg()),
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
const newUser = {
name: args.name,
username: args.username,
email: args.email,
};
return context.db.user.create({ data: newUser });
},
});
t.nonNull.list.field("findUser", {
type: "User",
args: {
email: nonNull(stringArg()),
},
resolve(_root, args, context) {
console.log(args);
return context.db.user.findMany({
where: {
email: args.email,
},
});
},
});
},
});
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.list.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
async resolve(_root, args, context) {
console.log(args.id);
return await context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});
我在 VS Code 中遇到了这个问题:
Type '(_root: {}, args: { id: string; }, context: Context) => Promise<User>' is not assignable to type 'FieldResolver<"Mutation", "deleteUser">'.
Type 'Promise<User>' is not assignable to type 'MaybePromise<({ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null)[]> | MaybePromise<...>'.
Type 'Promise<User>' is not assignable to type 'PromiseLike<MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]>'.
Types of property 'then' are incompatible.
Type '<TResult1 = User, TResult2 = never>(onfulfilled?: ((value: User) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[], TResult2 = never>(onfulfilled?: ((value: MaybePromise<...>[]) => TResult1 | PromiseLike<...>) | ... 1 more ... | undefined, onrejected?: (...'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type 'User' is missing the following properties from type 'MaybePromise<{ email?: string | null | undefined; id?: string | null | undefined; name?: string | null | undefined; username?: string | null | undefined; } | null>[]': length, pop, push, concat, and 29 more.
有谁知道如何解决这个问题?
从 `t.nonNull.list.field("deleteUser"{/***/}) 中删除 list
可以解决问题。
export const DeleteUserMutation = extendType({
type: "Mutation",
definition(t) {
t.nonNull.field("deleteUser", {
type: "User",
args: {
id: nonNull(stringArg()),
},
resolve(_root, args, context) {
return context.db.user.delete({
where: {
id: args.id,
},
});
},
});
},
});