美文网首页
TypeGraphql 尝试

TypeGraphql 尝试

作者: 盐酸洛美沙星 | 来源:发表于2020-12-08 17:40 被阅读0次

TypeGraphql 官方文档示例练习:

import {
  ObjectType,
  Field,
  ID,
  Int,
  Resolver,
  Query,
  Args,
  ArgsType,
  InputType,
  Mutation,
  Arg,
  FieldResolver,
  Root
} from 'type-graphql'
import { Min, Max } from 'class-validator'

// 定义类型和字段
@ObjectType({ description: 'test' })
class Recipe {
  @Field(() => ID, { nullable: true })
  id?: string

  @Field()
  title: string

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

  @Field()
  ratings?: number[]

  @Field()
  averageRating?: number
}

// 定义 Mutation 用到的参数类型
@InputType()
class AddRecipeInput implements Partial<Recipe> {
  @Field()
  title: string

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

// 定义一个 Query 用到的参数类型
@ArgsType()
class GetRecipesArgs {
  // 使用defaultValue,TypeGraphql会设置默认值并使该字段可为空
  @Field(() => Int, { defaultValue: 0 })
  @Min(0)
  skip: number

  @Field(() => Int)
  @Min(1)
  @Max(50)
  take = 25 // 属性初始化,和 defaultValue 的效果一样

  @Field({ nullable: true })
  title?: string

  // 可以在 args 中自定义辅助方法
  get startIndex(): number {
    return this.skip
  }

  get endIndex(): number {
    return this.skip + this.take
  }
}

// 创建一个解析器
@Resolver()
class RecipeResolver {
  // 使用依赖注入,将服务器/存储库/数据存储在resolver类的内部,保证每个应用都由唯一的实例
  private recipesCollection: Recipe[] = []

  // 创建一个 Query
  @Query(() => [Recipe])
  async recipes(
    @Args()
    { title, startIndex, endIndex }: GetRecipesArgs
  ): Promise<Recipe[]> {
    let recipes = this.recipesCollection
    if (title) {
      recipes = recipes.filter((recipe) => recipe.title === title)
    }
    return recipes.slice(startIndex, endIndex)
  }

  // 创建一个 Mutation
  @Mutation()
  addRecipe(
    @Arg('data')
    newRecipeData: AddRecipeInput
    // @Ctx()
    // ctx: Context
  ): Recipe[] {
    this.recipesCollection.push(newRecipeData)
    return this.recipesCollection
  }

  // 创建一个字段解析器
  @FieldResolver()
  averageRating(
    @Root()
    recipe: Recipe
  ) {
    const ratingsSum =
      recipe?.ratings?.reduce((prev, next) => prev + next, 0) || 0
    return recipe?.ratings?.length ? ratingsSum / recipe.ratings.length : null
  }
}

相关文章

  • TypeGraphql 尝试

    TypeGraphql 官方文档示例练习:

  • TypeGraphql的引入

    安装 安装主包 安装reflect-metadata,用来做类型反射 注意: 我们必须确保该包在我们使用/导入 t...

  • NestJS + TypeGraphQL + MySQL 从入门

    NestJS + TypeGraphQL + MySQL 从入门到实战视频教程(25 个视频) #1 Nestjs...

  • NestJS + TypeGraphQL + MySQL 从入门

    NestJS + TypeGraphQL + MySQL 从入门到实战视频教程(35 个视频) #1 Nestjs...

  • 2020 最新前后端编程学习视频

    2020 最新前后端编程学习视频 NestJS + TypeGraphQL + MySQL 从入门到实战视频教程(...

  • 2018-12-12 TypeGraphQL

    https://19majkel94.github.io/type-graphql/docs/introducti...

  • 尝试尝试~

    text

  • 尝试尝试

    颈椎病痛的实在厉害,打了封闭针也一点作用都不起!只好老老实实接受医生的深度“摆布”。 熏蒸是这次治疗“最享受”的一...

  • 尝试尝试

    别害怕,去尝试;别灰心,再去尝试;随缘,别后悔。 选择取决于自己的见识,背后其实都有自己强烈的意愿,后悔也无济于事...

  • 尝试再尝试

    梅心❤梅肺感恩百日行——55 我今天又进步了,不但清理了厨房,还炒了土豆丝和洋葱炒鸡蛋,给两个宝贝热了牛奶。和两个...

网友评论

      本文标题:TypeGraphql 尝试

      本文链接:https://www.haomeiwen.com/subject/tzrqgktx.html