[英]Gatsby WordPress Source - Flexible Content layouts ignored by graphql when not used
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
}
}
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查询将是“面向未来的”。
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. 同样,不确定这是否可行,但可以肯定会解决很多问题。
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.