Skip to content

Commit 388e340

Browse files
committed
Fix breaking changes after update
1 parent fdf3345 commit 388e340

File tree

7 files changed

+81
-50
lines changed

7 files changed

+81
-50
lines changed

src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class SchemaParser internal constructor(
319319
.build()
320320

321321

322-
output.add(schemaGeneratorHelper.buildDirective(directive, graphQLDirective, directiveLocation, runtimeWiring.comparatorRegistry))
322+
output.add(schemaGeneratorHelper.buildAppliedDirective(directive, graphQLDirective, directiveLocation, runtimeWiring.comparatorRegistry))
323323
}
324324
}
325325

src/main/kotlin/graphql/kickstart/tools/directive/SchemaGeneratorDirectiveHelper.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@
4343
@Internal
4444
public class SchemaGeneratorDirectiveHelper {
4545

46+
/**
47+
* This will return true if something in the RuntimeWiring requires a {@link SchemaDirectiveWiring}. This is to allow
48+
* a shortcut to decide that that we dont need ANY SchemaDirectiveWiring post processing
49+
*
50+
* @param directiveContainer the element that has directives
51+
* @param typeRegistry the type registry
52+
* @param runtimeWiring the runtime wiring
53+
* @param <T> for two
54+
*
55+
* @return true if something in the RuntimeWiring requires a {@link SchemaDirectiveWiring}
56+
*/
57+
public static <T extends GraphQLDirectiveContainer> boolean schemaDirectiveWiringIsRequired(
58+
T directiveContainer,
59+
TypeDefinitionRegistry typeRegistry,
60+
RuntimeWiring runtimeWiring
61+
) {
62+
63+
WiringFactory wiringFactory = runtimeWiring.getWiringFactory();
64+
65+
Map<String, SchemaDirectiveWiring> registeredWiring = runtimeWiring.getRegisteredDirectiveWiring();
66+
List<SchemaDirectiveWiring> otherWiring = runtimeWiring.getDirectiveWiring();
67+
boolean thereAreSome = !registeredWiring.isEmpty() || !otherWiring.isEmpty();
68+
if (thereAreSome) {
69+
return true;
70+
}
71+
72+
SchemaGeneratorDirectiveHelper.Parameters params = new SchemaGeneratorDirectiveHelper.Parameters(typeRegistry, runtimeWiring, new HashMap<>(), null);
73+
SchemaDirectiveWiringEnvironment<T> env = new SchemaDirectiveWiringEnvironmentImpl<>(directiveContainer, directiveContainer.getDirectives(), null, params);
74+
// do they dynamically provide a wiring for this element?
75+
return wiringFactory.providesSchemaDirectiveWiring(env);
76+
}
77+
4678
public static class Parameters {
4779
private final TypeDefinitionRegistry typeRegistry;
4880
private final RuntimeWiring runtimeWiring;

src/main/kotlin/graphql/schema/idl/SchemaGeneratorHelperExt.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import graphql.introspection.Introspection
44
import graphql.language.Directive
55
import graphql.schema.GraphQLDirective
66
import graphql.schema.GraphqlTypeComparatorRegistry
7+
import graphql.schema.idl.RuntimeWiring.MOCKED_WIRING
8+
import graphql.schema.idl.SchemaGenerator.Options.defaultOptions
79

810
class SchemaGeneratorHelperExt : SchemaGeneratorHelper() {
911
// Re-expose a package protected method as public
10-
fun buildDirective(directive: Directive,
11-
graphQLDirective: GraphQLDirective,
12-
directiveLocation: Introspection.DirectiveLocation,
13-
comparatorRegistry: GraphqlTypeComparatorRegistry): GraphQLDirective {
14-
// Note 1: for now, it seems safe to pass buildCtx = null, since the code path where buildCtx is used,
15-
// is never called (directive.name == graphQLDirective.name is always true, see line with
16-
// directiveDefOpt = FpKit.findOne ...)
17-
// Note 2: repeatable directives (new feature in graphql-java 16) likely don't work,
12+
fun buildAppliedDirective(directive: Directive,
13+
graphQLDirective: GraphQLDirective,
14+
directiveLocation: Introspection.DirectiveLocation,
15+
comparatorRegistry: GraphqlTypeComparatorRegistry): GraphQLDirective {
16+
// Note: repeatable directives (new feature in graphql-java 16) likely don't work,
1817
// since we don't pass in the full set of discovered directives
19-
return super.buildDirective(null, directive, setOf(graphQLDirective), directiveLocation, comparatorRegistry)
18+
val context = BuildContext(TypeDefinitionRegistry(), MOCKED_WIRING, emptyMap(), defaultOptions())
19+
return super.buildAppliedDirective(context, directive, setOf(graphQLDirective), directiveLocation, comparatorRegistry)
2020
}
21-
}
21+
}

src/test/kotlin/graphql/kickstart/tools/EndToEndSpecHelper.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ val customScalarId = GraphQLScalarType.newScalar()
420420
else -> null
421421
}
422422

423-
override fun parseValue(input: Any): UUID? = parseLiteral(input)
423+
override fun parseValue(input: Any): UUID = parseLiteral(input)
424424

425-
override fun parseLiteral(input: Any): UUID? = when (input) {
425+
override fun parseLiteral(input: Any): UUID = when (input) {
426426
is StringValue -> UUID.fromString(input.value)
427-
else -> null
427+
else -> throw CoercingParseLiteralException()
428428
}
429429
})
430430
.build()
@@ -440,11 +440,11 @@ val customScalarUUID = GraphQLScalarType.newScalar()
440440
else -> null
441441
}
442442

