graphql_client 0.0.3

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

graphql_client Build Status

GraphQL Client written in Dart 🎯.

It relies on the dart http client to send GQL queries. As the http client, tt's platform-independent, and can be used on the command-line, browser and flutter. It has a custom DSL to write GQL queries and will have soon a code generator converting GQL queries to this DSL. Stay tuned 🎤. If you want to help, please check this doc.

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
  OperationType get type => OperationType.query;

  @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.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['GITHUBQL_TOKEN'];

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

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

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

    final queryRes = await graphQLClient.execute(
      query,
      variables: {'issueId': 'MDU6SXNzdWUyNDQzNjk1NTI', 'body': 'Test issue 2'},
      headers: {
        '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: {'issueId': 'MDU6SXNzdWUyNDQzNjk1NTI', 'body': 'Test issue '},
      headers: {
        '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: {'issueId': 'efwef', 'body': 'Test issue'},
      headers: {
        '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.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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/graphql_client.dart';
  
Version Uploaded Documentation 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]
37
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
19
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Maintenance issues and suggestions

Running dartdoc failed. (-10 points)

Make sure dartdoc runs without any issues.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0