I'm developing an android application that uses GraphQL as the back-end. I have the query and mutation part working perfectly. But I couln't find any documentations for authentication.
So how can I pass the username and password to the server and authenticate it?
LocalApolloClient.java :
public class LocalApolloClient {
private static final String URL = "http://192.168.1.100/graphql/";
private static ApolloClient apolloClient;
private static String authHeader = "";
public static ApolloClient getApolloClient(){
//HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder().method(original.method(), original.body());
builder.header("Authorization", authHeader);
return chain.proceed(builder.build());
})
.build();
apolloClient = ApolloClient.builder()
.serverUrl(URL)
.okHttpClient(okHttpClient)
.build();
return apolloClient;
}
}
Please note :
So kindly Justify if you negative vote.
You need to set the header on ApolloClient, not on the OkHttpClient. Something like this:
ApolloClient.builder()
.serverUrl(context.getString(R.string.graphql_base_url))
.addApplicationInterceptor(object: ApolloInterceptor {
override fun interceptAsync(
request: ApolloInterceptor.InterceptorRequest,
chain: ApolloInterceptorChain,
dispatcher: Executor,
callBack: ApolloInterceptor.CallBack
) {
val newRequest = request.toBuilder().requestHeaders(RequestHeaders.builder().addHeader("Authorization", "Basic ...").build()).build()
chain.proceedAsync(newRequest, dispatcher, callBack)
}
override fun dispose() {
}
}).build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With