Creating GraphQL API resolvers in Orionjs
Orionjs provides a clean, decorator-based approach to define GraphQL resolvers. The @Resolvers()
decorator along with @Query()
, @Mutation()
, and other decorators make it easy to create type-safe GraphQL APIs with minimal boilerplate.
A resolvers controller is a class decorated with @Resolvers()
that contains methods decorated with @Query()
or @Mutation()
to define GraphQL operations:
Use the @Query()
decorator with the createQuery()
function to define GraphQL query operations:
The @Query()
decorator accepts the following options:
Use the @Mutation()
decorator with the createMutation()
function to define GraphQL mutation operations:
The @Mutation()
decorator accepts the same options as @Query()
:
Define parameters using the schemaWithName
function:
You can create complex parameter structures:
Specify the return type in the returns
property of createQuery
or createMutation
:
You can add middleware to your resolvers using the @UseMiddleware()
decorator:
You can apply multiple middlewares to a resolver:
For defining field resolvers on specific GraphQL types, see the Model Resolvers documentation.
For real-time functionality, use the @Subscriptions()
and @Subscription()
decorators:
Orionjs automatically handles errors in resolvers and formats them appropriately:
All resolver methods receive the viewer object as the second parameter, which contains the authenticated user and other context information:
You can access the GraphQL info object to optimize your queries:
To start the GraphQL server:
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.
Creating GraphQL API resolvers in Orionjs
Orionjs provides a clean, decorator-based approach to define GraphQL resolvers. The @Resolvers()
decorator along with @Query()
, @Mutation()
, and other decorators make it easy to create type-safe GraphQL APIs with minimal boilerplate.
A resolvers controller is a class decorated with @Resolvers()
that contains methods decorated with @Query()
or @Mutation()
to define GraphQL operations:
Use the @Query()
decorator with the createQuery()
function to define GraphQL query operations:
The @Query()
decorator accepts the following options:
Use the @Mutation()
decorator with the createMutation()
function to define GraphQL mutation operations:
The @Mutation()
decorator accepts the same options as @Query()
:
Define parameters using the schemaWithName
function:
You can create complex parameter structures:
Specify the return type in the returns
property of createQuery
or createMutation
:
You can add middleware to your resolvers using the @UseMiddleware()
decorator:
You can apply multiple middlewares to a resolver:
For defining field resolvers on specific GraphQL types, see the Model Resolvers documentation.
For real-time functionality, use the @Subscriptions()
and @Subscription()
decorators:
Orionjs automatically handles errors in resolvers and formats them appropriately:
All resolver methods receive the viewer object as the second parameter, which contains the authenticated user and other context information:
You can access the GraphQL info object to optimize your queries:
To start the GraphQL server:
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.