spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.5k stars 297 forks source link

RequestContextHolder.getRequestAttributes in virtual threads context is null #1001

Closed sfat closed 2 weeks ago

sfat commented 3 weeks ago

Hi, I've recently updated to spring boot 3.3.0 and enabled virtual threads.

I have a simple @QueryMapping, which isn't reactive.

Previously accessing RequestContextHolder.getRequestAttributes() would let me access request attributes, now it returns null.

Testing it against a simple spring rest controller, the request attributes are accessible and they are not null.

It is a bad practice to use RequestContextHolder in spring-graphql context or something is going on?

If I turn off virtual threads, everything is working fine

bclozel commented 2 weeks ago

We don't recommend using request attributes or any threadlocal-based solution for making metadata available during the processing of a GraphQL request. This is for two reasons:

If you would like to get something from the HTTP request and make it available to the rest of the processing chain, you can use an interceptor for that and push this data into the GraphQL Context. In general, the Context / Local Context are designed for such uses cases. You can then easily inject context/local context values as method arguments in controllers.

I hope this answers your question, I'll close this issue for now but don't hesitate to comment here.

rstoyanchev commented 2 weeks ago

Note that we do have the option to propagate context, but as Brian pointed out we recommend adapting transport layer information to GraphQL Context values. Note also that Spring Framework 6.2 will have a built-in RequestAttributesThreadLocalAccessor, but it's easy to replicate.