-
Notifications
You must be signed in to change notification settings - Fork 326
Injecting Spring beans into data fetchers #20
Comments
That seems like a good solution. Right now, the servlet used doesn't expose a way to alter the context:
That could be extended to allow you to pass your own context creator that adds the applicationContext. Static datafetchers are annoying, which is why I created my own library (https://github.com/graphql-java/graphql-java-tools) that let you have state, which means you can |
Hi @apottere I think it would be a good idea to allow altering the context to increase application flexibility. |
No, it's not compatible with annotations. They're both graphql-schema generation libraries, but they have different philosophies :) |
Currently releasing this: graphql-java-kickstart/graphql-java-servlet@0b62669 Once that's done, I'll update this project to expose it. |
A temporary workaround is to use // Register GraphQL servlet
@Bean
ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(graphQLServlet(), "/graphql/*");
}
@Bean
GraphQLServlet graphQLServlet() {
// Return custom GraphQL context
new SimpleGraphQLServlet(graphQLSchema()) {
@Override
protected GraphQLContext createContext(Optional<HttpServletRequest> request, Optional<HttpServletResponse> response) {
return new SpringGraphQLContext(request, response, applicationContext);
}
}
} and SpringGraphQLContext would look like this: class SpringGraphQLContext extends GraphQLContext {
private ApplicationContext applicationContext
SpringGraphQLContext(Optional<HttpServletRequest> request,
Optional<HttpServletResponse> response,
ApplicationContext applicationContext) {
super(request, response);
this.applicationContext = applicationContext;
}
ApplicationContext getApplicationContext() {
return applicationContext;
}
} than one can access ApplicationContext in a data fetcher: List<Movie> get(DataFetchingEnvironment environment) {
ApplicationContext applicationContext = environment.<SpringGraphQLContext> getContext().getApplicationContext()
} |
graphql-spring-boot |
Uh oh!
There was an error while loading. Please reload this page.
I am puzzled about how should I inject spring beans into data fetchers.
One way could be to pass
ApplicationContext
intographQL.execute
like this:Then DataFetcher implementation would look like:
What is the preferred way to do this with
graphql-spring-boot
?The text was updated successfully, but these errors were encountered: