【问题标题】:Graphql - creating a schema from a generated JSON fileGraphql - 从生成的 JSON 文件创建模式
【发布时间】:2019-05-11 08:03:56
【问题描述】:

我正在尝试创建一个自定义 graphql 架构以在我的 graphql 瑜伽服务器上使用。 graphql 瑜伽服务器只是另一个 graphql API 的代理,我已经设法从中检索 JSON 格式的模式。以下是该架构的预览:

{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      },
      "mutationType": null,
      "subscriptionType": null,
      "types": [
        {
          "kind": "OBJECT",
          "name": "Core",
          "description": null,
          "fields": [
            {
              "name": "_meta",
              "description": null,
              "args": [],
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "OBJECT",
                  "name": "Meta",
                  "ofType": null
                }
              },
              "isDeprecated": false,
              "deprecationReason": null
            },
            {
              "name": "_linkType",
              "description": null,
              "args": [],
              "type": {
                "kind": "SCALAR",
                "name": "String",
                "ofType": null
              },
              "isDeprecated": false,
              "deprecationReason": null
            }
          ],
          "inputFields": null,
          "interfaces": [
            { 

我现在想使用这个生成的 JSON 模式并使用它来创建一个 graphql 模式,以便在我的 graphql 瑜伽服务器中使用。我相信正确的方法是使用 graphql 中的 new GraphQLSchema 方法和根查询。这是我尝试这样做的代码:

schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: schema.data.__schema
  })
});

上面的代码给了我以下错误:

错误:Query.mutationType 字段配置必须是对象

不完全确定哪里出了问题,或者这是否是从生成的 JSON 创建 graphql 架构的正确方法?

【问题讨论】:

    标签: javascript node.js graphql


    【解决方案1】:

    您拥有的 JSON 是 introspection query 的结果。不幸的是,内省不允许您复制远程模式。这是因为虽然它确实确定了模式中存在哪些字段,但它并没有告诉您应该如何执行它们。例如,根据您发布的 sn-p,我们知道远程服务器公开了一个返回 Meta 类型的 _meta 查询——但我们不知道要运行什么代码来解析查询返回的值.

    从技术上讲,可以将自省查询的结果从graphql/utilities 模块传递给buildClientSchema。但是,正如文档指出的那样,该架构将不可执行:

    给定客户端运行自省查询的结果,创建并返回一个 GraphQLSchema 实例,该实例随后可与所有 GraphQL.js 工具一起使用,但不能用于执行查询,因为自省并不代表“解析器” 、“解析”或“序列化”函数或任何其他服务器内部机制。

    如果您想为另一个 GraphQL 端点创建代理,最简单的方法是使用 graphql-tools 中的 makeRemoteExecutableSchema

    这是基于the docs的示例:

    import { HttpLink } from 'apollo-link-http';
    import fetch from 'node-fetch';
    
    const link = new HttpLink({ uri: 'http://your-endpoint-url/graphql', fetch });
    async function getRemoteSchema () {
      const schema = await introspectSchema(link);
      return makeRemoteExecutableSchema({
        schema,
        link,
      });
    }
    

    生成的架构是一个GraphQLSchema 对象,可以像平常一样使用:

    import { GraphQLServer } from 'graphql-yoga'
    
    async function startServer () {
      const schema = await introspectSchema(link);
      const executableSchema = makeRemoteExecutableSchema({
        schema,
        link,
      });
      const server = new GraphQLServer({ schema: executableSchema })
      server.start()
    }
    
    startServer()
    

    graphql-tools 还允许您使用 stitch schemas together,如果您不仅想代理现有端点,而且还想添加它。

    【讨论】:

    • 感谢您的回复。所以大概我应该能够像这样将模式插入到graphql瑜伽配置中:const server = new GraphQLServer({ schema: getRemoteSchema(), context: req => ({ ...req, Prismic }) })。如果是这样,它会生成错误消息“Unexpected token
    • 我还应该注意 getRemoteSchema 正在向我返回一些不是错误的东西
    • 在示例中,getRemoteSchema 返回一个 Promise,因此您不能直接将其传递给 GraphQLServer 的构造函数。我已经使用graphql-yoga 使用更完整的示例更新了答案。但是,如果您看到格式错误的 JSON 错误,这可能意味着您对外部服务器的调用失败。检查您的端点 URL。
    • 现在完美运行,感谢详细解释!
    猜你喜欢
    • 2023-04-01
    • 2019-04-20
    • 2016-08-09
    • 2021-12-11
    • 2023-03-25
    • 2020-12-05
    • 2020-07-11
    • 2020-01-15
    • 2014-08-24
    相关资源
    最近更新 更多