Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share and modify variables between Express and the GraphQL/Apollo server #7822

Closed
qu8n opened this issue Jan 16, 2024 · 1 comment
Closed

Comments

@qu8n
Copy link

qu8n commented Jan 16, 2024

Background on my application

I have an Express application with the apollo-server-express package installed, which allows me to attach a GraphQL server to the running Express server.

The Express server is used for authentication/authorization (with Keycloak and Passport), and the GraphQL server serves as the API layer for querying data.

What I'd like to accomplish

Implement a way to auto logout users when they have been inactive for 10 minutes. "Inactive" is defined as users from making a query to the GraphQL server.

My current strategy

Create a global variable when initializing the Express application that tracks each logged-in user and the last time that they made a request. Example:

const userSessionTracker = {
    userId1: 300000, // a user's timer: last time they logged in/made a request (in ms)
    userId2: 5000,
    ...
}

Every time a user logs in/makes a request, the application will reset their timer back to 0. Next time they make a request, if the timer is above 10 minutes, the application will log the user out.

Problem

When the users call a GraphQL query, I'd like to check/reset their timer inside the GraphQL query resolver. How can I do so?

What I've tried

  • Passed userSessionTracker into the Apollo server's context, enabling the variable to be accessible from the resolver's contextValue
  • Tried editing userSessionTracker from inside the resolver
  • console.log(userSessionTracker) from the Express server
  • Expected userSessionTracker to show the edits made from the GraphQL resolver, but saw that it remains in the state it was in before the edits were made

In summary, I can access the userSessionTracker variable from inside a GraphQL resolver, but I can't edit it and have the edited version be accessible from the Express application.

This seems to be by design, as the Apollo docs stated that "Resolvers should never destructively modify the contextValue argument". How can I get around this limitation?

@qu8n qu8n closed this as completed Jan 16, 2024
Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant