resolveField
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