Building a Serverless GraphQL API in 7 Minutes

Rate this content
Bookmark

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!

8 min
10 Dec, 2021

Video Summary and Transcription

Maxime Beunier, a developer advocate at MongoDB, demonstrates how to build a serverless GraphQL API using MongoDB Atlas and Realm. He creates a new app called Galaxy, links it to the production cluster, and deploys it in Ireland. He then shows how to authenticate users and obtain a token to access the GraphQL API. The demo showcases the ease and speed of building a serverless GraphQL API and provides all the necessary links for further exploration.

Available in Español

1. Building a Serverless GraphQL API

Short description:

Hi, everybody. I'm Maxime Beunier, a developer advocate at MongoDB, based in Paris. Today, I want to show you how to build a serverless GraphQL API in less than seven minutes. I was inspired by John Hopkins University and their charts, so I ended up building a REST API, a GraphQL API, and some MongoDB Atlas charts. Let's build a GraphQL API now on MongoDB Atlas and Realm. I'm going to create a new app called Galaxy, link it to the production cluster, and deploy it in Ireland. I'll generate a schema and modify it as needed.

♪♪ ♪♪ ♪♪ Hi, everybody. I'm Maxime Beunier. 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 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 folder, they provide actually five files, so the global one, which is like for all the countries, and some specific one for the US with more details for the US. The files look all the same, they look like this, so basically, you know, 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 mongoinport 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 a date, and also, you want to work with, like, geosystem data, for example, for the localization, so it's more well-supported in MongoDB charts, for example, and for, like, 2D indexes or these kind of things. So there's also another file called lookup table where you have more information about the countries, like, for example, the population and, you know, 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, so where you have, like, for each country, so you have a bunch of fields, you know, to describe where the data is related to, one date, and the values for that particular date, right? So in the end, I have this, you know, one country, one place, one date for one particular date, and, you know, 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 gonna 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 gonna move now to Realm, which is the place where you can build your application, your serverless application, that's gonna stay and, you know, live on top of your MongoDB Atlas cluster. I'm gonna create a new app that's one in production. As you can see, I'm gonna call this Galaxy, for example. I'm gonna link this to the actual cluster in production, and I'm gonna 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 a sync as well, if you want to sync data across mobile devices, for example, but I'm gonna stick with the GraphQL. I'm gonna click here on Generate Schema because GraphQL relies on schema, so I need to generate a schema which drove 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. Say configure. Yes, I want to save. Got it. I'm gonna generate schema. I'm gonna click here on Generate Schema just for the global collection, and I'm gonna 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.

2. Using GraphQL and Authenticating Users

Short description:

I can say Save, and that's it, I'm ready to work. To use this, I need a token. So I need to authenticate my users, so I can use this outside of this GraphQL interface. I'm gonna go with the anonymous user. I'm gonna activate this, save the draft, and I'm gonna deploy my serverless application. As you can see, now I have a token. This token can be copy pasted here, and now I can run my query. Hopefully you like this demo. Thank you very much for listening. And you have all the links now that I promised.

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 Global 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 gonna go with easy one for today, but any of them could work. So I'm gonna go with the anonymous user. I'm gonna activate this, save the draft, and I'm gonna deploy my serverless application here at the top. So I need to scroll all the way down. Save and deploying.

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 date, you know, 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 it has 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.

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

GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
React Day Berlin 2022React Day Berlin 2022
29 min
Get rid of your API schemas with tRPC
Do you know we can replace API schemas with a lightweight and type-safe library? With tRPC you can easily replace GraphQL or REST with inferred shapes without schemas or code generation. In this talk we will understand the benefit of tRPC and how apply it in a NextJs application. If you want reduce your project complexity you can't miss this talk.

Workshops on related topic

GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Featured WorkshopFree
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura