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 eec75d2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 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 @@ -49,8 +49,10 @@ import com.netflix.graphql.dgs.scalars.UploadScalar
import graphql.ExceptionWhileDataFetching
import graphql.ExecutionInput
import graphql.GraphQL
import graphql.Scalars
import graphql.scalars.ExtendedScalars
import graphql.scalars.country.code.CountryCode
import graphql.scalars.currency.CurrencyScalar
import graphql.schema.DataFetchingEnvironment
import graphql.schema.idl.RuntimeWiring
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -66,9 +68,10 @@ import org.springframework.mock.web.MockMultipartFile
import org.springframework.web.multipart.MultipartFile
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Optional
import java.util.*
import kotlin.reflect.KClass


internal class InputArgumentTest {

private val contextRunner = ApplicationContextRunner()
Expand Down Expand Up @@ -806,39 +809,6 @@ internal class InputArgumentTest {
}
}

@Test
fun `A DgsInvalidInputArgumentException should be thrown when the @InputArgument collectionType doesn't match the parameter type`() {
val schema = """
type Query {
hello(person:[Person]): String
}
input Person {
name:String
}
""".trimIndent()

@DgsComponent
class Fetcher {
@DgsData(parentType = "Query", field = "hello")
fun someFetcher(@InputArgument("person") person: List<String>): String {
return "Hello, ${person.joinToString(", ") { it }}"
}
}

contextRunner.withBeans(Fetcher::class).run { context ->
val provider = schemaProvider(context)
val build = GraphQL.newGraphQL(provider.schema(schema)).build()

val executionResult = build.execute("""{hello(person: [{name: "tester"}, {name: "tester 2"}])}""")
assertThat(executionResult.errors).hasSize(1)
val exceptionWhileDataFetching = executionResult.errors[0] as ExceptionWhileDataFetching
assertThat(exceptionWhileDataFetching.exception).isInstanceOf(ConversionFailedException::class.java)
assertThat(exceptionWhileDataFetching.exception.message)
.contains("Failed to convert from type [java.util.LinkedHashMap<?, ?>] to type [@com.netflix.graphql.dgs.InputArgument java.lang.String]")
}
}

@Test
fun `An @InputArgument representing a complex type can be empty`() {
val schema = """
Expand Down Expand Up @@ -2259,6 +2229,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 eec75d2

Please sign in to comment.