viaduct.service.api) provides the primary interfaces for service engineers who integrate Viaduct into their organization’s tech stack. This API defines how to execute GraphQL operations, configure schemas, and integrate with infrastructure like observability and security.
Overview
The Service API is the boundary between your web server (or service layer) and the Viaduct GraphQL runtime. Key responsibilities:- Execute GraphQL operations via the
Viaductinterface - Configure execution input (query text, variables, request context)
- Handle execution results (data, errors, extensions)
- Manage schema variants and scopes
Core Interfaces
Viaduct
The main entry point for executing GraphQL operations against the Viaduct runtime.viaduct.service.api
Stability: Stable
Creation:
Instances are created via:
ViaductBuilder- For full SPI control and advanced configurationBasicViaductFactory- For simpler use cases with sensible defaults
Viaduct instance at startup and routes incoming GraphQL requests through its execute methods.
Methods
Executes a GraphQL operation asynchronously.Parameters:
executionInput: ExecutionInput- The execution input for this operationschemaId: SchemaId- The schema variant to execute against (defaults toSchemaId.Full)
CompletableFuture<ExecutionResult> with errors sorted by path then by messageExample:Executes a GraphQL operation synchronously.Parameters:
executionInput: ExecutionInput- The execution input for this operationschemaId: SchemaId- The schema variant to execute against (defaults toSchemaId.Full)
ExecutionResult with errors sorted by path then by messageExample:Returns the set of scope IDs applied to the given schema, or null if no scopes are configured.Parameters:
schemaId: SchemaId- The schema whose applied scopes to retrieve
ExecutionInput
Encapsulates the parameters necessary to execute a GraphQL operation.viaduct.service.api
Stability: Stable
Properties
Text of an executable GraphQL document as defined in the GraphQL specification.Example:
Name of the operation in
operationText to execute. May be null if operationText contains only one operation.A unique ID for the operation, used for instrumentation and caching. Auto-generated from operation text if not provided.
Values for variables defined by the operation. Defaults to empty map.
Unique request identifier for this execution. Auto-generated UUID if not provided.
Deployment-specific request context established by service engineers. Available to application code through execution contexts.Usage: Store authentication, authorization, tracing, or other request-scoped data. While dependency injection is preferred, this provides a simpler alternative.Example:
Builder Pattern
Creates a new Builder instance for constructing ExecutionInput objects.Example:
Convenience factory method for simple cases.Parameters:
operationText: String(required)operationName: String?(optional)variables: Map<String, Any?>(defaults to empty)requestContext: Any?(optional)
ExecutionResult
The result of executing a GraphQL operation through Viaduct.viaduct.service.api
Stability: Stable
Important: This is a Viaduct-specific wrapper that does not expose GraphQL Java types directly.
Data and Errors Relationship
- If
getData()returnsnull,errorswill contain at least one error explaining why - If
getData()returns data,errorsmay still contain errors for fields that failed (partial results)
Partial Results
GraphQL supports partial results (GraphQL Spec §6.4.4): if a nullable field encounters an error, it returnsnull but execution continues. The error is recorded in errors while other fields retain their data. Only errors in non-nullable fields cause null to bubble up to parent fields.
Methods and Properties
Returns the data from the GraphQL execution.The structure is Returns:
Map<String, Any?> where keys are top-level field names from the query.Nested value mappings:- GraphQL Objects →
Map<String, Any?> - GraphQL Lists →
List<Any?> - GraphQL Scalars → String, Int, Boolean, Double, etc.
- GraphQL Nulls →
null
null if execution failed at the root levelExample:
Given query:Errors that occurred during execution, sorted by path and then by message.This list is non-empty when:
getData()returnsnull(at least one error will explain why)- Partial results occurred (nullable fields that encountered errors)
- Validation or parsing failed
Additional execution metadata as key-value pairs. Use for custom metrics, tracing data, or other metadata.
Converts this result to the standard GraphQL specification format. This produces a map suitable for JSON serialization in HTTP responses.Example:
GraphQLError
Viaduct’s representation of a GraphQL error.viaduct.service.api
Stability: Stable
The error message to display to clients
The execution path where the error occurred (e.g.,
["user", "profile", "name"])Source locations in the GraphQL query where the field was requested
Additional error metadata (e.g., error codes, localized messages, custom fields)
SchemaId
Identifies which schema variant to use when executing a GraphQL operation.viaduct.service.api
Stability: Stable
Viaduct supports multiple schema variants for a single service:
The default, complete schema containing all types and fields.Example:
A subset of the full schema restricted by a set of scope IDs. Useful for multi-tenancy or permission-based field visibility.Properties:
id: String- The schema IDscopeIds: Set<String>- The set of scope IDs the schema is scoped to
Represents a non-existent schema, used as a sentinel value.
Integration Example
Here’s a complete example of integrating Viaduct into a REST controller:See Also
- Tenant API - Interfaces for implementing resolvers
- Engine API - Core execution engine
- Application Plugin - Gradle plugin for Viaduct applications