GraphQL. State management

Rate this content
Bookmark

In this talk, I will cover why your switch to GQL and Apollo Client 3 should make you walk away from Redux. I will also talk about my journey from Redux -> Apollo Client and use a few project examples for why it makes sense holistically and the challenges I experienced during that transition. Toward the end of this talk, you will be confident about the pros and cons of each approach.

FAQ

A State Manager in GraphQL is a tool used to manage the application state, transforming and accessing server data as per the application's requirements. It involves dispatching actions that can modify the state based on server responses.

A good State Manager should allow easy state updates, access, and ensure data normalization where data isn't duplicated across the application. It should also support consistent data transformation and provide tools for debugging and visualizing global state.

Redux simplifies data manipulation by managing application state in a predictable way, making debugging easier with tools like Redux DevTools. It structures the state management process around dispatching actions and managing state transitions through reducers.

Apollo Client simplifies GraphQL state management by handling data fetching, caching, and UI updates, minimizing the need for manual data handling and state updates. It provides features like normalized caching and reactive variables for more efficient state management.

Local-only fields in Apollo Client are fields that exist only on the client side and do not come from the server. They can be used to append or modify data within the client's cache without affecting the data on the server, enhancing the application's reactivity and efficiency.

Reactive variables in Apollo Client allow components to reactively update when the variable's value changes. Defined using the makeVar function, these variables help manage local state outside of the Apollo cache and can trigger UI updates efficiently.

Apollo Client 3 automates much of the work involved in state management such as data normalization and caching. Unlike Redux, which requires explicit action dispatching and state update logic, Apollo Client manages these aspects internally, simplifying frontend development.

Ankita Kulkarni
Ankita Kulkarni
22 min
08 Dec, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's talk is about GraphQL State Management and the benefits it offers. Redux and Apollo GraphQL are compared for their state management capabilities, with Apollo Client 3 highlighted as a powerful tool that simplifies front-end development. Data transformations and manipulation in Apollo Client, including type policies and reactive variables, are discussed as key features. The talk concludes by emphasizing the advantages of using Apollo Client for state management and providing access to the presentation materials and newsletter for further updates.

Available in Español: GraphQL. Gestión del estado

1. Introduction to GraphQL State Management

Short description:

Today's talk is about GraphQL State Management. A State Manager is a way to manage application state. It allows us to update and access state easily. A good State Manager normalizes data, transforms it consistently, and allows visualization of global state for easier debugging.

Hello, I'm Ankita Kulkarni and today's talk is about GraphQL State Management. I hope everyone's doing OK and hope everyone's having a lovely day or night, wherever you are.

So, yeah, let's get started. So as I said, I'm Ankita. I've been working in the tech industry for over 10 years now. I am currently working as an educator and creating digital products online, such as ebook and courses and whatnot. I have a Next.js course and an ebook if you want to check that out. And I also just create stuff to help you grow as a developer.

So one quick note, all snippets and slides that I use in this specific presentation are available at this link and here is a barcode for you as well, which is pretty handy.

So let's get started and talk about GraphQL State Management. So first let's take a step back and really understand what standards are we living up to, right? Like what is a State Manager? A State Manager is essentially a way that we can manage our overall application state. So whenever we are making a request to the server, the server is giving us back data and that data may or may not come in the right form as we like and this is why it's important that we use a State Manager to transform that data and access that data the way we like. So we can dispatch specific actions and then the state can change overall, right? And that's what we will take a look at now that we understand a little bit more about State Manager.

So what is a good State Manager? A good State Manager allows us to update the state easily. We can access state easily, right? And there are a bunch of different characteristics of a good State Manager that we can take a look at now. For example, your data is normalized. And what I mean by that is whenever you update data, whenever you try to update one thing, it's only going to update that one thing. Your data is not duplicated everywhere, so you are not going to have a bunch of different card queries, for example, or card data or product data or whatever data that you are using in multiple different places. You only have that one state piece of information and a good State Manager really allows you to do that. Now data transforms easily. So whenever, as I said, if whatever data you're getting back from the server, if we are able to transform that data consistently, then, for example, if you want to show a user's name, right? As simple as that, a full name, we can display that on the homepage, on the login page, and wherever else we want, but we are able to transform it to however we like, right? So we also want our data to transform consistently. You can visualize your global state. So as we all know, debugging is extremely important. So in order to help with debugging, we also need to be able to visualize global state. If we don't have tools to do that, then we won't be able to visualize it. We won't know how our data looks like. So we are kind of shooting in the dark, which is not what we want. And as I mentioned, debugging is a lot easier. So you can debug your applications very easily by adding any sort of console logs or trying to see what is the before and after state. If something were to go wrong, you know exactly what to do and what specific code probably caused it.

2. Introduction to Redux State Management

Short description:

Redux fixed majority of our problems by making state management and debugging easier. It made front-end development easier and data manipulation more straightforward. Although Redux may have a learning curve, it allows for efficient data transformation and manipulation.

So you know the flow of data really well. And as we all know, Redux, which is a very popular state management library, really fixed majority of our problems, right. The Redux came along wherein we were able to manage state debug easily with Redux dev tools really helped us along the way with the specific problem that we were having on the front end. In fact, I would also say that Redux made front-end development a lot easier because suddenly data manipulation was so much easier and straightforward. I would caveat that Redux might not be, if we want to get started with Redux, it's not as easy as we would think, but once you do get the hang of it, you would be able to transform data and basically manipulate your data in general, right, properly.

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
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!
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
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.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
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
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

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
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.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
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
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.