From 60750eeed8d1744f76eec8ba24ff9be73c347928 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Mon, 24 Jul 2017 22:37:13 -0400 Subject: [PATCH] feat: GraphQL base implementation --- package.json | 2 + server/index.js | 4 ++ server/middlewares/security.js | 4 +- server/models/user.js | 2 +- server/modules/auth.js | 20 ++++------ server/modules/db.js | 3 +- server/modules/graphql.js | 67 ++++++++++++++++++++++++++++++++++ yarn.lock | 36 +++++++++++++++++- 8 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 server/modules/graphql.js diff --git a/package.json b/package.json index d616a1e6..862d8fff 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "node": ">=6.11.1" }, "dependencies": { + "apollo-server-express": "~1.0.4", "auto-load": "~3.0.0", "axios": "~0.16.2", "bcryptjs-then": "~1.0.1", @@ -62,6 +63,7 @@ "follow-redirects": "~1.2.4", "fs-extra": "~4.0.0", "git-wrapper2-promise": "~0.2.9", + "graphql": "~0.10.5", "highlight.js": "~9.12.0", "i18next": "~8.4.3", "i18next-express-middleware": "~1.0.5", diff --git a/server/index.js b/server/index.js index 3f831aaa..d53f3fba 100644 --- a/server/index.js +++ b/server/index.js @@ -65,6 +65,8 @@ const session = require('express-session') const SessionRedisStore = require('connect-redis')(session) const graceful = require('node-graceful') const socketio = require('socket.io') +const graphqlApollo = require('apollo-server-express') +const graphqlSchema = require('./modules/graphql') var mw = autoload(path.join(wiki.SERVERPATH, '/middlewares')) var ctrl = autoload(path.join(wiki.SERVERPATH, '/controllers')) @@ -167,6 +169,8 @@ app.use(mw.flash) app.use('/', ctrl.auth) +app.use('/graphql', graphqlApollo.graphqlExpress({ schema: graphqlSchema })) +app.use('/graphiql', graphqlApollo.graphiqlExpress({ endpointURL: '/graphql' })) app.use('/uploads', mw.auth, ctrl.uploads) app.use('/admin', mw.auth, ctrl.admin) app.use('/', mw.auth, ctrl.pages) diff --git a/server/middlewares/security.js b/server/middlewares/security.js index 4a04056c..4b63612c 100644 --- a/server/middlewares/security.js +++ b/server/middlewares/security.js @@ -1,7 +1,5 @@ 'use strict' -/* global app */ - /** * Security Middleware * @@ -12,7 +10,7 @@ */ module.exports = function (req, res, next) { // -> Disable X-Powered-By - app.disable('x-powered-by') + req.app.disable('x-powered-by') // -> Disable Frame Embedding res.set('X-Frame-Options', 'deny') diff --git a/server/models/user.js b/server/models/user.js index 1a9bc05e..4d740b39 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -99,7 +99,7 @@ module.exports = (sequelize, DataTypes) => { deny: false }] } - return db.User.create(nUsr) + return wiki.db.User.create(nUsr) } return user || Promise.reject(new Error(wiki.lang.t('auth:errors:notyetauthorized'))) }) diff --git a/server/modules/auth.js b/server/modules/auth.js index 2b0a408d..60bc49d8 100644 --- a/server/modules/auth.js +++ b/server/modules/auth.js @@ -214,20 +214,17 @@ module.exports = function (passport) { return wiki.db.User.create({ provider: 'local', - email: 'guest', + email: 'guest@example.com', name: 'Guest', password: '', - rights: [{ - role: 'read', - path: '/', - exact: false, - deny: !wiki.config.public - }] + role: 'guest' }).then(() => { wiki.logger.info('[AUTH] Guest account created successfully!') + return true }).catch((err) => { wiki.logger.error('[AUTH] An error occured while creating guest account:') wiki.logger.error(err) + return err }) } }).then(() => { @@ -241,17 +238,14 @@ module.exports = function (passport) { email: process.env.WIKI_ADMIN_EMAIL, name: 'Administrator', password: '$2a$04$MAHRw785Xe/Jd5kcKzr3D.VRZDeomFZu2lius4gGpZZ9cJw7B7Mna', // admin123 (default) - rights: [{ - role: 'admin', - path: '/', - exact: false, - deny: false - }] + role: 'admin' }).then(() => { wiki.logger.info('[AUTH] Root admin account created successfully!') + return true }).catch((err) => { wiki.logger.error('[AUTH] An error occured while creating root admin account:') wiki.logger.error(err) + return err }) } else { return true } }) diff --git a/server/modules/db.js b/server/modules/db.js index 42e83eb3..a849031d 100644 --- a/server/modules/db.js +++ b/server/modules/db.js @@ -64,8 +64,7 @@ module.exports = { // Sync DB self.onReady = self.inst.sync({ - force: false, - logging: wiki.logger.verbose + force: false }) return self diff --git a/server/modules/graphql.js b/server/modules/graphql.js new file mode 100644 index 00000000..4de7a8c7 --- /dev/null +++ b/server/modules/graphql.js @@ -0,0 +1,67 @@ +'use strict' + +/* global wiki */ + +const gql = require('graphql') + +const User = new gql.GraphQLObjectType({ + name: 'User', + description: 'A User', + fields() { + return { + id: { + type: gql.GraphQLInt, + resolve(usr) { + return usr.id + } + }, + email: { + type: gql.GraphQLString, + resolve(usr) { + return usr.email + } + }, + provider: { + type: gql.GraphQLString, + resolve(usr) { + return usr.provider + } + }, + providerId: { + type: gql.GraphQLString, + resolve(usr) { + return usr.providerId + } + } + } + } +}) + +const Query = new gql.GraphQLObjectType({ + name: 'Query', + description: 'Root Query', + fields() { + return { + users: { + type: new gql.GraphQLList(User), + args: { + id: { + type: gql.GraphQLInt + }, + email: { + type: gql.GraphQLString + } + }, + resolve(root, args) { + return wiki.db.User.findAll({ where: args }) + } + } + } + } +}) + +const Schema = new gql.GraphQLSchema({ + query: Query +}) + +module.exports = Schema diff --git a/yarn.lock b/yarn.lock index 3094beea..0bac2239 100644 --- a/yarn.lock +++ b/yarn.lock @@ -65,7 +65,7 @@ dependencies: "@types/node" "*" -"@types/express@*": +"@types/express@*", "@types/express@^4.0.35": version "4.0.36" resolved "https://registry.yarnpkg.com/@types/express/-/express-4.0.36.tgz#14eb47de7ecb10319f0a2fb1cf971aa8680758c2" dependencies: @@ -76,6 +76,10 @@ version "1.0.2" resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-1.0.2.tgz#b02d10ab028e2928ac592a051aaa4981a1941d03" +"@types/graphql@^0.9.0", "@types/graphql@^0.9.1": + version "0.9.4" + resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-0.9.4.tgz#cdeb6bcbef9b6c584374b81aa7f48ecf3da404fa" + "@types/ldapjs@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/ldapjs/-/ldapjs-1.0.0.tgz#d940cb412140caec14edaa9c76d5b92799dab495" @@ -296,6 +300,26 @@ ap@~0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/ap/-/ap-0.2.0.tgz#ae0942600b29912f0d2b14ec60c45e8f330b6110" +apollo-server-core@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/apollo-server-core/-/apollo-server-core-1.0.2.tgz#6860a6b5680c42c8727153c524eefc84fb91e500" + optionalDependencies: + "@types/graphql" "^0.9.0" + +apollo-server-express@~1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/apollo-server-express/-/apollo-server-express-1.0.4.tgz#fad6045493d818b09ad4610faa389768386a68b6" + dependencies: + apollo-server-core "^1.0.2" + apollo-server-module-graphiql "^1.0.4" + optionalDependencies: + "@types/express" "^4.0.35" + "@types/graphql" "^0.9.1" + +apollo-server-module-graphiql@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/apollo-server-module-graphiql/-/apollo-server-module-graphiql-1.0.4.tgz#d6db21a8c60f052649124da5cde1d88dab702319" + app-root-path@^1.3.0: version "1.4.0" resolved "https://registry.yarnpkg.com/app-root-path/-/app-root-path-1.4.0.tgz#6335d865c9640d0fad99004e5a79232238e92dfa" @@ -2868,6 +2892,12 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6, version "1.0.1" resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" +graphql@~0.10.5: + version "0.10.5" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.10.5.tgz#c9be17ca2bdfdbd134077ffd9bbaa48b8becd298" + dependencies: + iterall "^1.1.0" + growly@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081" @@ -3482,6 +3512,10 @@ isurl@^1.0.0-alpha5: has-to-string-tag-x "^1.2.0" is-object "^1.0.1" +iterall@^1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.1.tgz#f7f0af11e9a04ec6426260f5019d9fcca4d50214" + jackpot@>=0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/jackpot/-/jackpot-0.0.6.tgz#3cff064285cbf66f4eab2593c90bce816a821849"