Skip to main content
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.

Structure and Naming

  • Use @Resolvers() decorator from @orion-js/graphql
  • Each resolver class should handle only one operation
  • Follow naming conventions:
    • Resolver classes: {Action}{Entity}Resolvers (e.g., GetUserResolvers, UpdateProductResolvers)
    • Mutations: descriptive of the operation (e.g., listProducts, createUser)
    • Queries: only the name of the entity (e.g., user, productsList, blogPosts)
  • Put resolvers in app/{component}/controllers/resolvers/{Entity}/{Action}/index.ts

File Organization

The resolvers folder should have an index.ts that exports all resolvers as a single array:
The controllers/resolvers/index.ts file:

Best Practices

  • Use @Inject(() => ServiceName) for dependency injection of services
  • Always define proper schemas for parameters and return values
  • Use createQuery(), createMutation() and createModelResolver() functions
  • Provide descriptive names and descriptions for GraphQL schema documentation
  • Always use schemaWithName() for schemas that will be used in GraphQL
  • When creating a schema for params, prefer to clone another schema with cloneSchema()

Query Example

Mutation with cloneSchema

Use cloneSchema to create parameter schemas based on existing schemas:

Simple Mutation with cloneSchema

Mutation Using a Service

Model Resolver

Paginated Query

Complex cloneSchema for Params

Subscription Resolvers

For real-time functionality:

Error Handling

Orionjs automatically handles errors in resolvers:

Context and Viewer

All resolver methods receive the viewer object as the second parameter:

Starting GraphQL Server

Query vs Mutation

  • Use @Query() for operations that fetch data without side effects
  • Use @Mutation() for operations that create, update, or delete data