diff --git a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/SchemaTypeSpecBuilder.kt b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/SchemaTypeSpecBuilder.kt index ac3343a3a3c..11c9d65acc7 100644 --- a/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/SchemaTypeSpecBuilder.kt +++ b/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/SchemaTypeSpecBuilder.kt @@ -222,7 +222,7 @@ class SchemaTypeSpecBuilder( fun responseMarshallerSpec(fieldSpecs: List): MethodSpec { val code = fieldSpecs .map { fieldSpec -> - if (fieldSpec.type.isOptional()) { + if (fieldSpec.type.isNullable()) { CodeBlock.builder() .addStatement("final \$T \$L = \$L", fieldSpec.type.unwrapOptionalType().withoutAnnotations(), "\$${fieldSpec.name}", fieldSpec.type.unwrapOptionalValue(fieldSpec.name)) diff --git a/apollo-compiler/src/test/graphql/com/example/fragments_with_type_condition_nullable/TestQuery.java b/apollo-compiler/src/test/graphql/com/example/fragments_with_type_condition_nullable/TestQuery.java index 433eab91093..00fb9248bb9 100644 --- a/apollo-compiler/src/test/graphql/com/example/fragments_with_type_condition_nullable/TestQuery.java +++ b/apollo-compiler/src/test/graphql/com/example/fragments_with_type_condition_nullable/TestQuery.java @@ -377,8 +377,14 @@ public ResponseFieldMarshaller marshaller() { return new ResponseFieldMarshaller() { @Override public void marshal(ResponseWriter writer) { - writer.writeFragment(humanDetails.marshaller()); - writer.writeFragment(droidDetails.marshaller()); + final HumanDetails $humanDetails = humanDetails; + if ($humanDetails != null) { + writer.writeFragment($humanDetails.marshaller()); + } + final DroidDetails $droidDetails = droidDetails; + if ($droidDetails != null) { + writer.writeFragment($droidDetails.marshaller()); + } } }; } @@ -575,8 +581,14 @@ public ResponseFieldMarshaller marshaller() { return new ResponseFieldMarshaller() { @Override public void marshal(ResponseWriter writer) { - writer.writeFragment(humanDetails.marshaller()); - writer.writeFragment(droidDetails.marshaller()); + final HumanDetails $humanDetails = humanDetails; + if ($humanDetails != null) { + writer.writeFragment($humanDetails.marshaller()); + } + final DroidDetails $droidDetails = droidDetails; + if ($droidDetails != null) { + writer.writeFragment($droidDetails.marshaller()); + } } }; } diff --git a/apollo-compiler/src/test/graphql/com/example/union_fragment/TestQuery.java b/apollo-compiler/src/test/graphql/com/example/union_fragment/TestQuery.java index 500f504dfe2..03a72be8bca 100644 --- a/apollo-compiler/src/test/graphql/com/example/union_fragment/TestQuery.java +++ b/apollo-compiler/src/test/graphql/com/example/union_fragment/TestQuery.java @@ -363,8 +363,14 @@ public ResponseFieldMarshaller marshaller() { return new ResponseFieldMarshaller() { @Override public void marshal(ResponseWriter writer) { - writer.writeFragment(character.marshaller()); - writer.writeFragment(starship.marshaller()); + final Character $character = character; + if ($character != null) { + writer.writeFragment($character.marshaller()); + } + final Starship $starship = starship; + if ($starship != null) { + writer.writeFragment($starship.marshaller()); + } } }; } diff --git a/apollo-normalized-cache/src/main/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCache.kt b/apollo-normalized-cache/src/main/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCache.kt index c16c8445506..af194b5de11 100644 --- a/apollo-normalized-cache/src/main/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCache.kt +++ b/apollo-normalized-cache/src/main/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCache.kt @@ -18,7 +18,7 @@ import kotlin.reflect.KClass * * A common configuration is to have secondary SQL cache. */ -class LruNormalizedCache internal constructor(evictionPolicy: EvictionPolicy) : NormalizedCache() { +open class LruNormalizedCache constructor(evictionPolicy: EvictionPolicy) : NormalizedCache() { private val lruCache: Cache = CacheBuilder.newBuilder().apply { @@ -97,4 +97,6 @@ class LruNormalizedCache internal constructor(evictionPolicy: EvictionPolicy) : put(this@LruNormalizedCache::class, lruCache.asMap()) putAll(nextCache?.dump().orEmpty()) } + + fun getFromCacheIfPresent(key: String): Record? = lruCache.getIfPresent(key) } diff --git a/apollo-normalized-cache/src/test/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCacheTest.kt b/apollo-normalized-cache/src/test/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCacheTest.kt index 3256471799a..be3d0d2d05c 100644 --- a/apollo-normalized-cache/src/test/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCacheTest.kt +++ b/apollo-normalized-cache/src/test/java/com/apollographql/apollo/cache/normalized/lru/LruNormalizedCacheTest.kt @@ -7,6 +7,8 @@ import com.apollographql.apollo.cache.normalized.NormalizedCache import com.apollographql.apollo.cache.normalized.Record import com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter import com.google.common.truth.Truth.assertThat +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Test import java.util.concurrent.TimeUnit @@ -248,6 +250,25 @@ class LruNormalizedCacheTest { assertThat(record2).isNull() } + @Test + fun test_getFromCacheIfPresent() { + val lruCache = createLruNormalizedCache() + val testRecord1 = createTestRecord("1") + val testRecordSet: MutableCollection = HashSet() + testRecordSet.add(testRecord1) + lruCache.merge(testRecordSet, CacheHeaders.NONE) + assertEquals(testRecord1, lruCache.getFromCacheIfPresent("key1")) + + val testRecord2 = createTestRecord("2") + lruCache.merge(testRecord2, CacheHeaders.NONE) + + assertEquals(testRecord2, lruCache.getFromCacheIfPresent("key2")) + + val testRecord3 = createTestRecord("2") + lruCache.merge(testRecord3, builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build()) + assertNull(lruCache.getFromCacheIfPresent("key3")) + } + private fun createLruNormalizedCache() = LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(10 * 1024.toLong()).build()).create(basicFieldAdapter) diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/interceptor/ApolloInterceptor.java b/apollo-runtime/src/main/java/com/apollographql/apollo/interceptor/ApolloInterceptor.java index 597d498b871..82332c2369d 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/interceptor/ApolloInterceptor.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/interceptor/ApolloInterceptor.java @@ -201,6 +201,9 @@ public Builder useHttpGetMethodForQueries(boolean useHttpGetMethodForQueries) { public Builder autoPersistQueries(boolean autoPersistQueries) { this.autoPersistQueries = autoPersistQueries; + if (autoPersistQueries) { + sendQueryDocument = false; + } return this; } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/subscription/RealSubscriptionManager.java b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/subscription/RealSubscriptionManager.java index 96d511f096b..fb4bc9e120f 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/internal/subscription/RealSubscriptionManager.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/internal/subscription/RealSubscriptionManager.java @@ -73,6 +73,7 @@ public void run() { } }; private final List onStateChangeListeners = new CopyOnWriteArrayList<>(); + private final boolean autoPersistSubscription; public RealSubscriptionManager(@NotNull ScalarTypeAdapters scalarTypeAdapters, @@ -89,6 +90,7 @@ public RealSubscriptionManager(@NotNull ScalarTypeAdapters scalarTypeAdapters, this.transport = transportFactory.create(new SubscriptionTransportCallback(this, dispatcher)); this.dispatcher = dispatcher; this.connectionHeartbeatTimeoutMs = connectionHeartbeatTimeoutMs; + this.responseNormalizer = responseNormalizer; this.autoPersistSubscription = autoPersistSubscription; } @@ -309,7 +311,9 @@ void onOperationServerMessage(OperationServerMessage message) { } else if (message instanceof OperationServerMessage.Complete) { onCompleteServerMessage((OperationServerMessage.Complete) message); } else if (message instanceof OperationServerMessage.ConnectionError) { - disconnect(true); + // Jordan - Ignore connection errors, as they don't always mean the socket should be disconnected + // See https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md#gql_connection_error +// disconnect(true); } else if (message instanceof OperationServerMessage.ConnectionKeepAlive) { resetConnectionKeepAliveTimerTask(); } diff --git a/apollo-runtime/src/main/java/com/apollographql/apollo/subscription/OperationClientMessage.java b/apollo-runtime/src/main/java/com/apollographql/apollo/subscription/OperationClientMessage.java index 5dd669f2871..660c6879568 100644 --- a/apollo-runtime/src/main/java/com/apollographql/apollo/subscription/OperationClientMessage.java +++ b/apollo-runtime/src/main/java/com/apollographql/apollo/subscription/OperationClientMessage.java @@ -58,6 +58,7 @@ public Init(@NotNull Map connectionParams) { public static final class Start extends OperationClientMessage { private static final String TYPE = "start"; private static final String JSON_KEY_QUERY = "query"; + private static final String JSON_KEY_ID = "id"; private static final String JSON_KEY_VARIABLES = "variables"; private static final String JSON_KEY_OPERATION_NAME = "operationName"; private static final String JSON_KEY_EXTENSIONS = "extensions"; @@ -65,6 +66,7 @@ public static final class Start extends OperationClientMessage { private static final String JSON_KEY_EXTENSIONS_PERSISTED_QUERY_VERSION = "version"; private static final String JSON_KEY_EXTENSIONS_PERSISTED_QUERY_HASH = "sha256Hash"; private final ScalarTypeAdapters scalarTypeAdapters; + private boolean enableAutoPersistedQueries; public final String subscriptionId; public final Subscription subscription; diff --git a/apollo-runtime/src/test/java/com/apollographql/apollo/internal/interceptor/ApolloAutoPersistedQueryInterceptorTest.java b/apollo-runtime/src/test/java/com/apollographql/apollo/internal/interceptor/ApolloAutoPersistedQueryInterceptorTest.java index 085ca80cf3c..2ae3f613424 100644 --- a/apollo-runtime/src/test/java/com/apollographql/apollo/internal/interceptor/ApolloAutoPersistedQueryInterceptorTest.java +++ b/apollo-runtime/src/test/java/com/apollographql/apollo/internal/interceptor/ApolloAutoPersistedQueryInterceptorTest.java @@ -104,7 +104,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request, ) ); } else if (proceedAsyncInvocationCount == 2) { - assertThat(request.sendQueryDocument).isTrue(); + assertThat(request.sendQueryDocument).isFalse(); assertThat(request.autoPersistQueries).isTrue(); callBack.onResponse( new ApolloInterceptor.InterceptorResponse( @@ -157,7 +157,7 @@ public void proceedAsync(@NotNull ApolloInterceptor.InterceptorRequest request, ) ); } else if (proceedAsyncInvocationCount == 2) { - assertThat(request.sendQueryDocument).isTrue(); + assertThat(request.sendQueryDocument).isFalse(); assertThat(request.autoPersistQueries).isTrue(); callBack.onResponse( new ApolloInterceptor.InterceptorResponse(