import { Mutation, Query, Resolvers, createQuery, createMutation } from '@orion-js/graphql'
import { Inject } from '@orion-js/services'
import { schemaWithName, InferSchemaType } from '@orion-js/schema'
import { ExampleService } from '../services/ExampleService'
// Define schemas with schemaWithName
export const ExampleParams = schemaWithName('ExampleParams', {
exampleId: { type: String }
})
export const ExampleSchema = schemaWithName('ExampleSchema', {
_id: { type: String },
name: { type: String },
createdAt: { type: Date }
})
// Infer types from schemas
export type ExampleParamsType = InferSchemaType<typeof ExampleParams>
export type ExampleSchemaType = InferSchemaType<typeof ExampleSchema>
@Resolvers()
export default class ExampleResolvers {
@Inject(() => ExampleService)
private exampleService: ExampleService
@Query()
example = createQuery({
params: ExampleParams,
returns: ExampleSchema,
resolve: async (params: ExampleParamsType) => {
return await this.exampleService.getAExample(params.exampleId)
}
})
@Query()
examples = createQuery({
returns: [ExampleSchema],
resolve: async () => {
return await this.exampleService.getExamples()
}
})
@Mutation()
createExample = createMutation({
returns: String,
resolve: async () => {
await this.exampleService.makeExample()
return 'Created example'
}
})
}