

We don't want to be grabbing a bunch of extraneous data all the time, but maintaining a fragment is pretty cool. We want to just ask for the fields we need. And this way, we're still adhering to the principles of GraphQL's declarative query syntax. I would maybe call this LiftDetails instead. So, the fragment is very useful in a case like this, but I would caution you, that make sure you really need all these fields when you're using them. I can send the exact same query and get back the exact same fields.

What's really cool about this, and the reason I said this was for composition, is let's say I wanted to send a query for all lifts, dot dot dot AllFields.

So, I'll use the spread syntax, dot dot dot, and I'll push in AllFields. So here, we'll add an id, a name, a status, a capacity, a night, an elevationGain, all these different fields, right? And now what I can do is I can remove name and status from our Lift query, and I can push them in right here. So, we couldn't create a fragment of trail and lift fields together, instead I would have to create this with a single fragment on the Lift type. First thing I'll say, is fragment AllFields, and when I create the fragment, I need to create it on a specific type. So, I'll show you a couple of uses for it. And so that I would say there is no operation for that, but there's a way that we can do this with a fragment that may be useful. So, usually at this point in the class, someone asked me, how do I do like a star query? Like give me all the fields please. So, we're familiar with this syntax, but let's say I wanted to reuse some fields. We'll take in the id of panorama and we'll get the name and the status. Then, you pass your newly configured cache to ApolloClient to complete the process.- Another nice thing to know about the GraphQL query language is that we can take advantage of GraphQL fragments for composition. Use possibleTypes.json to configure your cache during construction.Or use the plugin fragment-matcher for graphql-codegen and configure it for apollo client 3.
#Graphql fragments how to#
Read the documentation about how to extract possibleTypes automatically using an introspection query.
