Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Apollo Server DataSource to call a GraphQL API

In our GraphQL api (Apollo-Server) we would like to add a new dataSource which accesses GitHub's GraphQL api. We are looking to consume this data. It appears that using apollo-datasource-rest` is a good approach to do this. It's an established, still maintained module which provides caching, access to context and other dataSource benefits. It's also managed by the Apollo team. We want to verify that this is a good approach for making requests to other GraphQL APIs.

Other options are:

  • Roll your own datasource, which doesn't seem necessary or with apparent benefits
  • Build out a datasource using @apollo/client
  • There is a module, apollo-datasource-graphql, which appears fits this perfectly, though it has not been updated in two years and appears it may be unfinished with tests and request caching not complete.

Is using apollo-datasource-rest a good practice for accessing other GraphQL APIs as a dataSource in a GraphQL server service?

Is there a better, more established approach for doing this?

like image 788
Brettski Avatar asked Nov 14 '22 23:11

Brettski


1 Answers

We are having the same concern since our backend needs to consume, as a client, a graphql api. The REST interface approach is expecting http GET queries to be cacheable, but not verbs like POST, PUT, DELETE... My understanding of GraphQL is that if you are only using http POST as a communication pattern this is going to prevent apollo-datasource-rest to handle caching for your queries and then it may not be the appropriate lib.

Other approaches to consider:

  • apollo-datasource-http
  • Apollo server (and the GraphQL specification) also supports GET queries so it may solve apollo-datasource-rest caching issues
  • usage of graphql-code-generator to generate the consumer of the target GraphQL api (and then use the client directly inside a service, or define a custom datasource to wrap the client)
like image 173
A. Masson Avatar answered Feb 16 '23 03:02

A. Masson