Skip to content

Error creating bean with name 'scopedTarget...' for request scoped bean during injecting into DataFetcher #571

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

Closed
chengco opened this issue Jun 27, 2024 · 0 comments
Labels

Comments

@chengco
Copy link

chengco commented Jun 27, 2024

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.

@chengco chengco added the bug label Jun 27, 2024
@chengco chengco closed this as completed Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant