该问题的摘要已在Github问题中进行了描述:
https://github.com/apollographql/apollo-server/issues/2794
简而言之,有没有一种方法可以实现Apollo Federation,以便网关本身具有自己的架构?
@apollo/gateway@0.6.5
@apollo/federation@0.6.2
当我实例化Apollo网关服务器时,我希望我应该能够合并来自联合服务的架构以及来自网关本身的架构。
网关服务器无法挂载/graphql
路由,因为它希望所有服务在此之前都正在运行。
在运行时,控制台上会显示以下错误:
POST /graphql 404 11.251 ms - 147
Encountered error when loading gateway-app at http://localhost:8000/graphql: invalid json response body at http://localhost:8000/graphql reason: Unexpected token < in JSON at position 0
[DEBUG] Fri Jun 07 2019 12:11:07 GMT-0400 (Eastern Daylight Time) apollo-gateway: Configuration loaded for Gateway
[DEBUG] Fri Jun 07 2019 12:11:07 GMT-0400 (Eastern Daylight Time) apollo-gateway: Composing schema from service list:
remove-svc
TypeError: schema.toConfig is not a function
at Object.composeServices (/gateway-app/node_modules/@apollo/federation/dist/composition/compose.js:191:67)
at Object.composeAndValidate (/gateway-app/node_modules/@apollo/federation/dist/composition/composeAndValidate.js:13:41)
at ApolloGateway.createSchema (/gateway-app/node_modules/@apollo/gateway/dist/index.js:90:47)
at ApolloGateway.<anonymous> (/gateway-app/node_modules/@apollo/gateway/dist/index.js:81:22)
at Generator.next (<anonymous>)
at fulfilled (/gateway-app/node_modules/@apollo/gateway/dist/index.js:4:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
const url = new URL(`redis://${REDIS_URL}:${REDIS_PORT}/${REDIS_DATABASE}`).toString()
const cache = new RedisCache({ url })
const context = ({ req }) => {
if (!(req.user || req.headers.authorization === ROUTE_AUTH)) {
throw new AuthenticationError('Not authenticated')
}
return { user: req.user, req: req }
}
try {
const loadGateway = async () => {
try {
const { schema, executor } = await gateway.load()
return { schema, executor }
} catch (err) {
console.error(err)
return null
}
}
const gateway = new ApolloGateway({
debug: process.env.ENV !== 'prod',
serviceList: [
{ name: 'gateway', url: `${GATEWAY_HOSTNAME}/graphql` },
{ name: 'remote-svc', url: `${REMOTE_SERVICE_HOSTNAME}/graphql` },
],
})
const { schema, executor } = loadGateway()
const server = new ApolloServer({
schema,
executor,
cache,
dataSources,
engine: { apiKey: ENGINE_API_KEY },
tracing: true,
context,
})
server.applyMiddleware({ app })
} catch (err) {
console.error(err)
}
答案 0 :(得分:1)
根据我的问题报告的原始错误,有两个错误。
从阿波罗出发:
在这里,根据您的服务命名,我们将得出一个结论:您是否正在尝试在端口8000上运行网关并将其同时作为联合服务对待?如果是这样,那是行不通的。网关必须先解析所有架构,然后才能构成最终架构。
14.2.0
(请参阅Github Issue #2825)。从GraphQL JS更改日志中:
toConfig旨在通过减少您需要编写和维护的代码量来帮助进行模式转换,例如通过graphql-tools重新创建类型。
当我使用graphql-voyager时,我创建了自己的intermidiate格式来进行模式转换。
此外,toConfig将防止添加新字段时发生的错误。例如,lexicographicSortSchema不尊重新添加的acceptValid和allowedLegacyNames字段。
将模式从网关迁移到其自己的联合服务并将核心GraphQL依赖关系更新为适当的版本后,我成功地实现了网关/联合模式。