Load / reload GQL schema at run time

Can we load - reload GrqphQL schema at runtime. We need to introduce dynamic model extension a feature to our service -

  • Data would follow dynamic json-schema format

  • New schema format can be introduced at run time

  • Data must be interpreted run time by generating at GQL schema

You’d probably need to have your service poll your JSON schema at runtime, convert that to a schema that Apollo can load (possibly via @graphql-tools), and then restart itself with the new schema.

I don’t think you can change the schema while it’s actually running, so if you don’t want the service to be down for a second while it restarts, you’d probably need to listen on multiple ports like this:

// very much pseudocode
// index.ts

const port1 = 8080;
const port2 = 8081;

const initialSchema = getNewSchema();
let currentServer = new ApolloServer({ ..., schema: initialSchema });
let nextServer;

(async () => {
  await currentServer.listen(port1)

setInterval(60000, async () => {
  const nextSchema = await getNewSchema();
  nextServer = new ApolloServer({ schema: nextSchema });
  await nextServer.listen(port2);
  await currentServer.stop();
  currentServer = new ApolloServer({ schema: nextSchema });
  await currentServer.listen(port1);
  await nextServer.stop();

You would have to listen to requests on both ports, but you could theoretically hotswap this way.