com.apollographql.apollo.ApolloClient Java Examples

The following examples show how to use com.apollographql.apollo.ApolloClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultApolloClientFactory.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
@Override
public ApolloClient create() {
  var builder = builder()
      .okHttpClient(new OkHttpClient.Builder().build());
  configurer.accept(builder);
  return builder
      .serverUrl(streamRegistryUrl)
      .addCustomTypeAdapter(OBJECTNODE, new ObjectNodeTypeAdapter())
      .build();
}
 
Example #2
Source File: ResponseFetcherTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test public void defaultCacheControl() {
  ApolloClient apolloClient = ApolloClient.builder()
      .serverUrl("http://google.com")
      .okHttpClient(okHttpClient)
      .build();

  RealApolloCall realApolloCall = (RealApolloCall) apolloClient.query(emptyQuery);
  assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCachePolicy.FetchStrategy.NETWORK_ONLY);
  assertThat(realApolloCall.responseFetcher).isEqualTo(CACHE_FIRST);
}
 
Example #3
Source File: ResponseFetcherTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test public void setDefaultCachePolicy() {
  ApolloClient apolloClient = ApolloClient.builder()
      .serverUrl("http://google.com")
      .okHttpClient(okHttpClient)
      .defaultHttpCachePolicy(HttpCachePolicy.CACHE_ONLY)
      .defaultResponseFetcher(NETWORK_ONLY)
      .build();

  RealApolloCall realApolloCall = (RealApolloCall) apolloClient.query(emptyQuery);
  assertThat(realApolloCall.httpCachePolicy.fetchStrategy).isEqualTo(HttpCachePolicy.FetchStrategy.CACHE_ONLY);
  assertThat(realApolloCall.responseFetcher).isEqualTo(NETWORK_ONLY);
}
 
Example #4
Source File: ApolloCallTrackerTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  activeCallCounts = new ArrayList<>();
  Interceptor interceptor = new Interceptor() {
    @Override public okhttp3.Response intercept(Chain chain) throws IOException {
      activeCallCounts.add(apolloClient.activeCallsCount());
      return chain.proceed(chain.request());
    }
  };

  OkHttpClient okHttpClient = new OkHttpClient.Builder()
      .addInterceptor(interceptor)
      .build();

  apolloClient = ApolloClient.builder()
      .serverUrl(SERVER_URL)
      .okHttpClient(okHttpClient)
      .build();
}
 
Example #5
Source File: QueryRefetchTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Before public void setUp() throws IOException {
  server = new MockWebServer();
  server.start();
  OkHttpClient okHttpClient = new OkHttpClient.Builder()
      .dispatcher(new Dispatcher(Utils.INSTANCE.immediateExecutorService()))
      .build();

  apolloClient = ApolloClient.builder()
      .serverUrl(server.url("/"))
      .dispatcher(Utils.INSTANCE.immediateExecutor())
      .okHttpClient(okHttpClient)
      .normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver())
      .build();
}
 
Example #6
Source File: BaseFetcherTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Before public void setUp() {
  server = new MockWebServer();
  OkHttpClient okHttpClient = new OkHttpClient.Builder()
      .writeTimeout(2, TimeUnit.SECONDS)
      .readTimeout(2, TimeUnit.SECONDS)
      .dispatcher(new Dispatcher(Utils.INSTANCE.immediateExecutorService()))
      .build();

  apolloClient = ApolloClient.builder()
      .serverUrl(server.url("/"))
      .okHttpClient(okHttpClient)
      .normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver())
      .dispatcher(Utils.INSTANCE.immediateExecutor())
      .build();
}
 
Example #7
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test
public void checkIdlingResourceTransition_whenCallIsQueued() throws ApolloException {
  server.enqueue(mockResponse());

  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .dispatcher(new Executor() {
        @Override public void execute(@NotNull Runnable command) {
          command.run();
        }
      })
      .serverUrl(server.url("/"))
      .build();

  final AtomicInteger counter = new AtomicInteger(1);
  idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);

  idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
    @Override public void onTransitionToIdle() {
      counter.decrementAndGet();
    }
  });

  assertThat(counter.get()).isEqualTo(1);
  Rx2Apollo.from(apolloClient.query(EMPTY_QUERY)).test().awaitTerminalEvent();
  assertThat(counter.get()).isEqualTo(0);
}
 
