Amplify的自动生成的更新变异可能会失败,这个问题可以通过手动编写Mutation来解决。以下是一个示例:
type Post @model {
id: ID!
name: String!
description: String
}
amplify codegen
amplify api gql-compile
mutation updatePost($input: UpdatePostInput!) {
updatePost(input: $input) {
id
name
description
}
}
import { API, graphqlOperation } from 'aws-amplify'
const updatePost = /* GraphQL */ `
mutation updatePost($input: UpdatePostInput!) {
updatePost(input: $input) {
id
name
description
}
}`
const updateData = async (postId, postData) => {
try {
const result = await API.graphql(graphqlOperation(updatePost, {
input: {
id: postId,
...postData
}
}))
console.log('Mutation result', result)
} catch (err) {
console.log('Mutation error', err)
}
}
updateData('postId123', { name: 'New post name', description: 'New post description' })
对于更复杂的模型和变更需求,可能需要更详细的手动编写Mutation,以覆盖Amplify自动生成的Mutation的限制。
上一篇:Amplify的全局环境变量