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 ...
随机推荐
- Linux 命令详解(十三)如何启动、关闭和设置ubuntu防火墙
sudo ufw enable|disable 由于LInux原始的防火墙工具iptables过于繁琐,所以ubuntu默认提供了一个基于iptable之上的防火墙工具ufw. ubuntu 9.1 ...
- command not found
1.问题(problem) #wget gzip.x86_64 0:1.5-10.el7 -bash: wget command not found 2.解决(solution) 两个都能用 yum ...
- 【cpp】chap 8
1.输出“输入的内容” // basic file operations #include <iostream> #include <fstream> #include < ...
- Python3:关于列表的操作(合并、拼接,嵌套排序··)
一:# 将2个列表合并成字典,按最少个数key=['winnie','anna','lisa']value=[18,20,22] k_v=dict(zip(key,value))print(k_v) ...
- 使用node.js 脚手架搭建Vue项目
1.安装node.js https://nodejs.org/zh-cn/ 下载安装node.js 在命令行测试 node -v 输出版本号说明安装成功 2.使用npm更新安装cpnm npm ins ...
- HTML5商城开发五 实现返回页面顶部
本文内容主要是网上参考收集,介绍四种简单的返回页面顶部代码,可以使用简单的HTML锚标记,也可使用Javascript Scroll函数动态返回等等. 一.使用锚标记返回页面顶部 使用HTML锚标记最 ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- Spring扩展自定义的XML标签
在网上搜了许多,感觉不够全面,就找了官方文档,下面记录如何找到对应的文档进入 网上许多博客都是以dateformat为实例进行编写的,通过官方的foo,能够学到更多的东西,下面贴一段代码,在官方示例上 ...
- 代码中三种特殊注释——TODO、FIXME、XXX
在eclipse中,TODO.FIXME和XXX都会被eclipse的task视图所收集.在项目发布前,检查一下task视图是一个很好的习惯.进入window→show view→Other→输入ta ...
- 【原创】大数据基础之Zookeeper(3)选举算法
提到zookeeper选举算法,就不得不提Paxos算法,因为zookeeper选举算法是Paxos算法的一个变种: Paxos要解决的问题是:在一个分布式网络环境中有众多的参与者,但是每个参与者都不 ...