简体   繁体   English

如何使用支持分页的获取 api 从反应中编写 graphQL 查询?

[英]How to write graphQL query from react using fetch api which support pagination?

Information:信息:

  • I am using graphene-Django based backed我正在使用基于石墨烯-Django 的支持
  • Using Relay Node-based query使用基于中继节点的查询

Tasks that I can perform:我可以执行的任务:

  • Write REST-based CRUD operations using fetch API in react.在 react 中使用 fetch API 编写基于 REST 的 CRUD 操作。
  • Simple GraphQL queries that involve fetching a list or single item from the server.涉及从服务器获取列表或单个项目的简单 GraphQL 查询。
  • Able to write GraphQL queries on GraphiQL能够在 GraphiQL 上编写 GraphQL 查询

Want to do the following:想做以下事情:

  • fetch a list of items from the server with pagination, that fetches say 10 items and the option to go to the first page, last page, Prev Page, and Next Page.使用分页从服务器获取项目列表,该列表获取 10 个项目和 go 的选项到第一页、最后一页、上一页和下一页。
  • few examples would help几个例子会有所帮助

Solved by looking into various documentation, blogs, and my current requirement as:通过查看各种文档、博客和我目前的要求来解决:

const Post = (props) => {
  const [isLoading, setLoading] = useState(true);
  const [posts, setPosts] = useState([])
  const [offset, setOffset] = useState(0)
  const [hasPrev, setHasPrev] = useState(false)
  const [hasNext, setHasNext] = useState(false)
  const pageSize = 10

  useEffect(() => {
    const loadData = () => {
      setLoading(true)
      const requestOptions = getGraphqlRequestOptions(pageSize, offset)
      fetch(graphql_url, requestOptions)
        .then(response => {
          setLoading(false)
          return response.json()
        })
        .then(data => {
          setHasNext(data.data.allPosts.pageInfo.hasNextPage)
          setPosts(data.data.allPosts.edges)
          setHasPrev(offset !== 0)
        })
    }
    loadData()
  }, [offset]);

  const handleNextClick = () => {
    setOffset(offset + pageSize)
  };
  const handlePrevClick = () => {
    setOffset(offset - pageSize)
  };
  useEffect(()=> {
    document.title = "MyTitle"
    setOffset(0)
  }, []);

  return (
    <>
      ... code to display table
      <div className="row mb-5">
        <Button 
          variant="outline-primary"
          className="col-1 me-auto"
          disabled={!(hasPrev || isLoading)}
          size="sm" block
          onClick={!isLoading ? handlePrevClick : null}
        >
          {isLoading ?  'Loading...' : 'Previous'}
        </Button>
        <Button 
          className="col-1 ms-auto"
          disabled={!(hasNext || isLoading)}
          onClick={!isLoading ? handleNextClick: null}
          variant="outline-primary" size="sm" block
        >
          {isLoading ?  'Loading...' : 'Next'}
        </Button>
      </div>
    </>
  }

I was stuck because there were many options and no concrete example to implement this.我被卡住了,因为有很多选择并且没有具体的例子来实现它。
For example:例如:

  • We can use:我们可以用:
    1. query{allPosts(first:10, after:"last cursor"){pageInfo{hasPrevPage hasNextPage startCursor endCursor} edge{node{...}}}
    2. query{allPosts(first:10, offset:0){}} // currently used
    3. `query {allPosts(first:10, before:'start cursor' `query {allPosts(first:10, before:'start cursor'
    4. All above queries with last:10 instead of first:10, etc.以上所有查询都使用last:10而不是 first:10 等。
  • Beside this it returns a PageInfo object having hasNextPage, hasPrevPage, startCursor, endCursor .除此之外,它返回一个 PageInfo object 具有hasNextPage, hasPrevPage, startCursor, endCursor
    • It was giving informations that were hard to understand它提供了难以理解的信息
    • Using query (1) ie with first and after option, hasPrevPage was always false使用查询 (1) 即带有firstafter选项, hasPrevPage始终为false
    • Using query(3) ie with before option, hasNextPage was always false使用 query(3) 即带有before选项, hasNextPage始终为false
    • So need to track whether the Next and Prev page exists was difficult.因此需要跟踪 Next 和 Prev 页面是否存在是很困难的。

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

相关问题 如何使用 React 查询从 Web API 获取数据? - How to fetch data from Web API using React query? 使用 React UseEffect() 从 GraphQL 获取数据 - Using React UseEffect() to fetch data from GraphQL 如何将 Graphql 查询从 Wordpress API 中的反应组件而不是 GatsbyJS 中的模板中获取数据? - How to put Graphql queries to fetch data from Wordpress API inside react components instead of templates in GatsbyJS? 反应查询 useQuery,GraphQL + fetch。 如何传递变量? - React-query useQuery, GraphQL + fetch. How to pass variables? 如何在 React js 中为 GraphQL 中的链接表编写变异查询? - How to write mutation query for inter linked table in GraphQL in React js? 使用axios从其中具有API属性的API中获取数据 - Using axios to fetch data from an API which has an API attribute in it in react 如何从 GraphQL API 获取数据并将其传递给其他函数文件? - How to fetch data from GraphQL API and pass it to other function file? 如何从graphql查询中仅获取特定数据 - how to fetch only specific data from graphql query 如何在 React 中编写服务类型的函数,该函数可以使用函数的输入参数获取然后返回数据 - how to write service kind of function in React which can fetch then return data using input params of function 如何使用 axois 从 React 中的 api 获取数据? - How to fetch data from api in React by using axois?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM