Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
arnabkd committed Sep 3, 2021
1 parent f9a5be9 commit 2dcbd6f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/Payload.kt
@@ -0,0 +1,6 @@
data class WipeDataPayload(val success: Boolean) {
companion object {
val SUCCESS = WipeDataPayload(true)
val FAILURE = WipeDataPayload(false)
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/PersonRepository.kt
@@ -1,6 +1,8 @@
object PersonRepository {
var people: MutableList<Person> = mutableListOf()

fun clear() = people.clear()

fun addPerson(name: String): Person {
val id = nextId()
val person = Person(id, name, emptyList())
Expand All @@ -17,7 +19,9 @@ object PersonRepository {
fun addFriends(first: Person, second: Person): Boolean =
first.addFriend(second) && second.addFriend(first)

fun allPeople() = people.toList()
fun allPeople() = people.toList().also {
println("Expensive call to get all people")
}

// Note: this can be expensive, so look at the Person.kt class for a hint on how to batch calls
fun findById(id: Int) = people.first { it.id == id }
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/PetsRepository.kt
@@ -1,10 +1,14 @@
object PetsRepository {
var pets = mutableListOf<Pet>()

fun clear() = pets.clear()

fun findById(id: Int) = pets.first { it.id == id }
fun findByIds(ids: List<Int>) = pets.filter { it.id in ids}.also {
println("Another expensive call to get pets with ids: $ids")
}
fun findByIds(ids: List<Int>) = pets
.filter { it.id in ids}
.also {
println("Another expensive call to get pets with ids: $ids")
}

fun allPets() = pets.toList()

Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/schema.kt
Expand Up @@ -40,9 +40,19 @@ class QueryResolver : GraphQLQueryResolver {

@Suppress("unused") // GraphQL by reflection
class MutationResolver : GraphQLMutationResolver {
fun wipeData() = run {
try {
PersonRepository.clear()
PetsRepository.clear()
WipeDataPayload.SUCCESS
} catch (e: Exception) {
WipeDataPayload.FAILURE
}
}

fun generateData(): List<Person> {
val faker = Faker()
val newPeople = (1..2).map {
val newPeople = (1..10).map {
val fakePersonName = faker.name().nameWithMiddle()

val petIds = (0..Random.nextInt(5)).map {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/schema.graphql
Expand Up @@ -51,6 +51,11 @@ type Mutation {
addPerson(input: PersonInput!): Person!
addFriends(input: AddFriendsInput!): Boolean!
generateData: [Person!]!
wipeData: WipeDataPayload!
}

type WipeDataPayload {
success: Boolean!
}

# RFC3339: full-date
Expand Down

0 comments on commit 2dcbd6f

Please sign in to comment.