built_graphql 1.0.1 copy "built_graphql: ^1.0.1" to clipboard
built_graphql: ^1.0.1 copied to clipboard

Using built_value to deserialize GraphQL JSON responses. The package provides generic classes to deal with the fix format of the GraphQL response and errors.

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.

2
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Using built_value to deserialize GraphQL JSON responses. The package provides generic classes to deal with the fix format of the GraphQL response and errors.

Repository

License

BSD-3-Clause (LICENSE)

Dependencies

built_collection, built_value

More

Packages that depend on built_graphql