GraphQL APIs are a tad bit different from other types like REST in terms of implementation. Let's go through some of the ways we can use GraphQL APIs in React.
Whenever calling a GraphQL API, you will generally need to specify four things, regardless of the method used. Keep the following in mind.
HTTP METHOD: We need to specify the HTTP Method (GET, POST, PUT etc). We use the POST method for GraphQL APIs.
URL: The URL for the endpoint of the GraphQL API.
Header: We have to set the header to content-type
of application/json
for all GraphQL APIs.
Query: The query itself, which includes the request and variables in its JSON structure.
Any valid HTTP client can call a GraphQL API for fetching data. Axios is one of the simplest and usable example.
To use Axios, install and import it in your React project.
js
// Installnpm install axios// Import in your React.js fileimport axios from 'axios';
Now, we can request the API as follows:
js
import axios from 'axios';export default async function handler(req, res) {const options = {method: 'POST',url: 'https://geodb-cities-graphql.p.rapidapi.com/',headers: {'content-type': 'application/json'},data: {query: `{countries {namecapitalcurrency}}`}};axios.request(options).then(function (response) {console.log(response.data); // Response}).catch(function (error) {console.error(error);});}
Notice how we are specifying the four parts we discussed above. The query must be wrapped with backticks. If you want to pass query parameters, you can specify another object in data
called variables and include it as follows:
js
data: {query: `query getCountry($prefix: String!){countries(namePrefix: $prefix) {namecapitalcurrencyCodes}}`,variables: {prefix: inputCountryName,},},
You can also consume a GraphQL without using any third-party library. Fetch API is inlcuded in all modern browsers, and it can work just fine for this purpose. Here is an example:
js
fetch('https://geodb-cities-graphql.p.rapidapi.com/', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({query: `{countries {namecapitalcurrency}}`})}).then(res => res.json()).then(res => console.log(res.data));
Apollo Client is developed by GraphQL and provides more elaborate features compared to the other libraries. It handles data fetching and state management, allowing you to do stuff like display the loading or error states as the query is being processed.
js
import React from 'react'import { gql, useQuery } from '@apollo/client'const query = gql`{countries {namecapitalcurrency}}`;export const getCountries = () => {const { loading, error, data } = useQuery(query);if (loading) return 'Loading';if (error) return `Error ${error.message}`;
Urql is another library you can use to fetch data from GraphQL APIs. It offers nearly the same features as Apollo Client albeit being smaller in size. It is also relatively easy to setup.
js
npm install urql