I have an Apollo query:
const { error, loading, data: user } = useQuery(resolvers.queries.ReturnUser, {
variables: { userId: parseInt(id) },
});
The data object returned from the query has another object called returnUser
. So the actual object is:
data: {
returnUser: {
name: 'Some Name'
}
}
In my JSX I want to output the data:
return (
<div>
{ user ?
<p>Profile {user.returnUser.name}</p> :
<div>User not found</div> }
</div>
);
As you can see I need to access the returnUser
object on the user
object to get the actual user data. This is not great. Is there any way to destructure the data
object from the useQuery
so I can assign the nested object to user
?
//edit: While this looks like a duplicate question, the answer is different due to the async nature of useQuery
. If you don't set a default value you'll get an error.