Skip to content

Commit

Permalink
Replace context overloads with archunit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawf committed Nov 26, 2024
1 parent a71dee9 commit eb7bc7d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,17 @@ 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

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<NadelHydrationDefinition> {
return getAppliedDirectives(Keyword.hydrated)
.map(::NadelHydrationDefinitionImpl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -62,25 +56,13 @@ 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

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JavaMethodCall>("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)
}
}

0 comments on commit eb7bc7d

Please sign in to comment.