Since in the graphql-java annotation the data collector is defined by the annotation, it is built by the framework (using reflection to get the constructor), so it cannot bean.
The workaround I found for this is setting it as ApplicationContextAware , and then I can initialize some static field instead of bean. Not the nicest thing, but it works:
@Component public class MyDataFetcher implements DataFetcher, ApplicationContextAware { private static MyService myService; private static ApplicationContext context; @Override public Object get(DataFetchingEnvironment environment) { return myService.getData(); } @override public void setApplicationContext(ApplicationContext applicationContext) throws BeansExcepion { context = applicationContext; myService = context.getBean(MyService.class); } }
Basically, you still get a new instance of the data collector, initialized by the chart, but spring also initializes it, and since myService is static, you get initialized.
source share