Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

(examples) Add with-graphcache-pagination (#1593)

* (examples) Add with-graphcache-pagination

Changed files
+177 -1
examples
with-graphcache-pagination
with-pagination
+22
examples/with-graphcache-pagination/README.md
···
+
# Integrating with React
+
+
Integrating urql is as simple as:
+
+
1. Install packages [getting started](https://formidable.com/open-source/urql/docs/basics/react-preact/)
+
+
```sh
+
yarn add urql graphql
+
# or
+
npm install --save urql graphql
+
```
+
+
2. Install [graphcache](https://formidable.com/open-source/urql/docs/graphcache/)
+
```sh
+
yarn add @urql/exchange-graphcache
+
# or
+
npm install --save @urql/exchange-graphcache
+
```
+
+
3. Setting up the Client [here](src/App.js)
+
+
4. Execute the Query [here](src/pages/PaginatedNpmSearch.js)
+39
examples/with-graphcache-pagination/package.json
···
+
{
+
"name": "react-urql-query",
+
"version": "1.0.0",
+
"private": true,
+
"dependencies": {
+
"@urql/exchange-graphcache": "^4.0.0",
+
"graphql": "^15.5.0",
+
"react": "^17.0.2",
+
"react-dom": "^17.0.2",
+
"urql": "^2.0.2"
+
},
+
"devDependencies": {
+
"react-scripts": "4.0.3"
+
},
+
"scripts": {
+
"start": "react-scripts start",
+
"build": "react-scripts build",
+
"test": "react-scripts test",
+
"eject": "react-scripts eject"
+
},
+
"eslintConfig": {
+
"extends": [
+
"react-app",
+
"react-app/jest"
+
]
+
},
+
"browserslist": {
+
"production": [
+
">0.2%",
+
"not dead",
+
"not op_mini all"
+
],
+
"development": [
+
"last 1 chrome version",
+
"last 1 firefox version",
+
"last 1 safari version"
+
]
+
}
+
}
+30
examples/with-graphcache-pagination/src/App.js
···
+
import { createClient, Provider, dedupExchange, fetchExchange } from "urql";
+
import { cacheExchange } from '@urql/exchange-graphcache';
+
import { relayPagination } from '@urql/exchange-graphcache/extras';
+
+
import PaginatedNpmSearch from "./pages/PaginatedNpmSearch";
+
+
const client = createClient({
+
url: "https://trygql.dev/graphql/relay-npm",
+
exchanges: [
+
dedupExchange,
+
cacheExchange({
+
resolvers: {
+
Query: {
+
search: relayPagination(),
+
},
+
},
+
}),
+
fetchExchange
+
]
+
});
+
+
function App() {
+
return (
+
<Provider value={client}>
+
<PaginatedNpmSearch />
+
</Provider>
+
);
+
}
+
+
export default App;
+13
examples/with-graphcache-pagination/src/index.css
···
+
body {
+
margin: 0;
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
+
sans-serif;
+
-webkit-font-smoothing: antialiased;
+
-moz-osx-font-smoothing: grayscale;
+
}
+
+
code {
+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
+
monospace;
+
}
+12
examples/with-graphcache-pagination/src/index.js
···
+
import React from 'react';
+
import ReactDOM from 'react-dom';
+
import './index.css';
+
import App from './App';
+
+
ReactDOM.render(
+
<React.StrictMode>
+
<App />
+
</React.StrictMode>,
+
document.getElementById('root')
+
);
+
+60
examples/with-graphcache-pagination/src/pages/PaginatedNpmSearch.js
···
+
import React, { useState } from "react";
+
import { gql, useQuery } from "urql";
+
+
const limit = 5;
+
const query = "graphql";
+
+
const NPM_SEARCH = gql`
+
query Search($query: String!, $first: Int!, $after: String) {
+
search(query: $query, first: $first, after: $after) {
+
edges {
+
node {
+
id
+
name
+
}
+
}
+
pageInfo {
+
hasNextPage
+
endCursor
+
}
+
}
+
}
+
`;
+
+
+
const PaginatedNpmSearch = () => {
+
const [after, setAfter] = useState("");
+
+
const [result] = useQuery({
+
query: NPM_SEARCH,
+
variables: { query, first: limit, after }
+
});
+
+
const { data, fetching, error } = result;
+
+
const searchResults = data?.search;
+
+
return (
+
<div>
+
{error && <p>Oh no... {error.message}</p>}
+
+
{fetching && <p>Loading...</p>}
+
+
{searchResults && (
+
<>
+
{searchResults.edges.map(({ node }) => (
+
<div key={node.id}>{node.id}: {node.name}</div>
+
))}
+
+
{searchResults.pageInfo.hasNextPage && (
+
<button onClick={() => setAfter(searchResults.pageInfo.endCursor)}>
+
load more
+
</button>
+
)}
+
</>
+
)}
+
</div>
+
);
+
};
+
+
export default PaginatedNpmSearch;
+1 -1
examples/with-pagination/README.md
···
2. Setting up the Client [here](src/App.js)
-
3. Execute the Query [here](src/pages/PokemonList.js)
+
3. Execute the Query [here](src/pages/PaginatedNpmSearch.js)