# Resolver

@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Resolver(val objectValueFragment: String = "", val queryValueFragment: String = "", val variables: Array<Variable> = [])(source)

Annotation to mark a class as a resolver for a field or object in a GraphQL schema which requires custom resolution logic rather than simple property access.

Applied to fields that need runtime computation, database lookups, or complex business logic.

The @resolver directive supports two types of fragment syntax for efficient field resolution:

#### 1. Shorthand Fragment Syntax

    @Resolver("fieldName")
    class MyFieldResolver {
      override suspend fun resolve(ctx: Context): String {
        // Automatically delegates to the specified field
        return ctx.objectValue.getFieldName()
      }
    }

Content copied to clipboard

Use cases:

- Creating field aliases

- Simple field transformations

- Computed fields based on single existing fields

#### 2. Full Fragment Syntax

    @Resolver(
      """
        fragment _ on MyType {
            field1
            field2
            field3
        }
        """
    )
    class MyComputedFieldResolver {
      override suspend fun resolve(ctx: Context): String {
        val obj = ctx.objectValue
        // Can access all specified fields
        return "${obj.getField1()} - ${obj.getField2()} (${obj.getField3()})"
      }
    }

Content copied to clipboard

Use cases:

- Computed fields requiring multiple source fields

- Performance optimization by specifying exact field requirements

- Complex business logic combining multiple attributes

#### 3. Batch Resolver Fragment Syntax

    @Resolver(objectValueFragment = "fragment _ on Character { id name }")
    class CharacterBatchResolver : CharacterResolvers.SomeField() {
      override suspend fun batchResolve(contexts: List<Context>): List<FieldValue<String>> {
        // Process multiple contexts efficiently in one batch
        return contexts.map { ctx ->
          val character = ctx.objectValue
          FieldValue.ofValue("${character.getName()} processed in batch")
        }
      }
    }

Content copied to clipboard

Use cases:

- Preventing N+1 query problems

- Efficient batch processing of multiple field requests

- Performance optimization for list queries

Example:

    type Character {
      name: String @resolver
      homeworld: Planet @resolver      # Standard resolver (or batch resolver)
      displayName: String @resolver    # Shorthand fragment: @Resolver("name")
      summary: String @resolver        # Full fragment: @Resolver("fragment _ on Character { name birthYear }")
      filmCount: Int @resolver         # Batch resolver for efficient counting
      richSummary: String @resolver    # Batch resolver with complex logic
    }

Content copied to clipboard

## Properties

objectValueFragment
Link copied to clipboard
val objectValueFragment: String

This property is a GraphQL fragment that describes the selection set that must be fetched from the object type that contains the field being resolved. This fragment may include arguments to fields, which will be passed to the data fetchers of those fields.

queryValueFragment
Link copied to clipboard
val queryValueFragment: String

This property is a GraphQL fragment that describes the selection set that must be fetched from the root query type. This fragment may include arguments to fields, which will be passed to the data fetchers of those fields.

variables
Link copied to clipboard
val variables: Array<Variable>

This property is an array of Variable annotations that describe how to extract values from the graphql.schema.DataFetchingEnvironment and bind them to variables used in the fragments.