【问题标题】:How to wrap a remote schema in another query field?如何将远程模式包装在另一个查询字段中?
【发布时间】:2018-03-27 08:08:18
【问题描述】:

Apollo graphql-tools 现在有 Schema Stitching,这很棒。 我想合并多个端点来生成类似于GraphQL Hub的架构,像这样:

query { github: { ... } # GitHub Schema twitter: { ... } # Twitter Schema myOwnGraphqlSchema: { ... } }

最好的方法是什么?

GitHub 问题:https://github.com/apollographql/graphql-tools/issues/439

在这里进行测试:https://launchpad.graphql.com/3xlrn31pv

谢谢。

【问题讨论】:

    标签: graphql apollo


    【解决方案1】:

    编辑:我相信现在可以使用新的 graphql-tools 3.0!

    https://dev-blog.apollodata.com/the-next-generation-of-schema-stitching-2716b3b259c0


    原答案:

    这是我想出的解决方案(hack?),但可能有更好的方法:

    1. 使用introspectSchemamakeRemoteExecutableSchema 获取远程架构
    2. 使用printSchema 获取架构类型定义
    3. 将 printSchema 收到的根 typedef QueryMutation 重命名为其他名称,例如GitHubQueryGitHubMutation
    4. 使用类型为GitHubQuerygithub 字段创建根查询typedef
    5. 创建一个github 解析器,它使用execute 方法在远程github 架构中运行GitHubQuery

    源代码:https://launchpad.graphql.com/3xlrn31pv

    import 'apollo-link'
    import fetch from 'node-fetch'
    import {
      introspectSchema,
      makeExecutableSchema,
      makeRemoteExecutableSchema,
    } from 'graphql-tools'
    import { HttpLink } from 'apollo-link-http'
    import { execute, printSchema } from 'graphql'
    
    const link = new HttpLink({ uri: 'http://api.githunt.com/graphql', fetch })
    
    async function getGithubRemoteSchema() {
      return makeRemoteExecutableSchema({
        schema: await introspectSchema(link),
        link,
      })
    }
    
    async function makeSchema() {
      const githubSchema = await getGithubRemoteSchema()
      const githubTypeDefs = printSchema(githubSchema)
    
      const typeDefs = `
            ${githubTypeDefs // ugly hack #1
          .replace('type Query', 'type GitHubQuery')
          .replace('type Mutation', 'type GitHubMutation')}
    
        type Query {
          github: GitHubQuery
        }
    
        type Mutation {
          github: GitHubMutation
        }
        `
    
      return makeExecutableSchema({
        typeDefs,
        resolvers: {
          Query: {
            async github(parent, args, context, info) {
              // TODO: FIX THIS
    
              // ugly hack #2
              // remove github root field from query
              const operation = {
                ...info.operation,
                selectionSet:
                  info.operation.selectionSet.selections[0].selectionSet,
              }
              const doc = { kind: 'Document', definitions: [operation] }
    
              const result = await execute(
                githubSchema,
                doc,
                info.rootValue,
                context,
                info.variableValues,
                info.operation.name
              )
    
              return (result || {}).data
            },
          },
        },
      })
    }
    
    export const schema = makeSchema()
    

    【讨论】:

    • 天啊,我喜欢这个 hack。
    • 您能否详细说明 3.0 现在如何支持此功能的示例?
    【解决方案2】:

    在 graphql-tools-fork (https://www.npmjs.com/package/graphql-tools-fork) 中使用 WrapType 转换直接支持。

    https://github.com/yaacovCR/graphql-tools-fork/issues/24

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 2016-07-26
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多