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.

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

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
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.
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.
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
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

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 2022React Advanced 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