-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprismic-client.js
65 lines (57 loc) · 2 KB
/
prismic-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { InMemoryCache, IntrospectionFragmentMatcher } = require("apollo-cache-inmemory");
const ApolloClient = require("apollo-client").ApolloClient;
const gql = require("graphql-tag");
const PrismicLink = require("apollo-link-prismic").PrismicLink;
const _get = require('lodash.get');
const queries = require('./graphql/queries');
const introspectionQueryResultData = require('./schema/fragmentTypes.json');
const fragmentMatcher = new IntrospectionFragmentMatcher({ introspectionQueryResultData });
const client = new ApolloClient({
link: PrismicLink({
uri: `https://${process.env.PRISMIC_REPO_NAME}.cdn.prismic.io/graphql`,
accessToken: process.env.PRISMIC_ACCESS_TOKEN
}),
cache: new InMemoryCache({ fragmentMatcher })
});
const getBasePages = () => {
return new Promise((resolve, reject) => {
client.query({
query: gql`${queries.basePages}`
}).then(response => {
resolve(response.data);
}).catch(reject);
})
}
// need to keep going while pageInfo.hasNextPage is true
// then fold in
const entryQuery = (query = queries.firstEntries, edges = []) => {
return new Promise((resolve, reject) => {
client.query({
query: gql`${query}`
}).then((response) => {
const newEdges = _get(response, 'data.allEntrys.edges', []);
edges.push(...newEdges);
hasNextPage = _get(response, 'data.allEntrys.pageInfo.hasNextPage', false);
const lastEntryCursor = _get(newEdges[newEdges.length - 1], 'cursor');
if (hasNextPage) {
resolve(entryQuery(queries.entries(lastEntryCursor), edges));
} else {
resolve(edges);
}
}).catch(reject);
})
}
const getEntries = () => {
return new Promise(async (resolve, reject) => {
try {
const edges = await entryQuery();
resolve(edges);
} catch (e) {
reject(e);
}
})
}
module.exports = {
getBasePages,
getEntries
}