我正在构建一个扑动应用程序,并使用amplify_flutter0.2.1
和amplify v5.1.0
,当我从amplifyadmin UI中拉出项目时,它会生成一个graphQL模式schema.graphql
,这在前端是无用的,因为为了获取或修改文档,每次我们需要编写graphQL查询文档,像这样:
String graphQLDocument =
'''mutation CreateTodo($name: String!, $description: String) {
createTodo(input: {name: $name, description: $description}) {
id
name
description
}
}''';
var operation = Amplify.API.mutate(
request: GraphQLRequest<String>(document: graphQLDocument, variables: {
'name': 'my first todo',
'description': 'todo description',
}));
Amplify Flutter官方CRUD文档
我想这样写:
const input = {
name,
description
};
const output = {
id,name,description
};
var graphQLDoc = createToDO(input,output); // it should return the string object according to the input and output passed.
var operation = Amplify.API.mutate(
request: GraphQLRequest<String>(document: graphQLDoc, variables: {
'name': 'my first todo',
'description': 'todo description',
}));
或者可以像这样进阶:
const input = {
name:"hackrx",
description: "this works cool"
};
const output = {
id,name,description
};
var graphQLQueryRes = await createToDO(input,output); // it should return the whole fetched object according to the output passed.
你可以这样做,我在我的代码中使用:
String myMutation(name, description) {
var graphQLDocument = '''mutation CreateTodo {
createTodo(input: {name: $name, description: $description}) {
id
name
description
}
}
''';
return graphQLDocument;
}
var operation = Amplify.API.mutate(
request:
GraphQLRequest<String>(document: myMutation('john', 'doe')));
用于获取响应:您可以根据模式在生成数据模型的终端中运行amplify codegen models
。graphql表。之后,你可以这样做:例如,你有一个用户表
User.fromJson(operation.response.data['createToDo]);
现在你有一个用户类的对象。