visit
Running the user’s server
For simplicity and to allow for some customization, we created a GraphQuill config file to allow users to specify the path to their server file and the exposed port number. From there we checked to see if their server was already running, and spawned a Node child process to start it up if it wasn’t. One other thing we added for convenience is a command to create the GraphQuill config file with defaults that we’ve set, and comments to help users decide how to configure the options.Writing queries inline
Once the server was up and running, we needed to figure out how to identify users’ queries within their code. Sadly, GraphQL queries aren’t JavaScript so we had to come up with a way to let engineers write their queries inline with the rest of their code without triggering an error. We ultimately created an empty graphQuill function definition that would allow users to invoke it by passing in their queries as arguments. GraphQuill will automatically define the graphquill function in the file being used if it hasn’t already been defined to avoid linting or runtime errors.Parsing out queries
Next, we needed to separate queries from the rest of the code. To accomplish this, we parsed the code for instances of the string “graphquill” and then used a stack to track open parentheses to identify where queries began and ended. After parsing out all queries within a file, to avoid sending requests that would error out in the API, we validated that all braces, brackets, and parentheses were balanced using another stack.Rate limiting
With the ability to parse queries out of a file and validate them, we needed to determine when to parse, validate, and send them to the API. Thankfully, the offers multiple ways to do this. Quokka listens for individual key presses, but since we’re actually querying an API that is presumably pulling data from a database, we decided to listen for file saves to give users some control in when the queries were sent. We were able to do this with the event. As a last guard against overloading the server/database (and knowing the key-mashing habits of many developers), we debounced our query function so that it didn’t send a new request if a file was saved rapidly in succession.Querying the API and handling the response
Next came actually sending the queries off to the API and printing the responses to the GraphQuill output channel in VS Code. Here we leveraged Node’s async/await syntax to send concurrent requests and wait for all responses to resolve before printing to the output channel.Overall, this was a really fun project to work on, and we hope it proves to be a useful tool for many an engineer. The code is open source on , and can be downloaded from the . We just released a beta version and are looking for people to test it out and submit any issues they find so we can continue to make it a better tool.