Skip to content

Commit 59b6791

Browse files
committed
Move creative type hierarchy and use expression body methods.
1 parent cc0ed8c commit 59b6791

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/test/kotlin/graphql/kickstart/tools/EndToEndSpecHelper.kt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fun createSchema() = SchemaParser.newParser()
2222
.dictionary("ThirdItem", ThirdItem::class)
2323
.dictionary("ComplexMapItem", ComplexMapItem::class)
2424
.dictionary("NestedComplexMapItem", NestedComplexMapItem::class)
25-
.dictionary("OutOfBeerError", OutOfBeerError::class)
25+
.dictionary("NoDogError", NoDogError::class)
2626
.build()
2727
.makeExecutableSchema()
2828

@@ -85,8 +85,8 @@ type Query {
8585
8686
throwsIllegalArgumentException: String
8787
88-
allBars: [Bar!]!
89-
findAvailableBar(persons: Int!): BarResult!
88+
allDogs: [Dog!]!
89+
findSuitableDog(preferredColor: String!, minimumFluffiness: Int!): FindDogResult!
9090
}
9191
9292
type ExtendedType {
@@ -221,15 +221,17 @@ type ItemWithGenericProperties {
221221
keys: [String!]!
222222
}
223223
224-
type Bar {
225-
name: String
224+
type Dog {
225+
name: String!
226+
color: String!
227+
fluffiness: Int!
226228
}
227229
228-
type OutOfBeerError {
230+
type NoDogError {
229231
msg: String
230232
}
231233
232-
union BarResult = Bar | OutOfBeerError
234+
union FindDogResult = Dog | NoDogError
233235
"""
234236

235237
val items = listOf(
@@ -329,15 +331,12 @@ class Query : GraphQLQueryResolver, ListListResolver<String>() {
329331
throw IllegalArgumentException("Expected")
330332
}
331333

332-
fun allBars(): List<Bar> {
333-
return listOf(BarEntityImpl("123", "Bar Name"))
334-
}
335-
fun findAvailableBar(persons: Int): Any {
336-
if (persons < 56)
337-
return BarEntityImpl("123", "Bar Name");
338-
else
339-
return OutOfBeerError("No room for $persons persons")
340-
}
334+
fun allDogs(): List<Dog> = listOf(LabradorRetriever("Hershey", "chocolate", 42, 3.14159f))
335+
336+
fun findSuitableDog(preferredColor: String, minimumFluffiness: Int): Any =
337+
allDogs()
338+
.firstOrNull { it.color == preferredColor && it.fluffiness >= minimumFluffiness }
339+
?: NoDogError("No $preferredColor-colored dog found that is sufficiently fluffy")
341340
}
342341

343342
class UnusedRootResolver : GraphQLQueryResolver
@@ -434,10 +433,14 @@ class MockPart(private val name: String, private val content: String) : Part {
434433
override fun delete() = throw IllegalArgumentException("Not supported")
435434
}
436435

437-
interface Bar { val name: String }
438-
interface BarEntity : Bar { val id: String}
439-
class BarEntityImpl(override val id: String, override val name: String) : BarEntity
440-
class OutOfBeerError(val msg: String)
436+
interface Dog {
437+
val name: String
438+
val color: String
439+
val fluffiness: Int
440+
}
441+
interface Retriever : Dog { val speed: Float }
442+
class LabradorRetriever(override val name: String, override val color: String, override val fluffiness: Int, override val speed: Float) : Retriever
443+
class NoDogError(val msg: String)
441444

442445
val customScalarId = GraphQLScalarType.newScalar()
443446
.name("ID")

src/test/kotlin/graphql/kickstart/tools/EndToEndTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ class EndToEndTest {
183183
val data = assertNoGraphQlErrors(gql) {
184184
"""
185185
{
186-
findAvailableBar(persons: 42) {
187-
... on Bar { name }
188-
... on OutOfBeerError { msg }
186+
findSuitableDog(preferredColor: "chocolate", minimumFluffiness: 31) {
187+
... on Dog { name }
188+
... on NoDogError { msg }
189189
}
190190
}
191191
"""
192192
}
193193

194-
assertNotNull(data["findAvailableBar"])
194+
assertNotNull(data["findSuitableDog"])
195195
}
196196

197197
@Test

0 commit comments

Comments
 (0)