r/FlutterDev • • Nov 02 '26

Video How do you model GraphQL queries that select only some fields in Flutter?

I'm the author of GraphLink, a schema-first code generator for typed GraphQL clients (Dart, Java, Kotlin, TypeScript, Swift), so take this with that in mind.

GraphQL lets the client pick its fields, but a lot of client code still has one model per schema type, and anything you didn't select becomes nullable or missing. For a query that selects id, title and year from Book, GraphLink generates a class with only those three fields, so non-null fields in the schema stay non-null in the client.

I know other generators do something similar for operations. I'd like to hear what works for you, and where this breaks down.

Video (Dart): https://youtu.be/gHGtCBYaj7c

Write-up: https://medium.com/@graphlinkdev/custom-graphql-queries-in-dart-select-only-the-fields-you-need-8a9cf12887f5

2 Upvotes

7 comments sorted by

2

u/kalachlegend Nov 02 '26

graphql_codegen generates per-operation classes the same way and i like it. where it gets annoying for me is reuse: the same book shape selected in two queries turns into two unrelated classes, so one BookCard widget can't take both. fragments fix that since the query types implement the fragment type, but then you end up designing fragments around widgets. how does graphlink handle fragments?

1

u/graphlinkdev Nov 02 '26

1- Graphlink can generate all queries with _all_fields_<TYPE> meaning it generates a fragment for all the fields of a book. now if that is what you really want, and most small to medium apps, this will be enough.
2- Graphlink generates a Book_idTitle as another type, different from Book_id only for instance and also different from Book (with all fields)
The reason for this is graphlink keeps nullsafety. if we needed to keep only the base type Book, graphlink would have to generate all fields as nullable. otherwise the fromJson will fail when it expects a non null and gets passed a null value.

I explained in the video  https://youtu.be/gHGtCBYaj7c the how and why.

Graphlink supports interface/union generation. if you really need to resue, you should declare an interface as IBook and try the resued using casting.

1

u/Wise_Phone_4112 Nov 02 '26

Where it breaks down for me is conditional fields. Anything under @include or @skip has to be nullable in the generated class even if the schema says non-null, because the server just leaves it out when the condition is false. Unions and interfaces are the other painful bit: with inline fragments you really want a sealed class per __typename, plus a fallback case for types that get added to the schema after the app has shipped. How does GraphLink handle those two?

1

u/graphlinkdev Nov 02 '26

You got it right skip and include makes the field nullable and Graphlink generates that since day 1. For union and interfaces, it was painful to implement but it doesn't generate sealed classes (to support most of versions, I will think about it, maybe I will add that) but it generates normal classes that implement the interface. It uses the typename to decide which implemention the fromJson returns.

Try it, am sure you will love it.

1

u/[deleted] Nov 02 '26

[removed] — view removed comment

1

u/graphlinkdev Nov 02 '26

Thank you for the feedback. personally, I have been doing dart since flutter 1.x. when they added nullsafety, I really liked it, but generating all nullable would make the code so uncertain and makes us lose the nullsafety the compiler was here to offer.

1

u/Mathely-Caritino 29d ago

the defensive null checking everywhere is the worst part tbh, clutters up the UI code so much