Skip to content

Commit a63ec7d

Browse files
committed
Explicitly handle null values for @source for better error message
1 parent bc6e6ab commit a63ec7d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/method/SourceArgumentResolver.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ class SourceArgumentResolver : ArgumentResolver {
2828
dfe: DataFetchingEnvironment,
2929
): Any {
3030
val source = dfe.getSource<Any>()
31-
if (source != null && parameter.parameterType == source.javaClass) {
31+
if (source == null) {
32+
throw IllegalArgumentException("Source is null. Are you trying to use @Source on a root field (e.g. @DgsQuery)?")
33+
}
34+
35+
if (parameter.parameterType == source.javaClass) {
3236
return source
3337
} else {
3438
throw IllegalArgumentException(

graphql-dgs/src/test/kotlin/com/netflix/graphql/dgs/internal/SourceArgumentTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,36 @@ internal class SourceArgumentTest {
131131
).contains("Invalid source type 'com.netflix.graphql.dgs.internal.SourceArgumentTest\$Show'. Expected type 'java.lang.String'")
132132
}
133133
}
134+
135+
@Test
136+
fun `Using @Source on a root datafetcher should fail`() {
137+
@DgsComponent
138+
class Fetcher {
139+
@DgsQuery
140+
fun shows(
141+
@Source something: String,
142+
): List<Show> = listOf(Show("Stranger Things"))
143+
}
144+
145+
contextRunner.withBean(Fetcher::class.java).run { context ->
146+
val provider = schemaProvider(context)
147+
val schema = provider.schema().graphQLSchema
148+
149+
val build = GraphQL.newGraphQL(schema).build()
150+
val executionResult =
151+
build.execute(
152+
"""{
153+
| shows {
154+
| title
155+
| }
156+
|}
157+
""".trimMargin(),
158+
)
159+
160+
assertThat(executionResult.errors).isNotEmpty()
161+
assertThat(
162+
executionResult.errors[0].message,
163+
).contains("Source is null. Are you trying to use @Source on a root field (e.g. @DgsQuery)?")
164+
}
165+
}
134166
}

0 commit comments

Comments
 (0)