Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

236
Visualizações
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

Here its how LOAD_PRODUCT LOOKS LIKE

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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can also query a graphql endpoint using the Query component or the HOC. 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:

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);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda