But Can Your GraphQL Client Do This? — A Deep-Dive Into urql

Rate this content
Bookmark
Urql is a GraphQL client for React, React Native, and other frameworks developed by Formidable. It offers first-party support for frameworks like Next.js, Preact, Svelte, and Vue. Urql's modular architecture is built on exchanges for extensibility, including dedupe, cache, and fetch exchanges. The fetch exchange handles network requests, while the dedupe exchange removes duplicate requests. Urql supports authentication through the auth exchange and offers caching strategies like document caching and Graphcache for offline support and optimistic updates. Compared to Apollo, Urql is smaller and more modular, making it suitable for various projects. The community is active, providing quick support on GitHub. Developers can find more information and community discussions on the GitHub repository.

From Author:

Let’s explore how the urql GraphQL client came to be and what makes it stand out. We will explore how various challenges were solved in new ways from first principles. Expect a look at future standard features such as Offline Support, an overview of small design choices that are baked into urql, and some features that are already standard and have first-party support such as authentication and file upload.

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

FAQ

Urql is a GraphQL client for React, React Native, and other frameworks, developed as an open-source project by Formidable.

Urql stands out for its commitment to first-party support for framework bindings, extensibility through exchanges, and a small bundle size, making it adaptable and efficient for developers.

No, Urql does not currently have plans to add bindings for Angular. Developers using Angular might need to consider other options.

Urql differentiates itself with built-in support for various frameworks outside React, a modular architecture based on exchanges for custom functionality, and a community-focused approach that ensures active support and responsiveness.

Urql includes an 'auth exchange' for handling authentication and token exchange, supporting both synchronous and asynchronous token fetching, token refresh, and customizable authentication headers.

Urql uses document caching by default, which caches the result of each query indefinitely based on its unique key. It also offers an advanced caching solution called 'graph cache' for more complex needs like offline support and optimistic updates.

Urql maintains a small bundle size by keeping core functionalities minimal and modular, allowing developers to add only the features they need through separate exchanges.

Developers can find support and engage in community discussions about Urql on the discussion tab of its GitHub repository. The Urql community is actively maintained by its core developers.

Kadi Kraman
Kadi Kraman
37 min
02 Jul, 2021

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to Urql

Short description:

Hello, friends! Today I'm going to talk about Urql. It's a GraphQL client for React, React Native, and others, built by Formidable. Urql stands out with its first-party support for framework bindings. It has built-in support for Next.js, Preact, Svelte, and Vue. Apollo Client and Relay rely on third-party libraries for framework support.

Hello, friends! Thank you so much for having me. So today I'm going to talk about Urql. In particular, I'm going to dig into some of core values and standout features.

So a quick intro just on me. My name is Kaddy. I'm currently an engineering manager at Mutable, where I'm spending most of my time building mobile apps, both in React Native and GraphQL. I've been partial to GraphQL even from before I started using React Native, but having used both REST and GraphQL in mobile applications, I can definitely see how many of the benefits of GraphQL can be particularly useful for mobile. And I really hope that one day, GraphQL will be the standard for native applications.

So why am I here talking about Urql? Well, Urql is a GraphQL client for React, React Native, and others, and it's an open source project built by Formidable, and that's a company I work for. So what is Urql? At Formidable, we invest a lot in open source. We're all standing on the shoulders of giants here, and I mean, this very conference wouldn't even exist if Facebook hadn't decided to open source GraphQL in the first place. At Formidable, we have various initiatives to encourage folks to contribute to open source both within formida sauce, but also to personal and community projects. Urql is one of the many open-source projects over the years, but currently I think it's the most exciting one. Originally, it was created by Ken, then rewritten by Phil and now with contributions from the community as well as many of my formidable colleagues. Even I myself have not not long ago contributed by adding an auth exchange for handling authentication and token exchange, but more on that later. With that in mind, should you use Urql? Well, there are several other GraphQL clients out there. Apollo Client and Relay in particular. Ultimately, the choice on what GraphQL client to use and or not to use should really depend on the features that you need on your project. For the rest of the talk, I'm going to go through some of the features of Urql that I think are especially cool and just talk a bit about the general philosophy and the thought process that has gone into designing them.

