Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Graphql 如何在另一个查询字段中包装远程架构?_Graphql_Apollo - Fatal编程技术网

Graphql 如何在另一个查询字段中包装远程架构?

Graphql 如何在另一个查询字段中包装远程架构?,graphql,apollo,Graphql,Apollo,Apollographql工具现在有了模式拼接,这很好。 我想合并多个端点以生成类似的模式,如下所示: 查询{ github:{…}#github架构 twitter:{…}#twitter模式 myOwnGraphqlSchema:{…} } 最好的方法是什么 GitHub问题: 请在此分叉进行测试: 多谢各位 编辑:我相信这在新的graphql工具3.0中是可能的 原始答案: 我想出了一个解决方案(黑客?),但可能有更好的方法: 使用introspectSchema和makeRemot

Apollo
graphql工具
现在有了模式拼接,这很好。 我想合并多个端点以生成类似的模式,如下所示:

查询{
github:{…}#github架构
twitter:{…}#twitter模式
myOwnGraphqlSchema:{…}
}

最好的方法是什么

GitHub问题:

请在此分叉进行测试:

多谢各位

编辑:我相信这在新的graphql工具3.0中是可能的


原始答案:

我想出了一个解决方案(黑客?),但可能有更好的方法:

  • 使用
    introspectSchema
    makeRemoteExecutableSchema
  • 使用
    printSchema
  • 将printSchema接收到的根typedefs
    Query
    Mutation
    重命名为其他名称,例如
    GitHubQuery
    GitHubMutation
  • 使用类型为
    GitHubQuery
    github
    字段创建根查询typedef
  • 创建一个
    github
    解析器,该解析器使用
    execute
    方法在远程github架构中运行
    GitHubQuery
  • 源代码:


    使用WrapType转换在graphql工具fork()中直接支持


    请看

    OMG我喜欢这个黑客。你能详细说明一下3.0是如何支持它的吗?
    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()