diff --git a/seasoned_api/package.json b/seasoned_api/package.json index 85d0d19..5888900 100644 --- a/seasoned_api/package.json +++ b/seasoned_api/package.json @@ -22,6 +22,9 @@ "body-parser": "~1.18.2", "cross-env": "~5.1.4", "express": "~4.16.0", + "express-graphql": "^0.9.0", + "express-reload": "^1.2.0", + "graphql": "^14.5.8", "jsonwebtoken": "^8.2.0", "km-moviedb": "^0.2.12", "node-cache": "^4.1.1", diff --git a/seasoned_api/src/webserver/app.js b/seasoned_api/src/webserver/app.js index b1b625e..db3fe2e 100644 --- a/seasoned_api/src/webserver/app.js +++ b/seasoned_api/src/webserver/app.js @@ -1,4 +1,5 @@ const express = require('express'); +// const reload = require('express-reload'); const Raven = require('raven'); const bodyParser = require('body-parser'); const tokenToUser = require('./middleware/tokenToUser'); @@ -48,6 +49,32 @@ app.use(function onError(err, req, res, next) { res.end(res.sentry + '\n'); }); + + +/** + * GraphQL + */ +var graphqlHTTP = require('express-graphql'); +var { buildSchema } = require('graphql'); + +// var schema = buildSchema(` +// type Query { +// hello: String +// }`); +const schema = require('./graphql/requests.js'); + +const roots = { hello: () => 'Hello world!' }; + +app.use('/graphql', graphqlHTTP({ + schema: schema.schema, + graphiql: process.env.NODE_ENV === 'development' +})) + + + + + + /** * User */ diff --git a/seasoned_api/src/webserver/graphql/requests.js b/seasoned_api/src/webserver/graphql/requests.js new file mode 100644 index 0000000..9e5bce9 --- /dev/null +++ b/seasoned_api/src/webserver/graphql/requests.js @@ -0,0 +1,217 @@ +const graphql = require("graphql"); +const establishedDatabase = require('src/database/database'); + + +const TorrentType = new graphql.GraphQLObjectType({ + name: "Torrent", + fields: { + magnet: { type: graphql.GraphQLString }, + torrent_name: { type: graphql.GraphQLString}, + tmdb_id: { type: graphql.GraphQLString }, + date_added: { type: graphql.GraphQLString } + } +}); + +const RequestType = new graphql.GraphQLObjectType({ + name: "Request", + fields: { + id: { type: graphql.GraphQLID }, + title: { type: graphql.GraphQLString }, + year: { type: graphql.GraphQLInt}, + poster_path: { type: graphql.GraphQLString }, + background_path: { type: graphql.GraphQLString }, + requested_by: { type: graphql.GraphQLString }, + ip: { type: graphql.GraphQLString }, + date: { type: graphql.GraphQLString }, + status: { type: graphql.GraphQLString }, + user_agent: { type: graphql.GraphQLString }, + type: { type: graphql.GraphQLString }, + Torrent: { + required: true, + type: TorrentType, + resolve(parentValue, args) { + return establishedDatabase.get('select * from requested_torrent where tmdb_id = (?);', [parentValue.id]) + } + } + } +}); + +// create a graphql query to select all and by id +var queryType = new graphql.GraphQLObjectType({ + name: 'Query', + fields: { + //first query to select all + Requests: { + type: graphql.GraphQLList(RequestType), + resolve: (root, args, context, info) => { + return establishedDatabase.all("SELECT * FROM requests;") + .catch(error => console.error("something went wrong fetching 'all' query. Error:", error)) + } + }, + DownloadingRequests: { + type: graphql.GraphQLList(RequestType), + resolve: (root, args, context, info) => { + return establishedDatabase.all("SELECT * FROM requests;") + .then(data => data.filter(request => { if (request.id === '83666') { console.log('request', request, root);}; return request })) + .catch(error => console.error("something went wrong fetching 'all' query. Error:", error)) + } + }, + DownloadingRequestByName: { + type: RequestType, + args:{ + name:{ + type: new graphql.GraphQLNonNull(graphql.GraphQLString) + } + }, + resolve: (root, { name }, context, info) => { + return establishedDatabase.all("SELECT * FROM requests where requested_by = (?);", [name]) + .catch(error => console.error("something went wrong fetching 'all' query. Error:", error)) + } + }, + //second query to select by id + Request:{ + type: RequestType, + args:{ + id:{ + type: new graphql.GraphQLNonNull(graphql.GraphQLID) + } + }, + resolve: (root, {id}, context, info) => { + return establishedDatabase.get("SELECT * FROM requests WHERE id = (?);",[id]) + .catch(error => console.error(`something went wrong fetching by id: '${ id }'. Error: ${ error }`)) + } + }, + Torrents: { + type: graphql.GraphQLList(TorrentType), + resolve: (root, {id}, context, info) => { + console.log('parent', parent) + return establishedDatabase.all("SELECT * FROM requested_torrent") + .catch(error => console.error(`something went wrong fetching all torrents. Error: ${ error }`)) + } + }, + Torrent: { + type: TorrentType, + args: { + id: { + type: new graphql.GraphQLNonNull(graphql.GraphQLID) + } + }, + resolve: (parent, {id}, context, info) => { + console.log('searcing from parent', parent) + return establishedDatabase.get("SELECT * FROM requested_torrent WHERE tmdb_id = (?);", [id]) + } + } + } +}); +//mutation type is a type of object to modify data (INSERT,DELETE,UPDATE) +// var mutationType = new graphql.GraphQLObjectType({ +// name: 'Mutation', +// fields: { +// //mutation for creacte +// createPost: { +// //type of object to return after create in SQLite +// type: PostType, +// //argument of mutation creactePost to get from request +// args: { +// title: { +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// description:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// createDate:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// author:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// } +// }, +// resolve: (root, {title, description, createDate, author}) => { +// return new Promise((resolve, reject) => { +// //raw SQLite to insert a new post in post table +// database.run('INSERT INTO Posts (title, description, createDate, author) VALUES (?,?,?,?);', [title, description, createDate, author], (err) => { +// if(err) { +// reject(null); +// } +// database.get("SELECT last_insert_rowid() as id", (err, row) => { + +// resolve({ +// id: row["id"], +// title: title, +// description: description, +// createDate:createDate, +// author: author +// }); +// }); +// }); +// }) +// } +// }, +// //mutation for update +// updatePost: { +// //type of object to return afater update in SQLite +// type: graphql.GraphQLString, +// //argument of mutation creactePost to get from request +// args:{ +// id:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLID) +// }, +// title: { +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// description:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// createDate:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// }, +// author:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLString) +// } +// }, +// resolve: (root, {id, title, description, createDate, author}) => { +// return new Promise((resolve, reject) => { +// //raw SQLite to update a post in post table +// database.run('UPDATE Posts SET title = (?), description = (?), createDate = (?), author = (?) WHERE id = (?);', [title, description, createDate, author, id], (err) => { +// if(err) { +// reject(err); +// } +// resolve(`Post #${id} updated`); +// }); +// }) +// } +// }, +// //mutation for update +// deletePost: { +// //type of object resturn after delete in SQLite +// type: graphql.GraphQLString, +// args:{ +// id:{ +// type: new graphql.GraphQLNonNull(graphql.GraphQLID) +// } +// }, +// resolve: (root, {id}) => { +// return new Promise((resolve, reject) => { +// //raw query to delete from post table by id +// database.run('DELETE from Posts WHERE id =(?);', [id], (err) => { +// if(err) { +// reject(err); +// } +// resolve(`Post #${id} deleted`); +// }); +// }) +// } +// } +// } +// }); + +//define schema with post object, queries, and mustation +const schema = new graphql.GraphQLSchema({ + query: queryType, + // mutation: mutationType +}); + +//export schema to use on index.js +module.exports = { + schema +} \ No newline at end of file