0

我正在构建一个颤振应用程序并使用 amplify_flutter0.2.1和 amplify v ,当我从 Amplify-admin UI 中提取项目时,它会生成一个在前端无用5.1.0 的 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',
    }));


放大 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.

     
4

1 回答 1

1

你可以这样做,我在我的代码中使用:

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根据您的 schema.graphql 表在生成数据模型的终端中运行。之后,您可以这样做:例如,您有一个用户表

User.fromJson(operation.response.data['createToDo]);

现在你有一个用户类的对象。

于 2021-08-19T09:26:32.300 回答