-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treat some GraphQL Java types as pseudo sealed types
- Loading branch information
Showing
8 changed files
with
291 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
lib/src/main/java/graphql/nadel/engine/util/NadelPseudoSealedType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Util functions to treat some GraphQL Java classes _like_ sealed types. | ||
* | ||
* There are tests in `NadelPseudoSealedTypeKtTest` to ensure these assumptions are correct. | ||
*/ | ||
package graphql.nadel.engine.util | ||
|
||
import graphql.schema.GraphQLEnumType | ||
import graphql.schema.GraphQLFieldsContainer | ||
import graphql.schema.GraphQLInputObjectType | ||
import graphql.schema.GraphQLInputType | ||
import graphql.schema.GraphQLInterfaceType | ||
import graphql.schema.GraphQLNamedType | ||
import graphql.schema.GraphQLObjectType | ||
import graphql.schema.GraphQLOutputType | ||
import graphql.schema.GraphQLScalarType | ||
import graphql.schema.GraphQLUnionType | ||
|
||
internal inline fun <T> GraphQLFieldsContainer.whenType( | ||
interfaceType: (GraphQLInterfaceType) -> T, | ||
objectType: (GraphQLObjectType) -> T, | ||
): T { | ||
return when (this) { | ||
is GraphQLInterfaceType -> interfaceType(this) | ||
is GraphQLObjectType -> objectType(this) | ||
else -> throw IllegalStateException("Should never happen") | ||
} | ||
} | ||
|
||
internal inline fun <T> GraphQLNamedType.whenType( | ||
enumType: (GraphQLEnumType) -> T, | ||
inputObjectType: (GraphQLInputObjectType) -> T, | ||
interfaceType: (GraphQLInterfaceType) -> T, | ||
objectType: (GraphQLObjectType) -> T, | ||
scalarType: (GraphQLScalarType) -> T, | ||
unionType: (GraphQLUnionType) -> T, | ||
): T { | ||
return when (this) { | ||
is GraphQLEnumType -> enumType(this) | ||
is GraphQLInputObjectType -> inputObjectType(this) | ||
is GraphQLInterfaceType -> interfaceType(this) | ||
is GraphQLObjectType -> objectType(this) | ||
is GraphQLScalarType -> scalarType(this) | ||
is GraphQLUnionType -> unionType(this) | ||
else -> throw IllegalStateException("Should never happen") | ||
} | ||
} | ||
|
||
internal inline fun <T> GraphQLOutputType.whenUnmodifiedType( | ||
enumType: (GraphQLEnumType) -> T, | ||
interfaceType: (GraphQLInterfaceType) -> T, | ||
objectType: (GraphQLObjectType) -> T, | ||
scalarType: (GraphQLScalarType) -> T, | ||
unionType: (GraphQLUnionType) -> T, | ||
): T { | ||
return when (val unmodifiedType = this.unwrapAll()) { | ||
is GraphQLEnumType -> enumType(unmodifiedType) | ||
is GraphQLInterfaceType -> interfaceType(unmodifiedType) | ||
is GraphQLObjectType -> objectType(unmodifiedType) | ||
is GraphQLScalarType -> scalarType(unmodifiedType) | ||
is GraphQLUnionType -> unionType(unmodifiedType) | ||
else -> throw IllegalStateException("Should never happen") | ||
} | ||
} | ||
|
||
internal inline fun <T> GraphQLInputType.whenUnmodifiedType( | ||
enumType: (GraphQLEnumType) -> T, | ||
inputObjectType: (GraphQLInputObjectType) -> T, | ||
scalarType: (GraphQLScalarType) -> T, | ||
): T { | ||
return when (val unmodifiedType = this.unwrapAll()) { | ||
is GraphQLEnumType -> enumType(unmodifiedType) | ||
is GraphQLInputObjectType -> inputObjectType(unmodifiedType) | ||
is GraphQLScalarType -> scalarType(unmodifiedType) | ||
else -> throw IllegalStateException("Should never happen") | ||
} | ||
} | ||
|
59 changes: 59 additions & 0 deletions
59
lib/src/test/kotlin/graphql/nadel/archunit/ArchUnitEqualsClassesExactly.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package graphql.nadel.archunit | ||
|
||
import com.tngtech.archunit.core.domain.JavaClass | ||
import com.tngtech.archunit.lang.ArchCondition | ||
import com.tngtech.archunit.lang.ConditionEvents | ||
import com.tngtech.archunit.lang.SimpleConditionEvent | ||
import kotlin.reflect.KClass | ||
|
||
class ArchUnitEqualsClassesExactly<T : Any>( | ||
private val requiredClasses: List<Class<out T>>, | ||
) : ArchCondition<JavaClass>("equal exactly the given classes") { | ||
constructor(vararg requiredClasses: Class<out T>) : this(requiredClasses.toList()) | ||
|
||
constructor(vararg requiredClasses: KClass<out T>) : this(requiredClasses.map { it.java }) | ||
|
||
private val actualClasses: MutableList<JavaClass> = mutableListOf() | ||
|
||
override fun check(item: JavaClass, events: ConditionEvents) { | ||
actualClasses.add(item) | ||
} | ||
|
||
override fun finish(events: ConditionEvents) { | ||
val requiredClassNames = requiredClasses.mapTo(LinkedHashSet()) { it.name } | ||
val actualClassNames = actualClasses.mapTo(LinkedHashSet()) { it.fullName } | ||
|
||
val isValid = requiredClassNames == actualClassNames | ||
if (isValid) { | ||
events.add( | ||
SimpleConditionEvent( | ||
requiredClassNames, | ||
true, | ||
"Classes match $requiredClassNames", | ||
), | ||
) | ||
} else { | ||
val extraClassNames = actualClassNames - requiredClassNames | ||
val missingClassNames = requiredClassNames - actualClassNames | ||
|
||
if (extraClassNames.isNotEmpty()) { | ||
events.add( | ||
SimpleConditionEvent( | ||
requiredClassNames, | ||
false, | ||
"Found extra classes $extraClassNames", | ||
), | ||
) | ||
} | ||
if (missingClassNames.isNotEmpty()) { | ||
events.add( | ||
SimpleConditionEvent( | ||
requiredClassNames, | ||
false, | ||
"Missing classes $missingClassNames" | ||
), | ||
) | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ql/nadel/ForbiddenGraphQLJavaUsageTest.kt → ...archunit/ForbiddenGraphQLJavaUsageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...t/kotlin/graphql/nadel/NadelPrefixTest.kt → ...graphql/nadel/archunit/NadelPrefixTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.