35
loading...
This website collects cookies to deliver better user experience
--typescript
template flag,yarn create react-app client --template typescript
If you're having issues creating a new react app due to the recent version upgrade, check out this article on how to fix it.
yarn start
yarn add react-query
index.tsx
file in the project folder and set up React Query client;import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { QueryClient, QueryClientProvider } from "react-query";
const queryClient = new QueryClient();
ReactDOM.render(
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>,
document.getElementById('root')
);
components
folder, create a Countries.tsx
file. The Countries
component will display lists of countries from the free public GraphQL Countries API we'll be using in this tutorial.<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
Countries
component, add the following code;import React from "react";
const Countries: React.FunctionComponent = () => {
return (
<div className="bg-white rounded-lg shadow-md w-80 text-center">
<div className="mt-4 p-10 text-bold">
<p>Country</p>
<p>Capital</p>
<p>Continent</p>
</div>
</div>
);
};
export default Countries;
App.tsx
file in the project folder and update like soimport React from "react";
import Countries from "./components/Countries";
const App = () => {
return (
<>
<div className="bg-blue-100">
<header className="text-3xl text-center font-bold pt-6">Countries</header>
<div className="flex justify-center pb-2">
<div className="flex flex-col">
<div className="flex-grow">
<div className="m-4 grid grid-cols-2 sm:grid-cols-2 md:grid-cols-2 lg:grid-cols-2 xl:grid-cols-2 gap-8 mt-8">
<Countries />
</div>
</div>
</div>
</div>
</div>
</>
);
};
export default App;
graphlql-request
into our projectyarn add graphql graphql-request
graphql
added above is a dependency for graphql-request
.Countries
component like soimport React from "react";
import { GraphQLClient } from "graphql-request";
const graphqlClient = new GraphQLClient(
"https://countries.trevorblades.com/graphql"
);
const Countries: React.FunctionComponent = () => {
return (
<div className="bg-white rounded-lg shadow-md w-80 text-center">
<div className="mt-4 p-10 text-bold">
<p>Country</p>
<p>Capital</p>
<p>Continent</p>
</div>
</div>
);
};
export default Countries;
GraphQLClient
from graphql-request
and instantiated it. countries.graphql
file in the src folder. In this file, we'll define a query to get the list of countries data we need; the country names, capital and continents.query Countries {
countries {
name
capital
continent {
name
}
}
}
graphql-code-generator
so we can have it up and running in our application. Execute the following command;yarn add -D @graphql-codegen/cli @graphql-codegen/typescript @graphql-codegen/typescript-react-query @graphql-codegen/typescript-operations
graphql-codegen
CLI so we can run our codegen
script.graphql-codegen/typescript
and graphql-codegen/typescript-operations
plugins so we can get our generated types.graphql-codegen/typescript-react-query
plugin in order to generate custom React Query hooks.codegen.yml
file where we'll configure how we want our code to be generated.overwrite: true
schema: "https://countries.trevorblades.com/graphql"
documents: "src/**/*.graphql"
generates:
src/generated/index.ts:
plugins:
- "typescript"
- "typescript-operations"
- typescript-react-query
config:
fetcher: graphql-request
schema
is the URL for the free public GraphQL API we are using in this tutorial.documents
tells graphql-codegen
to locate graphql
files, in this case the countries.graphql
file.generates
defines the file path where the types will be generated once we run a defined graphql-codegen
script.plugins
represents the plugins we installed earlier.fetcher
configuration lets graphql-codegen
know we are using graphql-request
to fetch our data.graphql-codegen
script into our package.json file like so;src
as a folder titled generated
;yarn generate
Countries
componet, let's update our code as shown below;import React from "react";
import { GraphQLClient } from "graphql-request";
import { CountriesQuery, useCountriesQuery } from "../generated";
const graphqlClient = new GraphQLClient(
"https://countries.trevorblades.com/graphql"
);
const Countries: React.FunctionComponent = () => {
const { data, isLoading, error } = useCountriesQuery<CountriesQuery, Error>(
graphqlClient,
{}
);
if (isLoading) {
return <div className="box">Loading...</div>;
}
if (error) {
return <div>Error!</div>;
}
return (
<>
{data?.countries?.map((country) => {
return (
<div className="bg-white rounded-lg shadow-md w-80 text-center">
<div className="mt-4 p-10 text-bold">
<p>Country: {country.name}</p>
<p>Capital: {country.capital}</p>
<p>Continent: {country.continent.name}</p>
</div>
</div>
);
})}
</>
);
};
export default Countries;
graphql-codegen
in place of the usual useQuery
hook from React Query library. Then, we looped through the response data to display the lists of countries, capital and continents.