Skip to content

Commit

Permalink
Add DataLoader example. (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
neetkee authored and smyrick committed Oct 1, 2019
1 parent 37cd52c commit 593dc3a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.expediagroup.graphql.examples.dataloaders

import com.expediagroup.graphql.examples.model.Company
import com.expediagroup.graphql.examples.query.CompanyService
import com.expediagroup.graphql.spring.execution.DataLoaderRegistryFactory
import org.dataloader.DataLoader
import org.dataloader.DataLoaderRegistry
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.concurrent.CompletableFuture

@Configuration
class DataLoaderConfiguration(private val companyService: CompanyService) {
@Bean
fun dataLoaderRegistryFactory(): DataLoaderRegistryFactory {
return object : DataLoaderRegistryFactory {
override fun generate(): DataLoaderRegistry {
val registry = DataLoaderRegistry()
val companyLoader = DataLoader<Int, Company> { ids ->
CompletableFuture.supplyAsync { companyService.getCompanies(ids) }
}
registry.register("companyLoader", companyLoader)
return registry
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.expediagroup.graphql.examples.model

import com.expediagroup.graphql.annotations.GraphQLIgnore

data class Employee(
val name: String,
@GraphQLIgnore
val companyId: Int
) {
lateinit var company: Company
}

data class Company(val id: Int, val name: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.expediagroup.graphql.examples.query

import com.expediagroup.graphql.annotations.GraphQLDescription
import com.expediagroup.graphql.examples.model.Company
import com.expediagroup.graphql.examples.model.Employee
import com.expediagroup.graphql.spring.operations.Query
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.factory.BeanFactoryAware
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component
import java.util.concurrent.CompletableFuture

@Component
class CompanyService {
private val companies = listOf(
Company(id = 1, name = "FirstCompany"),
Company(id = 2, name = "SecondCompany")
)

fun getCompanies(ids: List<Int>): List<Company> = companies
}

@Component
class EmployeeQuery : Query {
private val employees = listOf(
Employee(name = "Mike", companyId = 1),
Employee(name = "John", companyId = 1),
Employee(name = "Steve", companyId = 2)
)

@GraphQLDescription("Get all employees")
fun employees(): List<Employee> {
return employees
}
}

@Component("CompanyDataFetcher")
@Scope("prototype")
class CompanyDataFetcher : DataFetcher<CompletableFuture<Company>>, BeanFactoryAware {
private lateinit var beanFactory: BeanFactory

override fun setBeanFactory(beanFactory: BeanFactory) {
this.beanFactory = beanFactory
}

override fun get(environment: DataFetchingEnvironment): CompletableFuture<Company> {
val companyId = environment.getSource<Employee>().companyId
return environment
.getDataLoader<Int, Company>("companyLoader")
.load(companyId)
}
}

0 comments on commit 593dc3a

Please sign in to comment.