0
votes

I have a React app and I need to make requests to existing REST API server with Apollo Client.

One possible way would be to create GraphQL server which wraps existing REST API but I would prefer to do that on the client side and call it directly. I consider that as a temporary solution for the transitional period.

The purpose of moving to GraphQL now, despite we don't have GraphQL server yet, is to simplify client code and leverage Apollo caching capabilities to speed up the app already.

Thanks.

2

2 Answers

2
votes

This should be achievable via Apollo links. Beware, however, as this is an area of active development within Apollo, so there will be rough edges.

For example, take a look at apollo-link-rest.

Also see the official links for inspiration & reference, if you end up wanting to write your own link(s) to support your needs.

2
votes

Answering my own question a half year later.

There were basically two possible alternatives: apollo-link-rest and apollo-bridge-link.

I have analyzed both of them, tried to use both of them, and decided to go with apollo-bridge-link despite it's not promoted by Apollo team and it had incomparably fewer downloads and likes than apollo-link-rest. And I can say it was the right decision. I had no difficulties with it.

Here is a good article on apollo-bridge-link written by its creator and a demo app.

You'll have to create a graphql schema and resolvers to make apollo-bridge-link work. Just like you would do this on the graphql server. And later, if you decide to build a graphql server, it will be quite easy to reuse this schema and resolvers on the server. What I can't say about apollo-link-rest.

Their approach is to avoid using schema to make things simple. But, on the other hand, you will have to write type patchers. Take a look at example in test suite to see how complex it can become in the real world app.