-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
62 lines (58 loc) · 2.12 KB
/
schema.js
File metadata and controls
62 lines (58 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { GraphQLID, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema } = require("graphql");
const UsersModel = require('./models/users');
const PostsModel = require('./models/posts');
const UserType = require('./types/user');
const PostType = require('./types/post');
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
description: 'Get users and posts data',
fields: {
users: {
type: GraphQLList(UserType),
description: 'Get an list of the users.',
resolve: (root, args, context, info) => {
return UsersModel.find().exec();
}
},
user: {
type: UserType,
description: 'Get the user data.',
args: {
id: { type: GraphQLNonNull(GraphQLID) }
},
resolve: (root, args, context, info) => {
return UsersModel.findById(args.id).exec();
}
},
posts: {
type: GraphQLList(PostType),
description: 'Get an list of the all posts.',
resolve: (root, args, context, info) => {
return PostsModel.find().exec();
}
},
user_posts: {
type: GraphQLList(PostType),
description: 'Get an list of the user posts.',
args: {
userId: { type: GraphQLNonNull(GraphQLID) }
},
resolve: (root, args, context, info) => {
return PostsModel.find({userId: args.userId}).exec();
}
},
post: {
type: PostType,
description: 'Get the post data.',
args: {
id: { type: GraphQLNonNull(GraphQLID) }
},
resolve: (root, args, context, info) => {
return PostsModel.findById(args.id).exec();
}
}
}
})
});
module.exports = exports = schema;