Skip to content

Commit 1030fc8

Browse files
authored
Validation to prevent all field types as @hidden (#646)
* added validation to prevent all field types being hidden * moved validation error and removed extra arg * added validation to prevent all field types being hidden * moved validation error and removed extra arg * master checkout * master checkout * added validation to prevent all field types being hidden * make variable constant * added tests for validation * added tests for validation * removed comments and annotations * removed unnecessary imports * refactor and error message update * added a positive test for allowing schema with at least one field as not hidden * revert unwanted file change
1 parent 8a5e1b4 commit 1030fc8

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

lib/src/main/java/graphql/nadel/validation/NadelFieldValidation.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package graphql.nadel.validation
22

33
import graphql.nadel.engine.util.unwrapAll
4+
import graphql.nadel.schema.NadelDirectives
45
import graphql.nadel.validation.NadelSchemaValidationError.IncompatibleArgumentInputType
56
import graphql.nadel.validation.NadelSchemaValidationError.IncompatibleFieldOutputType
67
import graphql.nadel.validation.NadelSchemaValidationError.MissingArgumentOnUnderlying
@@ -36,6 +37,9 @@ class NadelFieldValidation internal constructor(
3637
parent: NadelServiceSchemaElement.FieldsContainer,
3738
overallFields: List<GraphQLFieldDefinition>,
3839
): NadelSchemaValidationResult {
40+
if(areAllFieldsHidden(overallFields)) {
41+
return NadelSchemaValidationError.AllFieldsUsingHiddenDirective(parent)
42+
}
3943
return overallFields
4044
.asSequence()
4145
.let { fieldSequence ->
@@ -53,6 +57,10 @@ class NadelFieldValidation internal constructor(
5357
.toResult()
5458
}
5559

60+
private fun areAllFieldsHidden(overallFields: List<GraphQLFieldDefinition>): Boolean {
61+
return overallFields.all { it.hasAppliedDirective(NadelDirectives.hiddenDirectiveDefinition.name) };
62+
}
63+
5664
context(NadelValidationContext)
5765
fun validate(
5866
parent: NadelServiceSchemaElement.FieldsContainer,

lib/src/main/java/graphql/nadel/validation/NadelSchemaValidationError.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,19 @@ sealed interface NadelSchemaValidationError : NadelSchemaValidationResult {
409409

410410
override val subject = overallField
411411
}
412+
413+
data class AllFieldsUsingHiddenDirective(
414+
val parentType: NadelServiceSchemaElement,
415+
) : NadelSchemaValidationError {
416+
val service: Service get() = parentType.service
417+
418+
override val message = run {
419+
val ot = parentType.overall.name
420+
"Must have at least one field without @hidden on type $ot"
421+
}
422+
423+
override val subject = parentType.overall
424+
}
412425
}
413426

414427
private fun toString(element: GraphQLNamedSchemaElement): String {

lib/src/test/kotlin/graphql/nadel/validation/NadelFieldValidationTest.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,5 +676,55 @@ class NadelFieldValidationTest : DescribeSpec({
676676

677677
assert(error.message == "The overall field Query.fieldA defines argument id which does not exist in service test field Query.fieldA")
678678
}
679+
680+
it("fails if schema contains all fields as hidden") {
681+
val fixture = NadelValidationTestFixture(
682+
overallSchema = mapOf(
683+
"test" to """
684+
type Query {
685+
echo: String @hidden
686+
}
687+
""".trimIndent(),
688+
),
689+
underlyingSchema = mapOf(
690+
"test" to """
691+
type Query {
692+
echo: String
693+
}
694+
""".trimIndent(),
695+
),
696+
)
697+
698+
val errors = validate(fixture)
699+
assert(errors.map { it.message }.isNotEmpty())
700+
701+
val error = errors.assertSingleOfType<NadelSchemaValidationError>()
702+
assert(error.message == "Must have at least one field without @hidden on type Query")
703+
}
704+
705+
it("passes if schema contains at least one field as not hidden") {
706+
val fixture = NadelValidationTestFixture(
707+
overallSchema = mapOf(
708+
"test" to """
709+
type Query {
710+
echo: String @hidden
711+
hello: String
712+
713+
}
714+
""".trimIndent(),
715+
),
716+
underlyingSchema = mapOf(
717+
"test" to """
718+
type Query {
719+
echo: String
720+
hello: String
721+
}
722+
""".trimIndent(),
723+
),
724+
)
725+
726+
val errors = validate(fixture)
727+
assert(errors.isEmpty())
728+
}
679729
}
680730
})

0 commit comments

Comments
 (0)