Skip to content

Commit 6e91505

Browse files
authored
feat: add totalCount in getContents (#13)
BREAKING CHANGE: `getContents` now returns `{ data: [...], totalCount: X }` instead of `[...]`
1 parent 511f91c commit 6e91505

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

examples/wordpress/src/_app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function Page({ Component, ...props }) {
1414
return (
1515
<Query query={PAGE}>
1616
{({ data }) =>
17-
data && (
17+
data.page && (
1818
<>
1919
<h1>{data.page.title}</h1>
2020
<Component {...props} />

examples/wordpress/src/pages/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ export const contentFragment = gql`
1616
name
1717
}
1818
allBooks {
19-
metadata {
20-
id
21-
slug
19+
totalCount
20+
data {
21+
metadata {
22+
id
23+
slug
24+
}
25+
name
2226
}
23-
name
2427
}
2528
link {
2629
url
@@ -48,7 +51,8 @@ export default function Page({ title, book, specificBook, allBooks, blocks }) {
4851
<div>{specificBook && specificBook.name}</div>
4952
<h2>All books</h2>
5053
<ul>
51-
{allBooks.map(b => (
54+
{allBooks.totalCount}
55+
{allBooks.data.map(b => (
5256
<li key={b.metadata.id}>
5357
<Link waitBeforeTransition to={`/books/${b.metadata.slug}`}>
5458
{b.name}

examples/wordpress/src/schemas/contents/Page.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ export const typeDefs = gql`
55
title: String @field
66
book: Book @field
77
specificBook: Book
8-
allBooks: [Book]
8+
allBooks: BookResult!
99
date: Date @field
1010
dateTime: DateTime @field
1111
link: Link @field
1212
blocks: [Block] @field
1313
file: Media @field
1414
}
15+
16+
type BookResult {
17+
totalCount: Int!
18+
data: [Book!]!
19+
}
1520
`
1621

1722
export const resolvers = {

packages/smooth-backend-wordpress/src/acf/api.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,23 @@ export function createClient({ baseUrl, defaultLanguage }) {
2626
{ params },
2727
)
2828
if (data && data.status !== 'publish') return []
29-
return data ? [data] : []
29+
return data
30+
? { totalCount: 1, data: [data] }
31+
: { totalCount: 0, data: [] }
3032
}
3133

32-
const { data } = await axios.get(
34+
const { data, headers } = await axios.get(
3335
`${baseUrl}/wp-json/presspack/v1/contents/`,
3436
{ params, paramsSerializer },
3537
)
36-
return data
38+
return {
39+
totalCount: Number(headers['x-wp-total']),
40+
data,
41+
}
3742
},
3843
async getContent(options) {
39-
const results = await this.getContents(options)
40-
return results[0] || null
44+
const { data } = await this.getContents(options)
45+
return data[0] || null
4146
},
4247
async getContentPreview({ lang, id }) {
4348
const params = getParams({ lang: lang || defaultLanguage })

packages/smooth-backend-wordpress/src/acf/resolvers/createDefaultResolvers.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ function getQueryFieldResolver(node, helpers, state) {
100100
})
101101
}
102102

103-
const res = await state.apiClient.getContent({ type, lang, slug })
104-
105-
return res
103+
return state.apiClient.getContent({ type, lang, slug })
106104
},
107105
}
108106
}

0 commit comments

Comments
 (0)