-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
.../src/main/kotlin/com/expediagroup/graphql/examples/dataloaders/DataLoaderConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/model/Organization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
54 changes: 54 additions & 0 deletions
54
examples/spring/src/main/kotlin/com/expediagroup/graphql/examples/query/OrganizationQuery.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |