Recreating urql's GraphQL cache on the edge

Rate this content
Bookmark

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

Andy Richardson
Andy Richardson
114 min
13 Dec, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Workshop covered various topics related to software development and engineering. It included discussions on coding, communication, and troubleshooting Wrangler configuration. The participants learned about proxying requests in Cloudflare Workers, identifying mutations in GraphQL queries, and caching responses in a KV store. They also explored the AST with astexplorer.net and manipulated the AST to add type names to queries. The Workshop concluded with a discussion on caching subscriptions in GraphQL and handling mutations and query invalidation.

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

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.
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
Do you know we can replace API schemas with a lightweight and type-safe library? With tRPC you can easily replace GraphQL or REST with inferred shapes without schemas or code generation. In this talk we will understand the benefit of tRPC and how apply it in a NextJs application. If you want reduce your project complexity you can't miss this talk.
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'.
Living on the Edge
React Advanced Conference 2021React Advanced Conference 2021
36 min
Living on the Edge
React 18 introduces new APIs for rendering applications asynchronously on the server, enabling a simpler model for architecting and shipping user interfaces. When deployed on edge networking platforms like Cloudflare Workers, we can get dramatic performance and user experience improvements in our applications. In this talk, Sunil will demo and walk through this new model of writing React applications, with some insight into the implications for data fetching, styling, and overall direction of the React ecosystem.