【问题标题】:fetching data from graphql to react从 graphql 获取数据以做出反应
【发布时间】:2021-04-04 03:43:12
【问题描述】:

我克隆了这个 React dashboard 并根据我的需要对其进行了调整,然后我想从 graphql 获取数据并将其显示在一个表中(订单和产品表,我将继续讨论订单),然后当用户点击一个特定的按钮它会打开一个页面,其中包含有关他选择的订单的所有特定详细信息! id 将从第一个查询(显示许多订单的订单)中获取,并将作为变量传递给第二个查询,即 order 以显示该订单的详细信息。 我想知道如何显示来自第一个查询的数据,然后是单个订单的具体数据

这是 UI 的外观(它已更改,但我只是想进一步澄清这个想法):

订单清单:

订单详情:

graphql:

已经创建了一个可能被重用的输入类型。(PaginationInput)

查询:

query Orders($input1: PaginationInput) {
   Orders(input: $input1){
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
    edges{
      cursor
      node {
        id
        closed
        email
        createdAt
        updatedAt
        cancelledAt
        displayFinancialStatus
        displayFulfillmentStatus
        lineItems{
          edges{
            node {
              customAttributes{
                key
                value
              }
              id
              quantity
              title
              variant{
                id
                image {
                  altText
                  id
                  src
                }
                title
                weight
                weightUnit
                price
              }
            }
          }
        }
        phone
        subtotalPrice
        totalPrice
        totalRefunded
        totalTax
        processedAt
      }
    }
  }
}

query Order($id: ID!){
  Order (id: $id){
    id
    displayFinancialStatus
        displayFulfillmentStatus
        email
        id
        createdAt
        subtotalPrice
        totalRefunded
        totalShippingPrice
        totalPrice
        totalTax
        updatedAt
        lineItems{
            edges {
                node {
                    customAttributes{
                        key
                        value
                    }
                    quantity
                    title
                    id
                    variant{
                        id
                        title
                        weight
                        weightUnit
                        price
                        image {
                            src
                        }
                    }
                }
            }
        }
        shippingAddress {
            address1
            address2
            city
            company
            country
            countryCode
            firstName
            id
            lastName
            name
            phone
            province
            provinceCode
            zip
        }
  }
}

查询变量示例:

{
  "input1": {
    "num": 30
  },
  "id": "gid://shopify/Order/order_id_here"
} 

我还是 graphql 的新手,所以我不知道该怎么做!

【问题讨论】:

    标签: reactjs graphql react-apollo graphql-js


    【解决方案1】:

    我不了解 graphql,但我可以在 UI 部分为您提供帮助,用于显示特定订单的数据。

    您有一个表,该表的每一行都有一个唯一索引键。从数据库中,我们可以使用此键区分所有行。因此,在显示表格时,您可以使用节点包作为 react-tables 或传统方法,

    1. 创建一个名为“Action”的列,并且每行都需要一个按钮。
    2. 然后在其 onPress() 上设置 id,然后使用该 id 调用 API,您将通过在后端运行查询来获取详细数据,但是,您可以使用模态显示数据或重定向到不同的页面。

    【讨论】:

      【解决方案2】:

      Apollo Client 是一个非常好的 React 库,可以使用 graphql 与后端服务器进行通信。

      学习Apollo Client's useQuery hook 应该能满足你的需要。

      最终,类似于

      import { gql, useQuery } from '@apollo/client';
      
      const GET_ORDERS = gql`
        query Orders($input1: PaginationInput) {
          Orders(input: $input1) {
            ...
          }
        }
      `
      
      // within a functional component
      const { loading, error, data } = useQuery(GET_ORDERS, {variables: yourQueryVariableObject});
      
      

      然后您可以在 UI 中显示data

      我建议您先熟悉 Apollo Client 如何使用较小的示例应用程序,然后再尝试调用像 Orders 这样的大型查询。

      【讨论】:

      • 在我的 cade 中我应该在 { variables 中写什么;你的查询变量对象} !!!你能根据我发布的信息给我一个答案吗?
      • yourQueryVariableObject 基本上就像您提供的查询变量示例。
      • 请不要像这样绕过 stackoverflow 要求您的应用程序代码的完整解决方案。社区充其量只能为您提供想法、提示和解释,说明您可以做什么。您应该努力研究文档,阅读其他人的示例,然后自己实施。
      • 是的,现在可以了,我可以获取所有订单数据,但我仍然坚持其他任务,当我点击它时添加一个按钮,它会打开显示订单详细信息的其他页面, Order 查询有一个 ID 变量,如帖子所示!所以我不知道如何将它传递给子组件(单顺序组件)
      • 再一次,这也是你应该弄清楚的。请阅读有关状态如何工作以及如何传递道具的一些基本知识。有很多解决方案,所以找到一个你可以使用的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2021-08-12
      • 2017-09-24
      相关资源
      最近更新 更多