Joseph Arriaza
Joseph Arriaza

Reputation: 13724

Implement GraphQL & Flutter

Is there a way to implement GraphQL in flutter? I was trying making the API call with the query and variables objects in a JSON object.

type '_InternalLinkedHashMap' is not a subtype of type 'String' in type cast

Upvotes: 1

Views: 4401

Answers (2)

Dmytro Pashkov
Dmytro Pashkov

Reputation: 360

This is updated and working solution of @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);

Upvotes: 0

aqwert
aqwert

Reputation: 10789

I have been using graphql_flutter package for a few weeks now and it seems to work well enough. Here is an example:

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;
  }

To use just give it the graphql and any variables. i.e. a delete mutation may look like

String deleteMutation =
      '''mutation deleteItem(\$itemId: ID!) {
    deleteItem(input: { itemId: \$itemId}) {
      itemId
    }
  }'''.replaceAll('\n', ' ');

 await post(deleteMutation , variables: <String, dynamic>{'itemId': itemId});

Upvotes: 1

Related Questions