File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
main/kotlin/com/netflix/graphql/dgs/internal/method
test/kotlin/com/netflix/graphql/dgs/internal Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff 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 (
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments