Cer*_*ean 3 graphql keystonejs apollo-server graphql-schema
我正在使用 Apollo Server 编写 GraphQL 和 REST 服务之间的 GraphQL 接口。该接口将为 Apollo 客户端前端提供单个 GraphQL 端点。
目前唯一的服务是 KeystoneJS 应用程序,它通过(据我所知)Apollo Server 提供 GraphQL 端点。为了暂时简单起见,我从 KeystoneJS 服务器 GraphQL Playground 下载 GraphQL 架构,并将其用作我的界面的 GraphQL 架构(在删除 Keystone 生成且 Apollo Server 无法理解的定义之后)。
我想自动化这个过程——也就是说,以某种方式“抓取”KeystoneJS/Apollo Server 生成的 GraphQL 模式,就像我从 GraphQL Playground 下载它一样。有没有办法从端点做到这一点?(我不想触及 KeystoneJS 内部结构,只需通过端点访问模式)
Dan*_*den 10
您可以针对任何 GraphQL 服务运行内省查询(假设它们没有禁用内省——某些服务在生产中会这样做)。可以传递该查询的结果,buildClientSchema以根据内省结果重建模式(自然地,没有任何字段解析逻辑)。然后,您可以通过调用将返回的 GraphQLSchema 对象转换为 SDL printSchema。
这是使用 axios 的示例,但您可以使用任何您喜欢的 http 库来发出请求:
const { buildClientSchema, getIntrospectionQuery, printSchema } = require('graphql')
const axios = require('axios')
const ENDPOINT_URL = "";
(async () => {
const res = await axios.post(ENDPOINT_URL, { query: getIntrospectionQuery() })
const schema = buildClientSchema(res.data.data)
const sdl = printSchema(schema)
console.log(sdl)
})()
Run Code Online (Sandbox Code Playgroud)
注意:第一个“data”是 axios API 调用的来自服务器的实际 JSON 响应,而第二个“data”是实际的“data”属性,这是我们要传递给buildClientSchema.
| 归档时间: |
|
| 查看次数: |
4737 次 |
| 最近记录: |