Building a GraphQL-native serverless backend with Fauna

Rate this content
Bookmark

Welcome to Fauna! This workshop helps GraphQL developers build performant applications with Fauna that scale to any size userbase. You start with the basics, using only the GraphQL playground in the Fauna dashboard, then build a complete full-stack application with Next.js, adding functionality as you go along.

In the first section, Getting started with Fauna, you learn how Fauna automatically creates queries, mutations, and other resources based on your GraphQL schema. You learn how to accomplish common tasks with GraphQL, how to use the Fauna Query Language (FQL) to perform more advanced tasks.

In the second section, Building with Fauna, you learn how Fauna automatically creates queries, mutations, and other resources based on your GraphQL schema. You learn how to accomplish common tasks with GraphQL, how to use the Fauna Query Language (FQL) to perform more advanced tasks.

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

FAQ

Fauna is a serverless database that natively supports GraphQL, allowing you to create and manage databases with GraphQL queries and mutations. It auto-generates queries and mutations based on your GraphQL schema, offering a seamless way to interact with your data.

To get started with Fauna, you need to create a Fauna account, which you can do for free without a credit card. Once you have an account, you can create a new database, upload your GraphQL schema, and start interacting with your data using the auto-generated queries and mutations.

Fauna's serverless architecture offers key features such as no infrastructure concerns, automatic scaling, high availability and security, and a pay-for-value pricing model. This means you don't need to provision servers or manage infrastructure, and you only pay for the actual access to your data.

Custom resolvers in Fauna are created using User-Defined Functions (UDFs) in Fauna Query Language (FQL). You define these functions in the Fauna dashboard and then map them to GraphQL queries or mutations using the resolver keyword in your schema.

Fauna handles user authentication through User-Defined Functions (UDFs). You can create custom functions for registering and logging in users, which handle credentials securely. Fauna also supports integration with Auth0, Okta, and other authentication services.

The principle of least privilege in Fauna ensures that users have the minimum level of access required to perform their tasks. This is implemented through roles and attribute-based access control (ABAC), allowing fine-grained control over what data and functions each user can access.

Fauna ensures high availability and redundancy by spreading traffic across multiple regions. In case of an outage in one region, traffic fails over to other regions without any intervention required from your application. Fauna also maintains strong transaction consistency, ensuring you always have the most recent data.

You can integrate Fauna with your front-end application using Apollo Client for GraphQL. You need to set up Apollo Client with Fauna's GraphQL endpoint, manage authentication through session cookies, and query data using the client. Fauna's flexible API makes it easy to use with various front-end frameworks like Next.js, React, and Vue.

Fauna Query Language (FQL) is a proprietary language used to interact with Fauna databases. It follows a functional programming paradigm and allows for complex data manipulation and business logic to be executed within the database layer. FQL is similar to JavaScript and other functional languages like Scala.

You can find resources to learn more about Fauna and FQL on the Fauna documentation site, Fauna Labs on GitHub, and through various sample applications and repositories. Additionally, Fauna offers a Discord channel and Twitter support for any questions you may have.

Rob Sutter
Rob Sutter
Shadid Haque
Shadid Haque
143 min
08 Dec, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's workshop focused on serverless development with Fauna. The workshop covered topics such as building a client-serverless model, exploring data access patterns, and creating a serverless application. It also delved into user authentication, session management, and querying data in the front end. The workshop highlighted the power of Fauna for data manipulation and the flexibility it offers for building applications with fine-grained access control.

1. Introduction to Serverless

Short description:

Thank you for joining us. Today, we'll be discussing serverless and its benefits. Serverless eliminates infrastructure concerns, automatically scales, ensures high availability and security, and offers value-based pricing. With serverless, you can achieve zero downtime and satisfy compliance requirements. Fauna, a serverless database, provides strong transactions, fast performance, and works well with next-generation serverless providers. It's a great fit for front-end developers and offers a smart API layer for easy integration. Let's dive into the workshop and learn more about serverless!

Thank you for joining us. And now let's get started. Thanks for joining us today. We've been waiting to get started. My name's Rob. I'm one of the people who's gonna be walking you through the workshop today. I want to make sure that you're all in the Discord server with us. I'm going to go ahead and drop the link to the slides that I'll use in there now, and we'll also give you a link to the workshop later. so that you can go back and do this, either yourself, review it yourself or take it to your teams to do it so that you can run through it on your own. It is publicly available, but the differences will be here today to answer your questions as we go and to walk through it with you.

So without any further ado here, I'm just going to dive right into the workshop. So I'm going to go ahead and turn it over to Rob to give you a little bit of a tour of the discovery space. And then should give you some resources, and then Shadid is going to take you through the workshop.