443-
override fun parseValue(input: Any): UUID? = parseLiteral(input)
443+
override fun parseValue(input: Any): UUID = parseLiteral(input)
444444

445-
override fun parseLiteral(input: Any): UUID? = when (input) {
445+
override fun parseLiteral(input: Any): UUID = when (input) {
446446
is StringValue -> UUID.fromString(input.value)
447-
else -> null
447+
else -> throw CoercingParseLiteralException()
448448
}
449449
})
450450
.build()
@@ -455,12 +455,12 @@ val customScalarMap = GraphQLScalarType.newScalar()
455455
.coercing(object : Coercing<Map<String, Any>, Map<String, Any>> {
456456

457457
@Suppress("UNCHECKED_CAST")
458-
override fun parseValue(input: Any?): Map<String, Any> = input as Map<String, Any>
458+
override fun parseValue(input: Any): Map<String, Any> = input as Map<String, Any>
459459

460460
@Suppress("UNCHECKED_CAST")
461-
override fun serialize(dataFetcherResult: Any?): Map<String, Any> = dataFetcherResult as Map<String, Any>
461+
override fun serialize(dataFetcherResult: Any): Map<String, Any> = dataFetcherResult as Map<String, Any>
462462

463-
override fun parseLiteral(input: Any?): Map<String, Any> = (input as ObjectValue).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue).value }
463+
override fun parseLiteral(input: Any): Map<String, Any> = (input as ObjectValue).objectFields.associateBy { it.name }.mapValues { (it.value.value as StringValue).value }
464464
})
465465
.build()
466466

@@ -473,21 +473,18 @@ val uploadScalar: GraphQLScalarType = GraphQLScalarType.newScalar()
473473
throw CoercingSerializeException("Upload is an input-only type")
474474
}
475475

