Skip to content

Commit f53ee47

Browse files
author
JP Lew
authored
[cms-wordpress] Nest one-to-one relationships (vercel#15007)
There is a bug in the cms-wordpress example due to a Wordpress plugin dependency. After running `npm install`, then either `npm run dev` or `npm run build`, the following errors appear in the console: ```shell > cms-wordpress@1.0.0 dev /Users/jplew/Sites/projects/next.js/examples/cms-wordpress > next ready - started server on http://localhost:3000 info - Loaded env from /Users/jplew/Sites/projects/next.js/examples/cms-wordpress/.env.local event - compiled successfully event - build page: / wait - compiling... event - build page: /next/dist/pages/_error event - compiled successfully [ { message: 'Cannot query field "name" on type "NodeWithAuthorToUserConnectionEdge". Did you mean "node"?', extensions: { category: 'graphql' }, locations: [ [Object] ] }, { message: 'Cannot query field "firstName" on type "NodeWithAuthorToUserConnectionEdge".', extensions: { category: 'graphql' }, locations: [ [Object] ] }, { message: 'Cannot query field "lastName" on type "NodeWithAuthorToUserConnectionEdge".', extensions: { category: 'graphql' }, locations: [ [Object] ] }, { message: 'Cannot query field "avatar" on type "NodeWithAuthorToUserConnectionEdge".', extensions: { category: 'graphql' }, locations: [ [Object] ] } ] Error: Failed to fetch API at fetchAPI (webpack-internal:///./lib/api.js:31:11) ``` The reason for this is `wp-graphql` released version v0.10.0 ten days ago which introduced a number of breaking changes (https://github.com/wp-graphql/wp-graphql/releases/tag/v0.10.0). Specifically, this is the change that breaks the current example: > - One to One relationships are now nested. For example post.author and post.featuredImage now return an edge/node instead of the node directly. More info about this change can be found here: wp-graphql/wp-graphql#347 (comment) After my changes, `npm run dev` and `npm run build` succeed without errors.
1 parent ada41a8 commit f53ee47

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

examples/cms-wordpress/components/more-stories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export default function MoreStories({ posts }) {
1111
<PostPreview
1212
key={node.slug}
1313
title={node.title}
14-
coverImage={node.featuredImage}
14+
coverImage={node.featuredImage.node}
1515
date={node.date}
16-
author={node.author}
16+
author={node.author.node}
1717
slug={node.slug}
1818
excerpt={node.excerpt}
1919
/>

examples/cms-wordpress/lib/api.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,18 @@ export async function getAllPostsForHome(preview) {
7070
slug
7171
date
7272
featuredImage {
73-
sourceUrl
73+
node {
74+
sourceUrl
75+
}
7476
}
7577
author {
76-
name
77-
firstName
78-
lastName
79-
avatar {
80-
url
78+
node {
79+
name
80+
firstName
81+
lastName
82+
avatar {
83+
url
84+
}
8185
}
8286
}
8387
}
@@ -121,10 +125,14 @@ export async function getPostAndMorePosts(slug, preview, previewData) {
121125
slug
122126
date
123127
featuredImage {
124-
sourceUrl
128+
node {
129+
sourceUrl
130+
}
125131
}
126132
author {
127-
...AuthorFields
133+
node {
134+
...AuthorFields
135+
}
128136
}
129137
categories {
130138
edges {
@@ -156,7 +164,9 @@ export async function getPostAndMorePosts(slug, preview, previewData) {
156164
excerpt
157165
content
158166
author {
159-
...AuthorFields
167+
node {
168+
...AuthorFields
169+
}
160170
}
161171
}
162172
}

examples/cms-wordpress/pages/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export default function Index({ allPosts: { edges }, preview }) {
2222
{heroPost && (
2323
<HeroPost
2424
title={heroPost.title}
25-
coverImage={heroPost.featuredImage}
25+
coverImage={heroPost.featuredImage.node}
2626
date={heroPost.date}
27-
author={heroPost.author}
27+
author={heroPost.author.node}
2828
slug={heroPost.slug}
2929
excerpt={heroPost.excerpt}
3030
/>

examples/cms-wordpress/pages/posts/[slug].js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export default function Post({ post, posts, preview }) {
3636
</title>
3737
<meta
3838
property="og:image"
39-
content={post.featuredImage?.sourceUrl}
39+
content={post.featuredImage?.node?.sourceUrl}
4040
/>
4141
</Head>
4242
<PostHeader
4343
title={post.title}
44-
coverImage={post.featuredImage}
44+
coverImage={post.featuredImage.node}
4545
date={post.date}
46-
author={post.author}
46+
author={post.author.node}
4747
categories={post.categories}
4848
/>
4949
<PostBody content={post.content} />

0 commit comments

Comments
 (0)