Replies: 2 comments
-
You can generally add headers during creation of your client like this: https://netflix.github.io/dgs/advanced/java-client/#headers
Since you are using a custom client, you can use the example you linked above and set the headers you need prior to invoking execute on your client implementation. Somethinglike this:
|
Beta Was this translation helpful? Give feedback.
-
Hi. You can also use filters to alter the headers in a generic way. We do this to inject the Authorization token for instance. @Bean
fun graphQLClient(
serverProperties: ServerProperties,
clientRegistrationRepository: InMemoryReactiveClientRegistrationRepository,
clientService: ReactiveOAuth2AuthorizedClientService,
oAuth2OnBehalfOf: OAuth2OnBehalfOf
): WebClientGraphQLClient = MonoGraphQLClient.createWithWebClient(WebClient.builder()
.baseUrl(serverProperties.httpUrl)
.filter(
ServerOAuth2AuthorizedClientExchangeFilterFunction(
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(
clientRegistrationRepository,
clientService
)
).also {
// We know we use only one provider, so it's easier and less coupled to the actual value to do things like this
it.setDefaultClientRegistrationId(clientRegistrationRepository.first().registrationId)
}
)
.filter { request, next ->
request.headers()["Authorization"]?.first()
?.substring(7)
?.let { accessToken ->
oAuth2OnBehalfOf
.getRequestingPartyToken(accessToken)
.flatMap { rptToken ->
next.exchange(
ClientRequest.from(request)
.headers { headers -> headers.setBearerAuth(rptToken) }
.build()
)
}
} ?: next.exchange(request)
}
.build()
) In this code we're using 2 filters, one that handles authentication and another one that handles authorization. We probably could have done this with one filter but at least it gives you some insight on ways to use filters. |
Beta Was this translation helpful? Give feedback.
-
I am using
com.netflix.graphql.dgs:graphql-dgs-client:4.9.16
along with HttpClient (JDK 17)I am creating graphql client using below.
And my request executor is based on HttpClient.
The
requestExecutor.exec(String url, Map<String, ? extends List<String>> headers, String body)
method can accept the headers but I am not seeing a graphql client method that can pass or accept httpheader.What I am currently using is -
Neither of the above i.e.
GraphQLQueryRequest
constructor orexecuteQuery
method has a overloaded equivalent that accept HttpHeader and in-turn pass it torequestExecutor::exec
method.What is the point of exec method accepting
headers
if it can not be passed by the user code with out creating a new instance ofRequestExecutor
.Now how can i pass header to the graphql request?
I came across this in java-client/#plug-in-your-own-http-client docs -
-- from where is the headers parameter picked up, how can i add more headers to the above headers param??
-- Using the above suggested approach, it does not look like i can use the same requestExecutor i.e requestExecutor::exec in a generic approach.
Beta Was this translation helpful? Give feedback.
All reactions