Apollo iOS Client: Reusing structures


How do I reuse structures across queries?

I have an object called Match, which is kind of common

I access it through below two queries



Now I have a viewcontroller which is designed to display Matches. Since the match structure is under namespace, I cannot create something like just array of matches. In nutshell what is the best way to reuse a common structure, fragments??

Yes, fragments are probably the best way to go if your Match structure contains the same properties in both queries. That way you can have a single fragment type that’s used in both queries, and can be handed in to your View Controller without the VC having to know or care which query it came from.

If the two queries don’t get the same properties back, another way to go is to create a ViewModel type object that can be instantiated from either type, determines what to display based on what properties are actually available, and use that to power your VC.

Thanks for the reply. My view is kind of powered by ViewModels right now and yeah could work with multiple initialisers.
My only concern with fragments are the requirement to access those explicitly… lets say if I am having nested fragments… I am afraid that my accessor with end up like fragments.x.fragments.y.fragment.match dont know may be I am wrong.

It’s kinda like that to access the fragment itself, but theoretically once the fragment has been passed in to the view controller, you should have direct access to the properties on the fragment.

This would only be the case if your fragment is referencing other nested fragments itself.

I’d recommend having explicit non-nested fragments for view models if possible to clean this up.

It’s worth noting, that if your queries start to get complicated, we also recommend that you wrap your generated data structures in custom view models of your own. You can have an adapter layer that initializes a custom view model just passing the response data struct and mapping the values over to that view model. This can make it easier for your view controllers to consume the data in the way that makes most sense for them, rather than in the way that the query data has to be shaped based on your GraphQL schema.

We have nested fragments, to make things more reusable. Right now we are doing the ViewModel path. I hope it will take shape. I though liked the web codgen, it creates nice interfaces.