Nestjs Graphql
安装依赖:
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的更多相关文章
- 基于 GraphQL 的 BFF 实践
随着软件工程的发展,系统架构越来越复杂,分层越来越多,分工也越来越细化.我们知道,互联网是离用户最近的行业,前端页面可以说无时无刻不在变化.前端本质上还是用户交互和数据展示,页面的高频变化意味着对数据 ...
- GraphQL介绍&使用nestjs构建GraphQL查询服务
GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...
- 基于typescript 强大的 nestjs 框架试用
nestjs 一个nodejs 的graphql 框架 安装 npm i -g @nestjs/cli 初始化项目 nest new dalong 运行demo 使用yarn yarn start 添 ...
- Facebook的Web开发三板斧:React.js、Relay和GraphQL
2015-02-26 孙镜涛 InfoQ Eric Florenzano最近在自己的博客上发表了一篇题为<Facebook教我们如何构建网站>的文章,他认为软件开发有些时候需要比较大的跨 ...
- facebook graphql
思想先进,前端直接从后台调用所需要的数据. 最简单的理解: 从"select * from 学生表" 进化为"select name, sex from 学生表" ...
- Graphql介绍(Introduction to GraphQL)
Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this seri ...
- graphql 新API 开发方式
我们知道 GraphQL 使用 Schema 来描述数据,并通过制定和实现 GraphQL 规范 定义了支持 Schema 查询的 DSQL (Domain Specific Query Langua ...
- [GraphQL] Use GraphQLNonNull for Required Fields
While certain fields in a GraphQL Schema can be optional, there are some fields or arguments that ar ...
- [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 ...
随机推荐
- ACM-ICPC 2018 徐州赛区网络预赛 C Cacti Lottery(期望+暴力)
https://nanti.jisuanke.com/t/31455 题意 给一个3*3的方格填入 1-9 九个数 有些数是已知的,有些数是对方已知但我未知的,有些数是大家都未知的 我要计算取得最大的 ...
- ES6 Class语法学习
前言 大多数面向对象的编程语言都支持类和类继承的特性,而JS却不支持这些特性,只能通过其他方法定义并关联多个相似的对象,这种状态一直延续到了ES5.由于类似的库层出不穷,最终还是在ECMAScript ...
- [再寄小读者之数学篇](2014-05-28 Ladyzhenskaya 不等式)
$$\bex f\in C_c^\infty(\bbR^2)\ra \sen{f}_{L^4}\leq \sqrt{2} \sen{f}_{L^2}^{1/2} \sen{\p_1f}_{L^2}^{ ...
- 使用VS的生成事件命令行指令将生成的exe,dll文件复制到指定文件夹中
VS预生成事件命令行 和 生成后事件命令行 宏说明 $(ConfigurationName) 当前项目配置的名称(例如,“Debug|Any CPU”). $(OutDir) ...
- Codeforces 1088F(贪心+倍增)
题目链接 题意 构造一颗树使得满足计算方法的结果最小. 思路 考虑两棵树,一棵为题目中的询问构成的树$T1$,一棵为要构造的满足最终答案的树$T2$.从$T1$点权最小的点向外构造$T2$,在$T1$ ...
- 分别基于TensorFlow、PyTorch、Keras的深度学习动手练习项目
×下面资源个人全都跑了一遍,不会出现仅是字符而无法运行的状况,运行环境: Geoffrey Hinton在多次访谈中讲到深度学习研究人员不要仅仅只停留在理论上,要多编程.个人在学习中也体会到单单的看理 ...
- Centos7 设置vim 显示文本不同颜色
Centos7 设置vim 显示文本不同颜色 本人在查找设置 centos7 vim 文本显示颜色时, 学习了作者: luffy5459 (博客连接:https://blog.csdn.net/fei ...
- Python 爬虫 NewCnblogs (爬虫-Django-数据分析)
需求分析 数据库架构 注册 登录 首页 个人站点 文章+评论 后台 爬虫 数据分析 添加搜索+已上线
- Token令牌管理权限
什么是token HTTP是一种无状态的协议,也就是HTTP没法保存客户端的信息,没办法区分每次请求的不同. Token是服务器生成的一串字符,作为客户端请求的令牌.当第一次登陆后,服务器会分发Ton ...
- Thymleaf js直接获取后台传过来的对象或者对象的属性以及map
简单说明:第一次接触thymleaf模板,对于thymleaf在js中如何获取后台传递过来的值,真的挺简单的,记住就行了 代码: 后台代码: //传递一个org对象给jspublic String t ...