Example #8
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test
public void checkIsIdleNow_whenCallIsWatched() throws InterruptedException {
  server.enqueue(mockResponse());

  final CountDownLatch latch = new CountDownLatch(1);

  ExecutorService executorService = Executors.newFixedThreadPool(1);

  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .dispatcher(executorService)
      .serverUrl(server.url("/"))
      .build();

  idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);
  assertThat(idlingResource.isIdleNow()).isTrue();

  apolloClient.query(EMPTY_QUERY).watcher().enqueueAndWatch(new ApolloCall.Callback<Object>() {
    @Override public void onResponse(@NotNull Response<Object> response) {
      latch.countDown();
    }

    @Override public void onFailure(@NotNull ApolloException e) {
      throw new AssertionError("This callback can't be called.");
    }
  });

  assertThat(idlingResource.isIdleNow()).isFalse();

  latch.await(TIME_OUT_SECONDS, TimeUnit.SECONDS);

  executorService.shutdown();
  executorService.awaitTermination(TIME_OUT_SECONDS, TimeUnit.SECONDS);
  Thread.sleep(100);
  assertThat(idlingResource.isIdleNow()).isTrue();
}
 
Example #9
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test
public void checkIsIdleNow_whenCallIsQueued() throws InterruptedException {
  server.enqueue(mockResponse());

  final CountDownLatch latch = new CountDownLatch(1);

  ExecutorService executorService = Executors.newFixedThreadPool(1);

  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .dispatcher(executorService)
      .serverUrl(server.url("/"))
      .build();

  idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);
  assertThat(idlingResource.isIdleNow()).isTrue();

  apolloClient.query(EMPTY_QUERY).enqueue(new ApolloCall.Callback<Object>() {
    @Override public void onResponse(@NotNull Response<Object> response) {
      latch.countDown();
    }

    @Override public void onFailure(@NotNull ApolloException e) {
      throw new AssertionError("This callback can't be called.");
    }
  });
  assertThat(idlingResource.isIdleNow()).isFalse();

  latch.await(TIME_OUT_SECONDS, TimeUnit.SECONDS);

  executorService.shutdown();
  executorService.awaitTermination(TIME_OUT_SECONDS, TimeUnit.SECONDS);
  Thread.sleep(100);
  assertThat(idlingResource.isIdleNow()).isTrue();
}
 
Example #10
Source File: GraphQLClient.java    From YelpQL with MIT License 5 votes vote down vote up
public static ApolloClient getApolloClient(String authToken) {

        if (okHttpClient == null) {
            okHttpClient = getOkHttpClient(authToken);
        }

        return ApolloClient.builder()
                .serverUrl(YELP_API)
                .okHttpClient(okHttpClient)
                .build();
    }
 
Example #11
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test
public void checkValidIdlingResourceNameIsRegistered() {
  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .serverUrl(server.url("/"))
      .build();

  idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);

  assertThat(idlingResource.getName()).isEqualTo(IDLING_RESOURCE_NAME);
}
 
Example #12
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@SuppressWarnings("ConstantConditions") @Test
public void onNullNamePassed_NullPointerExceptionIsThrown() {
  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .serverUrl(server.url("/"))
      .build();

  try {
    idlingResource = ApolloIdlingResource.create(null, apolloClient);
    fail();
  } catch (Exception e) {
    assertThat(e).isInstanceOf(NullPointerException.class);
    assertThat(e.getMessage()).isEqualTo("name == null");
  }
}
 
Example #13
Source File: OverviewViewModel.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Inject
OverviewViewModel(UserManager userManager,
                         UserRestService service,
                         OrganizationService organizationService,
                         ApolloClient apolloClient) {
    super(userManager);
    this.userRestService = service;
    this.organizationService = organizationService;
    this.apolloClient = apolloClient;
}
 
Example #14
Source File: ApolloIdlingResource.java    From apollo-android with MIT License 5 votes vote down vote up
private ApolloIdlingResource(String name, ApolloClient apolloClient) {
  this.apolloClient = apolloClient;
  this.name = name;
  apolloClient.idleCallback(new IdleResourceCallback() {
    @Override public void onIdle() {
      ResourceCallback callback = ApolloIdlingResource.this.callback;
      if (callback != null) {
        callback.onTransitionToIdle();
      }
    }
  });
}
 
Example #15
Source File: GitHuntApplication.java    From apollo-android with MIT License 4 votes vote down vote up
public ApolloClient apolloClient() {
  return apolloClient;
}
 
Example #16
Source File: ExampleAgentApp.java    From stream-registry with Apache License 2.0 4 votes vote down vote up
@Bean
ApolloClient apolloClient(
    @Value("${streamRegistryUrl}") String streamRegistryUrl
) {
  return new DefaultApolloClientFactory(streamRegistryUrl).create();
}
 
Example #17
Source File: TestNetworkModule.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
@Provides
@Singleton
static ApolloClient provideApolloClient() {
    return mock(ApolloClient.class);
}
 
Example #18
Source File: ApolloProvider.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
public static ApolloClient getApollo(OkHttpClient okHttpClient) {
    return ApolloClient.builder()
            .serverUrl(BuildConfig.REST_URL + "graphql")
            .okHttpClient(okHttpClient)
            .build();
}
 
Example #19
Source File: NetworkModule.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
@Provides
@Singleton
static ApolloClient provideApolloClient(OkHttpClient okHttpClient) {
    return ApolloProvider.getApollo(okHttpClient);
}
 
Example #20
Source File: CommandFollowUser.java    From twitch4j with MIT License 4 votes vote down vote up
public CommandFollowUser(ApolloClient apolloClient, Long targetUserId, Boolean goLiveNotification) {
    super(apolloClient);
    this.targetUserId = targetUserId;
    this.goLiveNotification = goLiveNotification;
}
 
Example #21
Source File: CommandUnfollowUser.java    From twitch4j with MIT License 4 votes vote down vote up
public CommandUnfollowUser(ApolloClient apolloClient, Long targetUserId) {
    super(apolloClient);
    this.targetUserId = targetUserId;
}
 
Example #22
Source File: GitHuntApplication.java    From HelloApolloAndroid with MIT License 4 votes vote down vote up
public ApolloClient apolloClient() {
    return apolloClient;
}
 
Example #23
Source File: StreamRegistryClient.java    From stream-registry with Apache License 2.0 4 votes vote down vote up
public StreamRegistryClient(ApolloClient apolloClient) {
  this(new ApolloExecutor(apolloClient));
}
 
Example #24
Source File: GraphQLEventSender.java    From stream-registry with Apache License 2.0 4 votes vote down vote up
public GraphQLEventSender(ApolloClient client) {
  this(new ApolloExecutor(client), new GraphQLConverter());
}
 
Example #25
Source File: DefaultApolloClientFactory.java    From stream-registry with Apache License 2.0 4 votes vote down vote up
ApolloClient.Builder builder() {
  return ApolloClient.builder();
}
 
Example #26
Source File: ExampleAgentApp.java    From stream-registry with Apache License 2.0 4 votes vote down vote up
@Bean
EventSender eventSender(ApolloClient apolloClient) {
  return new GraphQLEventSender(apolloClient);
}
 
Example #27
Source File: ApolloIdlingResource.java    From apollo-android with MIT License 2 votes vote down vote up
/**
 * Creates a new {@link IdlingResource} from {@link ApolloClient} with a given name. Register this instance using
 * {@link androidx.test.espresso.IdlingRegistry} in your test suite's setup method.
 *
 * <pre>{@code
 * IdlingResource idlingResource = ApolloIdlingResource.create("Apollo", apolloClient);
 * IdlingRegistry.getInstance().register(idlingResource);
 * }</pre>
 *
 * @param name <strong>unique</strong> name of this IdlingResource instance.
 * @param apolloClient the apolloClient for which IdlingResource needs to be created.
 * @return a new ApolloIdlingResource.
 * @throws NullPointerException if name == null or apolloClient == null
 */
public static ApolloIdlingResource create(@NotNull String name, @NotNull ApolloClient apolloClient) {
  checkNotNull(name, "name == null");
  checkNotNull(apolloClient, "apolloClient == null");
  return new ApolloIdlingResource(name, apolloClient);
}
 
Example #28
Source File: BaseCommand.java    From twitch4j with MIT License 2 votes vote down vote up
/**
 * Constructor
 *
 * @param apolloClient Apollo Client
 */
public BaseCommand(ApolloClient apolloClient) {
    super(HystrixCommandGroupKey.Factory.asKey("GraphQL"));
    this.apolloClient = apolloClient;
}
 
Example #29
Source File: ApolloClientFactory.java    From stream-registry with Apache License 2.0 votes vote down vote up
ApolloClient create();