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 ...
随机推荐
- python 错误捕获机制分析
python语言是编程中使用率在Top 3之内的语言.python语言以灵活与简单著称,那么越是灵活的语言越需要判断出错的功力. 简单示例 以下是一个简单的错误程序,被除数不可为0,那么看看该代码的执 ...
- JS中小数相加相减时出现很长的小数点的解决方式
1.问题: 平时写的代码中会出现这种情况,parseFloat(11.3-10.1) 运行的结果依然是1.200000000000001 代码示例: var arr = [0.0111,11.002, ...
- SecureCRT的安装与破解(过程很详细!!!)
SecureCRT的安装与破解(过程很详细!!!) 使用SecureCRT可以方便用户在windows环境下对linux主机进行管理,这里为大家讲一下SecureCRT的破解方法,仅供大家参考学习: ...
- 第三周 数据分析之概要 Pandas库数据特征分析
数据的排序: 数据的基本统计分析 : 数据的累计统计分析: 数据的相关分析: 单元小结
- CIA402状态转换图
CIA402状态转换如下图所示: 要想改变参数并使其生效,需要先将状态转换到ready,然后修改要配置的参数,再使其运行(operation enabled). 要发送的报文顺序基本如下: 1) ...
- Solidity属性和方法的访问权限
属性:默认是internal的类型,外部是不可以访问调用的,如果加上public的话,那么是会自动为这个属性加上一个get的方法的,比如uint public _age; => functi ...
- 小程序 第一个学习示例(TodoList)
1. 概述 1.1 说明 在微信开发者工具环境下开发一个简易的TodoList功能,以便能够进行学习与熟练小程序相关功能与信息.. 示例中,初步计划包含以下功能: 1.能够进行新增计划信息 2.计划信 ...
- Python 爬虫 JD商品-scrapy+requests
目标站点需求分析 JD商品信息抓取 需求信息字段 涉及的库 scrapy, requests,re lxml 获取单页源码 解析单页源码 获取总页数 获取商品url 解析商品信息 保存本地文件 保存m ...
- 《剑指offer》把数组排成最小的数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- 办公用品管理系统VB——模块
'DbFunc.bas'== 标记数据库是否连接 == Private IsConnect As Boolean '== 标记执行Connect()函数后,访问数据库的次数 == Private Co ...