Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable key fields check if unnecessary and optimize its perf #3720

Merged
merged 5 commits into from
Dec 15, 2021
Merged
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 @@ -2,7 +2,6 @@ package com.apollographql.apollo3.ast

import com.apollographql.apollo3.annotations.ApolloExperimental
import com.apollographql.apollo3.ast.internal.buffer
import okio.Buffer

/**
* A wrapper around a schema GQLDocument that:
Expand Down Expand Up @@ -92,6 +91,15 @@ class Schema(
}
}

/**
* Returns whether the `typePolicy` directive is present on at least one object in the schema
*/
fun hasTypeWithTypePolicy(): Boolean {
return typeDefinitions.values.filterIsInstance<GQLObjectTypeDefinition>().any { objectType ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for our future selves: we'll want to support @typePolicy on interfaces and unions (see #3356). This will need to be updated when this happens

objectType.directives.any { it.name == TYPE_POLICY }
}
}

/**
* Returns the key fields for the given type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@ package com.apollographql.apollo3.ast
private class CheckKeyFieldsScope(
val schema: Schema,
val allFragmentDefinitions: Map<String, GQLFragmentDefinition>,
)
) {
private val implementedTypesCache = mutableMapOf<String, Set<String>>()
fun implementedTypes(name: String) = implementedTypesCache.getOrPut(name) {
schema.implementedTypes(name)
}

private val keyFieldsCache = mutableMapOf<String, Set<String>>()
fun keyFields(name: String) = keyFieldsCache.getOrPut(name) {
schema.keyFields(name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤

}
}

fun checkKeyFields(operation: GQLOperationDefinition, schema: Schema, allFragmentDefinitions: Map<String, GQLFragmentDefinition>) {
val parentType = operation.rootTypeDefinition(schema)!!.name
CheckKeyFieldsScope(schema, allFragmentDefinitions).checkField("Operation(${operation.name})", operation.selectionSet.selections, parentType)
}

fun checkKeyFields(fragmentDefinition: GQLFragmentDefinition, schema: Schema, allFragmentDefinitions: Map<String, GQLFragmentDefinition>) {
fun checkKeyFields(
fragmentDefinition: GQLFragmentDefinition,
schema: Schema,
allFragmentDefinitions: Map<String, GQLFragmentDefinition>,
) {
CheckKeyFieldsScope(schema, allFragmentDefinitions).checkField("Fragment(${fragmentDefinition.name})", fragmentDefinition.selectionSet.selections, fragmentDefinition.typeCondition.name)
}

Expand All @@ -25,7 +39,7 @@ private fun CheckKeyFieldsScope.checkField(
}

private fun CheckKeyFieldsScope.checkFieldSet(path: String, selections: List<GQLSelection>, parentType: String, possibleType: String) {
val implementedTypes = schema.implementedTypes(possibleType)
val implementedTypes = implementedTypes(possibleType)

val mergedFields = collectFields(selections, parentType, implementedTypes).groupBy {
it.field.name
Expand All @@ -36,7 +50,7 @@ private fun CheckKeyFieldsScope.checkFieldSet(path: String, selections: List<GQL
val fieldNames = mergedFields.map { it.first().field }
.filter { it.alias == null }
.map { it.name }.toSet()
val keyFieldNames = schema.keyFields(possibleType)
val keyFieldNames = keyFields(possibleType)

val missingFieldNames = keyFieldNames.subtract(fieldNames)
check(missingFieldNames.isEmpty()) {
Expand All @@ -58,6 +72,9 @@ private fun CheckKeyFieldsScope.collectFields(
parentType: String,
implementedTypes: Set<String>,
): List<FieldWithParent> {
if (selections.isEmpty()) {
return emptyList()
}
if (!implementedTypes.contains(parentType)) {
return emptyList()
}
Expand Down Expand Up @@ -94,4 +111,4 @@ private fun List<GQLDirective>?.hasCondition(): Boolean {
it.name == "skip" && (it.arguments!!.arguments.first().value as? GQLStringValue)?.value != "false"
|| it.name == "include" && (it.arguments!!.arguments.first().value as? GQLStringValue)?.value != "true"
} ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,15 @@ object ApolloCompiler {
// Remember the fragments with the possibly updated fragments
val allFragmentDefinitions = (fragments + incomingFragments).associateBy { it.name }

operations.forEach {
checkKeyFields(it, options.schema, allFragmentDefinitions)
}
fragments.forEach {
checkKeyFields(it, options.schema, allFragmentDefinitions)
// Check if all the key fields are present in operations and fragments
// (do this only if there are key fields as it may be costly)
if (options.schema.hasTypeWithTypePolicy()) {
operations.forEach {
checkKeyFields(it, options.schema, allFragmentDefinitions)
}
fragments.forEach {
checkKeyFields(it, options.schema, allFragmentDefinitions)
}
}

var alwaysGenerateTypesMatching = options.alwaysGenerateTypesMatching
Expand Down