From 1405b822f4d5188a62bdd931b322c5ba25b2117c Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 13 Aug 2017 14:39:07 -0400 Subject: [PATCH] feat: GraphQL Date scalar --- server/configure.js | 10 ++++----- server/modules/graphql.js | 4 +++- server/schemas/scalar-date.js | 22 +++++++++++++++++++ server/schemas/types.graphql | 40 +++++++++++++++++------------------ 4 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 server/schemas/scalar-date.js diff --git a/server/configure.js b/server/configure.js index 0766d695..bc6a282c 100644 --- a/server/configure.js +++ b/server/configure.js @@ -81,9 +81,9 @@ module.exports = (port, spinner) => { () => { const semver = require('semver') if (!semver.satisfies(semver.clean(process.version), '>=6.9.0')) { - throw new Error('Node.js version is too old. Minimum is v6.6.0.') + throw new Error('Node.js version is too old. Minimum is v6.11.1.') } - return 'Node.js ' + process.version + ' detected. Minimum is v6.9.0.' + return 'Node.js ' + process.version + ' detected.' }, () => { return Promise.try(() => { @@ -110,10 +110,10 @@ module.exports = (port, spinner) => { }, () => { const os = require('os') - if (os.totalmem() < 1000 * 1000 * 768) { - throw new Error('Not enough memory. Minimum is 768 MB.') + if (os.totalmem() < 1000 * 1000 * 512) { + throw new Error('Not enough memory. Minimum is 512 MB.') } - return _.round(os.totalmem() / (1024 * 1024)) + ' MB of system memory available. Minimum is 768 MB.' + return _.round(os.totalmem() / (1024 * 1024)) + ' MB of system memory available. Minimum is 512 MB.' }, () => { let fs = require('fs') diff --git a/server/modules/graphql.js b/server/modules/graphql.js index ed6f776c..a7f073b1 100644 --- a/server/modules/graphql.js +++ b/server/modules/graphql.js @@ -9,12 +9,14 @@ const _ = require('lodash') const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8') +const DateScalar = require('../schemas/scalar-date') const GroupResolvers = require('../schemas/resolvers-group') const UserResolvers = require('../schemas/resolvers-user') const resolvers = _.merge( GroupResolvers, - UserResolvers + UserResolvers, + DateScalar ) const Schema = gqlTools.makeExecutableSchema({ diff --git a/server/schemas/scalar-date.js b/server/schemas/scalar-date.js new file mode 100644 index 00000000..6fa5b585 --- /dev/null +++ b/server/schemas/scalar-date.js @@ -0,0 +1,22 @@ +'use strict' + +const gql = require('graphql') + +module.exports = { + Date: new gql.GraphQLScalarType({ + name: 'Date', + description: 'ISO date-time string at UTC', + parseValue(value) { + return new Date(value) + }, + serialize(value) { + return value.toISOString() + }, + parseLiteral(ast) { + if (ast.kind !== gql.Kind.STRING) { + throw new TypeError('Date value must be an string!') + } + return new Date(ast.value) + } + }) +} diff --git a/server/schemas/types.graphql b/server/schemas/types.graphql index a7e6e20d..95ce6ff8 100644 --- a/server/schemas/types.graphql +++ b/server/schemas/types.graphql @@ -25,16 +25,16 @@ enum RightRole { interface Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date } # TYPES type Comment implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date content: String document: Document! author: User! @@ -42,8 +42,8 @@ type Comment implements Base { type Document implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date path: String! title: String! subtitle: String @@ -57,8 +57,8 @@ type Document implements Base { type File implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date category: FileType! mime: String! extra: String @@ -70,15 +70,15 @@ type File implements Base { type Folder implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date name: String! } type Group implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date name: String! users: [User] rights: [Right] @@ -86,8 +86,8 @@ type Group implements Base { type Right implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date path: String! role: RightRole! exact: Boolean! @@ -96,8 +96,8 @@ type Right implements Base { type Setting implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date key: String! config: String! } @@ -105,16 +105,16 @@ type Setting implements Base { # Tags are attached to one or more documents type Tag implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date key: String! } # A User type User implements Base { id: Int! - createdOn: Date - updatedOn: Date + createdAt: Date + updatedAt: Date email: String! provider: String! providerId: String