如何从 schema.graphql 生成查询和变异字符串导出
How to generate queries and mutation string export from schema.graphql
我正在构建一个 flutter 应用程序并使用 amplify_flutter 0.2.1
并放大 v5.1.0
,当我从 Amplify-admin 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 official CRUD Doc
我想这样写:
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
在终端中根据你的 schema.graphql table 生成数据模型。之后,您可以这样做:例如,您有一个用户 table
User.fromJson(operation.response.data['createToDo]);
现在您有一个用户 class 的对象。
我正在构建一个 flutter 应用程序并使用 amplify_flutter 0.2.1
并放大 v5.1.0
,当我从 Amplify-admin 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 official CRUD Doc
我想这样写:
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
在终端中根据你的 schema.graphql table 生成数据模型。之后,您可以这样做:例如,您有一个用户 table
User.fromJson(operation.response.data['createToDo]);
现在您有一个用户 class 的对象。