Cerulean
Cerulean

Reputation: 6013

How can one make GraphQL queries and mutations _from_ Apollo Server?

We have a large project which will have a large number of distinct data sources/microservices -- some of these will be REST API, some GraphQL. We want to make an intermediary layer between those data sources and our final client, which will be NextJS/React/Apollo Client. Apollo Server seems a good choice for this -- receive data from the different data sources/microservices and make a unified GraphQL API that the Apollo Client front end will consume. Similarly, when we need to post data from the front end, provide a GraphQL interface which will either post REST API or GraphQL to the data sources.

The one thing I'm having trouble with is understanding how to fetch and post data from Apollo Server when the original data source/microservice has a GraphQL API. I looked through the Apollo Server documentation for fetching data and don't see what I'm looking for -- perhaps I'm missing it.

How would one do that if Apollo Server doesn't provide the API to actually fetch GraphQL data? Use Apollo Client in the Apollo Server?

I'm sure I'm missing the blindingly obvious, and would appreciate any clues!

Upvotes: 2

Views: 1186

Answers (2)

Abhishek
Abhishek

Reputation: 1332

I think you are looking for a GraphQL gateway server.

Check these links -

https://www.apollographql.com/docs/apollo-server/federation/implementing/

https://www.apollographql.com/docs/apollo-server/features/schema-delegation/#example

Edit

Case 1 - If you have microservices that are written in Graphql, so using the apollo gateway server you can stitch all the schema from all other microservices into one single schema which can be consumed by the client. It is easy to set up and manage. Even if in future you want to stitch your Graphql backends into one schema then it would be easy to scale and manage with minimal changes.

Case 2- If those microservices are a third party and you don't want to merge those schemas into one then you can use the Apollo server (without gateway) and write a wrapper resolvers that makes an external call.

You can simply use fetch API to make a network call from your resolvers. The creation of a Rest request is easy but the creation of complex Graphql requests (using fragments) might be difficult in case of fetch.

For rest calls - You can use Apollo RestDataScource to make external rest calls. It also supports apollo server caching.

For Graphql calls - you can use GraphqlDataSource or implement your own like this.

Upvotes: 1

ZachChilders
ZachChilders

Reputation: 425

It sounds like you're overthinking things a bit. You can just use Apollo Client in your intermediary layer to power the resolvers of your Apollo Server. Short of that, GraphQL servers should respond to a regular POST with the query as a body - you can even use fetch for that.

Upvotes: 3

Related Questions