apollographql / react-apollo

:recycle: React integration for Apollo Client
https://www.apollographql.com/docs/react/
MIT License
6.85k stars 790 forks source link

Multiple queries which depend on each other #3624

Open zsid opened 4 years ago

zsid commented 4 years ago

Intended outcome: I would like to be able to execute multiple queries that depend on one another. For example:

const { loading, error, data: user } = useQuery(GET_USER);

const { loading: loadingProfile, data: userPorfile } = useQuery(GET_USER_PROFILE, { 
  variables: { id: user.id } 
});

Actual outcome: It throws an error since user is undefined

How to reproduce the issue:

dylanwulf commented 4 years ago

I would pass the skip option into the second query like this:

const { loading, error, data: user } = useQuery(GET_USER);

const { loading: loadingProfile, data: userProfile } = useQuery(GET_USER_PROFILE, { 
  variables: { id: user && user.id },
  skip: user == null 
});

This tells the second query to not request anything until the user data is available.