如果AWS AppSync的graphqlMutation助手不更新查询,请尝试以下解决方法:
确保您的数据模型(即GraphQL schema)已正确定义,并包含要在更新操作中更新的字段。
确保您的resolvers(解析器)正确处理更新操作。在更新操作的解析器中,您需要更新相应的数据,并返回更新后的数据。
以下是一个示例代码,演示如何在AWS AppSync中更新查询:
首先,定义一个GraphQL schema,包含一个类型和一个更新操作:
type Book {
id: ID!
title: String!
author: String!
}
type Mutation {
updateBook(id: ID!, title: String!, author: String!): Book
}
然后,在AWS AppSync的resolvers中,创建一个解析器来处理更新操作:
// 设置resolvers
const resolvers = {
Mutation: {
updateBook: async (root, args, context) => {
const { id, title, author } = args;
// 通过id查找要更新的书籍
const book = await context.prisma.book.findUnique({ where: { id } });
if (!book) {
throw new Error('Book not found');
}
// 更新书籍的title和author字段
const updatedBook = await context.prisma.book.update({
where: { id },
data: {
title,
author,
},
});
return updatedBook;
},
},
};
在上述代码中,我们首先通过id查找要更新的书籍。如果找不到该书籍,则抛出错误。然后,我们使用context.prisma.book.update
方法更新书籍的title和author字段,并返回更新后的书籍对象。
请注意,上述示例中的context.prisma.book
是一个Prisma客户端实例,用于与数据库进行交互。您需要根据您的应用程序配置正确的Prisma客户端。
最后,将resolvers添加到AWS AppSync配置中,并重新部署您的AppSync API。现在,当您调用updateBook
mutation时,它将更新相应的书籍并返回更新后的数据。