'How can I run a query using data from a session?

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!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source