0

I'm using next-auth with useSession(). I want to call useSession() on a page and use a user ID stored in it to submit a query using GraphQL to my Apollo backend which will interact with my MongoDB.

In short, I'd like to:

const { data: session } = useSession();

const { loading, error, data } = useQuery(USER_INFO, { variables: { identifier: session.user.email }})

Where:

const USER_INFO = gql`
query getFundInfo($identifier:string) {
  getAllUserData(email: $identifier) {
    email
    accountStatus
    viewAccess
    accountCreationDate
    superSecurityFields
  }
}
`

I can't get this to work since the identifier variable depends on the session being there (also, I'm using TypeScript). Any thoughts on how I can use session data to query my database when a user goes to a new page? Should I store the user's ID somewhere else once they're logged in like in redux or something? I'm using nextjs for this. Thanks!

421
  • 83
  • 1
  • 7
  • Does this answer your question: [use query hook react Apollo conditional call](https://stackoverflow.com/questions/59489097/use-query-hook-react-apollo-conditional-call)? You can use the `skip` option to only make the request when `session.user.email` is set. – juliomalves Feb 05 '22 at 10:23

0 Answers0