r/dartlang • u/graphlinkdev • 28d ago
Flutter Generated Dart class per GraphQL selection, instead of one nullable model
I'm the author of GraphLink, a code generator for GraphQL clients, so take this with that in mind.
I wanted to see how far null safety can go with GraphQL. Say Book has a non-null author in the schema, and your query only selects id, title and year. If the client reuses Book, every field has to become nullable. So GraphLink generates a new class for that selection (it's called Book_IdTitleYear in the demo), with final fields, a constructor with required named parameters, and its own toJson and fromJson. No json_serializable needed.
It's a code generator, so the output is plain Dart and your project doesn't depend on GraphLink.
Video: https://youtu.be/gHGtCBYaj7c
Write-up: https://medium.com/@graphlinkdev/custom-graphql-queries-in-dart-select-only-the-fields-you-need-8a9cf12887f5
Is a generated class per selection the right trade-off, or would you rather have one model and handle the nulls?
1
u/Infamous-404 28d ago
amazing