Error creating bean with name 'scopedTarget...' for request scoped bean during injecting into DataFetcher
chengco opened this issue · 0 comments
chengco commented
Describe the bug
Hope this is right place I can put to raise this issue, I defined a graphql query to fetch users, and there is a UserDataLoader which is request scoped bean injected into UserDataFetcher, I encounter an below error when execute a graphql http request:
11:00:34.302 [pool-4-thread-1] ERROR graphql.kickstart.execution.error.DefaultGraphQLErrorHandler - Error executing query Exception while fetching data (/users[1]/user) : Error creating bean with name 'scopedTarget.userDataLoader': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) ~[spring-web-6.1.8.jar:6.1.8]
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:42) ~[spring-web-6.1.8.jar:6.1.8]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:373) ~[spring-beans-6.1.8.jar:6.1.8]
... 50 more
Wrapped by: org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.userDataLoader': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton
To Reproduce
gradle configration:
dependencySet(group: 'com.graphql-java-kickstart', version: '15.1.0') {
entry 'graphql-spring-boot-starter'
entry 'graphiql-spring-boot-starter'
entry 'voyager-spring-boot-starter'
entry 'graphql-spring-boot-starter-test'
entry 'graphql-spring-boot-test'
entry 'graphql-java-servlet'
}
dependency 'com.graphql-java-kickstart:graphql-java-tools:13.1.1'
dependency 'com.graphql-java:graphql-java:21.5'
UserDataFetcher :
@Component
public class UserDataFetcher implements DataFetcher<UserPresenter> {
private UserDataLoader userDataLoader;
public UserDataFetcher(UserDataLoader userDataLoader) {
this.userDataLoader = userDataLoader;
}
@Override
public UserPresenter get(DataFetchingEnvironment dataFetchingEnvironment) {
WithUserIdPresenter source = dataFetchingEnvironment.getSource();
String userId = source.getUserId();
return this.userDataLoader.load(UUID.fromString(userId));
}
}
UserDataLoader:
@Component
@Scope(
value = WebApplicationContext.SCOPE_REQUEST,
proxyMode = ScopedProxyMode.TARGET_CLASS
)
public class UserDataLoader {
private UserService userService;
public UserDataLoader(UserService userService) {
this.userService = userService;
}
public Optional<User> load(UUID id) {
return userService.getUser(id);
}
}
Expected behavior
No error
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]
Additional context
Add any other context about the problem here.