未验证 提交 f53ee47b 编写于 作者: J JP Lew 提交者: GitHub

[cms-wordpress] Nest one-to-one relationships (#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: https://github.com/wp-graphql/wp-graphql/issues/347#issuecomment-639071772

After my changes, `npm run dev` and `npm run build` succeed without errors.
上级 ada41a8b
...@@ -11,9 +11,9 @@ export default function MoreStories({ posts }) { ...@@ -11,9 +11,9 @@ export default function MoreStories({ posts }) {
<PostPreview <PostPreview
key={node.slug} key={node.slug}
title={node.title} title={node.title}
coverImage={node.featuredImage} coverImage={node.featuredImage.node}
date={node.date} date={node.date}
author={node.author} author={node.author.node}
slug={node.slug} slug={node.slug}
excerpt={node.excerpt} excerpt={node.excerpt}
/> />
......
...@@ -70,14 +70,18 @@ export async function getAllPostsForHome(preview) { ...@@ -70,14 +70,18 @@ export async function getAllPostsForHome(preview) {
slug slug
date date
featuredImage { featuredImage {
sourceUrl node {
sourceUrl
}
} }
author { author {
name node {
firstName name
lastName firstName
avatar { lastName
url avatar {
url
}
} }
} }
} }
...@@ -121,10 +125,14 @@ export async function getPostAndMorePosts(slug, preview, previewData) { ...@@ -121,10 +125,14 @@ export async function getPostAndMorePosts(slug, preview, previewData) {
slug slug
date date
featuredImage { featuredImage {
sourceUrl node {
sourceUrl
}
} }
author { author {
...AuthorFields node {
...AuthorFields
}
} }
categories { categories {
edges { edges {
...@@ -156,7 +164,9 @@ export async function getPostAndMorePosts(slug, preview, previewData) { ...@@ -156,7 +164,9 @@ export async function getPostAndMorePosts(slug, preview, previewData) {
excerpt excerpt
content content
author { author {
...AuthorFields node {
...AuthorFields
}
} }
} }
} }
......
...@@ -22,9 +22,9 @@ export default function Index({ allPosts: { edges }, preview }) { ...@@ -22,9 +22,9 @@ export default function Index({ allPosts: { edges }, preview }) {
{heroPost && ( {heroPost && (
<HeroPost <HeroPost
title={heroPost.title} title={heroPost.title}
coverImage={heroPost.featuredImage} coverImage={heroPost.featuredImage.node}
date={heroPost.date} date={heroPost.date}
author={heroPost.author} author={heroPost.author.node}
slug={heroPost.slug} slug={heroPost.slug}
excerpt={heroPost.excerpt} excerpt={heroPost.excerpt}
/> />
......
...@@ -36,14 +36,14 @@ export default function Post({ post, posts, preview }) { ...@@ -36,14 +36,14 @@ export default function Post({ post, posts, preview }) {
</title> </title>
<meta <meta
property="og:image" property="og:image"
content={post.featuredImage?.sourceUrl} content={post.featuredImage?.node?.sourceUrl}
/> />
</Head> </Head>
<PostHeader <PostHeader
title={post.title} title={post.title}
coverImage={post.featuredImage} coverImage={post.featuredImage.node}
date={post.date} date={post.date}
author={post.author} author={post.author.node}
categories={post.categories} categories={post.categories}
/> />
<PostBody content={post.content} /> <PostBody content={post.content} />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册