How can I use from GraphQl in android?

I need a simple use case GraphQlin android.

How can I use from GraphQlin android (tutorial).

+4
source share
2 answers

To use GraphQL (in general), you need two things:

1. GraphQL server

There are several ways to do this. Of course, you could just go and implement one of them on any server that you like. Other (faster) approaches are to use existing tools and create the GraphQL API using services like graphql-up or create-graphql-server or even services like Graphcool (disclaimer: I work for them).

2. GraphQL Client Library

, GraphQL HTTP ( POST), , , . GraphQL - Apollo, Android. . , , , HTTP.

+5

GraphQl . Retrofit 2:

// QueryHelper.java
// This line below is the simple format of Gql query
query = "query{me{name, location, majorOfInterest,profilePhoto{url(size: 400) }}}";

//Post the query using Retrofit2
GqlRetrofitClient.getInstance(getContext()).fetchUserDetails(new GqlQueryRequest(queryUserDetails)).enqueue(new Callback<UserDetails>() {
  @Override
  public void onResponse(Call<UserDetails> call, Response<UserDetails> response) {
     //OnResponse do something();       
     }

  @Override
  public void onFailure(Call<UserDetails> call, Throwable t) {
    Log.d(TAG, "Failed to fetch User details");
    }
    });


//GqlClient.java
public class GqlRetrofitClient {
public static final String BASE_URL = BuildConfig.DOMAIN;
private static GqlRetrofitClient sInstance;
private GqlRetrofitService mGqlRetrofitService;

Gson gson = new GsonBuilder().create();

  private GqlRetrofitClient(final Context context) {
    // Network Interceptor for logging
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder()
                            .addHeader("X-User-Token", "AUTH_TOKEN")
                            .addHeader("X-User_Email", "Email")
                            .addHeader("content-type", "application/json")
                            .build();
                    return chain.proceed(request);
                }
            })
            .addInterceptor(httpLoggingInterceptor)
            .build();

    // Retrofit initialization
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(okHttpClient)
            .build();

    mGqlRetrofitService = retrofit.create(GqlRetrofitService.class);
 }

  // Create an instance of GqlRetrofitClient to create retrofit service
  public static GqlRetrofitClient getInstance(Context context){
    if(sInstance == null){
        sInstance = new GqlRetrofitClient(context.getApplicationContext());
    }
    return sInstance;
  }

  // Method call to get User details
  public Call<UserDetails> fetchUserDetails(GqlQueryRequest queryUserDetails){
    return mGqlRetrofitService.getUserDetails(queryUserDetails);
  }
}

//GqlRetrofitService.java
public interface GqlRetrofitService{
  @POST("/api/graph.json")
  Call<UserDetails> getUserDetails(@Body GqlQueryRequest body);
}
+3

Source: https://habr.com/ru/post/1674362/


All Articles