Graphql 如何在另一个查询字段中包装远程架构?
ApolloGraphql 如何在另一个查询字段中包装远程架构?,graphql,apollo,Graphql,Apollo,Apollographql工具现在有了模式拼接,这很好。 我想合并多个端点以生成类似的模式,如下所示: 查询{ github:{…}#github架构 twitter:{…}#twitter模式 myOwnGraphqlSchema:{…} } 最好的方法是什么 GitHub问题: 请在此分叉进行测试: 多谢各位 编辑:我相信这在新的graphql工具3.0中是可能的 原始答案: 我想出了一个解决方案(黑客?),但可能有更好的方法: 使用introspectSchema和makeRemot
graphql工具
现在有了模式拼接,这很好。
我想合并多个端点以生成类似的模式,如下所示:
查询{
github:{…}#github架构
twitter:{…}#twitter模式
myOwnGraphqlSchema:{…}
}
最好的方法是什么
GitHub问题:
请在此分叉进行测试:
多谢各位
编辑:我相信这在新的graphql工具3.0中是可能的
原始答案: 我想出了一个解决方案(黑客?),但可能有更好的方法:
introspectSchema
和makeRemoteExecutableSchema
printSchema
Query
和Mutation
重命名为其他名称,例如GitHubQuery
和GitHubMutation
GitHubQuery
的github
字段创建根查询typedefgithub
解析器,该解析器使用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()