diff --git a/build.gradle b/build.gradle index 753d643c..998202a5 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ group 'com.github.pgutkowski' version '0.3.0' buildscript { - ext.kotlin_version = '1.3.0' + ext.kotlin_version = '1.3.10' repositories { mavenCentral() diff --git a/src/test/kotlin/com/github/pgutkowski/kgraphql/specification/typesystem/ScalarsSpecificationTest.kt b/src/test/kotlin/com/github/pgutkowski/kgraphql/specification/typesystem/ScalarsSpecificationTest.kt index accc86de..110ed0af 100644 --- a/src/test/kotlin/com/github/pgutkowski/kgraphql/specification/typesystem/ScalarsSpecificationTest.kt +++ b/src/test/kotlin/com/github/pgutkowski/kgraphql/specification/typesystem/ScalarsSpecificationTest.kt @@ -10,31 +10,32 @@ import com.github.pgutkowski.kgraphql.schema.scalar.StringScalarCoercion import org.hamcrest.CoreMatchers.equalTo import org.hamcrest.MatcherAssert.assertThat import org.junit.Test +import java.time.LocalDate import java.util.* @Specification("3.1.1 Scalars") class ScalarsSpecificationTest { - data class Person(val uuid : UUID, val name : String) + data class Person(val uuid: UUID, val name: String) @Test - fun `type systems can add additional scalars with semantic meaning`(){ + fun `type systems can add additional scalars with semantic meaning`() { val uuid = UUID.randomUUID() val testedSchema = KGraphQL.schema { stringScalar { description = "unique identifier of object" - coercion = object : StringScalarCoercion{ + coercion = object : StringScalarCoercion { override fun serialize(instance: UUID): String = instance.toString() override fun deserialize(raw: String): UUID = UUID.fromString(raw) } } - query("person"){ - resolver{ -> Person(uuid, "John Smith")} + query("person") { + resolver { -> Person(uuid, "John Smith") } } mutation("createPerson") { - resolver{ uuid : UUID, name: String -> Person(uuid, name) } + resolver { uuid: UUID, name: String -> Person(uuid, name) } } } @@ -42,29 +43,29 @@ class ScalarsSpecificationTest { assertThat(queryResponse.extract("data/person/uuid"), equalTo(uuid.toString())) val mutationResponse = deserialize(testedSchema.execute( - "{createPerson(uuid: \"$uuid\", name: \"John\"){uuid name}}" + "{createPerson(uuid: \"$uuid\", name: \"John\"){uuid name}}" )) assertThat(mutationResponse.extract("data/createPerson/uuid"), equalTo(uuid.toString())) assertThat(mutationResponse.extract("data/createPerson/name"), equalTo("John")) } @Test - fun `integer value represents a value grater than 2^-31 and less or equal to 2^31`(){ + fun `integer value represents a value grater than 2^-31 and less or equal to 2^31`() { val schema = KGraphQL.schema { mutation("Int") { - resolver { int : Int -> int } + resolver { int: Int -> int } } } - expect("is not valid value of type Int"){ - schema.execute("{Int(int: ${Integer.MAX_VALUE.toLong()+2L})}") + expect("is not valid value of type Int") { + schema.execute("{Int(int: ${Integer.MAX_VALUE.toLong() + 2L})}") } } @Test - fun `when float is expected as an input type, both integer and float input values are accepted`(){ + fun `when float is expected as an input type, both integer and float input values are accepted`() { val schema = KGraphQL.schema { - mutation("float"){ + mutation("float") { resolver { float: Float -> float } } } @@ -73,16 +74,16 @@ class ScalarsSpecificationTest { } @Test - fun `server can declare custom ID type`(){ + fun `server can declare custom ID type`() { val testedSchema = KGraphQL.schema { stringScalar { name = "ID" description = "unique identifier of object" - deserialize = { uuid : String -> UUID.fromString(uuid) } + deserialize = { uuid: String -> UUID.fromString(uuid) } serialize = UUID::toString } - query("personById"){ - resolver{ id: UUID -> Person(id, "John Smith")} + query("personById") { + resolver { id: UUID -> Person(id, "John Smith") } } } @@ -93,22 +94,22 @@ class ScalarsSpecificationTest { @Test - fun `For numeric scalars, input string with numeric content must raise a query error indicating an incorrect type`(){ + fun `For numeric scalars, input string with numeric content must raise a query error indicating an incorrect type`() { val schema = KGraphQL.schema { mutation("Int") { - resolver { int : Int -> int } + resolver { int: Int -> int } } } - expect(""){ + expect("") { schema.execute("{Int(int: \"223\")}") } } - data class Number(val int : Int) + data class Number(val int: Int) @Test - fun `Schema may declare custom int scalar type`(){ + fun `Schema may declare custom int scalar type`() { val schema = KGraphQL.schema { intScalar { @@ -116,8 +117,8 @@ class ScalarsSpecificationTest { serialize = { (int) -> int } } - query("number"){ - resolver { number : Number -> number } + query("number") { + resolver { number: Number -> number } } } @@ -129,7 +130,7 @@ class ScalarsSpecificationTest { data class Bool(val boolean: Boolean) @Test - fun `Schema may declare custom boolean scalar type`(){ + fun `Schema may declare custom boolean scalar type`() { val schema = KGraphQL.schema { booleanScalar { @@ -137,8 +138,8 @@ class ScalarsSpecificationTest { serialize = { (boolean) -> boolean } } - query("boolean"){ - resolver { boolean : Boolean -> boolean } + query("boolean") { + resolver { boolean: Boolean -> boolean } } } @@ -155,7 +156,7 @@ class ScalarsSpecificationTest { data class Multi(val boo: Boo, val str: String, val num: Num) @Test - fun `Schema may declare custom double scalar type`(){ + fun `Schema may declare custom double scalar type`() { val schema = KGraphQL.schema { floatScalar { @@ -163,8 +164,8 @@ class ScalarsSpecificationTest { serialize = { (double) -> double } } - query("double"){ - resolver { double : Dob -> double } + query("double") { + resolver { double: Dob -> double } } } @@ -174,7 +175,7 @@ class ScalarsSpecificationTest { } @Test - fun `Scalars within input variables`(){ + fun `Scalars within input variables`() { val schema = KGraphQL.schema { booleanScalar { deserialize = ::Boo @@ -197,11 +198,11 @@ class ScalarsSpecificationTest { serialize = { (str) -> str } } - query("boo") { resolver { boo : Boo -> boo } } - query("lon") { resolver { lon : Lon -> lon } } - query("dob") { resolver { dob : Dob -> dob } } - query("num") { resolver { num : Num -> num } } - query("str") { resolver { str : Str -> str } } + query("boo") { resolver { boo: Boo -> boo } } + query("lon") { resolver { lon: Lon -> lon } } + query("dob") { resolver { dob: Dob -> dob } } + query("num") { resolver { num: Num -> num } } + query("str") { resolver { str: Str -> str } } query("multi") { resolver { -> Multi(Boo(false), "String", Num(25)) } } } @@ -244,4 +245,44 @@ class ScalarsSpecificationTest { assertThat(response.extract("data/multi/str"), equalTo("String")) assertThat(response.extract("data/multi/num"), equalTo(25)) } + + data class NewPart(val manufacturer: String, val name: String, val oem: Boolean, val addedDate: LocalDate) + + @Test + fun `Schema may declare LocalDate custom scalar`() { + val schema = KGraphQL.schema { + stringScalar { + serialize = { date -> date.toString() } + deserialize = { dateString -> LocalDate.parse(dateString) } + description = "Date in format yyyy-mm-dd" + } + + mutation("addPart") { + description = "Adds a new part in the parts inventory database" + resolver { newPart: NewPart -> + println(newPart) + + newPart + } + } + + inputType {} + } + + val manufacturer = """Joe Bloggs""" + + val response = deserialize(schema.execute( + "mutation Mutation(\$newPart : NewPart!){ addPart(newPart: \$newPart) {manufacturer} }", + """ + { "newPart" : { + "manufacturer":"$manufacturer", + "name":"Front bumper", + "oem":true, + "addedDate":"2001-09-01" + }} + """.trimIndent()) + ) + + assertThat(response.extract("data/addPart/manufacturer"), equalTo(manufacturer)) + } } \ No newline at end of file