Gatsby Wordpress在GraphQL模式中使用createTypes来缺少ACF字段

时间:2019-05-24 07:56:20

标签: graphql gatsby

我正在使用gatsby-source-wordpress。如果我有一个ACF转发器字段,则在另一个字段(例如Link)中,如果任何帖子的字段为空,则会出现错误-例如: “-类型为'wordpress__POSTAcf'的未知字段'partners'。”

我的graphQL如下所示

{
 allWordpressPost {
   edges {
     node {
       title
       acf {
         project_students
         partners {
           partner_link {
             title
             url
             target
           }
         }
       }
     }
   }
 }

}

我一直在尝试针对ACF字段执行此操作,以便至少在我可以在JS中处理的字段上获得空值或null,现在Gatsby不会构建。这是我对Wordpress不太了解的Gatsby文档:

https://www.gatsbyjs.org/blog/2019-03-04-new-schema-customization/ https://www.gatsbyjs.org/docs/actions/#createTypes

我已经在gatsby-node.js中尝试了以下方法,但到目前为止还没有成功!

// using Gatsby Type Builder API
exports.sourceNodes = ({ actions, schema }) => {
  const { createTypes } = actions;
  const typeDefs = [
    schema.buildObjectType({
      name: "wordpressPost",
      acf: {
        partners: {
          partner_link: {
            title: "String!",
            url: "String!",
            target: "String!",
          }
        }
      },
      interfaces: ["Node"],
      extensions: {
        infer: false
      }
    })
  ];
  createTypes(typeDefs);
};

1 个答案:

答案 0 :(得分:0)

TL; DR: 在您的源代码处将其添加到您的function.php中:

function nullify_empty($value, $post_id, $field)
{
    if (empty($value)) {
        return null;
    }

    return $value;
}

add_filter('acf/format_value/type=image', 'nullify_empty', 100, 3);
add_filter('acf/format_value/type=relationship', 'nullify_empty', 100, 3);
// not sure if gallery is internally named gallery as well but this should work
add_filter('acf/format_value/type=gallery', 'nullify_empty', 100, 3); 

我也有同样的错误,这是我的票证解决了这个错误:https://github.com/gatsbyjs/gatsby/issues/4461