Skip to content

dtengeri/built_graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Deserialize GraphQL responses with built_value in Dart

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages