Skip to content

Commit 7add149

Browse files
authored
Track the spec edits (#6867)
1 parent 506615f commit 7add149

File tree

6 files changed

+122
-9
lines changed

6 files changed

+122
-9
lines changed

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

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
package com.apollographql.apollo.ast.internal
22

3+
import com.apollographql.apollo.ast.GQLBooleanValue
34
import com.apollographql.apollo.ast.GQLDefinition
45
import com.apollographql.apollo.ast.GQLDirective
56
import com.apollographql.apollo.ast.GQLDirectiveDefinition
67
import com.apollographql.apollo.ast.GQLDirectiveExtension
78
import com.apollographql.apollo.ast.GQLEnumTypeDefinition
89
import com.apollographql.apollo.ast.GQLEnumTypeExtension
10+
import com.apollographql.apollo.ast.GQLEnumValue
911
import com.apollographql.apollo.ast.GQLEnumValueDefinition
1012
import com.apollographql.apollo.ast.GQLFieldDefinition
13+
import com.apollographql.apollo.ast.GQLFloatValue
1114
import com.apollographql.apollo.ast.GQLInputObjectTypeDefinition
1215
import com.apollographql.apollo.ast.GQLInputObjectTypeExtension
1316
import com.apollographql.apollo.ast.GQLInputValueDefinition
17+
import com.apollographql.apollo.ast.GQLIntValue
1418
import com.apollographql.apollo.ast.GQLInterfaceTypeDefinition
1519
import com.apollographql.apollo.ast.GQLInterfaceTypeExtension
1620
import com.apollographql.apollo.ast.GQLListType
21+
import com.apollographql.apollo.ast.GQLListValue
1722
import com.apollographql.apollo.ast.GQLNamed
1823
import com.apollographql.apollo.ast.GQLNamedType
1924
import com.apollographql.apollo.ast.GQLNode
2025
import com.apollographql.apollo.ast.GQLNonNullType
26+
import com.apollographql.apollo.ast.GQLNullValue
2127
import com.apollographql.apollo.ast.GQLObjectTypeDefinition
2228
import com.apollographql.apollo.ast.GQLObjectTypeExtension
29+
import com.apollographql.apollo.ast.GQLObjectValue
2330
import com.apollographql.apollo.ast.GQLResult
2431
import com.apollographql.apollo.ast.GQLScalarTypeDefinition
2532
import com.apollographql.apollo.ast.GQLScalarTypeExtension
@@ -32,6 +39,8 @@ import com.apollographql.apollo.ast.GQLType
3239
import com.apollographql.apollo.ast.GQLTypeSystemExtension
3340
import com.apollographql.apollo.ast.GQLUnionTypeDefinition
3441
import com.apollographql.apollo.ast.GQLUnionTypeExtension
42+
import com.apollographql.apollo.ast.GQLValue
43+
import com.apollographql.apollo.ast.GQLVariableValue
3544
import com.apollographql.apollo.ast.Issue
3645
import com.apollographql.apollo.ast.MergeOptions
3746
import com.apollographql.apollo.ast.OtherValidationIssue
@@ -90,8 +99,8 @@ internal class ExtensionsMerger(private val definitions: List<GQLDefinition>, in
9099
return null
91100
}
92101

93-
if (incoming.description != null) {
94-
issues.add(OtherValidationIssue("Cannot merge field '${incoming.name}': descriptions cannot be merged", incoming.sourceLocation))
102+
if (incoming.description != null && incoming.description != existing.description) {
103+
issues.add(OtherValidationIssue("Cannot merge field '${incoming.name}': descriptions are different", incoming.sourceLocation))
95104
}
96105

97106
return existing.copy(
@@ -114,11 +123,11 @@ private fun ExtensionsMerger.mergeArguments(
114123
if (!areEqual(existing.type, incoming.type)) {
115124
issues.add(OtherValidationIssue("Cannot merge argument '${incoming.name}': wrong type '${incoming.type.toUtf8()}' (expected: '${existing.type.toUtf8()}')", incoming.sourceLocation))
116125
}
117-
if (incoming.description != null) {
118-
issues.add(OtherValidationIssue("Cannot merge argument '${incoming.name}': descriptions cannot be merged", incoming.sourceLocation))
126+
if (incoming.description != null && incoming.description != existing.description) {
127+
issues.add(OtherValidationIssue("Cannot merge argument '${incoming.name}': descriptions are different", incoming.sourceLocation))
119128
}
120-
if (incoming.defaultValue != null) {
121-
issues.add(OtherValidationIssue("Cannot merge argument '${incoming.name}': default values cannot be merged", incoming.sourceLocation))
129+
if (incoming.defaultValue != null && !areEqual(incoming.defaultValue, existing.defaultValue)) {
130+
issues.add(OtherValidationIssue("Cannot merge argument '${incoming.name}': default values are different", incoming.sourceLocation))
122131
}
123132
newArguments[index] = existing.copy(
124133
directives = mergeDirectives(existing.directives, incoming.directives)
@@ -467,6 +476,93 @@ private fun areEqual(a: GQLType, b: GQLType): Boolean {
467476
}
468477
}
469478

479+
private fun areEqual(a: GQLValue?, b: GQLValue?): Boolean {
480+
return when (a) {
481+
null -> {
482+
b == null
483+
}
484+
is GQLBooleanValue -> {
485+
if (b !is GQLBooleanValue){
486+
false
487+
} else {
488+
a.value == b.value
489+
}
490+
}
491+
is GQLEnumValue -> {
492+
if (b !is GQLEnumValue) {
493+
false
494+
} else {
495+
a.value == b.value
496+
}
497+
}
498+
is GQLFloatValue -> {
499+
if (b !is GQLFloatValue) {
500+
false
501+
} else {
502+
a.value == b.value
503+
}
504+
}
505+
is GQLIntValue -> {
506+
if (b !is GQLIntValue) {
507+
false
508+
} else {
509+
a.value == b.value
510+
}
511+
}
512+
is GQLListValue -> {
513+
if (b !is GQLListValue) {
514+
false
515+
} else {
516+
if (a.values.size != b.values.size) {
517+
false
518+
} else {
519+
for (i in a.values.indices) {
520+
if (a.values.get(i) != b.values.get(i)) {
521+
return false
522+
}
523+
}
524+
true
525+
}
526+
}
527+
}
528+
is GQLNullValue -> b is GQLNullValue
529+
is GQLObjectValue -> {
530+
if (b !is GQLObjectValue) {
531+
false
532+
} else {
533+
// TODO: Can object literal have duplicate fields?
534+
if (a.fields.size != b.fields.size) {
535+
false
536+
} else {
537+
for (i in a.fields.indices) {
538+
val aField = a.fields.get(i)
539+
val bField = b.fields.firstOrNull { it.name == aField.name }
540+
if (bField == null) {
541+
return false
542+
}
543+
return aField.value == bField.value
544+
}
545+
true
546+
}
547+
}
548+
}
549+
is GQLStringValue -> {
550+
if (b !is GQLStringValue) {
551+
false
552+
} else {
553+
a.value == b.value
554+
}
555+
}
556+
is GQLVariableValue -> {
557+
if (b !is GQLVariableValue) {
558+
false
559+
} else {
560+
a.name == b.name
561+
}
562+
}
563+
}
564+
}
565+
470566
private inline fun <reified T : GQLNode> ExtensionsMerger.mergeUniquesOrThrow(
471567
list: List<T>,
472568
others: List<T>,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OtherValidationIssue (8:10)
2-
Cannot merge argument 'id': default values cannot be merged
2+
Cannot merge argument 'id': default values are different
33
type Query {
44
random(id: ID!): Int
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OtherValidationIssue (8:10)
2-
Cannot merge argument 'id': descriptions cannot be merged
2+
Cannot merge argument 'id': descriptions are different
33
type Query {
44
random(id: ID!): Int
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
OtherValidationIssue (8:3)
2-
Cannot merge field 'random': descriptions cannot be merged
2+
Cannot merge field 'random': descriptions are different
33
type Query {
44
random: Int
55
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
type Query {
3+
"""
4+
a field
5+
"""
6+
random("an argument" id: ID!): Int @deprecated
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# PRAGMA allowMergingFieldDefinitions
2+
3+
type Query {
4+
"a field"
5+
random("an argument" id: ID!): Int
6+
}
7+
extend type Query {
8+
"a field"
9+
random("an argument" id: ID!): Int @deprecated
10+
}

0 commit comments

Comments
 (0)