简体   繁体   English

Gatsby WordPress来源-GraphQl在不使用时忽略了灵活的内容布局

[英]Gatsby WordPress Source - Flexible Content layouts ignored by graphql when not used

Summary 摘要

I've setup ~15 different layouts in WordPress using the Flexible Content feature in ACF. 我已经使用ACF中的“灵活内容”功能在WordPress中设置了约15种不同的布局。 Because there are so many it means that some are not used by the content author. 因为数量太多,这意味着内容作者不使用某些内容。 When gatsby pulls in the content and generates the graphql data structure, it will create the structure based on the layouts that have been used. 当gatsby提取内容并生成graphql数据结构时,它将基于已使用的布局创建该结构。 When I set up my queries to pull in all the layouts I get an error because some layouts are queried that aren't present in the graphql structure (because they are not being used by the author). 当我设置查询以获取所有布局时,会收到一个错误,因为查询了一些在graphql结构中不存在的布局(因为作者没有使用它们)。 Here is an example of the query. 这是查询的示例。

acf {
    content_layouts_page {
        __typename
        ... on WordPressAcf_pullout_lead_with_list {
             ...plwlFragment
        }
        ... on WordPressAcf_client_list {
            ...clFragment
        }
        ... on WordPressAcf_titled_list_with_item_description {
             ...tlwidFragment
        }
        ...more layouts defined
    }
}

Question

How can I set up my graphql queries that it will be "future proof " for when the author decides to use an unused layout. 当作者决定使用未使用的布局时,如何设置我的graphql查询将是“面向未来的”。

Potential solution 潜在解决方案

This is a solution that I have thought of but I am not sure if it is possible. 这是我想到的一种解决方案,但不确定是否可行。 If I can just query the __typename , which will return an array. 如果我只能查询__typename ,它将返回一个数组。 Then dynamically construct the graphql query based on the present layouts. 然后根据当前布局动态构建graphql查询。

// __typename array = ['WordPressAcf_pullout_lead_with_list', 'on WordPressAcf_client_list']
let queryString = ''
__typename.forEach(layout => {
    switch (layout) {
        case 'WordPressAcf_pullout_lead_with_list':
            queryString += '...on WordPressAcf_pullout_lead_with_list { ...plwlFragment }'
            break;
        ....
    }
})

// Use queryString in graphql query

Again, not sure if this is possible, but it would solve a lot of problems for sure. 同样,不确定这是否可行,但可以肯定会解决很多问题。

Github Issue Github问题

This question was also asked on the Gatsby Github repo and guidance has been given. Gatsby Github存储库上也有人问了这个问题,并给出了指导。 Read more here 在这里阅读更多

As of 1st of March 2019 it is not possible to dynamically create the graphql queries. 自2019年3月1日起,无法动态创建graphql查询。 The solution for this problem is to have a "dummy" page with all your content layouts and ignoring it in your graphql query. 解决此问题的方法是创建一个包含所有内容布局的“虚拟”页面,并在graphql查询中将其忽略。 Although you are ignoring it, your graphql schema will contain all the layout structures. 尽管您忽略了它,但您的graphql模式将包含所有布局结构。

allWordpress(Page|Post)(filter:{slug:{ne:"dummy-page"}}) {
    edges {
        node {
            ...
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gatsby-source-wordpress 错误:“发出 GraphQL 请求时返回了空字符串而不是响应。” - gatsby-source-wordpress error: "An empty string was returned instead of a response when making a GraphQL request." 将 ACF 与 GraphQL 和 Gatsby 结合使用,灵活的内容不会返回其子块的顺序 - Using ACF with GraphQL and Gatsby, flexible content is not returning the order of it's child blocks 在 Gatsby 模板的查询中引用 GraphQL 变量? - Referencing GraphQL variable used in query in Gatsby template? gatsby-source-graphql和GraphQL Shopify Admin API错误 - gatsby-source-graphql and GraphQL Shopify Admin API error 如何循环遍历 Gatsby 中的 ACF 灵活内容? - How can I loop through ACF flexible content in Gatsby? gatsby-source-filesystem 在主题中使用时不会从站点目录中获取文件 - gatsby-source-filesystem not sourcing files from site directory when used in a theme Gatsby 源插件仅显示 GraphQL 数组中的最后一项 - Gatsby source plugin only showing last item in array in GraphQL Wordpress 发布内容图像的 Gatsby Image 默认值 - Gatsby Image defaults for Wordpress post content images 在 Zeit 上使用 gatsby-source-buttercms 部署 gatsby 应用程序时出错 - Error when deploying the gatsby app with gatsby-source-buttercms on Zeit Gatsby + Markdown:GraphQL 查询呈现为组件,但在导入页面时不呈现 - Gatsby + Markdown: GraphQL query renders as a component but not when imported into a page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM