-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache pagination: add FieldNameGenerator and EmbeddedFieldsProvider (#…
…5772) * Add FieldNameGenerator for case where paginationArgs is not enough * Add EmbeddedFieldsProvider * Don't use argument.definition
- Loading branch information
Showing
23 changed files
with
1,069 additions
and
105 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
...ommonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/EmbeddedFieldsProvider.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,46 @@ | ||
package com.apollographql.apollo3.cache.normalized.api | ||
|
||
import com.apollographql.apollo3.annotations.ApolloExperimental | ||
import com.apollographql.apollo3.api.CompiledNamedType | ||
import com.apollographql.apollo3.api.InterfaceType | ||
import com.apollographql.apollo3.api.ObjectType | ||
|
||
@ApolloExperimental | ||
interface EmbeddedFieldsProvider { | ||
fun getEmbeddedFields(context: EmbeddedFieldsContext): List<String> | ||
} | ||
|
||
@ApolloExperimental | ||
class EmbeddedFieldsContext( | ||
val parentType: CompiledNamedType, | ||
) | ||
|
||
@ApolloExperimental | ||
object DefaultEmbeddedFieldsProvider : EmbeddedFieldsProvider { | ||
override fun getEmbeddedFields(context: EmbeddedFieldsContext): List<String> { | ||
return context.parentType.embeddedFields | ||
} | ||
} | ||
|
||
private val CompiledNamedType.embeddedFields: List<String> | ||
get() = when (this) { | ||
is ObjectType -> embeddedFields | ||
is InterfaceType -> embeddedFields | ||
else -> emptyList() | ||
} | ||
|
||
@ApolloExperimental | ||
class ConnectionEmbeddedFieldsProvider( | ||
connectionFields: Map<String, List<String>>, | ||
connectionTypes: Set<String>, | ||
) : EmbeddedFieldsProvider { | ||
companion object { | ||
private val connectionFieldsToEmbed = listOf("pageInfo", "edges") | ||
} | ||
|
||
private val embeddedFields = connectionFields + connectionTypes.associateWith { connectionFieldsToEmbed } | ||
|
||
override fun getEmbeddedFields(context: EmbeddedFieldsContext): List<String> { | ||
return embeddedFields[context.parentType.name].orEmpty() | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...rc/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/api/FieldNameGenerator.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,46 @@ | ||
package com.apollographql.apollo3.cache.normalized.api | ||
|
||
import com.apollographql.apollo3.annotations.ApolloExperimental | ||
import com.apollographql.apollo3.api.CompiledField | ||
import com.apollographql.apollo3.api.Executable | ||
|
||
@ApolloExperimental | ||
interface FieldNameGenerator { | ||
fun getFieldName(context: FieldNameContext): String | ||
} | ||
|
||
@ApolloExperimental | ||
class FieldNameContext( | ||
val parentType: String, | ||
val field: CompiledField, | ||
val variables: Executable.Variables, | ||
) | ||
|
||
@ApolloExperimental | ||
object DefaultFieldNameGenerator : FieldNameGenerator { | ||
override fun getFieldName(context: FieldNameContext): String { | ||
return context.field.nameWithArguments(context.variables) | ||
} | ||
} | ||
|
||
@ApolloExperimental | ||
class ConnectionFieldNameGenerator(private val connectionFields: Map<String, List<String>>) : FieldNameGenerator { | ||
companion object { | ||
private val paginationArguments = setOf("first", "last", "before", "after") | ||
} | ||
|
||
override fun getFieldName(context: FieldNameContext): String { | ||
return if (context.field.name in connectionFields[context.parentType].orEmpty()) { | ||
context.field.newBuilder() | ||
.arguments( | ||
context.field.arguments.filter { argument -> | ||
argument.name !in paginationArguments | ||
} | ||
) | ||
.build() | ||
.nameWithArguments(context.variables) | ||
} else { | ||
DefaultFieldNameGenerator.getFieldName(context) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.