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

163
Visualizações
Get the data from GraphQL query in React

I'm sorry if this is something obvious, I just don't get it. I'm trying to get data from GraphQL server. I'm following the docs on Apollo client and everything's fine. The problem is that I keep getting the data in the form of a promise, rather than a real object

const GET_DATA = gql`
  {
    category {
      name
      products {
        name
        inStock
      }
    }
  }
`;
const data = client
  .query({
    query: GET_DATA,
  })
  .then((result) => {
    return result;
  })
  .then((res) => res.data);
ReactDOM.render(
  <ApolloProvider client={client}>
    <React.StrictMode>
      <App data={data} />
    </React.StrictMode>
  </ApolloProvider>,
  document.getElementById("root")
);

I tried chaining another .then() method and converting the response with .json() method, it didn't work. Maybe because GraphQL responses are different?

I would use useQuery() hook but I'm specifically asked to work with Class Components

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

0

That data const variable will hold a promise, but the result you get from GraphQl should be the correct one, in the first .then block you need to save the result in a state, then return that state as data to your App component.

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