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 ...
随机推荐
- 2018-2019-2 《Java程序设计》第1周学习总结
# 20175319 2018-2019-2 <Java程序设计>第1周学习总结 ## 教材学习内容总结 第一周我根据老师提供的博客,下载和设置了各种需要的软件,并对这些软件进行初步的了解 ...
- CSS难点 为什么height设置100%会失效,分栏目等高布局(高度自适用布局)的实现方案
前言 相信在平时写CSS的时候大家都或多或少遇见过设置了height为百分比的时候发现不起作用.今天我们就来一探究竟 原因:父元素未设置具体高度,子元素设置height:100%是无效的. 现象以及方 ...
- 选择性搜索(SS)算法
一.目标检测和目标识别 目标识别(object recognition)是要指明一张图像中包含哪类目标.输入是图像,输出是图像中的目标属于的类别(class probability).目标检测是识别出 ...
- Highcharts开发图表
1.折线图 显示一个静态的折线图,显示如下数据 星期 温度 周一 9~14 周二 4~10 周三 1~7 周四 4~9 周五 5~11 周六 8~13 周天 7~10 新建demo1.html < ...
- Database学习 - mysql 视图/触发器/函数
- 2017-2018-2 20155303『网络对抗技术』Exp6:信息收集与漏洞扫描
2017-2018-2 20155303『网络对抗技术』 Exp6:信息收集与漏洞扫描 --------CONTENTS-------- 一.原理与实践说明 1.实践内容 2.基础问题 二.实践过程记 ...
- mysql官方测试库
sql语句优化时没有测试数据,oracle官方提供测试数据 https://dev.mysql.com/doc/employee/en/employees-installation.html 到 ht ...
- 【原创】大叔经验分享(27)linux服务器升级glibc故障恢复
redhat6系统默认安装的glibc-2.12,有的软件依赖的是glibc-2.14,这时需要升级glibc,下载安装 http://ftp.gnu.org/gnu/glibc/glibc-2.14 ...
- ie浏览器多开-----同时登陆多个账号
1.在电脑桌面右键 找到 新建快捷方式 在上图输入框中输入 "C:\Program Files\Internet Explorer\iexplore.exe" -noframeme ...
- file_get_contents函数偶尔报错的抑制显示
$result = @file_get_contents($url);可以使用@进行抑制file_get_contents()的报错 @是为了抑制错误显示,让用户看不到,提升用户体验.注意:只是抑制错 ...