Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SourceArgumentResolver : ArgumentResolver {
throw IllegalArgumentException("Source is null. Are you trying to use @Source on a root field (e.g. @DgsQuery)?")
}

if (parameter.parameterType == source.javaClass) {
if (parameter.parameterType.isAssignableFrom(source.javaClass)) {
return source
} else {
throw IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ internal class SourceArgumentTest {
),
)

interface Entertainment {
val title: String
}

data class Show(
val title: String,
)
override val title: String,
) : Entertainment

data class Movie(
override val title: String,
) : Entertainment

@Test
fun `@Source argument`() {
Expand Down Expand Up @@ -96,6 +104,60 @@ internal class SourceArgumentTest {
}
}

@Test
fun `@Source argument with interface type`() {
@DgsComponent
class Fetcher {
@DgsQuery
fun shows(): List<Show> = listOf(Show("Stranger Things"))

@DgsQuery
fun movies(): List<Movie> = listOf(Movie("Batman"))

@DgsData(parentType = "Show")
@DgsData(parentType = "Movie")
fun description(
@Source entertainment: Entertainment,
): String = "Description of ${entertainment.title}"
}

contextRunner.withBean(Fetcher::class.java).run { context ->
val provider = schemaProvider(context)
val schema = provider.schema().graphQLSchema

val build = GraphQL.newGraphQL(schema).build()
val executionResult =
build.execute(
"""{
| shows {
| title
| description
| }
|
| movies {
| title
| description
| }
|}
""".trimMargin(),
)

assertThat(executionResult.errors).isEmpty()
assertThat(executionResult.isDataPresent).isTrue
val data = executionResult.getData<Map<String, *>>()

@Suppress("UNCHECKED_CAST")
val showData = (data["shows"] as List<Map<*, *>>)[0]
assertThat(showData["title"]).isEqualTo("Stranger Things")
assertThat(showData["description"]).isEqualTo("Description of Stranger Things")

@Suppress("UNCHECKED_CAST")
val movieData = (data["movies"] as List<Map<*, *>>)[0]
assertThat(movieData["title"]).isEqualTo("Batman")
assertThat(movieData["description"]).isEqualTo("Description of Batman")
}
}

@Test
fun `Incorrect @Source argument type`() {
@DgsComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
type Query {
shows: [Show]
movies: [Movie]
}

type Show {
title: String
description: String
}

type Movie {
title: String
description: String
}