graphql_client 0.0.3

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

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

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.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

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
76 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
88
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 2 hints.

    Strong-mode analysis of lib/src/client.dart gave the following hint:

    line: 62 col: 15
    'JSON' is deprecated and shouldn't be used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
http ^0.11.3 0.11.3+16
logging ^0.11.3 0.11.3+1
meta ^1.1.1 1.1.5
Transitive dependencies
async 2.0.7
charcode 1.1.1
collection 1.14.10
http_parser 3.1.2
path 1.6.1
source_span 1.4.0
string_scanner 1.0.2
typed_data 1.1.5
Dev dependencies
test ^0.12.0