简体   繁体   English

在 Gatsby 中使用 GraphQL 和 React 进行小型 API 查询

[英]Small API queries in Gatsby with GraphQL and React

I'm trying to import items from Airtable to Gatsby.我正在尝试将项目从 Airtable 导入到 Gatsby。 My table has close to 800 records.我的表有近 800 条记录。 It's not a heavy load per se, but I would like to render a few items at a time, with a "more" button to call the next batch.这本身并不是一个繁重的负载,但我想一次渲染几个项目,并带有一个“更多”按钮来调用下一批。 This is not obvious in Gatsby, since it seems to be directed to queries of smaller databases.这在 Gatsby 中并不明显,因为它似乎是针对小型数据库的查询。

What is the best course of action here?这里最好的做法是什么? I've tried adding a function to the button that updates the startIndex and endIndex variables, but the page won't render again, and simply attaching a forceUpdate to the Button will not work.我尝试将 function 添加到更新 startIndex 和 endIndex 变量的按钮,但页面不会再次呈现,并且简单地将forceUpdate附加到按钮将不起作用。 I've also tried moving the items list to a component but placing the button next to it always throws an error.我也尝试将项目列表移动到一个组件,但将按钮放在它旁边总是会引发错误。 Should I store the query in a const first?我应该先将查询存储在 const 中吗? Has anyone attempted this before?有没有人尝试过这个?


import { graphql } from "gatsby"

export const query = graphql`
  {
    allAirtable(sort: {order: ASC, fields: id}, limit: 100) {
      nodes {
        id
        data {
          Name
          Area
          Year
        }
      }
    }
  }
 ` 

let startIndex = 0;
let endIndex = 10;

const IndexPage = ({ data }) => {

  return(
  <div>
    <h1>Item list</h1>
    <p>Page count: {data.allAirtable.pageInfo.pageCount}</p>
    <ul>
      {data.allAirtable.nodes.slice([startIndex], [endIndex]).map(node => (

        <li key={node.id}>
          {node.data.Name} <br />
          {node.data.Area} <br />
          {node.data.Year} <br />
        </li>

      ))}
    </ul>
    <button>More</button>
  </div>
 )}

 export default IndexPage

Search for 'load more in gatsby' ...搜索'load more in gatsby' ...

'Initial' gatsby query is for generate static content (gatsby 'server' doesn't exists later) - 'load more' is dynamic, can't be done this way , only paginaltion possible - see gatsby docs. '初始' gatsby 查询用于生成 static 内容(gatsby '服务器' 稍后不存在) - '加载更多' 是动态的,不能这样做,只能分页 - 请参阅 gatsby 文档。 Also read about 'fetch build and run time' .另请阅读'fetch build and run time'

Dynamics can be done with 'normal' (for react - run time) graphql client (fe apollo)... or loading some prepared, generated (by gatsby) static json files with fetch/axios.动态可以使用“正常”(用于反应 - 运行时间)graphql 客户端(fe apollo)...或加载一些准备好的、生成的(由 gatsby)static Z466DEEC76ECDF5FCA6D38571/F6Zios24 文件来完成。

There are a few ways to do this.有几种方法可以做到这一点。 For some use cases it makes sense to run a GraphQL server that you query client side (eg with Apollo) and use GraphQL stitching in Gatsby to render initial content server side.对于某些用例,运行 GraphQL 服务器来查询客户端(例如使用 Apollo)并在 Gatsby 中使用 GraphQL 拼接来呈现初始内容服务器端是有意义的。 For something simpler, like loading more posts in a feed, you would be well served by pre-compiled responses for each of those expected queries.对于更简单的事情,例如在提要中加载更多帖子,您会很好地为每个预期查询的预编译响应提供服务。 I've done this in the past by querying for the data I want in gatsby-node.js and writing out JSON files with the chunked data (in public ) and issuing normal HTTP queries for those files as needed from the client-side.过去,我通过在 gatsby-node.js 中查询我想要的数据并使用分块数据( public )写出 JSON 文件并根据需要从客户端发出对这些文件的正常 HTTP 查询来完成此操作。 This should be relatively straightforward, but if you need more help or an example implementation let me know in the comments.这应该相对简单,但是如果您需要更多帮助或示例实现,请在评论中告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM