【发布时间】:2019-02-25 06:50:41
【问题描述】:
有没有办法在 Flutter 中实现 GraphQL? 我正在尝试使用 JSON 对象中的查询和变量对象进行 API 调用。
类型“_InternalLinkedHashMap”不是类型转换中“String”类型的子类型
【问题讨论】:
有没有办法在 Flutter 中实现 GraphQL? 我正在尝试使用 JSON 对象中的查询和变量对象进行 API 调用。
类型“_InternalLinkedHashMap”不是类型转换中“String”类型的子类型
【问题讨论】:
我已经使用graphql_flutter 包几个星期了,它似乎工作得很好。这是一个例子:
import 'package:graphql_flutter/graphql_flutter.dart' show Client, InMemoryCache;
...
Future<dynamic> post(
String body, {
Map<String, dynamic> variables,
}) async {
final Client client = Client(
endPoint: endpoint,
cache: new InMemoryCache(),
);
final Future<Map<String, dynamic>> result =
client.query(query: body, variables: variables);
return result;
}
要使用它,只需给它 graphql 和任何变量。即删除突变可能看起来像
String deleteMutation =
'''mutation deleteItem(\$itemId: ID!) {
deleteItem(input: { itemId: \$itemId}) {
itemId
}
}'''.replaceAll('\n', ' ');
await post(deleteMutation , variables: <String, dynamic>{'itemId': itemId});
【讨论】:
.gql 文件进行graphql 查询难道不会让您的生活更轻松吗?就像你有语法工具一样。
这是@aqwert 的更新和工作解决方案
import 'package:graphql_flutter/graphql_flutter.dart';
...
HttpLink link = HttpLink(uri: /*your url here*/); // you can also use headers for authorization etc.
GraphQLClient client = GraphQLClient(link: link as Link, cache: InMemoryCache());
QueryOptions query = QueryOptions(
document:
r'''
mutation deleteItem($id: String!) {
deleteItem(callId: $id)
}
''',
variables: {'id' : id}
);
var result = await client.query(query);
【讨论】: