Created
July 3, 2019 18:47
-
-
Save ddialar/949b7a148db882381d577720648bb73a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logger from '@logger'; | |
import { | |
Resolver, | |
Query, | |
Arg | |
} from 'type-graphql'; | |
import Recipe from './recipe-type'; | |
import { createRecipeSamples } from './recipe-samples'; | |
@Resolver(of => Recipe) | |
class RecipeResolver { | |
private readonly items: Recipe[] = createRecipeSamples(); | |
@Query(returns => Recipe, { nullable: true }) | |
async recipe(@Arg('title') title: string): Promise<Recipe | undefined> { | |
return await this.items.find(recipe => recipe.title === title); | |
} | |
@Query(returns => [Recipe], { description: 'Get all recipes.' }) | |
async recipes(): Promise<Recipe[]> { | |
logger.info('>>> Returning recipes...'); | |
logger.trace('>>> Returning recipes...'); | |
return await this.items; | |
} | |
} | |
export default RecipeResolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { plainToClass } from 'class-transformer'; | |
import Recipe from './recipe-type'; | |
export function createRecipeSamples() { | |
return plainToClass(Recipe, [ | |
{ | |
description: 'Desc 1', | |
title: 'Recipe 1', | |
ratings: [0, 3, 1], | |
creationDate: new Date('2018-04-11'), | |
}, | |
{ | |
description: 'Desc 2', | |
title: 'Recipe 2', | |
ratings: [4, 2, 3, 1], | |
creationDate: new Date('2018-04-15'), | |
}, | |
{ | |
description: 'Desc 3', | |
title: 'Recipe 3', | |
ratings: [5, 4], | |
creationDate: new Date(), | |
}, | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Field, | |
ObjectType, | |
Int | |
} from 'type-graphql'; | |
@ObjectType() | |
class Recipe { | |
@Field() | |
title: string = ""; | |
@Field({ nullable: true, description: 'The recipe description with preparation info.' }) | |
description?: string; | |
@Field(type => [Int]) | |
ratings: number[] = []; | |
@Field() | |
creationData: Date = new Date(); | |
} | |
export default Recipe; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment