0.1.8 • Published 3 years ago

@proteria/type-graphql v0.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

TypeGraphQL

Fork of MichalLytek/type-graphql.

Differences

Babel macro

import {
  Authorized,
  FieldResolver,
  ObjectType,
  Query,
  Resolver,
} from "@proteria/type-graphql/macro";

@ObjectType()
class Recipe {
  @Field()
  id: string;

  @Field()
  title: string;

  @Field()
  ratings: Rate[];

  @Field()
  averageRating?: number;
}

@Resolver(Recipe)
class RecipeResolver {
  constructor(private recipeService: RecipeService) {}

  @Query()
  recipes(): Promise<Recipe[]> {
    return this.recipeService.findAll();
  }

  @Mutation()
  @Authorized(Roles.Admin)
  removeRecipe(@Arg() id: string): boolean {
    return this.recipeService.removeById(id);
  }

  @FieldResolver()
  averageRating(@Root() recipe: Recipe) {
    return recipe.ratings.reduce((a, b) => a + b, 0) / recipe.ratings.length;
  }
}