文档

工作示例

安装依赖:

npm i --save @nestjs/graphql apollo-server-express graphql-tools graphql

app.module.ts

import { Module } from '@nestjs/common';
import { AppService } from './app.service'; import { GraphQLModule } from '@nestjs/graphql';
import { AppResolver } from './app.resolvers'; @Module({
imports: [
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
}),
],
providers: [AppService, AppResolver],
})
export class AppModule {}

定义 typeDefs :

// app.graphql
type Query {
hello: String
findCat(id: ID): Cat
cats: [Cat]
} type Cat {
id: Int
name: String
age: Int
} type Mutation {
addCat(cat: InputCat): Cat
} input InputCat {
name: String
age: Int
}

定义 resolvers :

// app.resolvers.ts
import { ParseIntPipe } from '@nestjs/common';
import { Query, Resolver, Args, Mutation } from '@nestjs/graphql';
import { AppService } from './app.service'; @Resolver()
export class AppResolver {
constructor(private readonly appService: AppService) {} // query { hello }
@Query()
hello(): string {
return this.appService.hello();
} // query { findCat(id: 1) { name age } }
// 网络传输过来的id会是字符串类型,而不是number
@Query('findCat')
findOneCat(@Args('id', ParseIntPipe) id: number) {
return this.appService.findCat(id);
} // query { cats { id name age } }
@Query()
cats() {
return this.appService.findAll();
} // mutation { addCat(cat: {name: "ajanuw", age: 12}) { id name age } }
@Mutation()
addCat(@Args('cat') args) {
console.log(args);
return this.appService.addCat(args)
}
}

启动服务器,访问 http://localhost:5000/graphql

// 发送
query { hello } // 返回
{
"data": {
"hello": "hello nest.js"
}
}

app.service.ts

import { Injectable } from '@nestjs/common';
import { Cat } from './graphql.schema'; @Injectable()
export class AppService {
private readonly cats: Cat[] = [
{ id: 1, name: 'a', age: 1 },
{ id: 2, name: 'b', age: 2 },
];
hello(): string {
return 'Hello World!';
} findCat(id: number): Cat {
return this.cats.find(c => c.id === id);
} findAll(): Cat[] {
return this.cats;
} addCat(cat: Cat): Cat {
const newCat = { id: this.cats.length + 1, ...cat };
console.log(newCat);
this.cats.push(newCat);
return newCat;
}
}

graphql.schema.ts

export class Cat {
id: number;
name: string;
age: number;
}

Nestjs Graphql的更多相关文章

  1. 基于 GraphQL 的 BFF 实践

    随着软件工程的发展,系统架构越来越复杂,分层越来越多,分工也越来越细化.我们知道,互联网是离用户最近的行业,前端页面可以说无时无刻不在变化.前端本质上还是用户交互和数据展示,页面的高频变化意味着对数据 ...

  2. GraphQL介绍&使用nestjs构建GraphQL查询服务

    GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...

  3. 基于typescript 强大的 nestjs 框架试用

    nestjs 一个nodejs 的graphql 框架 安装 npm i -g @nestjs/cli 初始化项目 nest new dalong 运行demo 使用yarn yarn start 添 ...

  4. Facebook的Web开发三板斧:React.js、Relay和GraphQL

    2015-02-26 孙镜涛  InfoQ Eric Florenzano最近在自己的博客上发表了一篇题为<Facebook教我们如何构建网站>的文章,他认为软件开发有些时候需要比较大的跨 ...

  5. facebook graphql

    思想先进,前端直接从后台调用所需要的数据. 最简单的理解: 从"select * from 学生表" 进化为"select name, sex from 学生表" ...

  6. Graphql介绍(Introduction to GraphQL)

    Introduction to GraphQL  GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this seri ...

  7. graphql 新API 开发方式

    我们知道 GraphQL 使用 Schema 来描述数据,并通过制定和实现 GraphQL 规范 定义了支持 Schema 查询的 DSQL (Domain Specific Query Langua ...

  8. [GraphQL] Use GraphQLNonNull for Required Fields

    While certain fields in a GraphQL Schema can be optional, there are some fields or arguments that ar ...

  9. [GraphQL] Use Arguments in a GraphQL Query

    In GraphQL, every field and nested object is able to take in arguments of varying types in order to ...

随机推荐

  1. Vim使用技巧汇总

    一 写在开头 1.1 本文内容 Vim使用技巧与学习资源汇总. 二 Vim学习资源 1. Vimtutor 2. Vim中文帮助(http://vimcdoc.sourceforge.net/doc/ ...

  2. Spark Java API 计算 Levenshtein 距离

    Spark Java API 计算 Levenshtein 距离 在上一篇文章中,完成了Spark开发环境的搭建,最终的目标是对用户昵称信息做聚类分析,找出违规的昵称.聚类分析需要一个距离,用来衡量两 ...

  3. [Tex学习笔记]小于等于一个常数乘以...

    偏微分的论文中常用: 小于等于一个常数乘以... 这个要怎么输入呢. 只要输入\lesssim 就能得到 $\lesssim$...哈哈. 以前知道, 但是忘记了. 现在又要用.

  4. [物理学与PDEs]第3章习题7 快、慢及Alfv\'en 特征速度的比较

    证明: 当 $H_1\neq 0$ 及 $H_2^2+H_3^2\neq 0$ 时, 快.慢及 Alfv\'en 特征速度 $C_f$, $C_s$ 及 $C_a$ 满足 $$\bex 0<C_ ...

  5. DUMP3 企业级电商项目

    [开发模式]controller - service(合法校验问题) - dao   反过来也没问题 用户模块 登录 注册 用户名验证(实时反馈前端) 忘记密码 重置密码 退出登录 更新用户信息 获取 ...

  6. git中利用rebase来压缩多次提交 ----- 原文:https://blog.csdn.net/itfootball/article/details/44154121

    之前我们用git merge –squash来将分支中多次提交合并到master后,只保留一次提交历史.但是有些提交到github远程仓库中的commit信息如何合并呢? 使用下面的命令,最后一个数字 ...

  7. JSR286portlet中使用Ajax的方法

    JSR286portlet支持Ajax 一个平常的portlet先,jsr286类型. /Train01/WebContent/WEB-INF/portlet.xml <?xml version ...

  8. 简单使用Markdown

    Markdown是一种纯文本格式的标记语言.通过简单的标记语法,它可以使普通文本内容具有一定的格式. 相比WYSIWYG编辑器 优点: 1.因为是纯文本,所以只要支持Markdown的地方都能获得一样 ...

  9. C++设计模式——模板方法模式

    模板方法模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对模板方法模式是这样说的:定义一个操作中的算法骨架,而将一些步骤延迟到子类中.TemplateMethod使得子类可以不 ...

  10. 【Hibernate】Unable to locate appropriate constructor on class原因分析

    通常我们喜欢将hql查询结果封装到POJO对象syntax:select new POJO(id,name) from POJO ; 这种封装需要POJO类提供对应构造器,POJO(id,name)构 ...