How to pass Unique Id in Graphql query

Viewed 715

I'm trying to pass a unique id to a GraphQL query to get all data against the id. But when I print the console it shows undefined. Note that if I want to see all posts the then it becomes successful by another query. in my query.js file I have written the below query:

export const  Unique_Post_Query= gql`
  query SampleQueries($id: ID!) {  
    post(_id: $id) {
        id
        data {
          title
          body {
            text
          }
        }
    } 
  }`;

and the below code from another file where I'm trying to see the result of the query:

const id='e42fd2b5-b84a-4417-afd2-36cdbaa204dd';
const {  data , error,loading} = useQuery(Unique_Post_Query, { variables: {id ,},  });
//const { error, loading, data } = useQuery(PROFILE_QUERY);
          
console.log('yourrrrr f data',data);
1 Answers

Please check below reference

const id='e42fd2b5-b84a-4417-afd2-36cdbaa204dd';

const { loading, data } = useQuery(Unique_Post_Query, {
    variables: { id: id},
  });

useEffect(() => {
    if (data && data?.post) {
       console.log("data: ", data?.post);
    }
}, [data]);
Related