This is a follow-up issue (source: https://github.com/apollographql/apollo-server/issues/2794).
On start, gateway server can't successfully load a federated service. Returns the following error:
[DEBUG] Tue Jun 11 2019 12:36:04 GMT-0400 (Eastern Daylight Time) apollo-gateway: Loading configuration for Gateway
[DEBUG] Tue Jun 11 2019 12:36:04 GMT-0400 (Eastern Daylight Time) apollo-gateway: Configuration loaded for Gateway
[DEBUG] Tue Jun 11 2019 12:36:04 GMT-0400 (Eastern Daylight Time) apollo-gateway: Composing schema from service list:
remote-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)
Here is the source code:
const startGateway = async () => {
const gateway = new ApolloGateway({
debug: true,
serviceList: [
{ name: 'remote-svc', url: `${REMOTE_SVC}/graphql` },
],
})
let server, gatewayConfig
try {
gatewayConfig = await gateway.load() // this line throws the TypeError
server = new ApolloServer({
...gatewayConfig,
})
server.applyMiddleware({ app })
} catch (err) {
console.error(err)
}
}
Based on this implementation, I would expect something to be wrong with the federated service. But I am not seeing much of an issue in its console output or in the GraphQL playground. The following source code is from the federated service. It returns no error output.
const schema = buildFederatedSchema([{ typeDefs, resolvers }])
try {
const server = new ApolloServer({
schema,
debug: true,
dataSources,
})
server.applyMiddleware({ app })
} catch (err) {
console.error(err)
throw err
}
Is there anything about my gateway implementation or my federated service implementation that would cause this schema.toConfig TypeError to be thrown?
For the remote-svc federated service, I also ran it as a vanilla Apollo Server.
const schema = makeExecutableSchema({ typeDefs, resolvers })
try {
const server = new ApolloServer({
schema,
debug: true,
dataSources,
})
server.applyMiddleware({ app })
} catch (err) {
console.error(err)
throw err
}
Querying in GraphQL playground works the same as before. Effectively able to query for placeholder types I defined in my schema.
I suspect the issue lies in the implementation of the gateway server.
In case this is a version compatibility issue, I'm including the package.json dependencies for both.
"name": "gateway-app",
...
"dependencies": {
"@apollo/gateway": "^0.6.5",
"apollo-cache-inmemory": "^1.6.2",
"apollo-datasource-rest": "^0.5.0",
"apollo-link-context": "^1.0.17",
"apollo-link-http": "^1.5.14",
"apollo-server": "^2.6.2",
"apollo-server-cache-redis": "^1.0.0",
"apollo-server-express": "^2.6.2",
...
}
"name": "remote-svc",
...
"dependencies": {
"@apollo/federation": "^0.6.2",
"apollo-cache-inmemory": "^1.6.2",
"apollo-datasource-rest": "^0.5.0",
"apollo-link-context": "^1.0.17",
"apollo-link-http": "^1.5.14",
"apollo-server": "^2.6.2",
"apollo-server-cache-redis": "^1.0.0",
"apollo-server-express": "^2.6.2",
...
}
I've identified the issue. This is not an Apollo issue. It is a graphql versioning issue.
https://github.com/graphql/graphql-js/releases/tag/v14.2.0
PR https://github.com/graphql/graphql-js/pull/1331 adds the toConfig method. From the author:
toConfig is intended to help with schema transformation by minimizing the amount of code you need write and maintain, e.g. recreateType from graphql-tools.
When I worked on graphql-voyager I created my own intermidiate format just to do schema transformations.
Moreover, toConfig will prevent bugs that occur when a new field is added. For example, lexicographicSortSchema doesn't respect newly added assumeValid and allowedLegacyNames fields.
After updating my graphql dependency from 14.1.1 to 14.3.1, the error is now gone.
Solution above helped. Thank you.
const schema = loadSchemaSync(path.join(__dirname, './modules/**/schema.graphql'), {
loaders: [new GraphQLFileLoader()]
});
"graphql": "14.3.1"
Most helpful comment
I've identified the issue. This is not an Apollo issue. It is a
graphqlversioning issue.https://github.com/graphql/graphql-js/releases/tag/v14.2.0
PR https://github.com/graphql/graphql-js/pull/1331 adds the toConfig method. From the author:
After updating my
graphqldependency from14.1.1to14.3.1, the error is now gone.