Skip to content

Commit

Permalink
Test with currency as nested input
Browse files Browse the repository at this point in the history
  • Loading branch information
kailyak committed Oct 31, 2023
1 parent d8eb2e9 commit f82ebc6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import graphql.scalars.country.code.CountryCode;

import java.util.Currency;
import java.util.List;

public class JInputMessage {
Expand All @@ -26,6 +27,8 @@ public class JInputMessage {

private CountryCode countryCode;

private Currency currency;

public JGreetingType getType() {
return type;
}
Expand All @@ -49,4 +52,12 @@ public CountryCode getCountryCode() {
public void setCountryCode(CountryCode countryCode) {
this.countryCode = countryCode;
}

public Currency getCurrency() {
return currency;
}

public void setCurrency(Currency currency) {
this.currency = currency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,47 @@ internal class InputArgumentTest {
}
}

@Test
fun `The enum scalar in a nested input should be mapped diff type`() {
val schema = """
type Query {
hello(input:InputMessage): String
}
input InputMessage {
currency: Currency
}
scalar Currency
""".trimIndent()

@DgsComponent
class Fetcher {
@DgsData(parentType = "Query", field = "hello")
fun hello(@InputArgument input: JInputMessage): String {
assertThat(input.currency).isInstanceOf(Currency::class.java)
assertThat(input.currency).isEqualTo(Currency.getInstance(input.currency.toString()))
return "Hello, this is a ${input.currency} currency"
}

@DgsRuntimeWiring
fun addScalar(builder: RuntimeWiring.Builder): RuntimeWiring.Builder {
return builder.scalar(ExtendedScalars.Currency)
}
}

contextRunner.withBeans(Fetcher::class).run { context ->
val provider = schemaProvider(context)
val build = GraphQL.newGraphQL(provider.schema(schema)).build()
val executionResult = build.execute("""{hello(input: {currency: "USD"})}""")
assertThat(executionResult).isNotNull
assertThat(executionResult.errors).isEmpty()
assertThat(executionResult.isDataPresent).isTrue
val data = executionResult.getData<Map<String, *>>()
assertThat(data).extracting("hello").isEqualTo("Hello, this is a USD currency")
}
}

private fun ApplicationContextRunner.withBeans(vararg beanClasses: KClass<*>): ApplicationContextRunner {
var context = this
for (klazz in beanClasses) {
Expand Down

0 comments on commit f82ebc6

Please sign in to comment.