Tomek Poniatowicz
8/11/2020
Universal React Query Library (URQL) is a highly customizable GraphQL client, exposed as a set of React components by Formidable, aiming to become a lightweight alternative that would uphold the principal values of the GraphQL protocol & SDL.
Currently, the most popular client libraries come with large API footprints. URQL's main goal is to simplify some of the most popular aspects arising when using GraphQL by providing:
URQL Client internally manages the lifetime and updates for query & mutation operations in the background:
Source: Urql Docs
Caching is handled & customizable with so-called Exchanges. The default cacheExchange
offers basic cache implementation that will avoid sending the same requests to a GraphQL API repeatedly by caching the result of each query. For more complex cases containing data interdependencies, URQL offers normalized caching provided by @urql/exchange-graphcache
package.
URQL provides Exchanges
to abstract how the Client interacts with frameworks, GraphQL API, or your app. URQL's Exchanges
have access to all operations and all results (the concept is very similar to middlewares in Redux). The core package's default behaviors are implemented using Exchanges as both operations as their results are treated as a stream of events:
Source: Urql Docs
The method createClient
creates the GraphQL client which requires providing API's URL as a bare minimum. This Client will manage all your operations. To make it work in React & Preact provide it via the Context API with the help of the Provider
export.
import { createClient, Provider } from 'urql';
const client = createClient({
url: 'http://localhost:3000/graphql',
});
const App = () => (
<Provider value={client}>
<YourRoutes />
</Provider>
);
Source: Urql Docs
To get more details instructions & examples makes sure to visit official URQL documentation.