Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

use reified loader #29

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,7 @@ class ApplicationDeploymentResolver(
Namespace(applicationDeployment.namespaceId, applicationDeployment.affiliationId)

fun details(applicationDeployment: ApplicationDeployment, dfe: DataFetchingEnvironment) =
dfe.loader(ApplicationDeploymentDetails::class).load(applicationDeployment.id)
dfe.loader<ApplicationDeploymentDetails>().load(applicationDeployment.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jeg må innrømme at jeg egentlig ikke syntes dette er bedre. Det er ikke alltid det passer å bruke reification og generics for å overføre typeinformasjon som parameter. Etter min vurdering leser

dfe.loaderFor(ApplicationDeploymentDetails::class)
bedre enn

dfe.loader<ApplicationDeploymentDetails>() eller evt. dfe.loaderFor<ApplicationDeploymentDetails>()

Å bruke generics her er bare en konvoluted måte å sende inn en parameter på. En måte å vurdere om generics passer eller ei på er å sjekke om metodesignaturen passer ved å gå over til flertall. F.eks.

val loaders = dfe.loadersFor(ApplicationDeploymentDetails::class, DeploymentSpec::class)

En slik endring vil ikke kunne støttes med bruk av generics.


fun application(applicationDeployment: ApplicationDeployment): Application? {
return applicationService.getApplication(applicationDeployment.applicationId)
Expand Down
Expand Up @@ -43,4 +43,4 @@ class ApplicationDeploymentDetailsDataLoader(
}
}
}
}
}
Expand Up @@ -9,5 +9,5 @@ import org.springframework.stereotype.Component
class ApplicationDeploymentDetailsQueryResolver : GraphQLQueryResolver {

fun applicationDeploymentDetails(id: String, dfe: DataFetchingEnvironment) =
dfe.loader(ApplicationDeploymentDetails::class).load(id)
dfe.loader<ApplicationDeploymentDetails>().load(id)
}
Expand Up @@ -13,11 +13,11 @@ class DeploymentSpecsResolver : GraphQLResolver<DeploymentSpecs> {

fun current(specs: DeploymentSpecs, dfe: DataFetchingEnvironment): CompletableFuture<Try<DeploymentSpec>>? =
dfe.token?.let { token ->
specs.deploymentSpecCurrent?.let { dfe.loader(DeploymentSpec::class).load(UrlAndToken(it, token)) }
specs.deploymentSpecCurrent?.let { dfe.loader<DeploymentSpec>().load(UrlAndToken(it, token)) }
}

fun deployed(specs: DeploymentSpecs, dfe: DataFetchingEnvironment): CompletableFuture<Try<DeploymentSpec>>? =
dfe.token?.let { token ->
specs.deploymentSpecDeployed?.let { dfe.loader(DeploymentSpec::class).load(UrlAndToken(it, token)) }
specs.deploymentSpecDeployed?.let { dfe.loader<DeploymentSpec>().load(UrlAndToken(it, token)) }
}
}
Expand Up @@ -8,5 +8,5 @@ import org.springframework.stereotype.Component
@Component
class ImageRepositoryTagResolver : GraphQLResolver<ImageTag> {

fun lastModified(imageTag: ImageTag, dfe: DataFetchingEnvironment) = dfe.loader(ImageTag::class).load(imageTag)
fun lastModified(imageTag: ImageTag, dfe: DataFetchingEnvironment) = dfe.loader<ImageTag>().load(imageTag)
}
Expand Up @@ -10,5 +10,5 @@ import org.springframework.stereotype.Component
class NamespaceResolver : GraphQLResolver<Namespace> {

fun affiliation(namespace: Namespace, dfe: DataFetchingEnvironment) =
dfe.loader(Affiliation::class).load(namespace.affiliationId)
dfe.loader<Affiliation>().load(namespace.affiliationId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dette må være AffiliationDataLoader, hvis vi ikke setter "${simpleName}DataLoader" i loader() funksjonen.

}
Expand Up @@ -20,8 +20,9 @@ val DataFetchingEnvironment.token: String?
}
}

fun <T : Any> DataFetchingEnvironment.loader(type: KClass<T>): DataLoader<Any, Try<T>> {
val key = "${type.simpleName}DataLoader"

inline fun <reified T : Any> DataFetchingEnvironment.loader(): DataLoader<Any, Try<T>> {
val key = T::class.java.simpleName
val dataLoader = this.getContext<GraphQLContext>().dataLoaderRegistry.get()
.getDataLoader<Any, Try<T>>(key) ?: throw IllegalStateException("No $key found")
return dataLoader as? NoCacheBatchDataLoader ?: dataLoader as NoCacheBatchDataLoaderFlux
Expand Down