@Resolvers()
decorator along with @Query()
, @Mutation()
, and other decorators make it easy to create type-safe GraphQL APIs with minimal boilerplate.
Creating a Resolvers Controller
A resolvers controller is a class decorated with@Resolvers()
that contains methods decorated with @Query()
or @Mutation()
to define GraphQL operations:
Query Resolvers
Use the@Query()
decorator with the createQuery()
function to define GraphQL query operations:
Query Decorator Options
The@Query()
decorator accepts the following options:
Mutation Resolvers
Use the@Mutation()
decorator with the createMutation()
function to define GraphQL mutation operations:
Mutation Decorator Options
The@Mutation()
decorator accepts the same options as @Query()
:
Defining Parameters
Define parameters using theschemaWithName
function:
Nested Parameters
You can create complex parameter structures:Defining Return Types
Specify the return type in thereturns
property of createQuery
or createMutation
:
Middleware
You can add middleware to your resolvers using the@UseMiddleware()
decorator:
Middleware Chaining
You can apply multiple middlewares to a resolver:Model Resolvers
For defining field resolvers on specific GraphQL types, see the Model Resolvers documentation.Subscription Resolvers
For real-time functionality, use the@Subscriptions()
and @Subscription()
decorators:
Error Handling
Orionjs automatically handles errors in resolvers and formats them appropriately:Context and Viewer
All resolver methods receive the viewer object as the second parameter, which contains the authenticated user and other context information:GraphQL Info
You can access the GraphQL info object to optimize your queries:Starting GraphQL Server
To start the GraphQL server:Best Practices
- Organize by Domain: Group related resolvers in the same controller class.
-
Leverage Dependency Injection: Use
@Inject(() => Service)
to access services. - Keep Resolvers Focused: Each resolver method should handle one specific GraphQL operation.
-
Use Strong Typing: Define parameter and return types using schema and
InferSchemaType
. - Validate Input: The schema system automatically validates input.
- Implement Authorization: Use middleware for authentication and authorization.
- Handle Errors Gracefully: Catch and handle errors appropriately.
- Optimize Queries: Use the info parameter to optimize database queries.
- Document Your API: Add descriptions to your schemas and resolvers.
- Test Your Resolvers: Write unit tests for your resolver logic.