Skip to content

Commit

Permalink
Merge pull request #33 from frandiox/esm
Browse files Browse the repository at this point in the history
Use ES modules in server
  • Loading branch information
frandiox authored Jul 22, 2018
2 parents 79a0ffe + 5ddfff9 commit a1aad25
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"stylelint-config-prettier": "^3.3.0",
"stylelint-config-recess-order": "^2.0.0",
"stylelint-config-standard": "^18.2.0",
"stylelint-scss": "^3.1.3",
"stylelint-scss": "^3.2.0",
"vue-cli-plugin-apollo": "^0.15.0",
"vue-jest": "^2.6.0",
"vue-template-compiler": "^2.5.16",
Expand Down
9 changes: 8 additions & 1 deletion server/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: false,
parserOptions: {
sourceType: 'script',
sourceType: 'module',
ecmaVersion: '2018',
},
extends: [
Expand All @@ -10,5 +10,12 @@ module.exports = {
],
rules: {
'node/no-unpublished-require': 'off',
'node/no-unpublished-import': 'off',
'node/no-extraneous-import': 'error',
'node/no-missing-import': 'error',
'node/no-unsupported-features/es-syntax': [
'error',
{ ignores: ['modules'] },
],
},
}
7 changes: 4 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"name": "backend",
"version": "0.0.0",
"scripts": {
"start": "node -r multienv-loader/load src/index.js",
"dev": "nodemon -e js,json,graphql,gql -x node -r multienv-loader/load src/index.js",
"debug": "nodemon -e js,json,graphql,gql -x node -r multienv-loader/load --inspect src/index.js",
"start": "node -r multienv-loader/load -r esm src/index.js",
"dev": "nodemon -e js,json,graphql,gql -x node -r multienv-loader/load -r esm src/index.js",
"debug": "nodemon -e js,json,graphql,gql -x node -r multienv-loader/load -r esm --inspect src/index.js",
"playground": "graphql --dotenv .env playground",
"prisma": "prisma"
},
"dependencies": {
"esm": "^3.0.72",
"graphql-yoga": "^1.14.12",
"jsonwebtoken": "^8.3.0",
"multienv-loader": "^1.1.0",
Expand Down
4 changes: 2 additions & 2 deletions server/src/directives/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { AuthError } = require('../errors')
import { AuthError } from '../errors'

const getUser = ({ request: { user } = {} }) => user

Expand All @@ -7,7 +7,7 @@ const hasRole = (roles, ctx) => {
return role && roles.includes(role)
}

module.exports = {
export default {
isAuthenticated(next, source, args, ctx) {
if (getUser(ctx)) return next()
throw new AuthError('Access Token is missing or might be expired')
Expand Down
9 changes: 2 additions & 7 deletions server/src/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ class DefaultError extends Error {
}
}

class AuthError extends DefaultError {
export class AuthError extends DefaultError {
constructor(...messages) {
super('Not authorized', ...messages)
}
}

class BadRequestError extends DefaultError {
export class BadRequestError extends DefaultError {
constructor(...messages) {
super('Bad request', ...messages)
}
}

module.exports = {
AuthError,
BadRequestError,
}
12 changes: 6 additions & 6 deletions server/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { GraphQLServer } = require('graphql-yoga')
const { Prisma } = require('prisma-binding')
import { GraphQLServer } from 'graphql-yoga'
import { Prisma } from 'prisma-binding'

const verifyAccessToken = require('./middleware/verify-access-token')
const getUserFromDB = require('./middleware/get-user-from-db')
import verifyAccessToken from './middleware/verify-access-token'
import getUserFromDB from './middleware/get-user-from-db'

const resolvers = require('./resolvers')
const directiveResolvers = require('./directives')
import directiveResolvers from './directives'
import * as resolvers from './resolvers'

const db = new Prisma({
typeDefs: 'src/generated/prisma.graphql', // the Prisma DB schema
Expand Down
2 changes: 1 addition & 1 deletion server/src/middleware/get-user-from-db.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = async (req, res, next, db) => {
export default async function(req, res, next, db) {
if (req.user) {
const user = await db.query.user({
where: { auth0id: req.user.sub.split(`|`)[1] },
Expand Down
6 changes: 3 additions & 3 deletions server/src/middleware/verify-access-token.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const jwksClient = require('jwks-rsa')
const jwt = require('express-jwt')
import jwksClient from 'jwks-rsa'
import jwt from 'express-jwt'

// Authentication middleware. When used,
// if the access token exists, it will be
// verified against the Auth0 JSON Web Key Set

module.exports = jwt({
export default jwt({
// Dynamically provide a signing key
// based on the kid in the header and
// the signing keys provided by the JWKS endpoint.
Expand Down
7 changes: 2 additions & 5 deletions server/src/resolvers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
module.exports = {
Query: require('./query'),
Mutation: require('./mutation'),
// Subscription: require('./subscription'),
}
export { default as Query } from './query'
export { default as Mutation } from './mutation'
8 changes: 4 additions & 4 deletions server/src/resolvers/mutation/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jwksClient = require('jwks-rsa')
const jwt = require('jsonwebtoken')
const { AuthError } = require('../../errors')
import jwksClient from 'jwks-rsa'
import jwt from 'jsonwebtoken'
import { AuthError } from '../../errors'

const jwks = jwksClient({
cache: true,
Expand Down Expand Up @@ -37,7 +37,7 @@ function verifyAndDecodeIdToken(idToken) {
})
}

module.exports = {
export default {
async authenticate(parent, { idToken }, ctx, info) {
let userToken = null

Expand Down
4 changes: 2 additions & 2 deletions server/src/resolvers/mutation/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const { mergeDirectoryModules } = require('../../utils')
module.exports = mergeDirectoryModules(__dirname)
import { mergeDirectoryModules } from '../../utils'
export default mergeDirectoryModules(__dirname)
2 changes: 1 addition & 1 deletion server/src/resolvers/mutation/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
async createDraft(parent, { title, text }, ctx, info) {
const userId = ctx.request.user.id
return ctx.db.mutation.createPost(
Expand Down
2 changes: 1 addition & 1 deletion server/src/resolvers/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
feed(parent, args, ctx, info) {
return ctx.db.query.posts({ where: { isPublished: true } }, info)
},
Expand Down
2 changes: 1 addition & 1 deletion server/src/resolvers/subscription.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
feedSubscription: {
subscribe: (parent, args, ctx, info) => {
return ctx.db.subscription.post({}, info)
Expand Down
10 changes: 4 additions & 6 deletions server/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const fs = require('fs')
const path = require('path')
import fs from 'fs'
import path from 'path'

// Require all the modules in the specified directory
const mergeDirectoryModules = dirpath =>
export const mergeDirectoryModules = dirpath =>
fs
.readdirSync(dirpath, 'utf8')
.filter(filename => !/^[_|index.]/.test(filename))
.reduce(
(acc, filename) => ({
...acc,
...require(path.resolve(dirpath, filename)),
...require(path.resolve(dirpath, filename)).default,
}),
{}
)

module.exports = { mergeDirectoryModules }
10 changes: 7 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5224,6 +5224,10 @@ esm@^3.0.64:
version "3.0.71"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.71.tgz#3824987412d7d8fd7782d2381e990a621519a993"

esm@^3.0.72:
version "3.0.72"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.72.tgz#8f71b3f54b978bbbcbb9788f7f3569a36578c136"

espree@^3.5.2, espree@^3.5.4:
version "3.5.4"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.4.tgz#b0f447187c8a8bed944b815a660bddf5deb5d1a7"
Expand Down Expand Up @@ -13473,9 +13477,9 @@ stylelint-order@^0.8.1:
postcss "^6.0.14"
postcss-sorting "^3.1.0"

stylelint-scss@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/stylelint-scss/-/stylelint-scss-3.1.3.tgz#28f881ae298c3f5db667b10b6cf94a1a219001d6"
stylelint-scss@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/stylelint-scss/-/stylelint-scss-3.2.0.tgz#13545a1be5ab5435ea94e761b2d4824eb32033b3"
dependencies:
lodash "^4.17.10"
postcss-media-query-parser "^0.2.3"
Expand Down

0 comments on commit a1aad25

Please sign in to comment.