Skip to content

Commit

Permalink
Do not merge inline fragments with directives (#3189)
Browse files Browse the repository at this point in the history
* [compat] Fix inline fragments with include directive

Even if these inline fragments have the same type condition as their
parent type, we can't merge them as the nullability status of their
fields might be different

Closes #2796

* Compound -> Composite

* update test fixtures
  • Loading branch information
martinbonnin authored Jun 25, 2021
1 parent d7ba77d commit f233416
Show file tree
Hide file tree
Showing 60 changed files with 1,060 additions and 953 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ val CompiledBooleanType = ScalarType("Boolean")
@SharedImmutable
val CompiledIDType = ScalarType("ID")

fun CompiledNamedType.isCompound(): Boolean {
fun CompiledNamedType.isComposite(): Boolean {
return when (this) {
is UnionType,
is InterfaceType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,37 +190,50 @@ internal class OperationBasedModelGroupBuilder(
*/
val inlineFragmentsFields = selections.map { it.selection }.filterIsInstance<GQLInlineFragment>()
.groupBy {
InlineFragmentKey(it.typeCondition.name, it.directives.toBooleanExpression())
}.entries.map { entry ->
val inlineFragmentsWithSameKey = entry.value
val typeCondition = entry.key.typeCondition
val prefix = if (collectAllInlineFragmentFields) "as" else "on"

val childInfo = IrFieldInfo(
responseName = "$prefix${entry.key.toName()}",
description = "Synthetic field for inline fragment on $typeCondition",
deprecationReason = null,
type = IrModelType(IrUnknownModelId)
)

val possibleTypes = schema.possibleTypes(typeCondition)
val childCondition = entry.key.condition.and(BooleanExpression.Element(BPossibleTypes(possibleTypes))).simplify()

val childSelections = inlineFragmentsWithSameKey.flatMap {
it.selectionSet.selections.map { SelectionWithParent(it, typeCondition) }
}.toMutableList()
if (collectAllInlineFragmentFields) {
childSelections.addAll(selections.filter { it.selection is GQLField})
}

buildField(
root = root,
path = selfPath,
info = childInfo,
selections = childSelections,
condition = childCondition,
isSynthetic = true
)
it.typeCondition.name
}.entries.flatMap {
val typeCondition = it.key

// If there is only one fragment, no need to disambiguate it
val nameNeedsCondition = it.value.size > 1

it.value.groupBy { it.directives.toBooleanExpression() }
.entries.map { entry ->
val prefix = if (collectAllInlineFragmentFields) "as" else "on"

val name = if (nameNeedsCondition) {
InlineFragmentKey(typeCondition, entry.key).toName()
} else {
InlineFragmentKey(typeCondition, BooleanExpression.True).toName()
}

val childInfo = IrFieldInfo(
responseName = "$prefix$name",
description = "Synthetic field for inline fragment on $typeCondition",
deprecationReason = null,
type = IrModelType(IrUnknownModelId)
)

val possibleTypes = schema.possibleTypes(typeCondition)
val childCondition = entry.key.and(BooleanExpression.Element(BPossibleTypes(possibleTypes))).simplify()

var childSelections = entry.value.flatMap {
it.selectionSet.selections.map { SelectionWithParent(it, typeCondition) }
}

if (collectAllInlineFragmentFields) {
childSelections = selections.filter { it.selection is GQLField} + childSelections
}

buildField(
root = root,
path = selfPath,
info = childInfo,
selections = childSelections,
condition = childCondition,
isSynthetic = true
)
}
}

/**
Expand Down

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

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

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

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

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

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

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

Loading

0 comments on commit f233416

Please sign in to comment.