graphql-react
A GraphQL client for React using modern context and hooks APIs that’s lightweight (< 4 kB) but powerful; the first Relay and Apollo alternative with server side rendering.
The exports can also be used to custom load, cache and server side render any data, even from non-GraphQL sources.
Installation
Note
For a Next.js project, see the next-graphql-react
installation instructions.
For Node.js, to install graphql-react
and its react
peer dependency with npm, run:
npm install graphql-react react
For Deno and browsers, an example import map (realistically use 4 import maps, with optimal URLs for server vs client and development vs production):
{
"imports": {
"extract-files/": "https://unpkg.com/extract-files@13.0.0/",
"graphql-react/": "https://unpkg.com/graphql-react@20.0.0/",
"is-plain-obj": "https://unpkg.com/is-plain-obj@4.1.0/index.js",
"is-plain-obj/": "https://unpkg.com/is-plain-obj@4.1.0/",
"react": "https://esm.sh/react@18.2.0",
"react-waterfall-render/": "https://unpkg.com/react-waterfall-render@5.0.0/"
}
}
These dependencies might not need to be in the import map, depending on what graphql-react
modules the project imports from:
Polyfill any required globals (see Requirements) that are missing in your server and client environments.
Create a single Cache
instance and use the component Provider
to provide it for your app.
To server side render your app, use the function waterfallRender
from react-waterfall-render
.
Examples
Here is a basic example using the GitHub GraphQL API, with tips commented:
import useAutoLoad from "graphql-react/useAutoLoad.mjs";
import useCacheEntry from "graphql-react/useCacheEntry.mjs";
import useLoadGraphQL from "graphql-react/useLoadGraphQL.mjs";
import useWaterfallLoad from "graphql-react/useWaterfallLoad.mjs";
import React from "react";
const query = `
query ($repoId: ID!) {
repo: node(id: $repoId) {
... on Repository {
stargazers {
totalCount
}
}
}
}
`;
export default function GitHubRepoStars({ repoId }) {
const cacheKey = `GitHubRepoStars-${repoId}`;
const cacheValue = useCacheEntry(cacheKey);
const loadGraphQL = useLoadGraphQL();
const load = React.useCallback(
() =>
loadGraphQL(
cacheKey,
"https://api.github.com/graphql",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
},
body: JSON.stringify({
query,
variables: {
repoId,
},
}),
}
),
[cacheKey, loadGraphQL, repoId]
);
useAutoLoad(cacheKey, load);
const isWaterfallLoading = useWaterfallLoad(cacheKey, load);
return isWaterfallLoading
? null
: cacheValue
? cacheValue.errors
?
"Error!"
: cacheValue.data.repo.stargazers.totalCount
:
"Loading…";
}
Requirements
Supported runtime environments:
Consider polyfilling:
Non Deno projects must configure TypeScript to use types from the ECMAScript modules that have a // @ts-check
comment:
Exports
The npm package graphql-react
features optimal JavaScript module design. It doesn’t have a main index module, so use deep imports from the ECMAScript modules that are exported via the package.json
field exports
: