使用 graphql-tools 打印远程模式时出现 getDirectives 错误



我有两个节点服务器,都通过express-graphql为 graphql 提供服务。 我希望我的第一台服务器将其架构与第二台服务器的架构缝合在一起。

我已按照以下说明进行操作: https://www.apollographql.com/docs/graphql-tools/remote-schemas

因为我正在使用 graphql 工具。

我能够从我的第二个节点服务器检索架构,但是一旦我尝试打印它(仅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
}

我还想仅使用 https://github.com/apollographql/graphql-tools/blob/master/docs/source/remote-schemas.md#--introspectschemafetcher-context 中提到的承诺(没有异步/等待)来完成这项工作

欢迎任何建议。

makeRemoteExecutableSchema应该传递一个GraphQLSchema的实例,但你没有这样做 - 你正在做的是传递给它一个承诺,该承诺将解析为一个GraphQLSchema,这是行不通的。当你调用introspectSchema时,它必须进行内省调用,这是异步完成的。它返回一个 Promise,该承诺解析为生成的GraphQLSchema对象。我们需要使用awaitthen来获取该值,然后我们可以根据需要使用它。

没有异步/等待的不必要的混乱方式:

function schema () {
// ... rest of your code
return introspectSchema(infoApiLink).then(schema => {
const remoteSchema = makeRemoteExecutableSchema({ schema, link: infoApiLink })
return mergeSchemas({ schemas: [localSchema, remoteSchema] })
})
}

或者使用 async/await:

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,该 Promise 将解析为 mergeSchema 返回的值。因此,在您可以导入函数、调用它并直接使用结果之前,您将再次使用thenawait来获取 Promise 解析为的值:

import { schema } from './some-module'
schema()
.then(schema => {
const server = new ApolloServer({ schema })
server.listen()
})
.catch(error => {
// handle the Promise rejecting
})

最新更新