世界上最伟大的投资就是投资自己的教育
const graphql = require('graphql');
const _ = require('lodash');
const Book = require('../models/book');
const Author = require('../models/author');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList
} = require('graphql');
// 书是属于作者,作者有很多书, 一对多
// 查询一个作者的书
// 查询一本书的作者
// const books = [
// { name: "算法导论", genre: "计算机科学", id: "1", authorId: '1' },
// { name: "人性的弱点", genre: "社交", id: "2", authorId: '2' },
// { name: "明朝那些事儿", genre: "历史", id: "3", authorId: '3' },
// { name: "诱人的 GraqhQL 教程", genre: "计算机科学", id: "4", authorId: '1' },
// { name: "诱人的 mobx 教程", genre: "计算机科学", id: "5", authorId: '2' },
// ];
//
// const authors = [
// { name: "hfpp2012", age: 27, id: "1" },
// { name: "rails365", age: 30, id: "2" },
// { name: "lili", age: 21, id: "3" }
// ];
const BookType = new GraphQLObjectType({
name: "Book",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
genre: { type: GraphQLString },
author: {
type: AuthorType,
resolve(parent, args) {
return _.find(authors, { id: parent.authorId })
}
}
})
})
const AuthorType = new GraphQLObjectType({
name: "Author",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age: { type: GraphQLInt },
books: {
type: new GraphQLList(BookType),
resolve(parent, args) {
return _.filter(books, { authorId: parent.id })
}
}
})
})
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
// 从哪里得到数据,比如数据库或其他来源
// Mongodb mysql postgresql
console.log(typeof(args.id));
return _.find(books, { id: args.id })
}
},
author: {
type: AuthorType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return _.find(authors, { id: args.id })
}
},
books: {
type: GraphQLList(BookType),
resolve(parent, args) {
return books;
}
},
authors: {
type: GraphQLList(AuthorType),
resolve(parent, args) {
return authors;
}
}
}
})
const Mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
addAuthor: {
type: AuthorType,
args: {
name: { type: GraphQLString },
age: { type: GraphQLInt }
},
resolve(parent, args) {
let author = new Author({
name: args.name,
age: args.age
})
return author.save();
}
},
addBook: {
type: BookType,
args: {
name: { type: GraphQLString },
genre: { type: GraphQLString },
authorId: { type: GraphQLID }
},
resolve(parent, args) {
let book = new Book({
name: args.name,
genre: args.genre,
authorId: args.authorId
});
return book.save();
}
}
}
})
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});
mutation {
addBook(name: "诱人的 GraqhQL 教程", genre: "计算机科学", authorId: "5bfea8f12fd83b73fa66903c") {
name
genre
}
}
06:591Free诱人的 GraphQL & React & Apollo 实战视频教程 #1 介绍
05:402Free诱人的 GraphQL & React & Apollo 实战视频教程 #2 搭建 Express 项目框架
04:063Free诱人的 GraphQL & React & Apollo 实战视频教程 #3 搭建 GraphQL 项目框架
04:264Free诱人的 GraphQL & React & Apollo 实战视频教程 #4 创建第一个 GraphQL Schema
04:245Free诱人的 GraphQL & React & Apollo 实战视频教程 #5 Root Query
05:186Free诱人的 GraphQL & React & Apollo 实战视频教程 #6 Resolve Function
05:467Free诱人的 GraphQL & React & Apollo 实战视频教程 #7 在 Graphiql 中测试查询
03:498Free诱人的 GraphQL & React & Apollo 实战视频教程 #8 酷炫的本地测试软件 graphql-playground
03:119Free诱人的 GraphQL & React & Apollo 实战视频教程 #9 GraphQLID
05:1110Free诱人的 GraphQL & React & Apollo 实战视频教程 #10 Author Type 和 GraphQLInt
07:5311Free诱人的 GraphQL & React & Apollo 实战视频教程 #11 关联关系
05:3012Free诱人的 GraphQL & React & Apollo 实战视频教程 #12 GraphQLList
04:2713Free诱人的 GraphQL & React & Apollo 实战视频教程 #13 返回 GraphQL 列表
07:0414Free诱人的 GraphQL & React & Apollo 实战视频教程 #14 连接到 mLab 线上的 MongoDB 数据库
06:3615Free诱人的 GraphQL & React & Apollo 实战视频教程 #15 Mongoose Models
08:2716Free诱人的 GraphQL & React & Apollo 实战视频教程 #16 Mutations
06:04Free诱人的 GraphQL & React & Apollo 实战视频教程 #17 更多的 Mutations
08:3618Free诱人的 GraphQL & React & Apollo 实战视频教程 #18 更新 Resolve
04:1519Free诱人的 GraphQL & React & Apollo 实战视频教程 #19 GraphQLNonNull
08:1020Free诱人的 GraphQL & React & Apollo 实战视频教程 #20 添加 React 前端
08:3021Free诱人的 GraphQL & React & Apollo 实战视频教程 #21 建立 Apollo React 客户端
09:2822Free诱人的 GraphQL & React & Apollo 实战视频教程 #22 在 React 中发送查询语句
05:0723Free诱人的 GraphQL & React & Apollo 实战视频教程 #23 在组件中显示远程的数据
06:4524Free诱人的 GraphQL & React & Apollo 实战视频教程 #24 添加增加 Book 的表单组件
03:3825Free诱人的 GraphQL & React & Apollo 实战视频教程 #25 组织代码 - 外部查询文件
03:4026Free诱人的 GraphQL & React & Apollo 实战视频教程 #26 更新组件的 state
10:5927Free诱人的 GraphQL & React & Apollo 实战视频教程 #27 组合 Mutations 和 Queries
04:2928Free诱人的 GraphQL & React & Apollo 实战视频教程 #28 Mutation 查询变量
05:1229Free诱人的 GraphQL & React & Apollo 实战视频教程 #29 Mutation refetchQueries
07:3030Free诱人的 GraphQL & React & Apollo 实战视频教程 #30 Book Details Component
08:5731Free诱人的 GraphQL & React & Apollo 实战视频教程 #31 发送请求显示 Book Details
06:5832Free诱人的 GraphQL & React & Apollo 实战视频教程 #32 增加 CSS 样式
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
知乎:https://www.zhihu.com/people/rails365
掘金:https://juejin.cn/user/1574156379888263
b 站:https://space.bilibili.com/31152817
Github:https://github.com/hfpp2012
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
Discord:https://discord.gg/5rdjnEnU7F
Twitter:https://twitter.com/qiuzhi99pro
Facebook:https://twitter.com/qiuzhi99pro
Instagram:https://www.facebook.com/pro.qiuzhi/
▬▬▬▬▬▬ 微信相关 👋 ▬▬▬▬▬▬




© 汕尾市求知科技有限公司 | 专业版网站 | 在线学员:1125
粤公网安备 44152102000088号
| 粤ICP备19038915号