Skip to content

Add key fields to selections even when they're already selected with an alias #6503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.apollographql.apollo.ast.Schema
import com.apollographql.apollo.ast.definitionFromScope
import com.apollographql.apollo.ast.isAbstract
import com.apollographql.apollo.ast.rawType
import com.apollographql.apollo.ast.responseName
import com.apollographql.apollo.ast.rootTypeDefinition
import com.apollographql.apollo.compiler.ADD_TYPENAME_ALWAYS
import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_ABSTRACT
Expand Down Expand Up @@ -94,7 +95,7 @@ private fun List<GQLSelection>.addRequiredFields(
requiredFieldNames.add("__typename")
}

val fieldNames = parentFields + selectionSet.filterIsInstance<GQLField>().map { it.name }.toSet()
val fieldNames = parentFields + selectionSet.filterIsInstance<GQLField>().map { it.responseName() }.toSet()

var newSelections = selectionSet.map {
when (it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ internal fun checkKeyFields(
schema: Schema,
allFragmentDefinitions: Map<String, GQLFragmentDefinition>,
) {
CheckKeyFieldsScope(schema, allFragmentDefinitions).checkField("Fragment(${fragmentDefinition.name})", fragmentDefinition.selections, fragmentDefinition.typeCondition.name)
CheckKeyFieldsScope(schema, allFragmentDefinitions).checkField(
"Fragment(${fragmentDefinition.name})",
fragmentDefinition.selections,
fragmentDefinition.typeCondition.name
)
}

private fun CheckKeyFieldsScope.checkField(
Expand All @@ -61,9 +65,9 @@ private fun CheckKeyFieldsScope.checkFieldSet(path: String, selections: List<GQL

if (implementedTypes.contains(parentType)) {
// only check types that are actually possible
val fieldNames = mergedFields.map { it.first().field }
.filter { it.alias == null }
.map { it.name }.toSet()
val fieldNames = mergedFields
.flatMap { it.filter { it.field.alias == null }.map { it.field.name } }
.toSet()
val keyFieldNames = keyFields(possibleType)

val missingFieldNames = keyFieldNames.subtract(fieldNames)
Expand Down Expand Up @@ -101,13 +105,15 @@ private fun CheckKeyFieldsScope.collectFields(

listOf(FieldWithParent(it, parentType))
}

is GQLInlineFragment -> {
if (it.directives.hasCondition()) {
return@flatMap emptyList()
}

collectFields(it.selections, it.typeCondition?.name ?: parentType, implementedTypes)
}

is GQLFragmentSpread -> {
if (it.directives.hasCondition()) {
return@flatMap emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query GetA {
a {
...AFragment1
}
}

fragment AFragment1 on A {
foo
...AFragment2
}

fragment AFragment2 on A {
bar
myId: id
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading