-
Notifications
You must be signed in to change notification settings - Fork 326
Pass reactive context into GraphQL context #437
Comments
While this may not get you all the way it may point you in the right direction. I'm also struggling with getting the subscriber context to materialize in my resolvers and have gotten something working. Unfortunatly I am not in a position to share the complete source but I can give you a few snippets (backed by spring). First of all you are not limited to returning CompletableFuture or similar in your resolvers - these are the "default generic wrappers". Additional wrappers can be registered as such:
In your case I'm guessing you should be able to merge with ReactiveSecurityContextHolder.getContext(). Note that the wrapper must return a CompletableFuture. Also note that there is no wrapper for Flux since AFAIK CompletableFuture is only a single value. Wrapping Flux would be akin to myFlux.collectList().toFuture() and thus making the Flux have no purpose in regard to backpressure. If you still want to experiment with Flux a wrapper for that would be registered as such:
I'm not really sure why a variant of listCollectionWithTransformer isn't provided where DataFetchingEnvironment is passed to the transformer. |
i actually ended up changing library away from kickstart to https://github.com/graphql-java/graphql-java since this functionality was not described or answered by any maintainer. |
I found the ReactiveContext already lost when building GraphqlContext, got a null SecurityContext in custom Context builder. i end up with a WebFilter to put the authentication object into ServerWebExchange attributes, then make it accessible in DataFetchingEvnrionment context. this looks really ugly but i cannot find another better one. there is another way to use SecuirtyContextStorage but that need to use a session, really dislike that. any other advice? |
I have used generic wrapper (thanks to @magnusp) to reapply reactive context manually First I create custom GraphQL context holding
Recreate context magically for all resolvers returning Mono
In my resolver and all methods of my reactive stream, I'm able to use security method like I don't know if this solution is better 😄 |
@ooga, thanks for your advice. @magnusp's solution is definitely better one but i cannot make it work with latest 8.11 version of kickstart spring grphqal. can you confirm which version are you working with? also very interesting how did you fill the authentication into your customized context, do you mind share more idea please. |
I just pushed a tiny example repo to help you https://github.com/ooga/example-graphql-webflux-security |
@ooga, that is far more than a help. appreciate your time. |
@ooga, any experience to work with GraphqlFieldVisibility to hide fields base on the user roles in reactive context? SchemaDirectiveWiring can change the field definition, but cannot hide it from Introspection call as the "GraphqlFiledVisbility" dose. |
Hi All. See Kotlin Spring Boot GraphQL Server with Spring Security authentication and authorization example here: |
Uh oh!
There was an error while loading. Please reload this page.
Is your feature request related to a problem? Please describe.
After looking at the webflux examples, reading more i finally got graphql working in my Webflux application. Had to downgrade from 7.1.0 to 7.01 because of some other bug that people already have reported.
I use authentication in my webflux app and since the underlying graphql java engine only supports CompletableFuture and not
Mono
orFlux
, the theReactiveSecurityContextHolder.getContext()
will always return null since this is using thereactor context
which is not supported inCompletableFuture
.I have been trying to look into how to be able to pass in the reactor context into the graphql context but i have only been working for a day with this, if someone has any input in how to implement this or point me in the right direction i would be super happy to give it a go.
By digging into the code i see that the
ServerWebExchange
is used in theDefaultGraphQLSpringInvocationInputFactory#create
and then duringGraphQLSingleInvocationInput#createExecutionInput
it looks like it's getting passed into the GraphQL context.What i have been trying to figure out is how to access the context in the
GraphQLQueryResolver
in the regularDatafetchers
you get access to theDataFetchingEnvironment
where you can extract the context, but i can't figure out how it is done i this project.Describe the solution you'd like
Be able to use the
ReactiveSecurityContextHolder.getContext()
or any type of reactive context in queries.Describe alternatives you've considered
If someone could point me in the right direction i could experiment in trying to implement this myself.
The text was updated successfully, but these errors were encountered: