This is a stand-alone package based on the flutter-freezed
plugin to generate Freezed from your GraphQL schema.
Dart is awesome, but defining a "model" can be tedious. We may have to:
- define a constructor + the properties
- override toString, operator ==, hashCode
- implement a copyWith method to clone the object
- handling de/serialization
On top of that, Dart is also missing features such as union types and pattern-matching.
Implementing all of this can take hundreds of lines, which are error-prone and the readability of your model significantly.
Freezed tries to fix that by implementing most of this for you, allowing you to focus on the definition of your model. https://pub.dev/packages/freezed
Creating these models by hand becomes even more complex when you are working with a GraphQL server that can handle nested mutations.
See this GraphQL Playground and ask yourself, how would I create a model to handle the data returned by this nested mutation below?
Please change the address in the playground to use
https
instead ofhttp
# Write your query or mutation here
mutation createProgramme{
createProgramme(input: {
title: "GraphQL + Flutter",
programmeCode: "graphql-dart-flutter-101",
startLevel: 100,
endLevel: 100,
programmeOutlines: {
create: {
level: 100,
course: {
create:{
title: "Flutter Freezed",
courseCode: "CS-FF-101",
books: {
create:{
title: "GraphQL is way awesome",
author: "Parables Boltnoel"
}
}
}
}
}
}
}){
id
title
programmeCode
programmeOutlines{
level
course{
books{
id
title
author
}
}
}
}
}
Fortunately enough, GraphQL is strongly typed, and so is Dart.
Save yourself from implementing a model to match your strongly typed GraphQL operations, and let Freezed handle the work while you chill with this flutter-freezed
plugin
Currently supports the following features
Currently, the plugin supports the following features
- Generate Freezed classes for ObjectTypes
- Generate Freezed classes for InputTypes
- Support for EnumsTypes
- Support for custom ScalarTypes
- Support freeze documentation of class & properties from GraphQL SDL description comments
- Ignore/don't generate freezed classes for certain ObjectTypes
- Support directives
- Support deprecation annotation
- [] Support for InterfaceTypes
- Support for UnionTypes union/sealed classes
- Merge InputTypes with ObjectType as union/sealed class union/sealed classes
- Support Queries, Mutations, and Subscription: make it way easier to use GraphQL in flutter without going through any complex process. Inspired by KitQL
npm install -g graphql-codegen-flutter-freezed
In your Flutter project root, create a codegen.yml
file.
This file is allows you to configure the generator.
overwrite: true
schema: './lib/data/graphql/schema.graphql'
generates:
lib/data/models/app_models.dart:
plugins:
- typescript
- flutter-freezed
Then simply run
Please see CHANGELOG for more information what has changed recently.
yarn test
If you discover any security related issues, please email parables95@gmail.com instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.