476-
override fun parseValue(input: Any?): Part? {
476+
override fun parseValue(input: Any): Part {
477477
return when (input) {
478478
is Part -> {
479479
input
480480
}
481-
null -> {
482-
null
483-
}
484481
else -> {
485482
throw CoercingParseValueException("Expected type ${Part::class.java.name} but was ${input.javaClass.name}")
486483
}
487484
}
488485
}
489486

490-
override fun parseLiteral(input: Any): Part? {
487+
override fun parseLiteral(input: Any): Part {
491488
throw CoercingParseLiteralException(
492489
"Must use variables to specify Upload values")
493490
}

src/test/kotlin/graphql/kickstart/tools/MethodFieldResolverTest.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import graphql.ExecutionInput
44
import graphql.GraphQL
55
import graphql.language.StringValue
66
import graphql.schema.Coercing
7+
import graphql.schema.CoercingParseLiteralException
8+
import graphql.schema.CoercingSerializeException
79
import graphql.schema.GraphQLScalarType
810
import org.junit.Test
911
import java.lang.reflect.InvocationHandler
@@ -215,9 +217,9 @@ class MethodFieldResolverTest {
215217
val value get() = internalValue
216218

217219
companion object {
218-
fun of(input: Any?) = when (input) {
220+
fun of(input: Any) = when (input) {
219221
is String -> CustomScalar(input)
220-
else -> null
222+
else -> throw IllegalArgumentException()
221223
}
222224
}
223225
}
@@ -231,16 +233,16 @@ class MethodFieldResolverTest {
231233
.description("customScalar")
232234
.coercing(object : Coercing<CustomScalar, String> {
233235

234-
override fun parseValue(input: Any?) = CustomScalar.of(input)
236+
override fun parseValue(input: Any) = CustomScalar.of(input)
235237

236-
override fun parseLiteral(input: Any?) = when (input) {
238+
override fun parseLiteral(input: Any): CustomScalar = when (input) {
237239
is StringValue -> CustomScalar.of(input.value)
238-
else -> null
240+
else -> throw CoercingParseLiteralException()
239241
}
240242

241-
override fun serialize(dataFetcherResult: Any?) = when (dataFetcherResult) {
243+
override fun serialize(dataFetcherResult: Any) = when (dataFetcherResult) {
242244
is CustomScalar -> dataFetcherResult.value
243-
else -> null
245+
else -> throw CoercingSerializeException()
244246
}
245247
})
246248
.build()

src/test/kotlin/graphql/kickstart/tools/SchemaClassScannerTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ class SchemaClassScannerTest {
185185
.name("UUID")
186186
.description("Test scalars with duplicate types")
187187
.coercing(object : Coercing<Any, Any> {
188-
override fun serialize(dataFetcherResult: Any?): Any? = null
189-
override fun parseValue(input: Any?): Any? = null
190-
override fun parseLiteral(input: Any?): Any? = null
188+
override fun serialize(dataFetcherResult: Any): Any? = null
189+
override fun parseValue(input: Any): Any = input
190+
override fun parseLiteral(input: Any): Any = input
191191
}).build())
192192
.schemaString(
193193
"""
@@ -306,9 +306,9 @@ class SchemaClassScannerTest {
306306
val customMap = GraphQLScalarType.newScalar()
307307
.name("customMap")
308308
.coercing(object : Coercing<Map<String, Any>, Map<String, Any>> {
309-
override fun serialize(dataFetcherResult: Any?): Map<String, Any> = mapOf()
310-
override fun parseValue(input: Any?): Map<String, Any> = mapOf()
311-
override fun parseLiteral(input: Any?): Map<String, Any> = mapOf()
309+
override fun serialize(dataFetcherResult: Any): Map<String, Any> = mapOf()
310+
override fun parseValue(input: Any): Map<String, Any> = mapOf()
311+
override fun parseLiteral(input: Any): Map<String, Any> = mapOf()
312312
}).build()
313313

314314
val schema = SchemaParser.newParser()

src/test/resources/Place.graphqls

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ interface OtherPlace {
1818
reviews: [Review!]
1919
}
2020

21-
type Place1 implements Entity, Place, OtherPlace {
22-
id: ID!
23-
name: String
24-
other: String
25-
reviews: [Review1!]
26-
}
27-
28-
type Place2 implements Entity, Place, OtherPlace {
29-
id: ID!
30-
name: String
31-
other: String
32-
reviews: [Review2!]
21+
type Place1 implements Entity & Place & OtherPlace {
22+
id: ID!
23+
name: String
24+
other: String
25+
reviews: [Review1!]
26+
}
27+
28+
type Place2 implements Entity & Place & OtherPlace {
29+
id: ID!
30+
name: String
31+
other: String
32+
reviews: [Review2!]
3333
}
3434

3535
interface Review {

0 commit comments

Comments
 (0)