I'm trying to get apollo gateway to run in google's cloud functions (particularly through firebase). Running apollo server is super easy on cloud functions with the following code
const functions = require("firebase-functions");
const { ApolloServer } = require("apollo-server-express");
var app = require("express")();
const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }])
});
server.applyMiddleware({ app });
exports.apollo = functions.https.onRequest(app);
That all works nice and dandy because there's no async functions to wait on. But with apollo gateway, we do have an async function to wait on before the server can start listening.
The way it works with just expressjs is this:
const { ApolloServer } = require("apollo-server-express");
var app = require("express")();
const gateway = new ApolloGateway({
serviceList: [{ name: "clients", url:"http://localhost:5000/apollo/graphql" }]
});
(async () => {
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor });
server.listen().then(({ url }) => {
console.log(` Server ready at ${url}`);
});
})();
So now the question becomes how do I execute that in a cloud function? One idea I had was to pass the express app to the firebase functions, and THEN do all the async stuff to initialize the listening for federation. But that doesnt work :(. Any ideas?
const functions = require("firebase-functions");
const { ApolloServer } = require("apollo-server-express");
var app = require("express")();
const gateway = new ApolloGateway({
serviceList: [{ name: "clients", url: "http://localhost:5000/apollo/graphql" }]
});
exports.apollo = functions.https.onRequest(app);
(async () => {
const { schema, executor } = await gateway.load();
const server = new ApolloServer({ schema, executor });
server.applyMiddleware({ app });
})()