Skip to content

Commit 9769900

Browse files
committed
fix: remove required parameter from directives and implement a few unit tests for AbstractDirective
1 parent 02005e8 commit 9769900

File tree

9 files changed

+100
-23
lines changed

9 files changed

+100
-23
lines changed

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/AbstractDirective.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import graphql.schema.GraphQLType
99

1010
abstract class AbstractDirective(
1111
override val name: String,
12-
override val required: Boolean
1312
) : Directive {
1413
/**
1514
* Will check if the given [directive] will match against the reference ([reference]).

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/Directive.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ interface Directive {
1313
*/
1414
val name: String
1515

16-
/**
17-
* If the directive MUST be present on the schema.
18-
*/
19-
val required: Boolean
20-
2116
/**
2217
* The reference [GraphQLDirective], which is represented here.
2318
*/

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/DirectiveFacade.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,12 @@ object DirectiveFacade {
3333

3434
/**
3535
* Will check if the given [directive] exists on the given [schema] (using [Directive.name]).
36-
* Additional it will return the [GraphQLDirective] if it available on the schema
36+
* Additional it will return the [GraphQLDirective] if it's available on the schema
3737
*
3838
* @throws DirectiveValidationException If the directive is required but is not present on the [schema].
3939
*/
4040
private fun getDirectiveDefinition(directive: Directive, schema: GraphQLSchema): GraphQLDirective? {
41-
val schemaDirective = schema.getDirective(directive.name)
42-
43-
// Throw exception if the schema is required but not given on the schema.
44-
if (schemaDirective == null && directive.required)
45-
throw DirectiveValidationException(
46-
directive,
47-
"Not present on the schema, but is required. Consider adding it."
48-
)
49-
50-
return schemaDirective
41+
return schema.getDirective(directive.name)
5142
}
5243

5344
/**

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/implementation/DoubleNullDirective.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.auritylab.graphql.kotlin.toolkit.common.directive.AbstractDirective
44
import graphql.introspection.Introspection
55
import graphql.schema.GraphQLDirective
66

7-
object DoubleNullDirective : AbstractDirective("kDoubleNull", false) {
7+
object DoubleNullDirective : AbstractDirective("kDoubleNull") {
88
override val reference: GraphQLDirective =
99
GraphQLDirective.newDirective()
1010
.name(name)

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/implementation/GenerateDirective.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.auritylab.graphql.kotlin.toolkit.common.directive.AbstractDirective
44
import graphql.introspection.Introspection
55
import graphql.schema.GraphQLDirective
66

7-
object GenerateDirective : AbstractDirective("kGenerate", false) {
7+
object GenerateDirective : AbstractDirective("kGenerate") {
88
override val reference: GraphQLDirective =
99
GraphQLDirective.newDirective()
1010
.name(name)

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/implementation/PaginationDirective.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.auritylab.graphql.kotlin.toolkit.common.directive.AbstractDirective
44
import graphql.introspection.Introspection
55
import graphql.schema.GraphQLDirective
66

7-
object PaginationDirective : AbstractDirective("kPagination", false) {
7+
object PaginationDirective : AbstractDirective("kPagination") {
88
override val reference: GraphQLDirective =
99
GraphQLDirective.newDirective()
1010
.name(name)

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/implementation/RepresentationDirective.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import graphql.schema.GraphQLList
1010
import graphql.schema.GraphQLNonNull
1111

1212
object RepresentationDirective :
13-
AbstractDirective("kRepresentation", false),
13+
AbstractDirective("kRepresentation"),
1414
HasArgumentsDirective<RepresentationDirective.Model> {
1515

1616
override val reference: GraphQLDirective =

graphql-kotlin-toolkit-common/src/main/kotlin/com/auritylab/graphql/kotlin/toolkit/common/directive/implementation/ResolverDirective.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.auritylab.graphql.kotlin.toolkit.common.directive.AbstractDirective
44
import graphql.introspection.Introspection
55
import graphql.schema.GraphQLDirective
66

7-
object ResolverDirective : AbstractDirective("kResolver", false) {
7+
object ResolverDirective : AbstractDirective("kResolver") {
88
override val reference: GraphQLDirective =
99
GraphQLDirective.newDirective()
1010
.name(name)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,99 @@
11
package com.auritylab.graphql.kotlin.toolkit.common.directive
22

3-
import org.junit.jupiter.api.Assertions.*
3+
import com.auritylab.graphql.kotlin.toolkit.common.directive.exception.DirectiveValidationException
4+
import graphql.Scalars
5+
import graphql.introspection.Introspection
6+
import graphql.schema.GraphQLArgument
7+
import graphql.schema.GraphQLDirective
8+
import org.junit.jupiter.api.Nested
9+
import org.junit.jupiter.api.Test
10+
import org.junit.jupiter.api.assertDoesNotThrow
11+
import org.junit.jupiter.api.assertThrows
412

513
internal class AbstractDirectiveTest {
14+
@Nested
15+
inner class ValidateDefinition {
16+
@Test
17+
fun shouldThrowExceptionOnMissingArgument() {
18+
assertThrows<DirectiveValidationException> {
19+
// _directiveOne defines exactly one argument which is expected to be not available on _directiveEmpty,
20+
// because it does not define any argument.
21+
toInternal(_directiveOne).validateDefinition(_directiveEmpty)
22+
}
23+
}
624

25+
@Test
26+
fun shouldIgnoreTooManyArguments() {
27+
assertDoesNotThrow {
28+
// As a reference we have a directive with no arguments. We validate against a directive which defines
29+
// exactly one argument. Because additional directives do not matter here, we just assert that it does
30+
// not throw.
31+
toInternal(_directiveEmpty).validateDefinition(_directiveOne)
32+
}
33+
}
34+
35+
@Test
36+
fun shouldThrowExceptionOnMissingLocation() {
37+
assertThrows<DirectiveValidationException> {
38+
// _directiveTwo defines one valid location which is expected to be not available on _directiveEmpty.
39+
// Therefore, we assert that an exception will be thrown.
40+
toInternal(_directiveTwo).validateDefinition(_directiveEmpty)
41+
}
42+
}
43+
44+
@Test
45+
fun shouldIgnoreTooManyLocations() {
46+
assertDoesNotThrow {
47+
// As a reference we have a directive with no valid locations. We validate against a directive which
48+
// defines exactly one valid location. Because additional locations do not matter here, we assert that
49+
// it does not throw any exception.
50+
toInternal(_directiveEmpty).validateDefinition(_directiveTwo)
51+
}
52+
}
53+
54+
@Test
55+
fun shouldThrowExceptionOnDifferingArgumentTypes () {
56+
assertThrows<DirectiveValidationException> {
57+
// As a reference we have a directive with exactly one argument named 'name' of type 'String'. We try
58+
// to validate against a directive which also defines exactly one argument named 'name' but with a
59+
// differing type 'boolean'. Because those types differ we assert that an exception will be thrown.
60+
toInternal(_directiveOne).validateDefinition(_directiveThree)
61+
}
62+
}
63+
}
64+
65+
private val _directiveEmpty = GraphQLDirective.newDirective()
66+
.name("DirectiveEmpty").build()
67+
68+
private val _directiveOne = GraphQLDirective.newDirective()
69+
.name("Directive")
70+
.argument(
71+
GraphQLArgument.newArgument()
72+
.name("name")
73+
.type(Scalars.GraphQLString)
74+
).build()
75+
76+
private val _directiveTwo = GraphQLDirective.newDirective()
77+
.name("DirectiveTwo")
78+
.validLocations(Introspection.DirectiveLocation.FIELD)
79+
.build()
80+
81+
private val _directiveThree = GraphQLDirective.newDirective()
82+
.name("DirectiveThree")
83+
.argument(GraphQLArgument.newArgument().name("name").type(Scalars.GraphQLBoolean))
84+
.build()
85+
86+
/**
87+
* Creates a new [AbstractDirective] based on the given input parameters. The [input] will be used
88+
* as [AbstractDirective.reference]. All other parameters are optional and define default values.
89+
*/
90+
private fun toInternal(
91+
input: GraphQLDirective,
92+
name: String = "Test",
93+
required: Boolean = false
94+
): AbstractDirective {
95+
return object : AbstractDirective(name) {
96+
override val reference: GraphQLDirective = input
97+
}
98+
}
799
}

0 commit comments

Comments
 (0)