【问题标题】:Gatsby/Wordpress: GraphQLError: Fragment cannot be spread here as objects of type "foo" can never be of type "bar"Gatsby/Wordpress:GraphQLError:片段不能在这里传播,因为“foo”类型的对象永远不能是“bar”类型
【发布时间】:2021-07-16 14:32:45
【问题描述】:

我正在使用 GraphQL 从 Wordpress 中的自定义 ACF 字段中提取信息到我的 Gatsby 网站。我的查询最初查询了三个 ACF 字段(ImageRow、TextRow 和 ImageTextRow),但是我随后删除了前两行并将它们从查询中删除。我那时。添加了一个名为 HeroRow 的新行,但是从那以后我一直收到此错误:

ERROR #85901  GRAPHQL

There was an error in your GraphQL query:

Fragment cannot be spread here as objects of type "WPGraphQL_Page_Customcontentblocks" can never be of
type "WPGraphQL_Page_Customcontentblocks_Customcontentblocks_HeroRow".

File: gatsby-node.js:82:28

我的查询如下:

    query {
        wpgraphql {
            pages(first: 5000, where: {status : PUBLISH}){
                nodes {
                    title
                    uri
                    status
                    content
                    customContentBlocks {
                      customcontentblocks {
                        ... on WPGraphQL_Page_Customcontentblocks_Customcontentblocks_ImageTextRow {
                            fieldGroupName
                            version
                            imageV1 {
                                sourceUrl
                            }
                            titleV1
                            subtitleV1
                            textV1
                            linksV1 {
                                links {
                                  target
                                  title
                                  url
                                }
                              }
                            imageV2 {
                                sourceUrl
                            }
                            titleV2
                            subtitleV2
                            textV2
                            listV2 {
                                listItem
                            }
                            ctaV2 {
                                target
                                title
                                url
                            }
                          }
                        }
                        ... on WPGraphQL_Page_Customcontentblocks_Customcontentblocks_HeroRow {
                            fieldGroupName
                            title
                            image {
                              sourceUrl
                            }
                            links {
                              link {
                                target
                                title
                                url
                              }
                            }
                        }
                    }
                }
            }
        }
    }

如果我单独查询ImageTextRow,我可以毫无问题地查询它,但每当包含 HeroRow 时,我都会收到相同的错误消息。

我已以与 ImageTextRow 布局相同的方式设置 HeroRow 布局(两者都是兄弟,位于 customContentBlocks 字段组下),因此我希望查询对两者的工作方式相同。

我还使用GraphiQL IDE 在 Wordpress 端测试了查询,并且该查询在 Wordpress 端运行良好,问题似乎出在 Gatsby 端。

【问题讨论】:

    标签: wordpress graphql gatsby


    【解决方案1】:

    我设法解决了这个问题,但我不完全确定为什么会解决它:

    通过在我的查询中更改不同布局的顺序,使有问题的布局 (HeroRow) 不是查询中的最后一个,一切正常:

        query {
            wpgraphql {
                pages(first: 5000, where: {status : PUBLISH}){
                    nodes {
                        title
                        uri
                        status
                        content
                        customContentBlocks {
                          customcontentblocks {
                            ... on WPGraphQL_Page_Customcontentblocks_Customcontentblocks_HeroRow {
                                fieldGroupName
                                title
                                image {
                                  sourceUrl
                                }
                                links {
                                  link {
                                    target
                                    title
                                    url
                                  }
                                }
                            }                        
                            ... on WPGraphQL_Page_Customcontentblocks_Customcontentblocks_ImageTextRow {
                                fieldGroupName
                                version
                                imageV1 {
                                    sourceUrl
                                }
                                titleV1
                                subtitleV1
                                textV1
                                linksV1 {
                                    links {
                                      target
                                      title
                                      url
                                    }
                                  }
                                imageV2 {
                                    sourceUrl
                                }
                                titleV2
                                subtitleV2
                                textV2
                                listV2 {
                                    listItem
                                }
                                ctaV2 {
                                    target
                                    title
                                    url
                                }
                              }
                            }
                        }
                    }
                }
            }
        }
    

    我已经添加了多个后续行,并且一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2017-07-21
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多