首页 > 解决方案 > 实现 GraphQL 和 Flutter

问题描述

有没有办法在 Flutter 中实现 GraphQL?我正在尝试使用 JSON 对象中的查询和变量对象进行 API 调用。

类型“_InternalLinkedHashMap”不是类型转换中“String”类型的子类型

标签: dartfluttergraphql

解决方案


我已经使用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});

推荐阅读