graphql 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 95

GraphQL Client #

Build Status Coverage version MIT License All Contributors PRs Welcome

Watch on GitHub Star on GitHub Discord

Installation #

First, depend on this package:

  graphql: ^1.0.1-beta

And then import it inside your dart code:

import 'package:graphql/client.dart';

Usage #

To connect to a GraphQL Server, we first need to create a GraphQLClient. A GraphQLClient requires both a cache and a link to be initialized.

In our example below, we will be using the Github Public API. In our example below, we are going to use HttpLink which we will concatinate with AuthLink so as to attach our github access token. For the cache, we are going to use InMemoryCache.


final HttpLink _httpLink = HttpLink(
    uri: '',

final AuthLink _authLink = AuthLink(
    getToken: () async => 'Bearer $YOUR_PERSONAL_ACCESS_TOKEN',

final Link _link = _authLink.concat(_httpLink as Link);

final GraphQLClient _client = GraphQLClient(
        cache: InMemoryCache(),
        link: _link,


Once you have initialized a client, you can run queries and mutations.

Query #

Creating a query is as simple as creating a multiline string:

const String readRepositories = r'''
  query ReadRepositories($nRepositories: Int!) {
    viewer {
      repositories(last: $nRepositories) {
        nodes {

Then create a QueryOptions object with the query string as the document and pass any variables necessary.

In our case, we need pass nRepositories variable and the document name is readRepositories.

const int nRepositories = 50;

final QueryOptions options = QueryOptions(
    document: readRepositories,
    variables: <String, dynamic>{
        'nRepositories': nRepositories,

And finally you can send the query to the server and await the response:


final QueryResult result = await _client.query(options);

if (result.hasErrors) {

final List<dynamic> repositories =['viewer']['repositories']['nodes'] as List<dynamic>;


Mutations #

Creating a Matation is also similar to creating a query, with a small difference. First, start with a multiline string:

const String addStar = r'''
  mutation AddStar($starrableId: ID!) {
    action: addStar(input: {starrableId: $starrableId}) {
      starrable {

Then instead of the QueryOptions, for mutations we will MutationOptions, which is where we pass our mutation and id of the repository we are starring.


final MutationOptions options = MutationOptions(
  document: addStar,
  variables: <String, dynamic>{
    'starrableId': repositoryID,


And finally you can send the query to the server and await the response:


final QueryResult result = await _client.mutate(options);

if (result.hasErrors) {

final bool isStarrred =['action']['starrable']['viewerHasStarred'] as bool;

if (isStarrred) {
  print('Thanks for your star!');


See GitHub Releases.

  • Loosened initPayload to dynamic to support many use-cases, Removed InitOperation's excessive and inconsistent json encoding. Old implmentation can still be utilized as legacyInitPayload until deprecation

  • Fixed broken anonymous operations


Example Dart Application #

This is a simple command line application to showcase how you can use the Dart GraphQL Client, without flutter.

To run this application:

  1. First clone this repository and navigate to this directory

  2. Install all dart dependencies

  3. create a file bin/local.dart with the content:

  4. Then run the Application using the commands below:

    1. List Your Repositories
    pub run main.dart
    1. Star Repository
    pub run main.dart -a star --id <REPOSITORY_ID_HERE>
    1. Unstar Repository
    pub run main.dart -a unstar --id <REPOSITORY_ID_HERE>

NB: Replace repository id in the last two commands with a real Github Repository ID. You can get by running the first command, IDs are printed on the console.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  graphql: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:graphql/client.dart';
import 'package:graphql/internal.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on May 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/socket_client.dart. (-1.49 points)

Analysis of lib/src/socket_client.dart reported 3 hints:

line 18 col 22: 'legacyInitPayload' is deprecated and shouldn't be used.

line 57 col 14: 'LegacyInitOperation' is deprecated and shouldn't be used.

line 168 col 7: DO use curly braces for all flow control structures.

Fix lib/src/link/web_socket/link_web_socket.dart. (-1 points)

Analysis of lib/src/link/web_socket/link_web_socket.dart reported 2 hints:

line 45 col 7: DO use curly braces for all flow control structures.

line 54 col 9: DO use curly braces for all flow control structures.

Format lib/src/utilities/traverse.dart.

Run dartfmt to format lib/src/utilities/traverse.dart.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (rxdart).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
graphql_parser ^1.1.3 1.1.4
http ^0.12.0+2 0.12.1
http_parser ^3.1.3 3.1.4
meta ^1.1.6 1.1.8
mime ^0.9.6+2 0.9.6+3
path ^1.6.2 1.7.0
rxdart ^0.22.0 0.22.6 0.24.1
uuid ^2.0.1 2.0.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
pedantic 1.9.0
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
mockito ^4.0.0
perfectionist ^2.0.0
test ^1.5.3
test_coverage ^0.2.0