-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgraphql.js
More file actions
161 lines (152 loc) · 4.87 KB
/
graphql.js
File metadata and controls
161 lines (152 loc) · 4.87 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLList, GraphQLSchema, GraphQLBoolean, GraphQLNonNull } = require('graphql');
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLInt },
username: { type: GraphQLString },
email: { type: GraphQLString },
created_at: { type: GraphQLString },
articles: {
type: new GraphQLList(ArticleType),
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM articles WHERE author_id = ?', [parent.id], (err, results) => {
if (err) reject(err);
else resolve(results);
});
});
}
}
})
});
const ArticleType = new GraphQLObjectType({
name: 'Article',
fields: () => ({
id: { type: GraphQLInt },
title: { type: GraphQLString },
content: { type: GraphQLString },
summary: { type: GraphQLString },
author_id: { type: GraphQLInt },
category: { type: GraphQLString },
tags: { type: GraphQLString },
is_published: { type: GraphQLBoolean },
created_at: { type: GraphQLString },
author: {
type: UserType,
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM users WHERE id = ?', [parent.author_id], (err, results) => {
if (err) reject(err);
else resolve(results[0]);
});
});
}
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLInt } },
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM users WHERE id = ?', [args.id], (err, results) => {
if (err) reject(err);
else resolve(results[0]);
});
});
}
},
users: {
type: new GraphQLList(UserType),
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM users', (err, results) => {
if (err) reject(err);
else resolve(results);
});
});
}
},
article: {
type: ArticleType,
args: { id: { type: GraphQLInt } },
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM articles WHERE id = ?', [args.id], (err, results) => {
if (err) reject(err);
else resolve(results[0]);
});
});
}
},
articles: {
type: new GraphQLList(ArticleType),
args: {
category: { type: GraphQLString },
search: { type: GraphQLString }
},
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
let query = 'SELECT * FROM articles WHERE is_published = TRUE';
const params = [];
if (args.category) {
query += ' AND category = ?';
params.push(args.category);
}
if (args.search) {
query += ' AND (title LIKE ? OR content LIKE ? OR tags LIKE ?)';
const searchTerm = `%${args.search}%`;
params.push(searchTerm, searchTerm, searchTerm);
}
query += ' ORDER BY created_at DESC';
db.query(query, params, (err, results) => {
if (err) reject(err);
else resolve(results);
});
});
}
}
}
});
const RootMutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
createArticle: {
type: ArticleType,
args: {
title: { type: new GraphQLNonNull(GraphQLString) },
content: { type: new GraphQLNonNull(GraphQLString) },
summary: { type: GraphQLString },
category: { type: GraphQLString },
tags: { type: GraphQLString },
author_id: { type: new GraphQLNonNull(GraphQLInt) },
is_published: { type: GraphQLBoolean }
},
resolve(parent, args, { db }) {
return new Promise((resolve, reject) => {
const { title, content, summary, category, tags, author_id, is_published } = args;
db.query(
'INSERT INTO articles (title, content, summary, category, tags, author_id, is_published) VALUES (?, ?, ?, ?, ?, ?, ?)',
[title, content, summary || '', category || 'general', tags || '', author_id, is_published || false],
(err, result) => {
if (err) reject(err);
else {
db.query('SELECT * FROM articles WHERE id = ?', [result.insertId], (err, results) => {
if (err) reject(err);
else resolve(results[0]);
});
}
}
);
});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery,
mutation: RootMutation
});