Today, more and more applications are using GraphQL APIs. While you can consume GraphQL APIs through the usual fetch
API, axios
or Apollo, URQL provides a versatile tool for GraphQL data fetching.
Let's take a look at it.
URQL is a tailored data fetching tool for GraphQL that allows you to fetch, cache, and modify data in applications. Because it is meant for GraphQL, it will enable you to code efficiently while adhering to modern development practices.
You can always use the Fetch API, Axios, and React Query for fetching GraphQl APIs. To discover these ways further, you can read our guide about them. However, they don't support GraphQL-specific features like subscriptions or caching as GrapQL clients do. When it comes to GraphQL clients, Apollo is the most popular one. However, URQL is comparatively lightweight, and it stands out because of its flexibility, ease of use, and performance.
With that said, let's take a quick look at using URQL in React.
The urql library has built-in integration with React, and it takes advantage of React's features like hooks. Let's demonstrate how we can use it in a React application. The whole process can be divided into the following four steps.
You can get started by installing the npm package in your React application. Run the following command:
js
npm install urql
We also recommend installing graphql
, which libraries require when working with GraphQL APIs.
js
npm install graphql
If you use yarn
, run the following command:
sh
yarn add urql graphql
Once installed, we need to initialize an instance of the URQL client. You can initialize it in the component or page where you want to fetch the API, such as the App.js
or index.js
file of your application.
In index.js
, import the following:
js
import {createClient, Provider} from 'urql';
Now, we can initialize a new instance of the client. We configure it to call GeoDB API, a GraphQL API from RapidAPI Hub that provides data about countries and cities.
js
import {createClient, Provider} from 'urql';const client = createClient({url: 'https://geodb-cities-graphql.p.rapidapi.com/'});
Here url
is the endpoint of the GraphQL API we want to consume.
Provider
connects the initialized client to your React application. To do so, you need to wrap the required component/page, which needs to access the GraphQL data with <Provider value={client}></Provider>
. In our case, we will wrap our index.js
file.
jsx
import {createClient, Provider} from 'urql';const client = createClient({url: 'https://geodb-cities-graphql.p.rapidapi.com/'});export default function Home() {return (<Provider value={client}><div><h2>Consuming GraphQL API in React.</h2></div></Provider>);}
Finally, you can fetch data with the useQuery
hook. After defining your query, you can call it inside the useQuery
hook like this:
js
// index.jsimport React from 'react';import {createClient, Provider, useQuery} from 'urql';const client = createClient({url: 'https://geodb-cities-graphql.p.rapidapi.com/'});const GetCountryQuery = `query GetCountryData {countries(namePrefix: "America") {edges {node {namecapitalflagImageUricurrencyCodes}}}}`;export default function Home() {// useQuery hookconst [result] = useQuery({query: GetCountryQuery});// Get data, loading and error statesconst {data, fetching, error} = result;// Handle loading and error statesif (fetching) return <p>Loading...</p>;if (error) return <p>Error: {error.message}</p>;// Display data returned from APIreturn (<Provider value={client}><div><span>{data.name}</span><img src={data.flagImageUri} /></div></Provider>);}
The useQuery
hook will automatically get data from the API whenever the component loads/renders.
If you are building a React application that heavily relies on data from a GraphQL API, URQL offers a convenient tool for you without a huge learning curve.