Skip to content

Commit

Permalink
add faker
Browse files Browse the repository at this point in the history
  • Loading branch information
arnabkd committed Jun 11, 2021
1 parent e8920be commit d983fdc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation "com.google.code.gson:gson:2.8.7"
implementation "com.graphql-java:graphql-java:11.0"
implementation 'com.google.guava:guava:30.1.1-jre'

implementation 'com.github.javafaker:javafaker:1.0.2'
implementation "io.ktor:ktor-server-netty:1.6.0"
implementation 'com.graphql-java:java-dataloader:2021-03-30T02-56-19-5805475'
}
Expand Down
30 changes: 10 additions & 20 deletions src/main/kotlin/PersonRepository.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import graphql.schema.DataFetchingEnvironment

object PersonRepository {
var people: MutableList<Person> = mutableListOf()

init {
val rocket = PetsRepository.findById(1)
val sherlock = PetsRepository.findById(2)

val jessica = Person(3, "Jessica", emptyList())
val kim = Person(2, "Kim", listOf(sherlock.id))
val arnab = Person(1, "Arnab", listOf(sherlock.id, rocket.id))

addFriends(jessica, arnab)
addFriends(kim, arnab)
addFriends(kim, jessica)

people.add(arnab)
people.add(jessica)
people.add(kim)
}

fun addPerson(name: String): Person {
val id = (people.maxByOrNull { it.id }?.id ?: 0) + 1
val id = nextId()
val person = Person(id, name, emptyList())
people.add(person)
return person
}

fun addPerson(person: Person) =
if (people.find { it.id == person.id } != null)
throw IllegalArgumentException("Already one person with that ID")
else
people.add(person)

fun addFriends(first: Person, second: Person): Boolean =
first.addFriend(second) && second.addFriend(first)

Expand All @@ -37,4 +25,6 @@ object PersonRepository {
fun findByIds(ids: List<Int>) = people.filter { it.id in ids }.also {
println("An expensive call to find the people with id $ids")
}

fun nextId() = (people.maxByOrNull { it.id }?.id ?: 0) + 1
}
17 changes: 8 additions & 9 deletions src/main/kotlin/PetsRepository.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
object PetsRepository {
var pets = mutableListOf<Pet>()

init {
val rocket = Dog(1, "rocket")
val sherlock = Cat(2, "sherlock")
val max = Dog(3, "max")
pets.add(rocket)
pets.add(sherlock)
pets.add(max)
}

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 allPets() = pets.toList()

fun addPet(pet: Pet) =
if (pets.find { it.id == pet.id } != null)
throw IllegalArgumentException("Already exists")
else
pets.add(pet)

fun nextId() = (pets.maxByOrNull { it.id }?.id ?: 0) + 1
}
28 changes: 28 additions & 0 deletions src/main/kotlin/schema.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import com.github.javafaker.Faker
import graphql.kickstart.tools.GraphQLMutationResolver
import graphql.kickstart.tools.GraphQLQueryResolver
import graphql.kickstart.tools.SchemaParser
import com.google.common.io.Resources
import kotlin.random.Random

val schema = createExecutableSchema()

Expand Down Expand Up @@ -36,6 +38,32 @@ class QueryResolver : GraphQLQueryResolver {

@Suppress("unused") // GraphQL by reflection
class MutationResolver : GraphQLMutationResolver {
fun generateData(): List<Person> {
val faker = Faker()
val newPeople = (1..500).map {
val fakePersonName = faker.name().fullName()
val petId = PetsRepository.nextId()

val pet = when(Random.nextInt(2)) {
0 -> Dog(petId, faker.dog().name())
else -> Cat(petId, faker.cat().name())
}
PetsRepository.addPet(pet)

val person = Person(PersonRepository.nextId(), fakePersonName, listOf(pet.id))
PersonRepository.addPerson(person)

person
}

val allPeople = PersonRepository.allPeople()

newPeople.forEach {
PersonRepository.addFriends(it, allPeople.random())
}

return newPeople
}
fun addPerson(input: PersonInput): Person = PersonRepository.addPerson(input.name)
fun addFriends(input: AddFriendsInput): Boolean {
val first = PersonRepository.findById(input.first)
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ type Query {
type Mutation {
addPerson(input: PersonInput!): Person!
addFriends(input: AddFriendsInput!): Boolean!
generateData: [Person!]!
}

0 comments on commit d983fdc

Please sign in to comment.