Recreating urql's GraphQL cache on the edge

Rate this content
Bookmark
The video covers the essential aspects of working with GraphQL, particularly focusing on caching and query handling. It starts by explaining the difference between GraphQL mutations and queries, noting that mutations are used for pushing data and queries for retrieving and caching data. The use of Abstract Syntax Tree (AST) is highlighted for identifying whether a query is a mutation or not. The video demonstrates how to leverage Cloudflare Workers and Wrangler CLI for deploying and managing GraphQL services on the edge. It also provides detailed steps on how to handle cache invalidation by extracting type names from responses and mutations. The speaker emphasizes the importance of type names in maintaining data consistency and cache invalidation. The video also covers how to use tools like AST Explorer and the 'visit' function to manipulate GraphQL queries efficiently. Additionally, it discusses the process for setting up a key-value store using Cloudflare KV and handling GraphQL requests and responses, including caching strategies and error handling.

From Author:

Table of contents:
- Deploying a premade GraphQL API
- Creating an edge worker + cache on Cloudflare
- Configuring caching based on typenames
- Adding invalidation based on mutation return types

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

FAQ

The session is being wrapped up by Timo.

The content focuses on communication, conceptual understanding, and design.

You can create a basic graphical endpoint using the link provided in the chat.

You need to run npm install and then npm start to deploy services locally. Rangler, a CloudFlare CLI, will be used for deployment.

If you encounter a 403 error, try running wrangler init and ensure you are signed in with your CloudFlare account.

Cloudflare Workers can perform fetch requests and intercept requests, functioning similarly to service workers.

An AST (Abstract Syntax Tree) is a hierarchical representation of the source code. It is useful for parsing and manipulating queries in a normalized way.

You can identify it by checking if the operation type in the AST is 'query' or 'mutation'.

Type names are important for identifying the type of data returned by GraphQL queries and mutations, which helps in cache invalidation and maintaining data consistency.

Cache invalidation is handled by extracting type names from responses and mutations, then invalidating queries that depend on those type names.

Andy Richardson
Andy Richardson
114 min
13 Dec, 2021

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to Coding and Communication

Short description:

Next we're gonna wrap up the session with Timo. Can he get started? Can you show him how to code a course? Tyto, can you explain the concept you're talking about? A lot of my content focuses on communication. I feel overwhelmed but passionate. We've had some requests to get data and do some validations. If you have a basic graphical endpoint, fantastic. If not, use the provided link to create one. We can use that for the workshop. Let me send you a repo. Now let's get started. Install dependencies with NPMCI and Rangler. Login with your CloudFlare account. Run npm start and handle the type error. Start making GraphQL requests. We should see them console logged in. Let's proxy the request and handle the error.

Next we're gonna wrap up the session with Timo. Can he get started? He's here with you, can't he Timo? Can you show him how to code a course? He must be. So let's go ahead and get him started. Tyto, can you explain conceptual you're talking about? And can you talk about communication and design?

Yeah, definitely. So a lot of my content right now focuses on communication. I feel a little bit overwhelmed. Not that I want to admit, but I'm a little overwhelmed because I am very, very passionate about communication and I've had some requests to get data, but we also want to do some invalidations. So if you have a basic, like for a graphical endpoint, fantastic, if you don't, just head over to this link in the chat. As you can create a super, super basic graphical endpoint. So in this example, it gives us like a little JSON schema. Just click extend JSON, deploy API. And then we get a little link here and now we can actually do some graphQL requests. So if I go into, if I go into Insomnia here and say, get users, for example, we should see, there we go. We got a bunch of users. So just make your own, if everyone has their own, you'll see you'll get like a little unique URL. Keep that safe. And we can use that for the rest of the workshop. Although if you, like I said, if you have a API that you want to use as well, graphical API, then you can use that instead. And I'll just put in still one in advance.

All right. And let me send you a repo as well. Cool. We've got our own repo and we just basically just some boilerplate to get started. So I will share my screen and let's, let's get started. So change my end point here. Let me make sure I'm on the right repo. Cool. So what we want to do, is if you just run NPMCI and we'll install some dependencies. And one of those things that will be installed, oops, let's try that again. One of those things that will be installed will be Rangler, which is a Cloudess CLI for deploying services, things like that. And we're actually going to need Rangler for this project, even though we're running it locally for now. So what you wanna do is if you just run, I'll put this in the chat. Npm bin Rangler login, something like this. If you're on Windows, it might be slightly different. It will give you a prompt to open a browser and just sign in with your CloudFlare account. It will give you a prompt to open a browser and just sign in with your CloudFlare account.

