Skip to content

Commit

Permalink
#602 Paginated list of validation runs for Build type - using a cac…
Browse files Browse the repository at this point in the history
…he for the paginated list type
  • Loading branch information
dcoraboeuf committed May 14, 2018
1 parent 8ea568c commit 3ac2316
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Expand Up @@ -96,6 +96,7 @@ constructor(
// Paginated list of validation runs
.field(
paginatedListFactory.createPaginatedField<Build, ValidationRun>(
cache = cache,
fieldName = "validationRunsPaginated",
fieldDescription = "Paginated list of validation runs",
itemType = validationRun,
Expand Down Expand Up @@ -159,6 +160,7 @@ constructor(
// Build links - "usedBy" direction, with pagination
.field(
paginatedListFactory.createPaginatedField<Build, Build>(
cache = cache,
fieldName = "usedBy",
fieldDescription = "List of builds using this one.",
itemType = this,
Expand Down
Expand Up @@ -53,6 +53,7 @@ class GQLTypeValidationStamp(
// Paginated list of validation runs
.field(
paginatedListFactory.createPaginatedField<ValidationStamp, ValidationRun>(
cache = cache,
fieldName = "validationRunsPaginated",
fieldDescription = "Paginated list of validation runs",
itemType = validationRun,
Expand Down
Expand Up @@ -6,6 +6,7 @@ import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLObjectType
import net.nemerosa.ontrack.graphql.schema.GQLType
import net.nemerosa.ontrack.graphql.schema.GQLTypeCache
import net.nemerosa.ontrack.graphql.support.GraphqlUtils.stdList
import net.nemerosa.ontrack.model.pagination.PageRequest
import net.nemerosa.ontrack.model.pagination.PaginatedList
Expand Down Expand Up @@ -35,13 +36,15 @@ class GQLPaginatedListFactory(
* @param arguments Optional list of arguments to add to the field
*/
fun <P, T> createPaginatedField(
cache: GQLTypeCache,
fieldName: String,
fieldDescription: String,
itemType: GQLType,
itemPaginatedListProvider: (DataFetchingEnvironment, P, Int, Int) -> PaginatedList<T>,
arguments: List<GraphQLArgument> = emptyList()
): GraphQLFieldDefinition =
createBasePaginatedListField(
cache,
fieldName,
fieldDescription,
itemType,
Expand Down Expand Up @@ -69,6 +72,7 @@ class GQLPaginatedListFactory(
* @param arguments Optional list of arguments to add to the field
*/
fun <P, T> createPaginatedField(
cache: GQLTypeCache,
fieldName: String,
fieldDescription: String,
itemType: GQLType,
Expand All @@ -77,7 +81,7 @@ class GQLPaginatedListFactory(
arguments: List<GraphQLArgument> = emptyList()
): GraphQLFieldDefinition =
createBasePaginatedListField(
fieldName, fieldDescription, itemType, arguments
cache, fieldName, fieldDescription, itemType, arguments
).dataFetcher { environment ->
val source: P = environment.getSource<P>()
val offset = environment.getArgument<Int>(ARG_OFFSET) ?: 0
Expand All @@ -97,6 +101,7 @@ class GQLPaginatedListFactory(
}.build()

private fun createBasePaginatedListField(
cache: GQLTypeCache,
fieldName: String,
fieldDescription: String,
itemType: GQLType,
Expand All @@ -118,7 +123,7 @@ class GQLPaginatedListFactory(
.defaultValue(PageRequest.DEFAULT_PAGE_SIZE)
}
.argument(arguments)
.type(createPaginatedList(itemType))
.type(createPaginatedList(cache, itemType))

/**
* Creates a paginated GraphQL list type, linked to an actual
Expand All @@ -127,20 +132,27 @@ class GQLPaginatedListFactory(
* @param itemType Type of item in the list
*/
private fun createPaginatedList(
cache: GQLTypeCache,
itemType: GQLType
): GraphQLObjectType {
return GraphQLObjectType.newObject()
.name("${itemType.typeName}Paginated")
.field {
it.name("pageInfo")
.description("Information about the current page")
.type(pageInfo.typeRef)
val paginatedListTypeName = "${itemType.typeName}Paginated"
return cache.getOrCreate(
paginatedListTypeName,
{
GraphQLObjectType.newObject()
.name(paginatedListTypeName)
.field {
it.name("pageInfo")
.description("Information about the current page")
.type(pageInfo.typeRef)
}
.field {
it.name("pageItems")
.description("Items in the current page")
.type(stdList(itemType.typeRef))
}
.build()
}
.field {
it.name("pageItems")
.description("Items in the current page")
.type(stdList(itemType.typeRef))
}
.build()
)
}
}

0 comments on commit 3ac2316

Please sign in to comment.