Answer the question
In order to leave comments, you need to log in
How to pass variables to GraphQL query in React components?
Good afternoon. I have a Product component that receives an item id from another component. I need to pass this id to the request to GraphQL and, accordingly, get information about this product.
Here is the request code in Queries.js:
import {gql} from '@apollo/client';
export const CHOOSE_PRODUCT = gql`
query{
product(id:"huarache-x-stussy-le"){
name
description
}
}
`
import { useQuery} from "@apollo/client"
import {CHOOSE_PRODUCT} from "../graphql/Queries"
export default function Product(){
const {data} = useQuery(CHOOSE_PRODUCT)
if(data){
console.log(data)
}
Answer the question
In order to leave comments, you need to log in
import { gql } from "@apollo/client";
export const getChooseProductQuery = (id) => gql`
query {
product(id: ${id}) {
name
description
}
}
`;
import { useQuery } from "@apollo/client";
import { getChooseProductQuery } from "../graphql/Queries";
export default function Product(props) {
const { id } = props;
const { data } = useQuery(getChooseProductQuery(id));
if (data) {
console.log(data);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question