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
谢谢。
答案 0 :(得分:6)
编辑:我相信现在可以使用新的graphql-tools 3.0了!
https://dev-blog.apollodata.com/the-next-generation-of-schema-stitching-2716b3b259c0
原始回答:
这是一个解决方案(黑客?)我想出来了,但可能有更好的方法来做到这一点:
introspectSchema
和makeRemoteExecutableSchema
printSchema
Query
和Mutation
重命名为其他内容,例如: GitHubQuery
和GitHubMutation
github
GitHubQuery
字段创建根查询typedef
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()
答案 1 :(得分:0)
使用WrapType转换直接在graphql-tools-fork(https://www.npmjs.com/package/graphql-tools-fork)中受支持。