-
Notifications
You must be signed in to change notification settings - Fork 113
Ability to pass custom context within the same GraphQLServletContext #226
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
Comments
I also want to provide a custom context. |
You'd have to provide a custom implementation of @Component
class CustomGraphQLServletContextBuilder implements GraphQLServletContextBuilder {
public GraphQLContext build(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
return new MyGraphQLServletContext(httpServletRequest, httpServletResponse);
}
public GraphQLContext build(Session session, HandshakeRequest handshakeRequest) {
return new MyGraphQLWebSocketContext(session);
}
} |
I forgot to reply after I solved this, but I did exactly as @oliemansm suggested. |
@oliemansm Thanks for you reply. I have a custom implementation of GraphQLServletContextBuilder as you suggested. But getContext() of DataFetchingEnvironment only allows to return GraphQLContext or GraphQLServletContext. I hope to get MyGraphQLContext instead of GraphQLContext from DataFetchingEnvironment with the following CustomGraphQLServletContextBuilder. @component public MyGraphQLContext build(Session session, HandshakeRequest handshakeRequest) { I want to pass around some information in MyGraphQLContext. Please let me know how to do it. Thanks. |
You just have to cast it to your own type, i.e.: String someQueryMethod(DataFetchingEnvironment env) {
MyGraphQLContext context = (MyGraphQLContext) env.getContext();
return "bar";
} |
@oliemansm Please refer to the following pseudocode. I need to use DefaultGraphQLServletContext and also need to keep custom context. My issue is how to create MyGraphQLServletContext. @component
} public class DefaultMyGraphQLServletContext extends DefaultGraphQLServletContext implements MyGraphQLServletContext {
} public interface MyGraphQLServletContext extends GraphQLServletContext { |
In the end you have a method named The DefaultMyGraphQLServletContext.createServletContext(null, null)
.with(req)
.with(response)
.with(customContext) // or with(serviceClient)
.build(); You don't have to use a builder pattern if you don't want to. It seems to be a source of confusion at this time. Perhaps get rid of that builder in your case for now and create your context as you would any simple bean with a constructor and setters. At the bottom you're trying to set an instance variable from a static method btw. That's nog going to work. This is getting into the realm of more generic "how do I do stuff in Java" then it's really related to this library though. |
@oliemansm Thanks for you quick reply. ServiceClient should be CustomContext in my previous post. class DefaultMyGraphQLServletContext extends class DefaultGraphQLServletContext. Unfortunately, DefaultGraphQLServletContext's constructor is private. It prevents me to reuse the code of DefaultGraphQLServletContext for DefaultMyGraphQLServletContext. One way may be to duplicate the code of DefaultGraphQLServletContext in DefaultMyGraphQLServletContext. But it is out side of graphql-java-servlet library, I don't think it is good even it works. Any suggestions for this? Thanks. |
You can’t subclass it if the constructor is private. You can apply the decorator pattern though. So keep the default context as a field inside your custom context. And forward any calls you need to to that context and handle any others calls you want to yourself. |
@howardhaozhang Can you confirm this solved it so we can close this issue? |
@oliemansm It works. Thanks. |
I need to pass custom context within the same GraphQLServletContext for some information within the same Http Servlet request. getContext() of DataFetchingEnvironment only allows to return GraphQLContext or GraphQLServletContext. Is there any better way to do this? Thanks.
The text was updated successfully, but these errors were encountered: