It Depends — Examining GraphQL Myths and Assumptions

Rate this content
Bookmark

As with any technology choice, choosing GraphQL as an API style involves tradeoffs. Some tell us GraphQL is here to replace everything else, others tell us using GraphQL is a mistake. In this talk, we'll explore why both these things are false, and how everything is context dependent.

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

FAQ

Yes, caching GraphQL queries is possible. While HTTP caching of GraphQL can be challenging due to the nature of its operation and use of authorization headers, client-side caches and tools like Data Loader help in managing data efficiently by caching at the resolver level.

HTTP caching is a concern with GraphQL because it typically involves shared HTTP caches, which are complex to implement with GraphQL. The specification restricts caching of responses with authorization headers to prevent private data from being shared, which complicates caching authenticated APIs.

GraphQL's performance depends heavily on the use case. It can be faster than REST when handling complex queries that would otherwise require multiple REST calls. However, for simple queries, the overhead of a GraphQL engine might make REST faster. Performance optimizations like the defer and stream directives can also influence this comparison.

GraphQL offers great flexibility and efficiency in fetching data, but it also introduces trade-offs such as less predictable performance due to its flexibility, and potential complexity in handling diverse client requirements within a single schema.

While GraphQL can serve similar purposes as Backend for Frontend (BFF) by tailoring responses to different clients, it does not offer complete autonomy between teams as BFF does. GraphQL maintains a shared schema, which means types and fields are still shared among different client implementations.

REST is typically more suitable than GraphQL for public APIs with stable, unchanging data that is the same for every user, such as a list of countries. These scenarios benefit from REST's efficient HTTP caching capabilities.

Marc-André Giroux
Marc-André Giroux
25 min
02 Jul, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The talk titled 'It Depends' delves into the nuances of GraphQL and its effectiveness based on context. The speaker discusses the challenges of GraphQL caching, emphasizing that while HTTP caching is complex due to authorization headers, tools like Data Loader and normalized client-side caches can help manage data efficiently. Performance in GraphQL is less predictable compared to REST but offers advantages in handling evolving app requirements. The flexibility of GraphQL comes with trade-offs, especially in caching and performance, which are context-dependent. The talk also highlights that GraphQL as a backend for frontend (BFF) offers flexibility but doesn't provide full autonomy between teams, unlike traditional BFF patterns. For public APIs with static data, such as a list of countries, REST may be more suitable due to its efficient HTTP caching capabilities. The speaker underscores the importance of context in choosing between GraphQL and other technologies, advocating for better conversations to understand the trade-offs involved.

1. Introduction to GraphQL and Caching

Short description:

Today's talk is called 'It Depends.' We'll explore the nuanced discussions around GraphQL and how its effectiveness depends on the context. Caching is a polarizing subject in GraphQL. Some say it's impossible to cache, while others say it's not a concern. Let's dive into it. GraphQL clients have powerful normalized caches, and backend resolvers can be cached too. However, the challenge lies in HTTP caching and the absence of shared HTTP caches. Shared caching allows responses to be reused across different clients and servers.

Hey, hello again, everyone! Thank you for coming to watch my talk. So, today my talk is called, It Depends. It may sound a little boring, and it might be, but we'll see. We'll kind of examine how there's certain discussions around GraphQL that these days we either hear like that's totally true or totally false. But there's many things about GraphQL that just depend a lot on the context you're in and have so much nuance. So, we'll just try to dive real deep into some of that stuff.

So, my name is Mark. I work at GitHub, and I'm from Montreal, Canada. So, It Depends. What do I mean by that? So, as I was saying earlier, with GraphQL, there are some topics that we're going to see today that either people say you can't do with GraphQL, or that GraphQL is the best solution for. But in reality, something I've noticed, working with a lot of different people, working at different places on GraphQL, is that the context in which we take these decisions are way more important than GraphQL versus REST, or GraphQL versus gRPC, or these kind of things.

And the first thing I want to start with is caching. And I've talked about this before. So, if you've seen a talk of mine on caching before, there might be a little bit of a repeat here, but I think it's one of the most interesting subjects around GraphQL because it's such a polarized subject. On one end, you hear some people saying caching GraphQL is literally impossible. The other end, people say it isn't a concern at all. So, let's dive into it. When we talk about caching, and when we hear that GraphQL is hard to cache, sometimes it's a little hard to understand why. Because on the client side, literally any GraphQL client you look at has powerful normalized caches on the client side with a lot of features. And on the backend side, well, we're using just regular programming languages. We can cache what we want, resolvers are just functions. And we've got tools like Data Loader that allow us to cache data loading, which is great. So it's hard to tell really what is hard about it when we have so many good tools to do it. Reality is that when people say caching is hard, they usually talk about different caching than what we just saw. They talk about HTTP caching. And in particular, what they say will be missing is shared HTTP caches. So if you've never heard about this, it's actually quite a complex mechanism, but the problem itself is kind of basic to explain. So when clients generally hit your GraphQL server, they got a response back. With a shared cache, you can cache these responses and reuse. Because it's shared, you can reuse these responses across different clients and across different servers.

2. Caching in Authenticated APIs

Short description:

This kind of allows one client to take the cost for computing a response and other clients just getting it for free. The problem is the spec itself for HTTP says if you've got an authorization header in there, shared caches shouldn't cache things. If we do have all these great tools to do caching where we can and you've got an authenticated API, does it really matter? Well, it maybe does a little bit, but it's not the end of the world as we saw in the previous conversation where somebody says you literally shouldn't be using GraphQL because you lose caching. So, I think the first thing I want to talk about here is how every specific context informs a lot if GraphQL is a good choice or not so much of a good choice. But there's some truth to it as well in the sense that HTTP caching by itself is all about conventions. We have years of experience with HTTP caching. Browsers speak it natively. A lot of clients speak it. With GraphQL, as you saw, we do need to build those smart clients with normalized caches. But these tools are built and are ready to be used, so it might not make a big difference for you.

This kind of allows one client to take the cost for computing a response and other clients just getting it for free. This is great. We should always strive to have a cache like this if we can.

The problem is the spec itself for HTTP says if you've got an authorization header in there, shared caches shouldn't cache things. It may be a little weird if you just read the spec, but if you think about it, it makes a lot of sense because you don't want to be caching things that are specific to a user and be serving this in a shared way to other clients. The idea of a shared cache in authenticated API context really doesn't make that much sense.

If we do have all these great tools to do caching where we can and you've got an authenticated API, does it really matter? Well, it maybe does a little bit, but it's not the end of the world as we saw in the previous conversation where somebody says you literally shouldn't be using GraphQL because you lose caching. If your API is an authenticated API and deals with a lot of live data that just you can't afford to have stale, well, we aren't losing that much. We aren't losing that powerful shared cache because it would've been useful to us anyways.

So, I think the first thing I want to talk about here is how every specific context informs a lot if GraphQL is a good choice or not so much of a good choice. But there's some truth to it as well in the sense that HTTP caching by itself is all about conventions. We have years of experience with HTTP caching. Browsers speak it natively. A lot of clients speak it. With GraphQL, as you saw, we do need to build those smart clients with normalized caches. But these tools are built and are ready to be used, so it might not make a big difference for you.

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 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