-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
146 lines (124 loc) · 3.8 KB
/
schema.js
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
const sqlite3 = require("sqlite3").verbose();
const path = require("path");
const db_Path = path.resolve(__dirname, "db/example.sqlite");
const database = new sqlite3.Database(db_Path);
const graphql = require("graphql");
const { GraphQLJSON, GraphQLJSONObject } = require("graphql-type-json");
database.run("PRAGMA foreign_keys=ON");
let ExampleType = new graphql.GraphQLObjectType({
name: "example",
fields: {
uuid: { type: graphql.GraphQLString },
label: { type: graphql.GraphQLString },
},
});
let queryType = new graphql.GraphQLObjectType({
name: "Query",
fields: {
Table: {
type: graphql.GraphQLList(ExampleType), // RefType
resolve: (root, args, context, info) => {
return new Promise((resolve, reject) => {
database.all("SELECT * FROM example", function (err, rows) {
if (err) {
reject([]);
}
resolve(GraphQLJSON.parseValue(rows));
});
});
},
},
search: {
type: graphql.GraphQLList(ExampleType),
args: {
search: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
},
project_id: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
},
},
resolve: (root, { search, project_id }, context, info) => {
return new Promise((resolve, reject) => {
database.all(
"SELECT json, guid, json_extract(reference.bibtex, '$.title') as title, json_extract(reference.bibtex, '$.year') as year, json_extract(reference.bibtex, '$.journal') as journal, json_extract(reference.bibtex, '$.author') as author, json_extract(reference.bibtex, '$.editor') as editor, json_extract(reference.bibtex, '$.type') as type, bibtex FROM reference WHERE (json_extract(reference.bibtex, '$.title') LIKE (?) OR json_extract(reference.bibtex, '$.author') LIKE (?)) AND project_id = (?);",
["%" + search + "%", "%" + search + "%", project_id],
function (err, rows) {
if (err) {
reject([]);
}
resolve(rows);
}
);
});
},
},
},
});
var mutationRefType = new graphql.GraphQLObjectType({
name: "Mutation",
fields: {
createProject: {
type: ExampleType,
args: {
uuid: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
},
label: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
},
},
resolve: (root, { uuid, label }) => {
return new Promise((resolve, reject) => {
//raw SQLite to insert a new post in post table
database.run(
"INSERT INTO project (uuid, label) VALUES (?,?);",
[uuid, label],
(err) => {
if (err) {
reject(null);
}
database.get("SELECT last_insert_rowid() as id", (err, row) => {
resolve({
//id: row["id"],
uuid: uuid,
label: label,
});
});
}
);
});
},
},
deleteReference: {
type: graphql.GraphQLString,
args: {
id: {
type: new graphql.GraphQLNonNull(graphql.GraphQLID),
},
},
resolve: (root, { id }) => {
return new Promise((resolve, reject) => {
database.run(
"DELETE from Reference WHERE guid =(?);",
[id],
(err) => {
if (err) {
reject(err);
}
resolve(`Reference #${id} deleted`);
}
);
});
},
},
},
});
/**/
const schema = new graphql.GraphQLSchema({
query: queryType,
mutation: mutationRefType,
});
module.exports = {
schema,
};