Writing a Graphql NodeJS Server that fetches it’s schema from Git
Posted by Simeón Yordanov
A few months ago, I was asked to make some changes in TravelgateX's API GraphQL schema. After making the changes and installing our API in the production environment, I started thinking of ways that these GraphQL schema changes can be made more efficiently and using a mix of different tools and technologies that our company uses. A collegue of mine suggested that the schema can be loaded in a Github repository and then, once the application is started, it can be fetched and loaded as the current versión of the GraphQL schema used by the API.
I don’t know about you but when someone suggests an idea I find intriguing, I’m the type of person that just becomes obsessed of making the dream a reality. And so I did! In the following article I’m going to show you how to build an server in NodeJS that recovers the API GraphQL schema it uses from a Git repository. I’m going to explain the different parts of the project and in the end show the final result.
- NodeJS (version 18.0 or above) installed.
- An active Git account and Git installed in your machine.
- Preferably an IDE like VS Code where you can run and debug your code.
Fetching the GraphQL Schema from Git
The schema fetch is relatively easy. We are going to use the git commands you probably already know (git pull, git clone, etc) launching them from our main application when we start the server. To do this we will have to launch an internal child process (another thread) that will launch these git commands. For this purpose we will use the following node module: