Skip to content

Commit cd0be1f

Browse files
committed
Add test, use SortedSet in internal data classs
1 parent 7f446ab commit cd0be1f

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ internal object AddKeyFieldsExecutableDocumentTransform : ExecutableDocumentTran
109109
val parentTypeKeyFields = keyFields[parentType] ?: emptySet()
110110
newSelections.filterIsInstance<GQLField>().forEach {
111111
// Disallow fields whose alias conflicts with a key field, or is "__typename"
112-
if (parentTypeKeyFields.contains(it.alias) || it.alias == "__typename") {
112+
if (it.alias != null && (parentTypeKeyFields.contains(it.alias) || it.alias == "__typename")) {
113113
throw SourceAwareException(
114114
error = "Apollo: Field '${it.alias}: ${it.name}' in $parentType conflicts with key fields",
115115
sourceLocation = it.sourceLocation

normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/getTypePolicies.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import com.apollographql.apollo.ast.GQLTypeDefinition
77
import com.apollographql.apollo.ast.Schema
88
import com.apollographql.apollo.ast.Schema.Companion.TYPE_POLICY
99
import com.apollographql.apollo.ast.SourceAwareException
10+
import java.util.SortedSet
1011

1112
internal data class TypePolicy(
12-
val keyFields: Set<String>,
13+
val keyFields: SortedSet<String>,
1314
val embeddedFields: Set<String>,
1415
)
1516

@@ -83,7 +84,7 @@ private fun Schema.validateAndComputeTypePolicy(
8384

8485
private fun GQLDirective.toTypePolicy(): TypePolicy {
8586
return TypePolicy(
86-
keyFields = extractFields("keyFields"),
87+
keyFields = extractFields("keyFields").toSortedSet(),
8788
embeddedFields = extractFields("embeddedFields")
8889
)
8990
}

normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/GetTypePoliciesTest.kt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,43 @@ class GetTypePoliciesTest {
5858
).getOrThrow()
5959

6060
val expected = mapOf(
61-
"User" to TypePolicy(keyFields = setOf("id"), embeddedFields = emptySet()),
62-
"Animal" to TypePolicy(keyFields = setOf("kingdom", "species"), embeddedFields = emptySet()),
63-
"Lion" to TypePolicy(keyFields = setOf("kingdom", "species"), embeddedFields = emptySet()),
64-
"HasId" to TypePolicy(keyFields = setOf("id"), embeddedFields = emptySet()),
65-
"Circle" to TypePolicy(keyFields = setOf("id"), embeddedFields = emptySet()),
66-
"Square" to TypePolicy(keyFields = setOf("radius"), embeddedFields = emptySet()),
61+
"User" to TypePolicy(keyFields = sortedSetOf("id"), embeddedFields = emptySet()),
62+
"Animal" to TypePolicy(keyFields = sortedSetOf("kingdom", "species"), embeddedFields = emptySet()),
63+
"Lion" to TypePolicy(keyFields = sortedSetOf("kingdom", "species"), embeddedFields = emptySet()),
64+
"HasId" to TypePolicy(keyFields = sortedSetOf("id"), embeddedFields = emptySet()),
65+
"Circle" to TypePolicy(keyFields = sortedSetOf("id"), embeddedFields = emptySet()),
66+
"Square" to TypePolicy(keyFields = sortedSetOf("radius"), embeddedFields = emptySet()),
67+
)
68+
69+
assertEquals(expected, schema.getTypePolicies())
70+
}
71+
72+
@Test
73+
fun ensureTypePolicyKeyFieldsAreSorted() {
74+
// language=GraphQL
75+
val schema = """
76+
type Query {
77+
animal: Animal
78+
}
79+
80+
type Animal @typePolicy(keyFields: "kingdom species genus class domain") {
81+
kingdom: String!
82+
species: String!
83+
genus: String!
84+
domain: String!
85+
class: String!
86+
}
87+
""".trimIndent()
88+
.parseAsGQLDocument().getOrThrow()
89+
.validateAsSchema(
90+
SchemaValidationOptions(
91+
addKotlinLabsDefinitions = true,
92+
foreignSchemas = emptyList()
93+
)
94+
).getOrThrow()
95+
96+
val expected = mapOf(
97+
"Animal" to TypePolicy(keyFields = sortedSetOf("class", "domain", "genus", "kingdom", "species"), embeddedFields = emptySet()),
6798
)
6899

69100
assertEquals(expected, schema.getTypePolicies())

0 commit comments

Comments
 (0)