Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
feat: create and expose cursor encoding helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonfancher committed Jul 12, 2019
1 parent eb0fa26 commit a1959ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/pagination/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { graphql, Link } from 'gatsby';
import { get } from 'lodash';
import React, { useEffect, useRef } from 'react';
import { getCursorFromDocumentIndex } from 'gatsby-source-prismic-graphql';
import Layout from '../components/layout';

export const query = graphql`
Expand Down Expand Up @@ -46,7 +47,7 @@ const Home = props => {
}

props.prismic
.load({ variables: { after: btoa(`arrayconnection:${page}`) } })
.load({ variables: { after: getCursorFromDocumentIndex(page) } })
.then(res => setData(res.data));
}, [page]);

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-source-prismic-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { withPreview } from './components/withPreview';
export {
PrismicLink,
getCookies,
getCursorFromDocumentIndex,
getDocumentIndexFromCursor,
registerLinkResolver,
linkResolver,
fieldName,
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby-source-prismic-graphql/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export function getCookies() {
return parseQueryString(document.cookie, ';');
}

export function getDocumentIndexFromCursor(cursor: string) {
return atob(cursor).split(':')[1];
}

export function getCursorFromDocumentIndex(index: number) {
return btoa(`arrayconnection:${index}`);
}

export function fetchStripQueryWhitespace(url: string, ...args: any) {
const [hostname, qs = ''] = url.split('?');
const queryString = parseQueryString(qs);
Expand Down

0 comments on commit a1959ac

Please sign in to comment.