built_graphql 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

Built Value for GraphQL responses #

A library that contains basic built_value classes to help deserializing GraphQL responses.

Thanks for the great built_value library from Google.

Usage #

The example directory contains a Serializer definition and 2 basic classes.

First, add all of your classes to your serializer in the @SerializersFor. Then you have to add the GraphQLSerializerPlugin and StandardJsonPlugin.

For each type that you want to deserialize with GraphQLResponse, the serializer must contain a builder factory definition. Here is an example for Hero and Friend classes:

final standardSerializers = (serializers.toBuilder()
      ..addPlugin(StandardJsonPlugin())
      ..addPlugin(GraphQLSerializerPlugin())
      ..addBuilderFactory(GraphQLResponse.createFullTypeFor(Friend),
          GraphQLResponse.createBuilder<Friend>())
      ..addBuilderFactory(GraphQLResponse.createFullTypeFor(Hero),
          GraphQLResponse.createBuilder<Hero>()))
    .build();

You can deserialize a Hero from GraphQL response by

final GraphQLResponse<Hero> response = standardSerializers.deserialize(
        json.decode(jsonStr),
        specifiedType: GraphQLResponse.createFullTypeFor(Hero));

final Hero hero = response.data;

You can find more examples in the tests.

Limitations #

The library assumes that the name of the class, that will hold your data, and the key in the response are the same.

For the Hero class, the JSON response must have a hero key:

{
  "data": {
    "hero": {
      ...
    }
  }
}

Features and bugs #

The library supports only deserialization of the JSON responses. Serialization of the objects won't give the same result.

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version, created by dtengeri

example/main.dart

import 'dart:convert';
import 'package:built_graphql/built_graphql.dart';

import 'hero.dart';
import 'serializers.dart';

void main() {
  final String jsonStr = '''
    {
      "data": {
        "hero": {
          "name": "R2-D2",
          "heroFriends": [
            {
              "id": "1000",
              "name": "Luke Skywalker"
            },
            {
              "id": "1002",
              "name": null
            },
            {
              "id": "1003",
              "name": "Leia Organa"
            }
          ]
        }
      }
    }
    ''';
  final GraphQLResponse<Hero> response = standardSerializers.deserialize(
      json.decode(jsonStr),
      specifiedType: GraphQLResponse.createFullTypeFor(Hero));

  print(response);
}

Use this package as a library

1. Depend on it

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


dependencies:
  built_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:built_graphql/built_graphql.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
18
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

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

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
built_collection >=2.0.0 <5.0.0 4.2.2
built_value >=5.5.5 <7.0.0 6.7.1
Transitive dependencies
collection 1.14.12
fixnum 0.10.9
matcher 0.12.5
meta 1.1.7
path 1.6.4
quiver 2.0.5
stack_trace 1.9.3
Dev dependencies
build_runner ^1.0.0
built_value_generator ^6.3.0
pedantic ^1.0.0
test ^1.0.0