Skip to content

Node Resolvers

Schema

Nodes are types that are resolvable by ID and implement the Node interface. A node type gets a corresponding node resolver only when it also declares a type-level @resolver.

interface Node {
  id: ID!
}

type User implements Node @resolver {
  id: ID!
  firstName: String
  lastName: String
  displayName: String @resolver
}

If a type implements Node but omits the type-level @resolver, Viaduct does not generate a node resolver base class for it.

Generated base class

Viaduct generates an abstract base class for object types that both implement Node and declare @resolver. For the User example above, Viaduct generates the following code:

object NodeResolvers {
  abstract class User {
    abstract suspend fun resolve(ctx: Context): viaduct.api.grts.User

    class Context: NodeExecutionContext<viaduct.api.grts.User>
  }

  // If there were more nodes, their base classes would be generated here
}

The nested Context class is described in more detail below.

The schema's batching setting determines which resolver method is generated. With @resolver(isBatching: true), the generated User base class declares batchResolve instead of resolve:

abstract suspend fun batchResolve(
  contexts: List<Context>
): Map<Context, FieldValue<viaduct.api.grts.User>>

Implementation

Implement a node resolver by subclassing the generated base class, annotating the class with @Resolver, and overriding its generated resolve or batchResolve method.

Here's an example of a non-batching resolver for User that calls a user service to get data for a single user:

@Resolver
class UserNodeResolver @Inject constructor(
  val userService: UserServiceClient
): NodeResolvers.User() {
  override suspend fun resolve(ctx: Context): User {
    // Fetches data for a single User ID
    val data = userService.fetch(ctx.id.internalID)
    return User.Builder(ctx)
      .id(ctx.id)
      .firstName(data.firstName)
      .lastName(data.lastName)
      .build()
  }
}

Points illustrated by this example:

  • Dependency injection can be used to provide access to values beyond what’s in the execution context.
  • You should not provide values for fields outside the resolver's output selection set. In the example above, we do not set displayName when building the User GRT.

Alternatively, if the user service provides a batch endpoint, you should implement a batch node resolver. Node resolvers typically implement batchResolve to avoid the N+1 problem. Learn more about batch resolution here.

Context

Both resolve and batchResolve take Context objects as input. For ordinary node resolvers this class is an instance of NodeExecutionContext:

interface NodeExecutionContext<R: NodeObject>: ResolverExecutionContext {
  val id: GlobalID<R>
}
For the example User type, the R type would be the User GRT.

NodeExecutionContext includes the ID of the node to be resolved. Most node resolvers are non-selective, which means they should return the same data for a given node ID regardless of what fields were requested.

Since NodeExecutionContext implements ResolverExecutionContext, it also includes the utilities provided there, which allow you to:

Non-Selective and Selective Node Resolvers

There are two primary categories of Node Resolver:

  1. Non-Selective Node Resolvers: Serve most use cases and are the default option. These resolvers always return the same data for a given node ID and benefit from higher cache hit rates.
  2. Selective Node Resolvers: Can vary the response data returned for a given node ID based on the fields requested by the caller. These resolvers are declared directly in SDL with @resolver(isSelective: true).

Selective node resolvers opt in through schema, not by implementing a marker interface:

type User implements Node @resolver(isSelective: true) {
  id: ID!
  firstName: String
  expensiveField: String
}

When a node is declared with @resolver(isSelective: true), the generated resolver Context implements SelectiveNodeExecutionContext and exposes ctx.selections():

@Resolver
class SelectiveUserNodeResolver @Inject constructor(
  val userService: UserServiceClient
): NodeResolvers.User() {
  override suspend fun resolve(ctx: Context): User {
    val sel = ctx.selections()
    return User.Builder(ctx)
      .id(ctx.id)
      .apply {
        // Conditionally fetch expensive fields based on what's requested
        if (sel.contains(User.expensiveField)) {
          expensiveField(fetchExpensiveData())
        }
      }
      .build()
  }
}

Non-selective node resolvers should not use ctx.selections(), and their generated Context does not expose that API. Field-level @resolver(isSelective: true) directives inside a node type do not change node resolver generation or make the node resolver selective.

Output selection set

The node resolver is responsible for resolving all fields, including nested fields, that do not have their own resolver. These are typically core fields that are stored together and can be efficiently retrieved together.

In the example above, the node resolver for User is responsible for returning the id, firstName, and lastName fields, but not the displayName field, which has its own resolver. Since the ID is an input to the node resolver, it can populate the field directly from ctx.id.

Node resolvers are also responsible for determining whether the node exists. An unbatched resolver should throw an exception when the requested node does not exist. A batch resolver should return a FieldValue error for the corresponding Context, as described in Batch Resolution.