我有两个节点服务器,两者均通过express-graphql
提供graphql服务。
我希望第一台服务器将其模式与第二台服务器的模式进行缝合。
我已按照以下说明进行操作:https://www.apollographql.com/docs/graphql-tools/remote-schemas
我正在使用graphql-tools。
我能够从我的第二个节点服务器上检索该模式,但是一旦我尝试打印它(仅用console.log
),就会出现此错误:
Uncaught exception TypeError: schema.getDirectives is not a function
at printFilteredSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql/utilities/schemaPrinter.js:61:27)
at Object.printSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql/utilities/schemaPrinter.js:47:10)
at makeRemoteExecutableSchema (/Users/cchabert/git-repo/client-configuration-api/node_modules/graphql-tools/dist/stitching/makeRemoteExecutableSchema.js:60:30)
at schema (/Users/cchabert/git-repo/client-configuration-api/app/api/schema.js:68:24)
at module.exports (/Users/cchabert/git-repo/client-configuration-api/app/routes.js:102:13)
at Object.<anonymous> (/Users/cchabert/git-repo/client-configuration-api/app/index.js:15:20)
at Module._compile (internal/modules/cjs/loader.js:799:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:810:10)
at Module.load (internal/modules/cjs/loader.js:666:32)
at tryModuleLoad (internal/modules/cjs/loader.js:606:12)
at Function.Module._load (internal/modules/cjs/loader.js:598:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:862:12)
at internal/main/run_main_module.js:21:11
这会导致服务器崩溃,但我也能够以某种方式看到该模式:
received schema: {"_queryType":"Query","_mutationType":"Mutation",
"_subscriptionType":null,"_directives":["@skip","@include","@deprecated"],
"_typeMap":{"Query":"Query","String":"String","Client":"Client",
"ID":"ID","DateTime":"DateTime",[...]}
我确实在远程模式定义中看到了_directives
字段,但是文档对于如何处理这个问题并不清楚。
我也浏览了graphql-tools
回购的github问题,但找不到任何东西。
这是一个代码段:
const {
addMockFunctionsToSchema,
makeExecutableSchema,
makeRemoteExecutableSchema,
introspectSchema,
mergeSchemas
} = require('graphql-tools')
const _ = require('lodash)
const { createHttpLink } = require('apollo-link-http')
const fetch = require('node-fetch')
[..]
const customFetch = (uri, options = {}) => {
const httpOptions = _.merge(options, {
headers: {
'Content-type': 'application/json'
}
})
return fetch(uri, httpOptions)
}
function schema() {
const Query = `
type Query {
_empty: String
}
type Mutation {
_empty: String
}
`
const resolvers = {}
const mocks = {}
const localSchema = makeExecutableSchema({
typeDefs: [Query, [...]],
resolvers: [resolvers, [...]]
}) // by itself this schema works without any issues
const mergedMocks = _.merge(mocks, [...])
addMockFunctionsToSchema({
schema: localSchema,
mocks: mergedMocks,
preserveResolvers: true
})
const infoApiLink = createHttpLink({ uri, fetch: customFetch })
const remoteSchema = makeRemoteExecutableSchema({
schema: introspectSchema(infoApiLink).then(remoteSchema => {
console.log('received schema: ', JSON.stringify(remoteSchema))
return remoteSchema
}),
link: infoApiLink
})
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
}
module.exports = {
schema
}
中提到的Promises(不使用异步/等待)来完成这项工作
欢迎提出任何建议。
答案 0 :(得分:1)
makeRemoteExecutableSchema
应该传递一个GraphQLSchema
的实例,但是您没有这样做-您正在传递的是一个Promise,它将解决为GraphQLSchema
,这是行不通的。调用introspectSchema
时,它必须进行自省调用,该调用是异步完成的。它返回一个Promise,该Promise解析为结果的GraphQLSchema
对象。我们需要使用await
或then
来获得该值,然后可以根据需要使用它。
无需异步/等待的不必要的混乱方式:
function schema () {
// ... rest of your code
return introspectSchema(infoApiLink).then(schema => {
const remoteSchema = makeRemoteExecutableSchema({ schema, link: infoApiLink })
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
})
}
或使用异步/等待:
async function schema () {
// ... rest of your code
const schema = await introspectSchema(infoApiLink)
const remoteSchema = makeRemoteExecutableSchema({ schema, link: infoApiLink })
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
}
无论哪种方式,请记住通过调用schema
函数,您将返回一个Promise,它将解析为mergeSchemas返回的值。因此,在导入函数,调用函数并直接使用结果之前,您将不得不再次使用then
或await
来获取Promise首先解析的值:
import { schema } from './some-module'
schema()
.then(schema => {
const server = new ApolloServer({ schema })
server.listen()
})
.catch(error => {
// handle the Promise rejecting
})