世界上最伟大的投资就是投资自己的教育
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 })
return Author.findById(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 })
return Book.find({ 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 })
return Book.findById(args.id);
}
},
author: {
type: AuthorType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
// return _.find(authors, { id: args.id })
return Author.findById(args.id);
}
},
books: {
type: GraphQLList(BookType),
resolve(parent, args) {
// return books;
return Book.find({});
}
},
authors: {
type: GraphQLList(AuthorType),
resolve(parent, args) {
// return authors;
return Author.find({});
}
}
}
})
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
});
{
book(id: "5bff4b33024d736c42697a64") {
name
id
genre
author {
name
books {
name
}
}
}
}
{
author(id: "5bfea882bc5433700ca5dae7") {
name
age
books {
name
genre
}
}
}
{
books {
name
genre
id
}
}
{
books {
name
genre
id
author {
name
age
books {
name
}
}
}
}
{
authors {
name
age
id
books {
name
genre
id
author {
name
age
}
}
}
}
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:0417Free诱人的 GraphQL & React & Apollo 实战视频教程 #17 更多的 Mutations
08:36Free诱人的 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 样式
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
微信:qiuzhi99pro
b 站:https://space.bilibili.com/31152817
知乎:https://www.zhihu.com/people/rails365
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
© 汕尾市求知科技有限公司 | 专业版网站 | 关于我们 | 在线学员:1149
粤公网安备 44152102000088号
| 粤ICP备19038915号
强制转换为 objectid 失败
改一下类型就可以了
👏