0

How do I write a resolver for making multiple API calls to fulfil a GraphQL query using Apollo-iOS on the client side (in my swift project)?

For Example: If, to construct a Person object from a query having name and age parameters - I have to fetch name from a service call and age from another service call and stitch them to form the Person object, then how would the resolver look like and where should I write it?

Any help is appreciated.

1 Answers1

-1

I believe GraphQL resolvers are only a concept server-side. The client simply asks for the data. The server is supposed to resolve the query sent by the client so your resolvers should make the necessary calls to different services to be able to resolve the query completely by accessing the single endpoint.

A more in-depth explanation of resolvers in GraphQL: https://medium.com/paypal-engineering/graphql-resolvers-best-practices-cd36fdbcef55

You could write your own swift code to grab data from two separate GraphQL endpoints/services if needed.

  • 1
    I think from the GraphQL client side React documentation - https://www.apollographql.com/docs/react/data/local-state/. - It mentions of `local resolvers`, on client side, I believe it can be achieved in the swift client as well. But that would be a manual process we have to do. Is there a way in which the query/schema definition can achieve this? – gArImA Mahapatra Apr 10 '20 at 00:51
  • Local Resolvers in that context are used only to modify the cached graphql data. If for example, have a user record stored and need to simply update a field on the client side due to some change by the user you could use a local resolver. Although one could technically create a local resolver that connects to endpoints and always call the client resolver, this would not be a best practice. The best solution would be to have the apollo server resolver make all necessary service calls. Also, local resolvers seem not to be available in the iOS sdk yet. – David Gorski Apr 11 '20 at 15:04