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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hidden field validation #664

Merged
merged 1 commit into from
Jan 7, 2025
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
18 changes: 15 additions & 3 deletions lib/src/main/java/graphql/nadel/validation/NadelFieldValidation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class NadelFieldValidation internal constructor(
parent: NadelServiceSchemaElement.FieldsContainer,
overallFields: List<GraphQLFieldDefinition>,
): NadelSchemaValidationResult {
if(areAllFieldsHidden(overallFields)) {
if (isTypeBrokenByHiddenFields(parent, overallFields)) {
return NadelSchemaValidationError.AllFieldsUsingHiddenDirective(parent)
}

return overallFields
.asSequence()
.let { fieldSequence ->
Expand All @@ -57,8 +58,19 @@ class NadelFieldValidation internal constructor(
.toResult()
}

private fun areAllFieldsHidden(overallFields: List<GraphQLFieldDefinition>): Boolean {
return overallFields.all { it.hasAppliedDirective(NadelDirectives.hiddenDirectiveDefinition.name) };
context(NadelValidationContext)
private fun isTypeBrokenByHiddenFields(
parent: NadelServiceSchemaElement.FieldsContainer,
overallFields: List<GraphQLFieldDefinition>,
): Boolean {
// Means type is deleted, so we're fine
if (hiddenTypeNames.contains(parent.overall.name)) {
return false
}

return overallFields.all {
it.hasAppliedDirective(NadelDirectives.hiddenDirectiveDefinition.name)
}
}

context(NadelValidationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class NadelSchemaValidation internal constructor(

val operationTypes = getOperationTypeNames(engineSchema)
val namespaceTypes = getNamespaceOperationTypes(engineSchema)
val hiddenTypeNames = getHiddenTypeNames(engineSchema)

val definitions = definitionParser.parse(engineSchema)
.onError { return it }
Expand All @@ -60,6 +61,7 @@ class NadelSchemaValidation internal constructor(
hydrationUnions = getHydrationUnions(engineSchema),
namespaceTypeNames = namespaceTypes,
combinedTypeNames = namespaceTypes + operationTypes.map { it.name },
hiddenTypeNames = hiddenTypeNames,
definitions = definitions,
hook = hook,
)
Expand Down Expand Up @@ -208,6 +210,37 @@ private fun makeFieldContributorMap(services: List<Service>): Map<FieldCoordinat
.toMapStrictly()
}

/**
* Gets the types that will be removed by the `@hidden` directive.
*
* i.e. all fields that reference this type are `@hidden`
*/
private fun getHiddenTypeNames(engineSchema: GraphQLSchema): Set<String> {
val hiddenTypeNames = HashMap<String, Boolean>(engineSchema.typeMap.size)

engineSchema
.typeMap
.values
.asSequence()
.filterIsInstance<GraphQLFieldsContainer>()
.forEach { type ->
type.fields.forEach { field ->
val outputTypeName = field.type.unwrapAll().name

if (field.hasAppliedDirective(NadelDirectives.hiddenDirectiveDefinition.name)) {
val existing = hiddenTypeNames[outputTypeName]
if (existing == null) {
hiddenTypeNames[outputTypeName] = true
}
} else {
hiddenTypeNames[outputTypeName] = false
}
}
}

return hiddenTypeNames.keys
}

private fun getOperationTypeNames(engineSchema: GraphQLSchema): List<GraphQLObjectType> {
return listOfNotNull(
engineSchema.queryType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class NadelValidationContext internal constructor(
val hydrationUnions: Set<String>,
val namespaceTypeNames: Set<String>,
val combinedTypeNames: Set<String>,
val hiddenTypeNames: Set<String>,
val definitions: Map<NadelSchemaMemberCoordinates, List<NadelDefinition>>,
val hook: NadelSchemaValidationHook,
) {
Expand Down
Loading
Loading