Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql - Apollo Server - Hot update schema

I am looking to hot reload my GraphQL schema on my apollo-server-express server. Anyone would have any idea about how to do that?

I'm currently using schemas stitching to gather multiple API's schemas, however I do not want to have to restart my application every time one of these schema changes.

I've tried to look for the express route and remove it but that dit not work, I also tried to call graphQLExpress() again on the same route and that did not update it.

Thanks for your help!

like image 339
Antoine Savignac Avatar asked Jan 15 '18 10:01

Antoine Savignac


2 Answers

I dug into the graphqlExpress call a bit, it's fairly lightweight and just returns a middleware function using the provided schema. I was able to ensure it's always using my latest schema by wrapping it:

let schema = createSchema();
app.use('/graphql', bodyParser.json(), function(req, res, next) {
  // ensure latest schema is always passed in, we'll reload it automatically
  const graphql = graphqlExpress({ schema });
  graphql(req, res, next);
});

I'm working with local / static schema at the moment, so a file watcher allows me to update schema here as needed.

like image 87
nickwesselman Avatar answered Oct 16 '22 04:10

nickwesselman


I managed to get it done by removing the express route and then re-creating it. But actually I discovered that because makeRemoteExecutableSchema is sending the introspection query at every request, you actually don't need to update your schema, it gets updated by itself. That does not imply Graphiql though.

like image 45
Antoine Savignac Avatar answered Oct 16 '22 02:10

Antoine Savignac