resolveField

override fun resolveField(    field: CompiledField,     variables: Executable.Variables,     parent: Map<String, @JvmSuppressWildcards Any?>,     parentId: String): Any?

Resolves a field from the cache. Called when reading from the cache, usually before a network request.

  • takes a GraphQL field and operation variables as input and generates data for this field

  • this data can be a CacheKey for objects but it can also be any other data if needed. In that respect, it's closer to a resolver as might be found in apollo-server

  • is used before a network request

  • is used when reading the cache

It can be used to map field arguments to CacheKey:

{
user(id: "1"}) {
id
firstName
lastName
}
}
override fun resolveField(field: CompiledField, variables: Executable.Variables, parent: Map<String, Any?>, parentId: String): Any? {
val id = field.resolveArgument("id", variables)?.toString()
if (id != null) {
return CacheKey(id)
}

return super.resolveField(field, variables, parent, parentId)
}

The simple example above isn't very representative as most of the time @fieldPolicy can express simple argument mappings in a more concise way but still demonstrates how resolveField works.

resolveField can also be generalized to return any value:

override fun resolveField(field: CompiledField, variables: Executable.Variables, parent: Map<String, Any?>, parentId: String): Any? {
if (field.name == "name") {
// Every "name" field will return "JohnDoe" now!
return "JohnDoe"
}

return super.resolveField(field, variables, parent, parentId)
}

See also @fieldPolicy See also CacheKeyGenerator

Return

a value that can go in a Record. No type checking is done. It is the responsibility of implementations to return the correct type

Parameters

field

the field to resolve

variables

the variables of the current operation

parent

the parent object as a map. It can contain the same values as Record. Especially, nested objects will be represented by CacheKey

parentId

the id of the parent. Mainly used for debugging