So when we talk about serverless, we talk about four key things. These are typically the same for all your different serverless providers. There. First, there's no infrastructure concerns, so you don't need to provision clusters, servers, patch operating systems, all of that stuff. You don't need to worry about that. The provider takes care of it for you. And access is just provided via an API or or as a service. Second, it automatically scales, so you don't need to think about provisioning. If you have zero users, it should scale down to zero and you shouldn't be paying for any of that compute. If you have a burst of traffic, it should scale up immediately. And you shouldn't have to worry about any of that. And that's what Fonda gives you for your database, similar to what function as a service providers give you for your compute. Third, it should be highly available and secure. And so these aren't things that you should have to worry about. Right. If there's an outage somewhere, the provider should be taking care of keeping you up and giving you access to your data. And that's what we do at Fonda. And then finally, it should be paid for value. If you have no access to your database, you shouldn't pay for access during that period. If you store, you know, 10 gigs, then that should be 10 times as expensive as storing one gig. Right. And, of course, there's a free tier that we give you to help you with that. But the principle there is you pay only for the actual access. You don't pay for hours of running Fonda or anything like that. If you have a lot of access and those reads and writes go up, if you have zero, it scales back down. And that scale to zero concept is really important in serverless. Some of the benefits real quick of using a serverless back in like Fonda, you should really be able to achieve zero downtime, at least in terms of your your database. And this helps you like yesterday, there was this big outage that everybody knows about. If you were architected on a single region and it happened to be that one that went down, then off you go. Right. There's no way to avoid that. But Fonda has spread across and above these regions in a way that all of our traffic failed over to the other regions without your application having to do anything. So as far as I know, we didn't have any reported outages yesterday, even though we were dependent on one of the on the region that went down and we have zero reported cases of data loss. So this is a really big advantage for you as a front end developer who just wants to store and get your data back. So all these things that you don't have to think about. So this can also help you satisfy compliance requirements. This is with like region groups that we offer, and we're not going to go into this today because the point of today is to build. It can help you reduce latency with phone number because your users are accessing the closest endpoint. We'll get to that later. And it's a minimal increase in spending to have all that redundancy. You're not running two copies of your database, right? You're running your traffic spread, which is the same total cost as opposed to doubling the cost or tripling the cost to have it in multiple locations. We also do this without the problem that you see on the side of eventual consistency, because Fauna has strong transactions. So instead of getting in a case where you have to check and make sure that you have the most recent data or use locks or anything like that, you just write to a single endpoint and your users read from the endpoint closest to them because everything is a strong transaction. You're just always going to have the most recent data there. Other serverless databases don't give you this guarantee. I mentioned earlier, it's also fast. Right. These are the actual performance numbers. You can confirm these for yourself by going to status.fauna.com. You're looking at single digit, millisecond reads and double digit, millisecond, strong, strongly consistent transactional writes. So it's really performant for your applications, which is important when you're creating front end experiences. Right. Fauna works with next generation serverless providers very well. We're not going to get into deployment today, but if you're already running your app on something like CloudFlare pages or on Purcell, it's a great fit and we have lots of material to help you integrate with that. And again, this is forms sort of a smart API later over Fauna. The GraphQL service does the same thing for you. So you don't need to worry about where to route your traffic.

2. Building a Client-Serverless Model with UDFs

Short description:

You'll learn how to build a client-serverless model using user-defined functions (UDFs) and protect access to your data. By the end of the workshop, you'll have a production-ready model for authenticating users and extending GraphQL queries. We'll guide you from knowing nothing about Fauna to setting up a ready-to-use system in just a few hours. UDFs help you separate business logic from front-end code, making queries and mutations simpler. We'll also cover the principle of least privilege, generating temporary tokens for user login, and authorizing access to resources. The workshop focuses on UDFs and secure data access.

You just hit graphql.db.fauna.com, as you'll see in the workshop today. And we take care of that routing for you. And what you're going to build today is a model that we call client serverless, where you don't have all this other infrastructure in between. You just have your front end and communicating directly with Fauna. And that's the thing that you manage. And that's the thing that you concern yourself with.

This workshop is derived from something we call the Zen of Fauna. You're not going to cover all of this today. The workshop mainly covers the first two. Always using user defined functions, which are implementations of your resolvers in your GraphQL schema. We'll talk about why. And there's a security piece that we're going to teach you about how to only put the keys in your client that can log in and nothing else. Right. So this is how to protect access to your data.

So when you're done with the workshop today, you're going to have a production ready model for authenticating users and for extending your GraphQL queries. This workshop builds up pretty smoothly. What was the goal where we try to only introduce one thing at a time that's new for you. But we still take you from knowing nothing about Fonna to having this production ready setup that you can use in the next over the next three hours. So we talk about user defined functions being everywhere. And the reason that we do this is because it lets you take the business logic out of your front end code and store it somewhere else. Right. For basic queries and mutations and graph QL, that's already going to be provided for you by Fonna. When you create a type, we give you a find type by ID, update type, read type, delete type. I'm sorry, create type, delete type. But for more complex cases like we'll cover in the workshop, you're going to encapsulate that logic in a UDF. You can test it, but it also makes your your queries and your mutations simpler. And then, of course, we talked about this piece of least privilege, the principle of least privilege, here with the login keys. Were going to show you how to generate temporary tokens for users when they log in and how those tokens are used to authorize access to resources in your database. I want to go back real quick. The other important thing to note there is every other permission is denied. In the workshop, you can register a user, you can login a user, but you can't do anything else. Because you have to ship these keys out with your clients, right? There's no way to get it out into your users browser without exposing it. You want to make sure once it's exposed that it can't do anything else. We're going to show you how to do that in this workshop.

Watch more workshops on 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.
How to Solve Real-World Problems with Remix
Remix Conf Europe 2022Remix Conf Europe 2022
195 min
How to Solve Real-World Problems with Remix
Featured Workshop
Michael Carter
Michael Carter
- Errors? How to render and log your server and client errorsa - When to return errors vs throwb - Setup logging service like Sentry, LogRocket, and Bugsnag- Forms? How to validate and handle multi-page formsa - Use zod to validate form data in your actionb - Step through multi-page forms without losing data- Stuck? How to patch bugs or missing features in Remix so you can move ona - Use patch-package to quickly fix your Remix installb - Show tool for managing multiple patches and cherry-pick open PRs- Users? How to handle multi-tenant apps with Prismaa - Determine tenant by host or by userb - Multiple database or single database/multiple schemasc - Ensures tenant data always separate from others
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.

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.
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.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.