Building a Serverless GraphQL API in 7 Minutes


At the beginning of the COVID-19 pandemic, Johns Hopkins University worked on gathering and refining data from multiple sources and provided the world with a Github repository and a bunch of CSV files. The Problem? It’s nearly impossible to build anything from raw data like this.

In this live coding session, MongoDB Developer Advocate Maxime Beugnet will show you how to build a serverless GraphQL API based on this dataset using Python and the MongoDB Data Platform. We are finally going to put serverless and the MongoDB generous free tier to good use!


♪♪ ♪♪ ♪♪ ♪♪ Hi, everybody. I'm Maxime Bagné. I'm a developer advocate at mongodb, based in Paris. I'm super happy to be talking today at graphql Galaxy. And today I want to show you how to build a serverless graphql api in less than seven minutes. So buckle up, because it's going to be very, very quick. So at the very beginning of COVID, everybody knows it was like about two years ago. My idea was to start to build some charts and some APIs to show the data and make everybody aware and make the data available to everybody. So I ended up building a REST api, a graphql api, and some mongodb Atlas charts, as you can see here. And you will find the links to all of that in the last slide of this presentation. So I was inspired by John Hopkins University and their charts. So they actually provide, in the term of use here, a GitHub repository. This repository, sorry, looks like this. It's just a very simple repository with some CSV files. As you can see in the time series folders, they provide actually five files. So the global one, which is for all the countries, and some specific ones for the US, with more details for the US. The files look all the same. They look like this. So basically, state, province, country, region, latitude, longitude, and one colon for each date. And so if you import those files into mongodb using, for example, the Mongo import tool that supports CSV, you will end up with an absolute terrible schema. So you don't want to do that. So instead, you want to, for example, work with dates like this. Instead of being a key, you want that to be a value, and preferably an user date. And also, you want to work with geosystem data, for example, for the localization. So it's more well-supported in mongodb charts, for example, and for 2D indexes or these kind of things.

So there is also another file called a lookup table where you have more information about the countries, like, for example, the population and some country codes, et cetera. So I wanted also this in the mix. So I ended up doing some Python scripts, which is also available in one of the links. And in the end, I ended up with some documents like this, where you have, like, for each country, so you have a bunch of fields to describe where the data is related to, one date, and the values for that particular date. So in the end, I have this one country, one place, one date for one particular day, and the numbers for that particular day. So let's build a graphql api now built on this. So as you can see, I ended up with, like, those five collections here at the bottom, and I'm going to work with the global one just to show you how to do that. So let's hack. So to do this, I'm moving now into mongodb Atlas, which is where you can host your mongodb databases in the cloud of your choice. And I have this cluster in production here called COVID-19. So I'm going to move now to Realm, which is the place where you can build your application, your serverless application that's going to stay and live on top of your mongodb Atlas cluster. I'm going to create a new app that's one in production. As you can see, I'm going to call this Galaxy, for example. I'm going to link this to the actual cluster in production, and I'm going to be deploying this in Ireland because I want this to be in Ireland because the cluster is also in Ireland, so, like, they are both in the same location. So as you can see here, you have a bunch of options, and I have triggers. I have, like, sync as well if you want to sync data across mobile devices, for example, but I'm going to stick with graphql. I'm going to click here on Generate Schema because graphql relies on schema, so I need to generate a schema, which drives me back to rules. In rules, I can select my collection here, global. I can say that a user can read all data because I'm doing a public api here. I'm going to say configure. Yes, I want to save. Got it. I'm going to generate schema. I'm going to click here on Generate Schema just for the global collection, and I'm going to say Generate Schema. So I have now a schema that has been generated.

I see it's missing the state, so I can copy-paste the country, for example, and I can also modify as I want the schema. I can say Save, and that's it. I'm ready to work. So if I go back now to graphql, as you can see, I have a webhook, and I have a query, and I can send a query, and if I put globals with plural, I get multiple documents. I get an array of documents. So as you can see, it's working.

To use this, I need a token, so I need to authenticate my users, so I can use this outside of this graphql interface. So I can use the authentication here. As you can see, I have a bunch of options, so I'm going to go with the easy one for today, but any of them could work. So I'm going to go with the anonymous user. I'm going to activate this, save the draft, and I'm going to deploy my serverless application here at the top. I need to scroll all the way down, say Save, and Deploy.

So now if I come back in here, I have prepared already a few queries with one that looks like this. So I'm querying for France, for the mainland, and for some dates back in May. So let me just grab this app ID, which is the application ID of my serverless app now in Realm. I need to copy-paste this here at the top. And I think I'm missing this. Oh, no. I need to copy-paste everywhere, so I need to copy-paste here. So here I can get an anonymous user. Oops. Cool. As you can see, now I have a token. This token can be copy-pasted here. And now I can run my query here.

And as you can see, I have my answer.

It's zoomed a bit, but yeah.

As you can see, I have my answer, and I asked for daily confirmed cases and for the dates, so that's what I get.

So hopefully you like this demo.

Thank you very much for listening.

And you have all the links now that I promised, a bunch of blog posts, and also the charts that I presented at the beginning.

Thank you very much, and see you next time.


[♪ Music playing ♪ [♪ Music playing ends ♪

8 min
10 Dec, 2021

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Workshops on related topic