阿波罗服务器入门

λ yarn add --dev @babel/core @babel/cli @babel/preset-env
λ yarn add --dev nodemon // "start": "nodemon --exec babel-node index.js"
λ yarn add apollo-server graphql express
http://localhost:4000/?query={hello}
const { gql, ApolloServer } = require("apollo-server");
const _ = require("lodash");
const cats = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }];
const l = console.log;
const typeDefs = gql`
type Query {
cats: [Cat]
findCat(id: ID!): Cat
} type Mutation {
addCat(cat: InputCat): Cat
changeCat(id: ID!, name: String): Cat
} input InputCat {
name: String
} type Cat {
id: String
name: String
}
`; const resolvers = {
Query: {
cats: (parent, args, context, info) => {
return cats;
}, findCat(parent, args, context, info) {
const { id } = args;
return _.find(cats, { id: +id });
},
},
Mutation: {
addCat(parent, args, context, info) {
const { cat } = args;
cats.push({
...cat,
id: _.size(cats),
});
return _.last(cats);
},
changeCat(parent, args, context, info) {
l(args);
const { id, name } = args;
cats[_.findIndex(cats, ["id", +id])].name = name;
return _.find(cats, ["id", +id]);
},
},
}; const server = new ApolloServer({
typeDefs,
resolvers,
}); server.listen().then(({ url }) => {
console.log(` Server ready at ${url}`);
});
get: http://localhost:4000/?query={cats { id name }}
get: http://localhost:4000/?query={ findCat(id: 1) { name } }

express+apollo+mongodb的更多相关文章

  1. Node.js、express、mongodb 实现分页查询、条件搜索

    前言 在上一篇Node.js.express.mongodb 入门(基于easyui datagrid增删改查) 的基础上实现了分页查询.带条件搜索. 实现效果 1.列表第一页. 2.列表第二页 3. ...

  2. React+Node.js+Express+mongoskin+MongoDB

    首发:个人博客,更新&纠错&回复 采用React + Node.js + Express + mongoskin + MongoDB技术开发的一个示例,演示地址在这里,项目源码在这里. ...

  3. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录

    最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...

  4. 使用的vue、elementUI、vuex、express、mongoDB的单页应用

    基于vue.vuex.express.mongodb的一个单页应用,包括前后端,前端主要是使用vue,后端是node的express,数据库是使用的mongodb 1.下载使用 git clone h ...

  5. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(二)

    用mogoose搭建restful测试接口 接着上一篇(Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一))记录,今天单独搭建一个restful测试接口,和项目前 ...

  6. Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)

    最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...

  7. react + node + express + ant + mongodb 的简洁兼时尚的博客网站

    前言 此项目是用于构建博客网站的,由三部分组成,包含前台展示.管理后台和后端. 此项目是基于 react + node + express + ant + mongodb 的,项目已经开源,项目地址在 ...

  8. 关于express 连接 mongodb数据库报错

    关于express 连接 mongodb数据库报错 nodejs DeprecationWarning: current URL string parser is deprecated, and wi ...

  9. Node.js、express、mongodb 入门(基于easyui datagrid增删改查)

    前言 从在本机(win8.1)环境安装相关环境到做完这个demo大概不到两周时间,刚开始只是在本机安装环境并没有敲个Demo,从周末开始断断续续的想写一个,按照惯性思维就写一个增删改查吧,一方面是体验 ...

随机推荐

  1. HBase与Zookeeper的关系

    HBase与Zookeeper的关系 一.HBase与Zookeeper的关系 Zookeeper Client Master RegionServer 一.HBase与Zookeeper的关系 Cl ...

  2. spark-submit提交spark任务的具体参数配置说明

    spark-submit提交spark任务的具体参数配置说明 1.spark提交任务常见的两种模式 2.提交任务时的几个重要参数 3.参数说明 3.1 executor_cores*num_execu ...

  3. Yacc使用优先级

    Yacc使用优先级 本示例是龙书4.9.2的示例,见图4-59. 和前一章一样,新建xUnit项目,用F#语言.起个名C4F59安装NuGet包: Install-Package FSharpComp ...

  4. c#的dllimport使用方法详解(Port API)

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL(托管/非托管是微软的.net framework中特有的概念,其中, ...

  5. SpringSecurity注解的使用

    @Secured 判断用户具有某个角色,可以访问方法 开启注解功能 使用注解先要开启注解功能!可以在启动类上,也可以在配置类上添加 @EnableGlobalMethodSecurity(secure ...

  6. AcWing 216 Rainbow 的信号

    题意 给定一个长度为n的序列,然后从\(1\sim N\) 这 N 个数中选取两个数\(l,r\) , 如果\(l>r\),则交换\(l,r\).把第\(l\) 个数到第\(r\)个数取出来构成 ...

  7. poj1821——Fence

    题意: 一个栅栏一共有n(从1--n)个木板,我们找k个工人去粉刷它,li表示每个人有限制粉刷木板数量,pi表示粉刷一个木板得到的钱,si表示他开始在那个木板前面 如果一个工人要粉刷,那么他必须粉刷s ...

  8. Codeforces Round #582 (Div. 3) A. Chips Moving

    传送门 题解: 给你n个数的坐标,你需要把他们移动到一个位置,有两种移动方式 1.向左或者右移动2 2.向左或者右移动1,但是耗费1 求最小耗费 题解: 很简单就可以想到,看一下偶数坐标多还是奇数坐标 ...

  9. POJ2429 GCD & LCM Inverse pollard_rho大整数分解

    Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...

  10. 2020牛客暑期多校训练营(第二场) F.Fake Maxpooling (单调队列)

    题意:有一个\(n\)x\(m\)的矩阵,\(A_{i,j}=lcm(i,j)\),对于每个\(k\)x\(k\)的子矩阵,其最大元素贡献给答案,求答案的最大值. 题解:矩阵构成我们直接\(i*j/g ...