简体   繁体   English

如何从 Apollo Server 进行 GraphQL 查询和变更?

[英]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.我们有一个大型项目,它将拥有大量不同的数据源/微服务——其中一些将是 REST API,一些是 GraphQL。 We want to make an intermediary layer between those data sources and our final client, which will be NextJS/React/Apollo Client.我们希望在这些数据源和我们的最终客户端之间建立一个中间层,这将是 NextJS/React/Apollo 客户端。 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. Apollo Server 似乎是一个不错的选择——从不同的数据源/微服务接收数据,并制作一个统一的 GraphQL API,Apollo Client 前端将使用它。 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.同样,当我们需要从前端发布数据时,提供一个 GraphQL 接口,它将 REST API 或 GraphQL 发布到数据源。

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.当原始数据源/微服务具有 GraphQL API 时,我遇到的一件事是理解如何Apollo Server 获取和发布数据。 I looked through the Apollo Server documentation for fetching data and don't see what I'm looking for -- perhaps I'm missing it.我查看了Apollo Server 文档以获取数据,但没有看到我在寻找什么——也许我错过了它。

How would one do that if Apollo Server doesn't provide the API to actually fetch GraphQL data?如果 Apollo Server 不提供 API 来实际获取 GraphQL 数据,人们会怎么做? Use Apollo Client in the Apollo Server?在 Apollo Server 中使用 Apollo Client?

I'm sure I'm missing the blindingly obvious, and would appreciate any clues!我敢肯定我错过了令人眼花缭乱的明显问题,并希望得到任何线索!

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.您可以在中间层中使用 Apollo Client 来为 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.除此之外,GraphQL 服务器应该以查询作为正文来响应常规 POST - 您甚至可以为此使用fetch

I think you are looking for a GraphQL gateway server.我认为您正在寻找 GraphQL 网关服务器。

Check these links -检查这些链接 -

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

https://www.apollographql.com/docs/apollo-server/features/schema-delegation/#example 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.案例 1 - 如果您有用 Graphql 编写的微服务,那么使用 apollo 网关服务器,您可以将所有其他微服务的所有架构拼接成一个可由客户端使用的单一架构。 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.即使将来您想将 Graphql 后端缝合到一个模式中,也可以通过最少的更改轻松扩展和管理。

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.案例 2 - 如果这些微服务是第三方并且您不想将这些模式合并为一个,那么您可以使用 Apollo 服务器(不带网关)并编写一个进行外部调用的包装器解析器。

You can simply use fetch API to make a network call from your resolvers.您可以简单地使用fetch API从解析器进行网络调用。 The creation of a Rest request is easy but the creation of complex Graphql requests (using fragments) might be difficult in case of fetch.创建 Rest 请求很容易,但在 fetch 的情况下创建复杂的 Graphql 请求(使用片段)可能很困难。

For rest calls - You can use Apollo RestDataScource to make external rest calls.对于休息调用 - 您可以使用 Apollo RestDataScource进行外部休息调用。 It also supports apollo server caching.它还支持 apollo 服务器缓存。

For Graphql calls - you can use GraphqlDataSource or implement your own like this.对于 Graphql 调用 - 您可以使用GraphqlDataSource或像这样实现自己的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM