Skip to content

Instantly share code, notes, and snippets.

@elie222
Last active August 18, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elie222/789fb32ad5ed795c40bfcb74973f0c35 to your computer and use it in GitHub Desktop.
Save elie222/789fb32ad5ed795c40bfcb74973f0c35 to your computer and use it in GitHub Desktop.
import { Field, ObjectType, ID } from 'type-graphql'
import {
Entity,
ObjectIdColumn,
ObjectID,
Column,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm'
@ObjectType()
@Entity()
export default class Bike {
@Field(type => ID)
@ObjectIdColumn()
_id: ObjectID
@Field()
@CreateDateColumn({ type: 'timestamp' })
createdAt: Date
@Field({ nullable: true })
@UpdateDateColumn({ type: 'timestamp' })
updatedAt?: Date
@Field()
@Column()
name: string
@Field({ nullable: true })
@Column({ nullable: true })
icon?: string
}
import { Service } from 'typedi'
import { Arg, Resolver, Query, Mutation, InputType, Field, Ctx, Authorized, ID } from 'type-graphql'
import { Context } from '../common/context'
import Bike from './BikeEntity'
import { BikeService } from './BikeService'
import { Role } from '../user/consts'
import { GraphQLUpload, FileUpload } from 'graphql-upload'
import { uploadImageStream } from '../common/cloudinary'
import { Stream } from 'stream'
@InputType()
class CreateBikeInput implements Partial<Bike> {
@Field()
name: string
@Field({ nullable: true })
icon?: string
}
@InputType()
class UpdateBikeInput implements Partial<Bike> {
@Field()
name?: string
@Field({ nullable: true })
icon?: string
}
@Service()
@Resolver(Bike)
export default class BikeResolver {
constructor(private readonly service: BikeService) {}
@Query(returns => Bike, { nullable: true })
async Bike(@Arg('_id', type => ID) _id: string) {
const doc = await this.service.findOne(_id)
return doc
}
@Query(returns => [Bike])
async allBikes() {
const all = await this.service.find()
return all
}
@Authorized(Role.Admin)
@Mutation(returns => Bike)
async createBike(
@Arg('data', type => CreateBikeInput) data: CreateBikeInput,
@Ctx() ctx: Context
) {
const res = await this.service.create(data)
return res
}
@Authorized(Role.Admin)
@Mutation(returns => Bike)
async updateBike(
@Arg('_id', type => String) _id: string,
@Arg('data', type => UpdateBikeInput) data: UpdateBikeInput,
@Ctx() ctx: Context
) {
const res = await this.service.update(_id, data)
return this.service.findOne(_id)
}
@Authorized(Role.Admin)
@Mutation(returns => Boolean)
async deleteBike(@Arg('_id', type => String) _id: string, @Ctx() ctx: Context) {
await this.service.remove(_id)
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment