From e9ea7927c6bd18a369565fa4ffef4f0b98422104 Mon Sep 17 00:00:00 2001 From: Franklin Wang Date: Wed, 27 Nov 2024 12:38:42 +1300 Subject: [PATCH] Replace context overloads with archunit test --- .../hydration/NadelHydrationDefinition.kt | 13 ------ .../renamed/NadelRenamedDefinition.kt | 18 -------- .../NadelValidationDefinitionsTest.kt | 44 +++++++++++++++++++ 3 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 lib/src/test/kotlin/graphql/nadel/validation/NadelValidationDefinitionsTest.kt diff --git a/lib/src/main/java/graphql/nadel/definition/hydration/NadelHydrationDefinition.kt b/lib/src/main/java/graphql/nadel/definition/hydration/NadelHydrationDefinition.kt index db186a6be..c223a8545 100644 --- a/lib/src/main/java/graphql/nadel/definition/hydration/NadelHydrationDefinition.kt +++ b/lib/src/main/java/graphql/nadel/definition/hydration/NadelHydrationDefinition.kt @@ -8,7 +8,6 @@ import graphql.nadel.definition.NadelDefinition import graphql.nadel.definition.hydration.NadelHydrationDefinition.Keyword import graphql.nadel.engine.util.JsonMap import graphql.nadel.engine.util.parseDefinition -import graphql.nadel.validation.NadelValidationContext import graphql.schema.GraphQLAppliedDirective import graphql.schema.GraphQLFieldDefinition @@ -16,22 +15,10 @@ fun FieldDefinition.hasHydratedDefinition(): Boolean { return hasDirective(Keyword.hydrated) } -context(NadelValidationContext) -@Deprecated("Mistake to use this inside validation", level = DeprecationLevel.ERROR) -fun GraphQLFieldDefinition.hasHydratedDefinition(): Nothing { - throw UnsupportedOperationException() -} - fun GraphQLFieldDefinition.hasHydratedDefinition(): Boolean { return hasAppliedDirective(Keyword.hydrated) } -context(NadelValidationContext) -@Deprecated("Mistake to use this inside validation", level = DeprecationLevel.ERROR) -fun GraphQLFieldDefinition.parseHydrationDefinitions(): Nothing { - throw UnsupportedOperationException() -} - fun GraphQLFieldDefinition.parseHydrationDefinitions(): List { return getAppliedDirectives(Keyword.hydrated) .map(::NadelHydrationDefinitionImpl) diff --git a/lib/src/main/java/graphql/nadel/definition/renamed/NadelRenamedDefinition.kt b/lib/src/main/java/graphql/nadel/definition/renamed/NadelRenamedDefinition.kt index c7a0478d0..058ebd5a8 100644 --- a/lib/src/main/java/graphql/nadel/definition/renamed/NadelRenamedDefinition.kt +++ b/lib/src/main/java/graphql/nadel/definition/renamed/NadelRenamedDefinition.kt @@ -48,12 +48,6 @@ sealed class NadelRenamedDefinition : NadelDefinition { } } -context(NadelValidationContext) -@Deprecated("Mistake to use this inside validation", level = DeprecationLevel.ERROR) -fun GraphQLDirectiveContainer.hasRenameDefinition(): Nothing { - throw UnsupportedOperationException() -} - fun GraphQLDirectiveContainer.hasRenameDefinition(): Boolean { return hasAppliedDirective(Keyword.renamed) } @@ -62,12 +56,6 @@ fun DirectivesContainer<*>.hasRenameDefinition(): Boolean { return hasDirective(Keyword.renamed) } -context(NadelValidationContext) -@Deprecated("Mistake to use this inside validation", level = DeprecationLevel.ERROR) -fun GraphQLFieldDefinition.parseRenamedOrNull(): Nothing { - throw UnsupportedOperationException() -} - fun GraphQLFieldDefinition.parseRenamedOrNull(): NadelRenamedDefinition.Field? { val directive = getAppliedDirective(Keyword.renamed) ?: return null @@ -75,12 +63,6 @@ fun GraphQLFieldDefinition.parseRenamedOrNull(): NadelRenamedDefinition.Field? { return NadelRenamedDefinition.Field(directive) } -context(NadelValidationContext) -@Deprecated("Mistake to use this inside validation", level = DeprecationLevel.ERROR) -fun GraphQLNamedType.parseRenamedOrNull(): Nothing { - throw UnsupportedOperationException() -} - fun GraphQLNamedType.parseRenamedOrNull(): NadelRenamedDefinition.Type? { val directive = (this as GraphQLDirectiveContainer).getAppliedDirective(Keyword.renamed) ?: return null diff --git a/lib/src/test/kotlin/graphql/nadel/validation/NadelValidationDefinitionsTest.kt b/lib/src/test/kotlin/graphql/nadel/validation/NadelValidationDefinitionsTest.kt new file mode 100644 index 000000000..210565c25 --- /dev/null +++ b/lib/src/test/kotlin/graphql/nadel/validation/NadelValidationDefinitionsTest.kt @@ -0,0 +1,44 @@ +package graphql.nadel.validation + +import com.tngtech.archunit.base.DescribedPredicate +import com.tngtech.archunit.base.DescribedPredicate.not +import com.tngtech.archunit.core.domain.JavaClass +import com.tngtech.archunit.core.domain.JavaMethodCall +import com.tngtech.archunit.core.importer.ClassFileImporter +import com.tngtech.archunit.core.importer.ImportOption +import com.tngtech.archunit.lang.conditions.ArchConditions.callMethodWhere +import com.tngtech.archunit.lang.conditions.ArchConditions.not +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes +import kotlin.test.Test + +class NadelValidationDefinitionsTest { + @Test + fun `do not use parse definitions functions in validation`() { + val importedClasses = ClassFileImporter() + .withImportOption(ImportOption.DoNotIncludeTests()) + .importPackages("graphql.nadel.validation") + + val rule = classes() + .that( + not( + JavaClass.Predicates.belongTo( + JavaClass.Predicates.simpleNameEndingWith("DefinitionParser") + ), + ), + ) + .should( + not( + callMethodWhere( + object : DescribedPredicate("definition parse methods") { + override fun test(invocation: JavaMethodCall): Boolean { + return invocation.targetOwner.getPackage().name.startsWith("graphql.nadel.definition") + && invocation.target.name.startsWith("parse") + } + } + ), + ), + ) + + rule.check(importedClasses) + } +}