Skip to main content

TypeGraphQL

From https://typegraphql.com/

@ObjectType()
export class Rate {
@Field((type) => Int)
value: number;
}

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

@Field({ nullable: true })
description?: string;

@Field((type) => [Float])
ratings: number[];

@Field((type) => Float, { nullable: true })
get averageRating() {
const sum = this.ratings.reduce((a, b) => a + b, 0);
return sum / this.ratings.length;
}
}

Grats

import { Float, Int } from "grats";

/** @gqlType */
class Recipe {
/** @gqlField */
title: string;

/** @gqlField */
description?: string;

/** @gqlField */
ratings: Float[];

/** @gqlField */
get averageRating(): Float | null {
const sum = this.ratings.reduce((a, b) => a + b, 0);
return sum / this.ratings.length;
}
}
Playground