Killing BFFs with GraphQL and Next.js

Rate this content
Bookmark

Frontend applications are getting more and more complicated, often delivering far more than just a UI. With this growing complexity comes a growing need for knowledge from the developers creating it, as they have to deal with matters like state-management, authorization, routing and more. In this talk, I'll show you how to use GraphQL in your Next.js application to create a data layer for your application, that exists between your frontend and your backend. In this data layer, you can handle complex matters to help you to keep your frontend application clean and "stupid".

This talk has been presented at React Advanced Conference 2021, check out the latest edition of this React Conference.

FAQ

The talk focuses on discussing the concept of 'Backend for Frontend' (BFF) and how to effectively implement it using Next.js.

The speaker is Roy, an entrepreneur, software engineer, author, and conference speaker. He can be found on Twitter with the handle @GetHackTeam.

BFFs can lead to complications such as different clients (web and mobile) having inconsistent implementations, leading to extra work for developers to maintain multiple backends for different frontends.

A monolith is a single API that serves all clients, often becoming complex as it needs to cater to various client requirements. It can be a REST API, GraphQL API, or even SOAP.

Microservices are small, independent services that each serve a specific function, unlike a monolith, which is a single, large API. Microservices often require an API gateway to unify the services for the frontend.

Benefits of BFF include separation of concerns, autonomy for different frontend teams (web and mobile), and tailored backend services for specific frontend needs.

Next.js API routes allow developers to create backend functionalities directly within a Next.js app, reducing the need for separate deployments and making it easier to manage and maintain.

Hiding sensitive data, such as API keys and architectural details, is crucial to prevent security risks and unauthorized access. Using API routes in Next.js ensures sensitive data is handled on the backend, not exposed to users.

Next.js API routes support middleware, allowing developers to integrate various functionalities such as authentication and logging, similar to Express.js.

You can learn more from the Next.js documentation and by taking courses offered by Roy, available at hackteam.io/courses.

Roy Derks
Roy Derks
21 min
25 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk discusses the challenges and benefits of using Backend for Frontends (BFFs) and microservices in software development. It highlights how Next.js can simplify the creation of BFFs and unify the gateway for microservices. The talk also emphasizes the advantages of Next.js API routes in simplifying the development, deployment, and maintenance of APIs. It showcases the implementation of a BFF using Next.js and API routes, and the extension of API routes in an executable way. The speaker also mentions launching a course on using Next.js API routes for building a serverless GraphQL API.

1. Introduction to Killing BFFs with Next.js

Short description:

Hello, everyone, and welcome to my talk, Killing BFFs with Next.js. This talk is about microservices, BFFs, and monoliths, and the extra work they can entail for your team. We will discuss how to address this issue. Enjoy the talk and stay tuned for the Q&A session.

Hello, everyone, and welcome to my talk, Killing BFFs with Next.js. I guess it's almost Halloween, so that's a great way to start out with some Halloween opening.

So Killing BFFs with Next.js, but it's not a Halloween talk, but it is something that is probably interesting for most of you. So what is this talk about? Let's start out about... You probably already know about microservices or maybe you're still using a monolith. You also have BFFs and monoliths, but those are a bit different.

So imagine you have microservices and then you have a backend for frontend for your web client, you maybe have a separate backend for frontend for your mobile client. So this is probably perfectly fine, but it is something that takes a lot of extra work for your team and maybe also for your backend developers because they need to make sure the microservices are tailored to two different backend for frontends. So that's something we are discussing today in this talk. So I hope you all enjoy and if you have any questions afterwards, there's some time for Q&A.

2. Backend for Frontends and Microservices

Short description:

Backend for frontends (BFFs) can be both beneficial and complicated. It allows different clients to have tailored experiences, but can also lead to inconsistencies. Monoliths, which are one API to rule them all, are commonly used. Microservices, on the other hand, offer separate services for different goals. However, coordinating these services can be challenging. Even if you don't use BFFs, this talk will show you how Next.js can provide a unified gateway for microservices.

So backend for frontends is... Well, it could also be this, when you and your backend for frontend know something nobody else does. So this is actually a problem because your mobile client may be doing something with their mobile backend for frontend while the web client wants to do the same thing, but maybe the logic isn't implemented there. So backend for frontends aren't only a way to make things easier for the client. They can also lead to extra complications because maybe one client knows something that another client doesn't.

So a little bit about myself. So my name is Roy. You can find me on Twitter with my handle at GetHackTeam. I'm an entrepreneur, a software engineer, but I also I've written some books and a regular speaker at conferences. So if you don't know me already, please find me on Twitter. Currently I also work with a company called Stepsen. What they're doing, they're making one unified GraphQL API for all your data sources. So that's pretty cool if you want to get started with GraphQL. And also I give workshops and trainings to my own company Hack Team, so make sure to go to the website as well if you have questions about that. But also make sure to just enjoy this talk and try to learn something new.

So how do we go to back-end for front-end? So back-end for front-end or BFFs, as I already said, how do we actually get there? So I think the first time most of you got in touch with API for the first time was with monoliths. So monoliths are, as they say, one API to rule them all. And monoliths can be REST APIs, they can also be GraphQL APIs, maybe even SOAP if you started web development or development in general a long time ago. So monoliths are basically one API to rule them all. And if you're working with different clients from our monolith, typically you would have separate REST endpoints or if you're using GraphQL, which can be a great way to have different clients for one monolith. You'll probably have different queries that are tailored to different clients that are consuming your GraphQL or REST API. So it's basically monoliths, one API to rule them all, which is perfectly fine because most companies don't even get at a size that they need to have microservices, but if you get there, then microservices is also something that's very interesting for API developers. But for front-end developers or React developers, it will lead to some extra difficulties, because I always like this meme about microservices. So when your boss tells you we're converting to microservices, this actually can happen. So microservices are tiny services, that's why micro, and it's one service for a separate goal. So maybe you have one service for your authentication, you have one service to get your users, one service to get maybe information about products, another service to get for your check-out orders, all these things will be divided into different microservices. And these microservices of course need to be unified somewhere, maybe an API gateway, maybe your front-end is calling all the microservices separately, which is something you should try to avoid. So even if you're not using back-end for front-ends, it can be an interesting outcome for this talk, learn about how you can use Next.js in order to have one unified gateway for all your microservices.

So what are back-end for front-ends good for? So we now sort of know why we have back-end for front-ends, so we had monolith APIs, which is nice, but can be confusing if you have multiple clients using the same monolith. We already also saw microservices, which are also perfectly fine, but it can also be very confusing if you don't have an API gateway in place or the division about who's responsible for what microservices very, very strange.

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

Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.
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.
A Practical Guide for Migrating to Server Components
React Advanced Conference 2023React Advanced Conference 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
The New Next.js App Router
React Summit 2023React Summit 2023
27 min
The New Next.js App Router
Watch video: The New Next.js App Router
Today's Talk is about the Next.js App Router, which has evolved over the years and is now a core feature of Next.js. The Talk covers topics such as adding components, fetching remote data, and exploring layouts. It also discusses submitting form data, simplifying code, and reusing components. The App Router allows for coexistence with the existing pages router and enables data fetching at the layout level using React Server Components.
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.

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
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.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up