简体   繁体   English

如何从基于类的反应组件中查询 graphql endpint

[英]How to query graphql endpint from a class based react component

The Load product is maybe we call it hook whose purpose of the life is fetch the data from a graphql backend Load 产品也许我们称之为钩子,其生命的目的是从 graphql 后端获取数据

Here its how LOAD_PRODUCT LOOKS LIKE这是 LOAD_PRODUCT 的样子

import { gql } from '@apollo/client'

export const LOAD_PRODUCTS = gql`
    query{
        categories {
            name
            products {
              id,
              name,
              inStock,
              gallery,
              category
            }
          }
    }
`

import React, { Component } from 'react'
import { useQuery,gql } from '@apollo/client'
import { LOAD_PRODUCTS } from '../../graphql/productAction'
export class ProductListing extends Component {

  constructor(){
    super();
    const {error,loading,data} = useQuery()

  }

  render() {
    return (
      <div>ProductListing</div>
    )
  }
}

export default ProductListing

for now i just want to fire the load user hook and save set the data to the different state there must be a method to do this i search on google but nothing help i just cant use fetch method to get the result现在我只想触发加载用户挂钩并将数据保存为不同的状态必须有一种方法可以做到这一点我在谷歌上搜索但没有任何帮助我只是不能使用 fetch 方法来获取结果

You can also query a graphql endpoint using the Query component or the HOC .您还可以使用Query组件或HOC查询 graphql 端点。 But note that since class based components are considered legacy those do not receive any updates anymore.但请注意,由于基于类的组件被视为遗留组件,因此它们不再接收任何更新。

Using the Query component:使用查询组件:

import { Query } from '@apollo/client/react/components';

export class ProductListing extends Component {

  render() {
    return (
      <Query query={LOAD_PRODUCTS}>
          {({data}) => <div>Render data here ...</div>}
      </Query>
    )
  }
}

Using the HOC:使用 HOC:

import { graphql } from '@apollo/client/react/hoc';

class ProductListing extends Component {

  render() {
    const data = this.props.data;

    return <div>Render data here ...</div>;
  }
}

export default graphql(LOAD_PRODUCTS)(ProductListing);

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

相关问题 是否可以将 Graphql 查询与 React Class 组件一起使用? - Is it possible to use Graphql query with React Class component? React Apollo:从Component状态动态更新GraphQL查询 - React Apollo: Dynamically update GraphQL query from Component state 如何在反应中将同步数据从函数发送到基于类的组件? - How to send synchronous data from a functional to a class based component in react? 如何将 graphQL 查询转换为反应组件? - How would I turn a graphQL query into a react component? 如何从基于 class 的组件中的反应 function 调用返回的 const - How to call returned const from a react function in a class based component 如何从单个反应组件调用多个graphql突变? - How to call multiple graphql mutation from a single react component? 在React组件GraphQL查询中使用GraphQL上下文变量 - Using GraphQL context variable in React component GraphQL query 如何将基于 React 类的组件转换为函数式组件? - How to convert a React class-based component into a functional component? 如何在 React 中使用来自 aws 的 GraphQL Get 查询 - How to use the GraphQL Get query from aws in React Graphql,react-apollo如何在加载组件状态时将变量传递给查询 - Graphql, react-apollo how to transfer variable to query at loading component state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM