Skip to content

Commit 788958e

Browse files
committed
Ignore adding key fields if the cache compiler plugin is present
1 parent e221fe1 commit 788958e

File tree

3 files changed

+41
-20
lines changed
  • libraries

3 files changed

+41
-20
lines changed

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/definitions.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,41 @@ internal val kotlinLabsDefinitions_0_3 = """
3535
""${'"'}
3636
directive @typePolicy(
3737
""${'"'}
38-
a selection set containing fields used to compute the cache key of an object. Order is important.
38+
A selection set containing fields used to compute the cache key of an object.
39+
Nested selection sets are currently not supported. Order is important.
40+
41+
Key fields can be defined on interfaces. In that case, the key fields apply to all sub-types. Sub-types are not allowed to define their own key fields.
42+
43+
The key fields are automatically added to the operations by the compiler.
44+
Aliased key fields are not recognized and the compiler adds a non-aliased version of the field if that happens.
45+
If a type is queried through an interface/union, this may add fragments.
46+
47+
For an example, this query:
48+
49+
```graphql
50+
query {
51+
product {
52+
price
53+
}
54+
}
55+
```
56+
57+
is turned into this one after compilation:
58+
59+
```graphql
60+
query {
61+
product {
62+
... on Book {
63+
isbn
64+
}
65+
... on Movie {
66+
id
67+
}
68+
price
69+
}
70+
}
71+
```
72+
3973
""${'"'}
4074
keyFields: String! = "",
4175
""${'"'}

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,16 @@ object ApolloCompiler {
232232

233233
/**
234234
* Step 2, Modify the AST to add typename, key fields and call any user-provided transform.
235-
* We only do this if we detect that the cache plugin is not applied since it's also doing those same computations.
235+
* If we detect that the cache compiler plugin is present, we skip adding the keyfields because it will do it.
236+
* TODO: deprecate `addTypename`
236237
*/
237238
val hasCacheCompilerPlugin = try {
238239
Class.forName("com.apollographql.cache.apollocompilerplugin.internal.ApolloCacheCompilerPlugin")
239240
true
240241
} catch (_: ClassNotFoundException) {
241242
false
242243
}
243-
244+
244245
var document = ApolloOperationsTransform(options.addTypename ?: defaultAddTypename, !hasCacheCompilerPlugin).transform(
245246
schema = schema,
246247
document = GQLDocument(userDefinitions, sourceLocation = null),

libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/TypenameTest.kt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.apollographql.apollo.compiler
22

3-
import com.apollographql.apollo.ast.GQLDocument
4-
import com.apollographql.apollo.ast.GQLFragmentDefinition
5-
import com.apollographql.apollo.ast.GQLOperationDefinition
63
import com.apollographql.apollo.ast.toExecutableDocument
74
import com.apollographql.apollo.ast.toGQLDocument
85
import com.apollographql.apollo.ast.toSchema
96
import com.apollographql.apollo.ast.toUtf8
10-
import com.apollographql.apollo.ast.validateAsSchema
11-
import com.apollographql.apollo.compiler.internal.addRequiredFields
7+
import com.apollographql.apollo.compiler.internal.ApolloOperationsTransform
128
import com.google.common.truth.Truth.assertThat
139
import com.google.testing.junit.testparameterinjector.TestParameter
1410
import com.google.testing.junit.testparameterinjector.TestParameterInjector
@@ -28,19 +24,9 @@ class TypenameTest(
2824
val schemaFile = File("src/test/graphql/schema.graphqls")
2925
val schema = schemaFile.toGQLDocument().toSchema()
3026

31-
val definitions = graphQLFile.source().buffer().toExecutableDocument(schema).definitions
27+
val document = graphQLFile.source().buffer().toExecutableDocument(schema)
3228

33-
val fragments = definitions.filterIsInstance<GQLFragmentDefinition>().associateBy { it.name }
34-
val documentWithTypename = GQLDocument(
35-
definitions = definitions.map {
36-
when (it) {
37-
is GQLOperationDefinition -> addRequiredFields(it, addTypename, schema, fragments)
38-
is GQLFragmentDefinition -> addRequiredFields(it, addTypename, schema, fragments)
39-
else -> it
40-
}
41-
},
42-
sourceLocation = null
43-
).toUtf8()
29+
val documentWithTypename = ApolloOperationsTransform(addTypename, false).transform(schema, document, emptyList()).toUtf8()
4430

4531
val extra = when(addTypename) {
4632
"ifFragments" -> "" // for backward compat

0 commit comments

Comments
 (0)