【问题标题】:"gatsby-source-graphql" doesn't link to Drupal 8“gatsby-source-graphql”没有链接到 Drupal 8
【发布时间】:2020-05-01 02:28:57
【问题描述】:
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    {
      resolve: "gatsby-source-graphql",
      options: {
        // Arbitrary name for the remote schema Query type
        typeName: "DRUPAL",
        // Field under which the remote schema will be accessible. You'll use this in your Gatsby query
        fieldName: "drupal",
        // Url to query from
        url: "https://intl-pgs-rsm-growth-platform.pantheonsite.io/graphql",
      },
    },
  ],
}

这是我的 gatsby-config.js 文件 当我运行 gatsby clean && gatsby develop 或只是 gatsby build 我得到 ​​p>

success createSchemaCustomization - 0.005s

 ERROR #11321  PLUGIN

"gatsby-source-graphql" threw an error while running the sourceNodes lifecycle:

Unexpected token < in JSON at position 0



  ServerParseError: Unexpected token < in JSON at position 0

  - JSON.parse

  - index.js:35 
    [test-gatsby]/[apollo-link-http-common]/lib/index.js:35:25

  - next_tick.js:68 process._tickCallback
    internal/process/next_tick.js:68:7


not finished source and transform nodes - 0.506s

我的 Drupal 8 站点是安装了 graphql 模块的新站点。盖茨比网站也是全新的。

在工作了一段时间后,我周一开始遇到这个问题。我的 gatsby 没有代码更改,但突然之间我不再能够获取 drupal 数据。

似乎很少有使用“gatsby-source-graphql”插件的例子,所以如果有人可以帮忙,请做

【问题讨论】:

    标签: drupal graphql drupal-8 gatsby


    【解决方案1】:

    就我而言,这是 Drupal 中的权限问题。我通过转到 admin → people → 权限并为匿名用户设置与 GraphQL 相关的所有权限来解决它。

    【讨论】:

      【解决方案2】:

      Drupal Graph QL 模块requires authentication 使用令牌。您可以使用Simple OauthJWT。我使用 Simple Oauth 并通过以下步骤解决了我的问题:

      1. 使用 composer 安装 Simple Oauth 模块,以便安装它的依赖项$ composer require drupal/simple_oauth,然后启用该模块。

      2. 为您的第三方应用创建用户角色并为该角色分配内容查看权限

      3. admin/config/people/simple_oauth 中添加令牌过期时间,使用提供的按钮生成密钥(确保它们是在drupal Web 根目录之外生成的)并添加公钥和私钥文件的路径。

      4. admin/config/services/consumer添加一个新的消费者(或者使用默认的消费者)

      5. Secret 字段中添加密码并选择您在Scopes 下创建的新角色,然后保存配置页面。

      6. 使用 curl 或 postman 向您的站点 https://intl-pgs-rsm-growth-platform.pantheonsite.io/oauth/token 发出发布请求,以使用以下正文字段生成令牌:

      grant_type: password
      client_id: The client id generated `admin/config/services/consumer`
      client_secret: The secret you entered in step 4
      username: A user in your drupal site that has the role you created
      password: The password assigned to that account
      
      1. 如果成功,发布请求应返回访问令牌。将您的令牌添加到您的应用程序的 .env.development 文件并使用 Authorization 标头更新您的 gatsby-config.js 文件:(有关在 gatsby 应用程序中设置环境变量的更多信息可以找到 here
      {
          resolve: "gatsby-source-graphql",
          options: {
              typeName: "DRUPAL",
              fieldName: "drupal",
              url: "https://intl-pgs-rsm-growth-platform.pantheonsite.io/graphql",
              headers: {
                  "Authorization": `Bearer ${process.env.GATSBY_API_TOKEN}`
              },
          },
      },
      
      

      简单Oauth设置步骤的视频教程可以found here

      有关此设置过程的更多信息可以found here

      【讨论】:

        猜你喜欢
        • 2019-10-26
        • 2020-08-11
        • 2020-02-27
        • 1970-01-01
        • 2021-05-08
        • 2020-02-05
        • 2019-09-29
        • 2019-04-02
        • 2020-04-20
        相关资源
        最近更新 更多