diff --git a/src/main/kotlin/Payload.kt b/src/main/kotlin/Payload.kt new file mode 100644 index 0000000..7f3b24d --- /dev/null +++ b/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) + } +} diff --git a/src/main/kotlin/PersonRepository.kt b/src/main/kotlin/PersonRepository.kt index f3be26f..0ee50d6 100644 --- a/src/main/kotlin/PersonRepository.kt +++ b/src/main/kotlin/PersonRepository.kt @@ -1,6 +1,8 @@ object PersonRepository { var people: MutableList = mutableListOf() + fun clear() = people.clear() + fun addPerson(name: String): Person { val id = nextId() val person = Person(id, name, emptyList()) @@ -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 } diff --git a/src/main/kotlin/PetsRepository.kt b/src/main/kotlin/PetsRepository.kt index 5e77ede..1d1513d 100644 --- a/src/main/kotlin/PetsRepository.kt +++ b/src/main/kotlin/PetsRepository.kt @@ -1,10 +1,14 @@ object PetsRepository { var pets = mutableListOf() + fun clear() = pets.clear() + fun findById(id: Int) = pets.first { it.id == id } - fun findByIds(ids: List) = pets.filter { it.id in ids}.also { - println("Another expensive call to get pets with ids: $ids") - } + fun findByIds(ids: List) = pets + .filter { it.id in ids} + .also { + println("Another expensive call to get pets with ids: $ids") + } fun allPets() = pets.toList() diff --git a/src/main/kotlin/schema.kt b/src/main/kotlin/schema.kt index cb0b5d9..6a63fc6 100644 --- a/src/main/kotlin/schema.kt +++ b/src/main/kotlin/schema.kt @@ -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 { 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 { diff --git a/src/main/resources/schema.graphql b/src/main/resources/schema.graphql index 0429108..0b08962 100644 --- a/src/main/resources/schema.graphql +++ b/src/main/resources/schema.graphql @@ -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