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

Add GraphQLContextHolder like SecurityContextHolder? #1140

Open
Sam-Kruglov opened this issue Mar 3, 2025 · 1 comment
Open

Add GraphQLContextHolder like SecurityContextHolder? #1140

Sam-Kruglov opened this issue Mar 3, 2025 · 1 comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@Sam-Kruglov
Copy link

Sam-Kruglov commented Mar 3, 2025

It appears there's no way to access GraphQLContext other than inject it into a resolver method argument like so:

@QueryMaping
fun myQuery(context: GraphQLContext)...

And then we must pass it around in all the downstream methods so that they can access it. Spring Security has the same problem of accessing the current user information from JWT and they have SecurityContextHolder (https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html#servlet-authentication-securitycontextholder) which simply stores all the context in a ThreadLocal. And they have something else for reactive context too.

Why don't we do the same for graphql context? I feel like it's inconvenient to pass it around, it should be doable to just store it in a thread local?

Or perhaps we could add the whole DataFetchingEnvironment in a thread local?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2025
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Mar 6, 2025

It is not possible to answer your question without knowing more details about where the data you want to propagate originates from.

In the case of SecurityContextHolder, it is set in a Filter or WebFilter and is propagated from there. In Spring for GraphQL we already propagate context from the web layer (like Security context), through the GraphQL engine, and down to individual controller method invocation, and it is available in any methods you call too. We do that through the context-propagation library, and this is also explained in the section on context propagation in the Spring for GraphQL docs.

This is the same mechanism that you can use, but it depends what data you need to propagate and where it is set from.

@rstoyanchev rstoyanchev added the status: waiting-for-feedback We need additional information before we can continue label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants