小编Vim*_*Joe的帖子

用于安卓的身份验证 Apollo Graphql

我正在开发一个使用 GraphQL 作为后端的 android 应用程序。我让查询和变异部分完美地工作。但是我找不到任何用于身份验证的文件。

那么如何将用户名和密码传递给服务器并对其进行身份验证?

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意 :

  • 这不是一个重复的问题
  • graphQl 没有适当的文档

如果你投反对票,那么请为自己辩解。

authentication graphql apollo-client graphql-java

4
推荐指数
1
解决办法
1228
查看次数