Skip to content

Instantly share code, notes, and snippets.

@ddialar
Created July 3, 2019 18:47
Show Gist options
  • Save ddialar/949b7a148db882381d577720648bb73a to your computer and use it in GitHub Desktop.
Save ddialar/949b7a148db882381d577720648bb73a to your computer and use it in GitHub Desktop.
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;
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(),
},
]);
}
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