[英]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.