9

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?

Brettski
  • 19,351
  • 15
  • 74
  • 97
  • Any leads found ??Even I am confused if to use a rest data source for graphql api? – Prerna shah Jun 27 '21 at 11:01
  • 2
    Yes, the Apollo rest data source library worked really well for us to make GraphQL calls from our api. – Brettski Jun 27 '21 at 22:46
  • @Brettski can you please demonstrate how to call GraphQL API via apollo-rest-data-source? – LaravelCoder Feb 12 '22 at 13:23
  • @LaravelCoder Yes I can demonstrate it, though I am not sure where the best pace to do that. Our code is opensource and I would reference back to that with examples. This question here doesn't feel like the correct place. – Brettski Feb 12 '22 at 20:26
  • @Brettski it could be very helpful if you share your source code or the links to the examples you referring to. Thank you – LaravelCoder Feb 13 '22 at 01:56
  • Like I mention, not sure where to put that. It's too much information for a comment. Using Apollo-datasource-rest is similar to making a GraphQL call using fetch, etc. You make a POST request with a JSON payload which contains the `query` and `variables`. One implementation is here, https://github.com/ThatConference/that-api-communications/blob/main/src/dataSources/rest/thatApi.js. It's called here, https://github.com/ThatConference/that-api-communications/blob/8a005a1d81f33b352a202ff769a796505caa2202/src/lib/graphql/fetch.js. I do understand this is hard to follow without explanation. – Brettski Feb 13 '22 at 06:15

1 Answers1

1

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:

A. Masson
  • 2,287
  • 3
  • 30
  • 36