How to call remote GraphQL API from another Apollo GraphQL service
@Neha_Tiwari can you share a few more details outlining what you would like to do? What Apollo GraphQL service would you like to call an API from?
We have RestDataSource to call the rest API from a GraphQL service. Do we have any such thing available to call a GraphQL service from another GraphQL service (without using federation)? Design is as follows
Apollo doesn’t currently provide a pre-built data source for this, but one option is to create a custom data source that uses the core Apollo Client library! Most of the Apollo Client docs focus on React, but you can see a basic generic JS query in the Apollo Client getting started article, and the core functions are documented here.
Making graphql calls from a federated graphql service to another federated graphql service sounds like an anti-pattern. Recommend making use of federation for this use case. You have a few oprions here.
- Return only min data (key field) needed to hydrate your type from Service B so that gateway can resolve the rest of the fields from Service A.
requiresdirective. If a field
amountin Service B requires a field
weightfrom Service A, adding the
@requires(fields: "weight")directive on the
weightfield should work as expected. Gateway will first resolve the
weightfield from Service A and then pass it to Service B.
- Make Service B a REST API.