How can I use a generated client with @Autowired for Spring #908
-
I am using the gradle plugin now and successfully created a client for my GraphQL query. Now I want to use this via Dependency Injection by Spring. I saw that the generated class is not configured as Spring Component and wonder how I can achieve this to easily use the client in my spring services. Here is my configuration:
I wonder if I need to add something to let it generate and apply it to a spring context |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
In order to use the generated clients as Spring beans you will need to manually register them with Spring context, e.g. given @Configuration
class QueryConfiguration {
@Bean
fun graphQLWebClient(builder: WebClient.Builder) =
GraphQLWebClient(url = "http://localhost:8080/graphql", builder = builder)
@Bean
fun helloWorldQuery(graphQLWebClient: GraphQLWebClient) = HelloWorldQuery(graphQLWebClient)
} You can then autowire |
Beta Was this translation helpful? Give feedback.
-
@smyrick wondering whether we should automatically add Additional considerations - |
Beta Was this translation helpful? Give feedback.
In order to use the generated clients as Spring beans you will need to manually register them with Spring context, e.g. given
HelloWorldQuery
you could create it asYou can then autowire
helloWorldQuery: HelloWorldQuery
bean to any of your services.