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!

This talk has been presented at GraphQL Galaxy 2021, check out the latest edition of this Tech Conference.

FAQ

Maxime Beunier is a developer advocate at MongoDB, based in Paris.

Maxime Beunier presented on how to build a serverless GraphQL API in less than seven minutes at GraphQL Galaxy.

Maxime Beunier was inspired by John Hopkins University and their charts to build charts and APIs to make COVID data available to everyone.

Maxime Beunier used the mongoinport tool to import CSV files into MongoDB and wrote Python scripts to adjust the data schema and integrate additional information.

MongoDB Atlas is a cloud database service where you can host your MongoDB databases in the cloud of your choice.

Realm in MongoDB Atlas allows users to build serverless applications that operate on top of their MongoDB Atlas clusters.

Users can access the GraphQL API by generating a schema, configuring user permissions, and using an authentication token for queries.

Generating a schema in GraphQL is crucial as it defines how the API will understand and interact with the data, ensuring the API operates correctly.

Maxime used an anonymous user authentication method to simplify access for demonstration purposes, allowing users to interact with the API without complex authentication processes.

Maxime improved the schema by transforming date keys to values, using proper date formats, and enhancing geolocation data handling for better support in MongoDB charts and indexing.

Maxime Beugnet
Maxime Beugnet
8 min
10 Dec, 2021

Comments

Sign in or register to post your comment.

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.

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

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Get rid of your API schemas with tRPC
React Day Berlin 2022React Day Berlin 2022
29 min
Get rid of your API schemas with tRPC
Today's Talk introduces TRPC, a library that eliminates the need for code generation and provides type safety and better collaboration between front-end and back-end. TRPC is demonstrated in a Next JS application integrated with Prisma, allowing for easy implementation and interaction with the database. The library allows for seamless usage in the client, with automatic procedure renaming and the ability to call methods without generating types. TRPC's client-server interaction is based on HTTP requests and allows for easy debugging and tracing. The library also provides runtime type check and validation using Zod.
You Don’t Know How to SSR
DevOps.js Conf 2024DevOps.js Conf 2024
23 min
You Don’t Know How to SSR
The Talk covers the speaker's personal journey into server-side rendering (SSR) and the evolution of web development frameworks. It explores the use of jQuery for animations in SSR, the challenges faced in integrating React with Umbraco, and the creation of a custom SSR framework. The Talk also discusses the benefits of Next.js and the use of serverless artifacts for deployment. Finally, it highlights the features of Astro, including its function per route capability.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
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
AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
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.
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
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 for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
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.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
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.