【问题标题】:How to access fetched data through graphql query?如何通过 graphql 查询访问获取的数据?
【发布时间】:2019-08-28 07:51:45
【问题描述】:

代码从服务器获取数据数组

fetch('/api/collections/get/posts?token=xxtokenxx', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        filter: {published:true},
    })
})
.then(res=>res.json())
.then(res => console.log(res));

我需要使用 Graphql 存储这些数据并获取特定数据,但我不知道如何通过 Gatsby js 实现这一点(如果可能的话)。

【问题讨论】:

  • 尝试添加更多屏幕截图。我没有看到您正在使用 GraphQL 端点。当你记录数据时你有什么?显示您在服务器上的内容。基本上,我建议你先玩 GraphQL Playground。
  • 您的意思是获取这些数据,然后将它们导入到 gatsby 中,以便在开发过程中使用 graphql 查询它们吗?

标签: reactjs rest graphql gatsby


【解决方案1】:

Gatsby 区分了内部 GraphQL API,您可以通过它在内部存储和查询数据,以及它使用的任何 API(例如由无头 CMS 提供的 API),您可以使用 source plugins 从中检索数据。

从您的 URI 的外观来看,您可能正在使用 Cockpit 作为 CMS,它有一个可用于 Gatsby 的源插件:gatsby-source-cockpit。安装插件:

npm install --save gatsby-plugin-cockpit

要配置插件,请将以下内容添加到 gatsby-config.js,将 http://localhost:8888 替换为您自己的 Cockpit 站点:

  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `src`,
        path: `${__dirname}/src/`,
      },
    },
    {
      resolve: 'gatsby-plugin-cockpit',
      options: {
        cockpitConfig: {
          baseURL: 'http://localhost:8888',
          folder: '/cockpit',
          accessToken: '4d659efb084077fd24aeb4871d4386',
          collections: ['posts'],
          regions: ['footer'],
          customComponents: [],
        },
      },
    },
  ],

现在,运行:

gatsby develop

您当地的 Gatsby 网站将在例如http://localhost:8000/,之后您可以导航到 http://localhost:8000/___graphql,在那里您可以使用 GraphiQL 探索您的 GraphQL 架构。

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 2021-11-05
    • 2020-05-24
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多