graphql_client 0.0.4+1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 69

graphql_client Build Status

GraphQL Client written in Dart 🎯.

It relies on the dart http client to send GQL queries. As the http client is platform-independent, and can be used on the command-line, browser and Flutter. It has a custom DSL to write GQL queries.

Usage #

For now, you have to write your GQL queries with the graphql_client DSL. You will be able to convert GQL queries into this DSL soon using a dart transformer.

The following code sample allows you to retrieve the current github user bio using the Github GraphQL API v4.

import 'dart:async';
import 'dart:io'; // Optional because I am reading env variables.

import 'package:http/http.dart';
import 'package:logging/logging.dart'; // Optional
import 'package:graphql_client/graphql_client.dart';
import 'package:graphql_client/graphql_dsl.dart';

/**
 * Define a custom GQL query.
 *
 * The corresponding GQL is :
 * query ViewerBioQuery {
 *   viewer {
 *     bio
 *   }
 * }
 */

class ViewerBioQuery extends Object with Fields implements GQLOperation {
  ViewerResolver viewer = new ViewerResolver();

  @override
  String get type => queryType;

  @override
  String get name => 'ViewerBioQuery';

  @override
  List<GQLField> get fields => [viewer];

  @override
  ViewerBioQuery clone() => new ViewerBioQuery()..viewer = viewer.clone();
}

class ViewerResolver extends Object with Fields implements GQLField {
  BioResolver bio = new BioResolver();

  @override
  String get name => 'viewer';

  @override
  List<GQLField> get fields => [bio];

  @override
  ViewerResolver clone() => new ViewerResolver()..bio = bio.clone();
}

class BioResolver extends Object with Scalar<String> implements GQLField {
  @override
  String get name => 'bio';

  @override
  BioResolver clone() => new BioResolver();
}

Future main() async {
  Logger.root // Optional
    ..level = Level.ALL
    ..onRecord.listen((rec) {
      print('${rec.level.name}: ${rec.time}: ${rec.message}');
    });
    
  const endPoint = 'https://api.github.com/graphql';
  final apiToken = Platform.environment['GITHUBQL_TOKEN'];

  final client = new Client();
  final logger = new Logger('GQLClient'); // Optional.
  final graphQLClient = new GQLClient(
    client: client,
    logger: logger,
    endPoint: endPoint,
  );

  final query = new ViewerBioQuery();

  try {
    final queryRes = await graphQLClient.execute(
      query,
      variables: {},
      headers: {
        'Authorization': 'bearer $apiToken',
      },
    );

    print(queryRes.viewer.bio.value); // => 'My awesome Github Bio!'
  } on GQLException catch (e) {
    print(e.message);
    print(e.gqlErrors);
  }
}

Roadmap #

You can find it here.

Changelog #

0.0.4 #

  • Dart 2 support

0.0.3 #

Features #

  • Support GQL directive

Breaking changes #

  • Refactor OperationType. Remove the enum and replace its value by constant strings

Minor changes #

  • Remove getter and setter of Scalar & ScalarCollection mixin

0.0.2 #

  • Refactor the client
  • Create a DSL that supports very GQL features
  • Ready to support code generation

0.0.1 #

  • Initial version.
  • Very simple PoC of a reflective GraphQL client with example.
  • Establishing the ROADMAP

example/graphql_client_example.dart

// Copyright Thomas Hourlier. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

// ignore_for_file: public_member_api_docs

import 'dart:async';
import 'dart:io';

import 'package:http/http.dart';
import 'package:logging/logging.dart';

import 'package:graphql_client/graphql_client.dart';

import 'queries_examples.dart';

Future main() async {
  Logger.root
    ..level = Level.ALL
    ..onRecord.listen((rec) {
      print('${rec.level.name}: ${rec.time}: ${rec.message}');
    });

  const endPoint = 'https://api.github.com/graphql';
  final apiToken = Platform.environment['GQL_GITHUB_TOKEN'];

  final client = Client();
  final logger = Logger('GQLClient');
  final graphQLClient = GQLClient(
    client: client,
    logger: logger,
    endPoint: endPoint,
  );

  final query = LoginQuery();
  final mutation = AddTestCommentMutation();

  try {
    print('\n\n===================== TEST 1 =====================');

    final queryRes = await graphQLClient.execute(
      query,
      variables: <String, String>{'issueId': 'MDU6SXNzdWUyNDQzNjk1NTI', 'body': 'Test issue 2'},
      headers: <String, String>{
        'Authorization': 'bearer $apiToken',
      },
    );

    print('=== . ===');
    print(queryRes.viewer.login.value);
    print(queryRes.viewer.bio.value);
    print(queryRes.viewer.bio2.value);

    print('=== .repository ===');
    print(queryRes.viewer.repository.createdAt.value);
    print(queryRes.viewer.repository.description.value);
    print(queryRes.viewer.repository.id.value);
    print(queryRes.viewer.repository.repoName.value);

    print('=== .gist ===');
    print(queryRes.viewer.gist.description.value);

    print('=== .repositories ===');
    for (var n in queryRes.viewer.repositories.nodes) {
      print(n.repoName.value);
    }
  } on GQLException catch (e) {
    print(e.message);
    print(e.gqlErrors);
  } finally {
    print('=================== END TEST 1 ===================\n\n');
  }

  try {
    print('\n\n===================== TEST 2 =====================');

    final mutationRes = await graphQLClient.execute(
      mutation,
      variables: <String, String>{'issueId': 'MDU6SXNzdWUyNDQzNjk1NTI', 'body': 'Test issue '},
      headers: <String, String>{
        'Authorization': 'bearer $apiToken',
      },
    );

    print('=== .body ===');
    print(mutationRes.addComment.commentEdge.node.body.value);
  } on GQLException catch (e) {
    print(e.message);
    print(e.gqlErrors);
  } finally {
    print('=================== END TEST 2 ===================\n\n');
  }

  try {
    print('\n\n===================== TEST 3 =====================');

    await graphQLClient.execute(
      mutation,
      variables: <String, String>{'issueId': 'efwef', 'body': 'Test issue'},
      headers: <String, String>{
        'Authorization': 'bearer $apiToken',
      },
    );
  } on GQLException catch (e) {
    print(e.message);
    print(e.gqlErrors);
    print(e.gqlErrors.first);
  } finally {
    print('=================== END TEST 3 ===================\n\n');
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  graphql_client: ^0.0.4+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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/graphql_client.dart';
  
Version Uploaded Documentation Archive
0.0.4+1 Mar 26, 2019 Go to the documentation of graphql_client 0.0.4+1 Download graphql_client 0.0.4+1 archive
0.0.4 Mar 25, 2019 Go to the documentation of graphql_client 0.0.4 Download graphql_client 0.0.4 archive
0.0.3 Aug 25, 2017 Go to the documentation of graphql_client 0.0.3 Download graphql_client 0.0.3 archive
0.0.2 Aug 25, 2017 Go to the documentation of graphql_client 0.0.2 Download graphql_client 0.0.2 archive
0.0.1 Jul 19, 2017 Go to the documentation of graphql_client 0.0.1 Download graphql_client 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Apr 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:graphql_client/graphql_client.dart.

Health suggestions

Format lib/src/client.dart.

Run dartfmt to format lib/src/client.dart.

Format lib/src/exceptions.dart.

Run dartfmt to format lib/src/exceptions.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http ^0.11.3 0.11.3+17 0.12.0+2
logging ^0.11.3 0.11.3+2
meta ^1.1.1 1.1.7
Transitive dependencies
async 2.2.0
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
path 1.6.2
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
perfectionist ^2.0.0
test ^1.5.3