【问题标题】:Querying Global Fields in Craft CMS with CraftQL and Gatsby JS使用 CraftQL 和 Gatsby JS 查询 Craft CMS 中的全局字段
【发布时间】:2018-11-07 18:07:56
【问题描述】:

我正在通过 CraftQL 插件在无头模式下使用 Craft。我的前端是使用 Gatsby JS 构建的。

我可以查询条目并将数据输出到我的模板,但是全局字段不可用。这是我的代码:

// gatsby-config.js
plugins: [
  {
        resolve: `gatsby-source-craftcms`,
        options: {
            endpoint: `http://cms.local/api`,
            token: `REDACTED`,
            query: `{
                globals: globals {
                    contact {
                        address
                    }
                },
                home: entries(section:[home]) {
                    id
                    title
                    ... on Home {
                        subHeading
                        intro
                        ctaButton {
                            ... on CtaButtonButton {
                                __typename
                                text
                                linkUrl
                            }
                        }
                    }
                },
// etc

然后在我的模板中:

export const query = graphql`
  query {
    home {
        title
        subHeading
        intro
        ctaButton {
            text
            linkUrl
        }
    }
    globals {
        contact {
            address
        }
    }
  }
`

在我的控制台中,我得到:

error  Cannot query field "global" on type "Query"

如果我从查询中删除global,我可以成功构建并输出data.home.title。

我已经尝试在 CMS 中使用 CraftQL 浏览器,并且可以成功查询全局变量:

我确信我遗漏了一些东西,但在 Gatsby-Source-Craft 或 CraftQL 的文档中找不到任何内容。

有人知道我在这里做错了什么吗??

【问题讨论】:

    标签: graphql gatsby craftcms


    【解决方案1】:

    所以昨天与 CraftQL 的制造商交谈后,我的错误是假设我需要使用 gatsby-source-craftcms 作为我的源插件。实际上,标准的gatsby-source-graphql 更好也更抽象,这意味着您不需要在gatsby-config 中构建巨大的graphQL 查询。它的文档也得到了更好的解释。

    现在我的gatsby-config.js 看起来像这样:

    plugins: [
        {
            resolve: `gatsby-source-graphql`,
            options: {
        typeName: "Craft",
        fieldName: "craft",
        // Url to query from
        url: "http://cms.local/api",
        // HTTP headers
        headers: {
          Authorization: `bearer ACCESSTOKENHERE`,
        }
            },
        },
    

    我的查询看起来像这样

    {
      craft {
        globals {
          contact {
            address
          }
        }
      }
    }
    

    这更理智,更接近普通的 Twig API

    您可以查看Twitter exchange here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2020-11-10
      • 2020-09-08
      • 2020-08-31
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多