非常教程

Nest参考手册

GRAPHQL

突变

在GraphQL中,为了修改服务器端数据,我们使用了突变(在这里阅读更多)a。官方的Apollo文档共享一个upvotePost()变异示例。该突变允许增加后votes属性值。为了在Nest中创建等效变异,我们将使用@Mutation()装饰器。让我们AuthorResolver在上一节中扩展我们的用法(参见解析器)。

@Resolver('Author')
export class AuthorResolver {
  constructor(
    private readonly authorsService: AuthorsService,
    private readonly postsService: PostsService,
  ) {}

  @Query('author')
  async getAuthor(@Args('id') id: number) {
    return await this.authorsService.findOneById(id);
  }

  @Mutation()
  async upvotePost(@Args('postId') postId: number) {
    return await this.postsService.upvoteById({ id: postId });
  }

  @ResolveProperty('posts')
  async getPosts(@Parent() { id }) {
    return await this.postsService.findAll({ authorId: id });
  }
}

请注意,我们假设业务逻辑已移至PostsService(分别查询post和incrementing votes属性)。

类型定义

最后一步是将我们的变异添加到现有的类型定义中。

type Author {
  id: Int!
  firstName: String
  lastName: String
  posts: [Post]
}

type Post {
  id: Int!
  title: String
  votes: Int
}

type Query {
  author(id: Int!): Author
}

type Mutation {
  upvotePost(postId: Int!): Post
}

upvotePost(postId: Int!): Post现在的突变应该可用。

Nest

Nest 是一个用于构建高效,可扩展的 Node.js 服务器端应用程序的框架

Nest目录

1.介绍
2.常见问题
3.CLI
4.方法
5.执行上下文
6.微服务
7.WEBSOCKETS
8.GRAPHQL
9.技术
10.基本内容
11.迁移指南