Cool, alright, I'm gonna assume that you're logged in. So everyone, if you just run now, npm run, I think it was dev, it's called a, oh yeah, npm start, there we go. If you're on npm start, what we're actually going to do now is get a type error, totally intentional, knew that was gonna happen. So let's just quickly just do a, let's do return null as any, and then let's just console log request and see what we get. And npm start, there we go. Cool, so in a second you'll get a little prompt, here we go, that we've got a server listening on this endpoint. And now we can basically start making GraphQL requests to this endpoint, we should see them console logged in. So I have a, I've got Insomnia here, but you can use Postman or Kill or whatever you want. I'm just gonna do H7, H7. Cool, so I'm gonna do a quick report. Let's proxy our request to that. And we get an error, work through exception. That's totally fine, that's because it didn't return. But what we should see is that parameter one is not typed.

2. Proxying Requests in Cloudflare Workers

Short description:

Let's proxy our request in Cloudflare Workers using fetch requests. We'll make a new request to our GraphQL endpoint, taking the necessary details from the fetch request. If you're experiencing a 403 error when signing in to Wrangler, try running Wrangler in it to set up your project. If that doesn't work, try removing the wrangler.toml file and running wrangler inherit. As long as the Annet works, we can proceed. If you're encountering errors when running Yarn start or NPM start, it's likely due to the Annet itself. Annet should create your Wrangler and an instance of the worker in Cloudflare.

Okay, let's ignore that. Let's proxy our request. So in Cloudflare Workers, you can do fetch requests. So, for example, if we want to do a request to Google, we need something like that. And it's just your standard like in-browser fetch request. And the environment we're in in a Cloudflare Worker is basically, they call it Worker because it's kind of like a service worker. There's some constraints as to what you can do in the environment. But in general, you can do things like fetch requests and intercept requests and things like that.

So what we're gonna do is, to start off with, we're just gonna do a fetch and we're gonna make a new request as part of this fetch. The destination is gonna be our GraphQL endpoint. So you'll see that we've got new request destination, and then the rest of it, the body, the headers, things like that, we're just gonna take from the request that's being passed to us from the fetch function.

So if I quickly do another request now, I'll work it through its section. What is going on? Hmm, anyone else getting this error? I was thinking I might've broken something beforehand. I guess not. Ah, there we go. Now it's working, all right so just give me a thumbs up in chat if you aren't getting that weird error that I totally was expecting not. Let's have a look. Okay so Marken's getting a 403, Rodrigo's getting a 403 as well. Okay you're all getting a 403. Okay so that probably means, okay cloud flag client V4 accounts, if you, okay so you're getting a 403 when you're signing in to Wrangler. Before you start, just try and run Wrangler in it so you can do that same thing again with npm bin if you want something like that and you should, in theory then, be set up to go. If you feel comfortable doing that, we can just add one as a console. We can just add one and then just do an empty VERTX and then once I'm done with that, I have a hidden flag here. Just a quick confirmation in the chat if that's working now. Oh, I see. Hm, interesting. Okay, let's have a quick look. Okay, so it looks like what you might need to do is just get rid of the, I'm just gonna try this very quickly. I just did a code break. And I got corrupted and I've found out that it's just because it's not TrueType API. So I'll use the normal module. If you guys can... Yes. Okay, so what we can do is if you just get rid of the wrangler.toml... I need it. No. If you just get rid of wrangler dot toml for now, in your projects. And then run wrangler inherit and in theory, then that should create a project. How's it going for you folks? Is that working now? Okay. Don't worry about that for now. As long as the Annet works, what I'm actually going to do is, we're going to take the, did install something, but now it's failing, same as Marcus, okay. And, am I right in thinking that that's failing when you run Yarn start now, right, or NPM start. The Annet itself is, I'm guessing it's passed. Oh, I'm sorry. Installed something and now it's failing, same as Marcus. Okay, cool. So, if you, what you would do is, we do want to use that previous Wrangler that we have. But, what happens on Annet, I'm pretty sure I need to double check this. It's been a while since I've done this. What Annet will do is probably create your Wrangler, but also create an instance of that worker in Cloudflare, which is the thing that we were missing, which is why it wasn't working.

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

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.
Get rid of your API schemas with tRPC
React Day Berlin 2022React Day Berlin 2022
29 min
Get rid of your API schemas with tRPC
Today's Talk introduces TRPC, a library that eliminates the need for code generation and provides type safety and better collaboration between front-end and back-end. TRPC is demonstrated in a Next JS application integrated with Prisma, allowing for easy implementation and interaction with the database. The library allows for seamless usage in the client, with automatic procedure renaming and the ability to call methods without generating types. TRPC's client-server interaction is based on HTTP requests and allows for easy debugging and tracing. The library also provides runtime type check and validation using Zod.
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.
Living on the Edge
React Advanced Conference 2021React Advanced Conference 2021
36 min
Living on the Edge
The Talk discusses the future of React and introduces new APIs, including streaming rendering and server components. React Suspense allows for asynchronous loading of components and data fetching. The use of serverless computing, specifically Cloudflare Workers, is explored as a way to improve performance. The Talk emphasizes the potential for simplifying the React ecosystem and the excitement about the new API.