Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
How to get multiple values from useQuery GraphQL

I am making an online store and I want to calculate the total cost of products in it. The cart looks like this: [{productId, count}, {productId, count}], and I get the cost of the product through a query by productId.

Below is an example of what the ideal structure looks like that I would like to get, but useQuery cannot be used inside a map and useQuery is asynchronous. I've already tried various ways to solve this, but to no avail.

Can you please tell me how to solve this problem or is there another way?

import {useQuery, useMutation} from "@apollo/client";
import {GET_PRODUCT_PRICE} from "../query/product";

export const GetTotalPrice = (cart) => {
    let totalPrice = 0;
    const {data, loading, error} = useQuery(GET_PRODUCT_PRICE, {
        variables: {
            id: productId
        }
    });
    
    cart.map((product) => {
        const {data, loading, error} = useQuery(GET_PRODUCT_PRICE, {
            variables: {
                id: product.productId
            }
        });

        totalPrice += data.getProduct.price * product.count;
    })

    return totalPrice;
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda