captbaritone / grats

Implementation-First GraphQL for TypeScript

Home Page:https://grats.capt.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support get methods

captbaritone opened this issue · comments

import { Float as GqlFloat, Int as GqlInt } from "grats";
/** @gqlScalar */
type GqlDate = Date;

/** 
 * Object representing cooking recipe
 * @gqlType
 */
export class Recipe {
  /** @gqlField */
  title!: string;

  /** 
   * @gqlField
   * @deprecated Use 'description' field instead
   */
  get specification(): string | undefined {
    return this.description;
  }

  /** 
   * The recipe description with preparation info
   * @gqlField
   */
  description?: string;

  /** @gqlField */
  ratings!: GqlInt[];

  /** @gqlField */
  creationDate!: GqlDate;

  /** @gqlField */
  ratingsCount!: GqlInt;

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

    return ratingsSum / ratingsCount;
  }
}

Playground link