Comments (4)
Fantastic, this gives me plenty to work with. Thanks!
from graphql-react.
There is a great way, but it it's a bit tricky to explain right now. Essentially, you use recursion to render pages. Each page has its own query, and if that page is cached it renders, if not a load button is rendered instead and recursion stops.
from graphql-react.
Here is an example, you can see it live at https://whimsy.art/art
import { ButtonSubmit, Heading, Scroll } from 'device-agnostic-ui';
import PropTypes from 'prop-types';
import React from 'react';
import { Header } from '../../components/Header';
import { LinkCardArtwork } from '../../components/LinkCardArtwork';
import { Meta } from '../../components/Meta';
import { PageLoader } from '../../components/PageLoader';
import {
CARD_ARTWORK_IMAGE_HEIGHT,
CARD_ARTWORK_IMAGE_WIDTH,
CARD_PAGINATION_LIMIT,
LOCALE,
PIXEL_DENSITY,
} from '../../config';
import { useWhimsyAPI } from '../../hooks/useWhimsyAPI';
const fragmentArtworkConnectionFields = /* GraphQL */ `
fragment queryArtworkConnectionFields on QueryArtworkConnection {
page(
cursor: $cursor
limit: ${CARD_PAGINATION_LIMIT}
ascending: false
) {
hasNextPage
cursorLast
edges {
node {
id
title
price {
format
asCurrency {
format(locale: "${LOCALE}")
}
}
sold
image {
srcWebp: url(
width: ${CARD_ARTWORK_IMAGE_WIDTH}
height: ${CARD_ARTWORK_IMAGE_HEIGHT}
devicePixelRatio: ${PIXEL_DENSITY}
format: WEBP
resourceHints: { preload: true }
)
srcFallback: url(
width: ${CARD_ARTWORK_IMAGE_WIDTH}
height: ${CARD_ARTWORK_IMAGE_HEIGHT}
devicePixelRatio: ${PIXEL_DENSITY}
format: JPG
)
}
}
}
}
}
`;
const queryPaginatedArtworks = /* GraphQL */ `
${fragmentArtworkConnectionFields}
query($cursor: ID!) {
artworks {
...queryArtworkConnectionFields
}
}
`;
const PaginatedArtworks = ({ page, loadedEdges = page.edges }) => {
const operation = React.useMemo(
() => ({
variables: { cursor: page.cursorLast },
query: queryPaginatedArtworks,
}),
[page.cursorLast]
);
const { load, loading, cacheValue: { data } = {} } = useWhimsyAPI({
loadOnReload: true,
operation,
});
return data ? (
<PaginatedArtworks
page={data.artworks.page}
loadedEdges={[...loadedEdges, ...data.artworks.page.edges]}
/>
) : (
<>
{loadedEdges.map(({ node: { id, title, price, sold, image } }) => (
<LinkCardArtwork
key={id}
artworkId={id}
title={title}
priceBitcoin={price.format}
priceCurrency={price.asCurrency.format}
sold={sold}
imageSrcWebp={image.srcWebp}
imageSrcFallback={image.srcFallback}
/>
))}
{page.hasNextPage && (
<ButtonSubmit onClick={load} loading={loading}>
More
</ButtonSubmit>
)}
</>
);
};
PaginatedArtworks.propTypes = {
page: PropTypes.shape({
cursorLast: PropTypes.string,
hasNextPage: PropTypes.bool.isRequired,
edges: PropTypes.array,
}).isRequired,
loadedEdges: PropTypes.array,
};
const queryArtPage = /* GraphQL */ `
${fragmentArtworkConnectionFields}
query($cursor: ID) {
artworks {
count
...queryArtworkConnectionFields
}
}
`;
const ArtPage = () => {
const operation = React.useMemo(
() => ({
query: queryArtPage,
}),
[]
);
const pageQueryStatus = useWhimsyAPI({
loadOnMount: true,
loadOnReload: true,
loadOnReset: true,
operation,
});
return (
<>
<Meta title="Art" description="All artworks." />
<PageLoader
graphqlOperationStatus={pageQueryStatus}
renderData={(data) => (
<>
<Header>
<Heading size={1}>
{data.artworks.count} artwork
{data.artworks.count === 1 ? '' : 's'}
</Heading>
</Header>
{!!data.artworks.page.edges.length && (
<Scroll>
<PaginatedArtworks page={data.artworks.page} />
</Scroll>
)}
</>
)}
/>
</>
);
};
export default ArtPage;
from graphql-react.
You can see another pagination example in production here (the reviews):
https://www.bookwell.com.au/venue/kis-hair/melbourne/3000#reviews
from graphql-react.
Related Issues (20)
- hello can you convert for native js? HOT 6
- Document the `load` function HOT 3
- Support automatic persisted queries HOT 2
- How should you implement auth using this library? HOT 8
- Possibly missing files within package HOT 2
- Add ability to load on mount only if no cache HOT 2
- How to move gql query to useGraphQL operation query HOT 1
- Publish the GraphQL client in a separate package HOT 5
- Wrong type calculation on useGraphQL HOT 1
- How to use useGraphQL hooks when handleLoadMore functions trigger? HOT 1
- Allow React component displayName and propTypes to be removed in production builds HOT 3
- New composable React hooks HOT 2
- Add a license file HOT 3
- Add ability to clear not currently subscribed cache
- Switch to user defined cache keys
- Publish SSR functionality in a separate package
- error - unhandledRejection: TypeError: Head.rewind is not a function -> can't get the official example to work HOT 3
- How to use useLoading() to get the old loading from useGraphql()? HOT 4
- Clear code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-react.