Reputation: 541
We are considering using GraphQL on top of a REST service (using the FHIR standard for medical records).
I understand that the pattern with GraphQL is to aggregate the results of multiple, independent resolvers into the final result. But a FHIR-compliant REST server offers batch endpoints that already aggregate data. Sometimes we’ll need à la carte data—a patient’s age or address only, for example. But quite often, we’ll need most or all of the data available about a particular patient.
So although we can get that kind of plenary data from a single REST call that knits together multiple associations, it seems we will need to fetch it piecewise to do things the GraphQL way.
An optimization could be to eager load and memoize all the associated data anytime any resolver asks for any data. In some cases this would be appropriate while in other cases it would be serious overkill. But discerning when it would be overkill seems impossible given that resolvers should be independent. Also, it seems bloody-minded to undo and then redo something that the REST service is already perfectly capable of doing efficiently.
So—
My question is different from this question and this question because neither touches on how to take advantage of another service’s ability to aggregate data.
Upvotes: 1
Views: 144
Reputation: 84697
An alternative approach would be to parse the request inside the resolver for a particular query. The fourth parameter passed to a resolver is an object containing extensive information about the request, including the selection set. You could then await the batched request to your API endpoint based on the requested fields, and finally return the result of the REST call, and let your lower level resolvers handle parsing it into the shape the data was requested in.
Parsing the info object can be a PITA, although there's libraries out there for that, at least in the Node ecosystem.
Upvotes: 1