这个问题出现在使用AWS Amplify API时,当返回的模型中的非空字段 _version返回null时会发生。解决方法是使用AWS Amplify提供的@version自动更新模型版本号。以下是一个代码示例:
npm install aws-amplify aws-amplify-react-native --save
{ ... "API": { "aws_appsync_graphqlEndpoint": "xxx", "aws_appsync_region": "xxx", "aws_appsync_authenticationType": "AWS_IAM", "aws_appsync_apiKey": "xxx" "aws_appsync_iamRegion": "xxx", //使用 IAM 时必须配置此项 "aws_appsync_disableOffline": false //启用离线功能时可以设置为 true } }
import { API, graphqlOperation } from 'aws-amplify';
// 创建模型类 class Todo { constructor(id, name, description, completed = false, version = null) { this.id = id; this.name = name; this.description = description; this.completed = completed; this._version = version; } }
// GraphQL 查询
const getTodo = query getTodo($id: ID!) { getTodo(id:$id) { id name description completed _version } };
// GraphQL 变更
const updateTodo = mutation updateTodo($input: UpdateTodoInput!) { updateTodo(input: $input) { id name description completed _version } };
// 调用查询 async function fetchTodo(id) { const result = await API.graphql(graphqlOperation(getTodo, { id })); const todo = new Todo(result.data.getTodo.id, result.data.getTodo.name, result.data.getTodo.description, result.data.getTodo.completed, result.data.getTodo._version); return todo; }
// 调用变更 async function updateCompleted(id