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

Validation to prevent all field types as @hidden #646

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
@@ -1,6 +1,7 @@
package graphql.nadel.validation

import graphql.nadel.engine.util.unwrapAll
import graphql.nadel.schema.NadelDirectives
import graphql.nadel.validation.NadelSchemaValidationError.IncompatibleArgumentInputType
import graphql.nadel.validation.NadelSchemaValidationError.IncompatibleFieldOutputType
import graphql.nadel.validation.NadelSchemaValidationError.MissingArgumentOnUnderlying
Expand Down Expand Up @@ -36,6 +37,9 @@ class NadelFieldValidation internal constructor(
parent: NadelServiceSchemaElement.FieldsContainer,
overallFields: List<GraphQLFieldDefinition>,
): NadelSchemaValidationResult {
if(areAllFieldsHidden(overallFields)) {
return NadelSchemaValidationError.AllFieldsUsingHiddenDirective(parent)
}
return overallFields
.asSequence()
.let { fieldSequence ->
Expand All @@ -53,6 +57,10 @@ class NadelFieldValidation internal constructor(
.toResult()
}

private fun areAllFieldsHidden(overallFields: List<GraphQLFieldDefinition>): Boolean {
return overallFields.all { it.hasAppliedDirective(NadelDirectives.hiddenDirectiveDefinition.name) };
}

context(NadelValidationContext)
fun validate(
parent: NadelServiceSchemaElement.FieldsContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ sealed interface NadelSchemaValidationError : NadelSchemaValidationResult {

override val subject = overallField
}

data class AllFieldsUsingHiddenDirective(
val parentType: NadelServiceSchemaElement,
) : NadelSchemaValidationError {
val service: Service get() = parentType.service

override val message = run {
val ot = parentType.overall.name
"Must have at least one field without @hidden on type $ot"
}

override val subject = parentType.overall
}
}

private fun toString(element: GraphQLNamedSchemaElement): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,5 +676,30 @@ class NadelFieldValidationTest : DescribeSpec({

assert(error.message == "The overall field Query.fieldA defines argument id which does not exist in service test field Query.fieldA")
}

it("fails if schema contains all fields as hidden") {
val fixture = NadelValidationTestFixture(
overallSchema = mapOf(
"test" to """
type Query {
echo: String @hidden
}
""".trimIndent(),
),
underlyingSchema = mapOf(
"test" to """
type Query {
echo: String
}
""".trimIndent(),
),
)

val errors = validate(fixture)
assert(errors.map { it.message }.isNotEmpty())

val error = errors.assertSingleOfType<NadelSchemaValidationError>()
assert(error.message == "Must have at least one field without @hidden on type Query")
}
}
})
Loading