resolveField

open override fun resolveField(context: ResolverContext): 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 a GraphQL server

  • used before a network request

  • used when reading the cache

It can be used to map field arguments to CacheKey:

{
user(id: "1"}) {
id
firstName
lastName
}
}
override fun resolveField(context: ResolverContext): Any? {
val id = context.field.resolveArgument("id", context.variables)?.toString()
if (id != null) {
return CacheKey(id)
}

return super.resolveField(context)
}

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(context: ResolverContext): Any? {
if (context.field.name == "name") {
// Every "name" field will return "JohnDoe" now!
return "JohnDoe"
}

return super.resolveField(context)
}

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. The value can be wrapped in a ResolvedValue to provide additional information.

Parameters

context

the field to resolve and associated information to resolve it