All right, one of the things that really stands out in Urql compared to others is the commitment to first-party support for framework bindings. So, outside of React, Urql has first-party built-in support for Next.js, Preact, Svelte, and Vue. Although there are no plans to add bindings to Angular, so if that's your jam, you might have to look elsewhere. Apollo Client and Relay don't provide any kind of first-party support for any framework bindings outside of React, and they rely fully on third-party libraries. This is completely fine. Those libraries were designed for React, after all, and it's the community that wanted to use them for other frameworks. However, this can have detrimental ripple effects. For example, when the library implements new features, or breaking changes and upgrades, all these take time to propagate into the third-party libraries. If you have first-party support, these changes are built into the bindings from day one. If you're interested in a more detailed comparison of these libraries, there is a whole section on the article docs where you can find a pretty honest feature comparison between Urql, Apollo Client, and Relay. However, for the remainder of this talk, I'm going to focus on Urql only.

2. Flexibility and Exchanges in Urql

Short description:

If there's one thing we know when it comes to tech is that things change fast and all the time. When creating Urql, one of the goals was to make sure that it's as flexible as possible, so it would be able to adapt to the changing tech landscape. Extensibility really is at the heart of Urql, which brings us to exchanges. Urql is built on exchanges. When you install Urql and execute a query, by default, Urql uses three exchanges. These come packaged into UrqlCore. So we have the dedupe, the cache, and the fetch exchanges. The dedupe exchange removes duplicate requests when the exact request is already in flight. The cache exchange returns the data from the cache if there is some. And finally, the fetch exchange does the actual network request and puts the result back into the output stream.

If there's one thing we know when it comes to tech is that things change fast and all the time. New language features come, libraries, conventions, the things that are cool change, and even business requirements crop up all the time.

When creating Urql, one of the goals was to make sure that it's as flexible as possible, so it would be able to adapt to the changing tech landscape. So in order to make Urql as adaptable as possible, it's built to be as extensible as possible.

Extensibility really is at the heart of Urql, which brings us to exchanges. Urql is built on exchanges. So if you have a think about what a GraphQL client does at its core, it takes a request, a query, or a mutation, and it gives the appropriate response, so a cache hit, a response from the API, or an error. So as a result, most GraphQL clients, including Urql, are built to be stream-based. So an exchange is just a plugin that allows you to inspect and modify both the incoming and outgoing streams.

Let me show you this, by example, because that will make it a whole lot clearer. When you install Urql and execute a query, by default, Urql uses three exchanges. These come packaged into UrqlCore. So we have the dedupe, the cache, and the fetch exchanges. All of these are completely replaceable, by the way, so you can swap out any of these for custom or alternate implementation.

So first, the dedupe exchange, it removes duplicate requests when the exact request is already in flight. So imagine a website with a header and a sidebar. And both of these want to display the user's name. Both of these will use a use query with exactly the same query. What the fetch exchange, the dedupe exchange does, is it makes sure that only one instance of that query is passed on to the next exchange, and therefore, removing the need for a duplicate API request.

Then the cache exchange returns the data from the cache if there is some. And finally, the fetch exchange, which will always be in the last exchange in the array, does the actual network request and puts the result back into the output stream. Then the result is passed from the network request exchange to the cache exchange, where it is stored for next time, and finally, it's passed back to the use query result.

Now, since you can literally add an unlimited amount of plugins, the only way to guarantee that they're all playing nice with each other is to make sure each plugin remains unopinionated. Let's have a look at an actual example of this in Exchanges. First, we have the fetch exchange that we already talked about. This is the default fetch exchange included in Urql Core. It fetches the data from the API and adds to the output stream. Now, a separate package add-on, you can install a separate add-on package for automatic persisted queries in a persistent fetch exchange. So, this exchange used the same fetch logic as the actual fetch exchange. But with automatic persisted queries, basically the way it works is that it allows server-side caching of GraphQL data.

QnA

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