Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cokoapps/server
  • jgutix/server
2 results
Show changes
Commits on Source (4)
......@@ -2,6 +2,13 @@
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## [1.3.0](https://gitlab.coko.foundation/cokoapps/server/compare/v1.2.0...v1.3.0) (2020-06-13)
### Features
* **middleware:** add email middleware ([f0e6fb9](https://gitlab.coko.foundation/cokoapps/server/commit/f0e6fb9456f33e12be84181f2f70bf430f242399))
## [1.2.0](https://gitlab.coko.foundation/cokoapps/server/compare/v1.1.0...v1.2.0) (2020-05-28)
......
......@@ -86,6 +86,46 @@ const permissions = require('../path/to/myPermissions.js')
Please refer to shield's [documentation](https://github.com/maticzav/graphql-shield#overview) for more details.
### Email middleware
Email notifications get sent out as a side effect of a **_mutation_** in your schema.
Create a file wherever you like:
```js
const myFunc = async (resolve, parent, args, ctx, info) => {
// do some stuff before the resolver
const result = await resolve(parent, args, ctx, info)
// do some stuff after the resolver
// send your email
return result
}
module.exports = {
// myMutation needs to match a mutation name in your schema
myMutation: myFunc,
}
```
Now in your config:
```js
// config/default.js
const emailService = require('path/to/your/email/file')
{
emailMiddleware: {
service: emailService
}
}
```
This middleware uses `graphql-middleware` to provide its functionality, so make sure to read its [documentation](https://github.com/prisma-labs/graphql-middleware) and understand its function signature.
### Cron support
All you need for cron-based scheduled tasks to run is to provide the path to your cron jobs.
......@@ -154,5 +194,4 @@ Useful if you have custom login resolvers.
### Future features
- Notification middleware
- Include more pubsweet packages into the bundle
{
"name": "@coko/server",
"version": "1.2.0",
"version": "1.3.0",
"description": "A tweaked version of pubsweet-server for use by Coko's projects",
"main": "src/index.js",
"scripts": {
......
const { ApolloServer } = require('apollo-server-express')
const isEmpty = require('lodash/isEmpty')
const config = require('config')
const logger = require('@pubsweet/logger')
const errors = require('@pubsweet/errors')
const config = require('config')
const connectors = require('pubsweet-server/src/connectors')
const loaders = require('pubsweet-server/src/graphql/loaders')
const helpers = require('pubsweet-server/src/helpers/authorization')
const schema = require('./graphqlSchema')
const hostname = config.has('pubsweet-server.hostname')
......
......@@ -2,12 +2,47 @@ const config = require('config')
const isEmpty = require('lodash/isEmpty')
const { applyMiddleware } = require('graphql-middleware')
const { shield } = require('graphql-shield')
let schema = require('pubsweet-server/src/graphql/schema')
const logger = require('@pubsweet/logger')
const schema = require('pubsweet-server/src/graphql/schema')
const emailMiddleware = require('./middleware/email')
const baseMessage = 'Coko server =>'
const logRegistration = name =>
logger.info(`${baseMessage} Middleware: Registered ${name} middleware`)
const middleware = []
console.log('') // eslint-disable-line no-console
logger.info(`${baseMessage} Registering graphql middleware...`)
/**
* Authorization middleware
*/
const permissions = config.has('permissions') && config.get('permissions')
if (permissions && !isEmpty(permissions)) {
schema = applyMiddleware(schema, shield(permissions))
if (!isEmpty(permissions)) {
const authorizationMiddleware = shield(permissions)
middleware.push(authorizationMiddleware)
logRegistration('authorization')
}
module.exports = schema
/**
* Email middleware
*/
const emailConfig =
config.has('emailMiddleware') && config.get('emailMiddleware')
if (!isEmpty(emailConfig)) {
middleware.push(emailMiddleware)
logRegistration('email')
}
console.log('') // eslint-disable-line no-console
const schemaWithMiddleWare = applyMiddleware(schema, ...middleware)
module.exports = schemaWithMiddleWare
const config = require('config')
const { isEmpty } = require('lodash')
const { middleware } = require('graphql-middleware')
// const logger = require('@pubsweet/logger')
const generator = middleware(schema => {
const emailService =
config.has('emailMiddleware.service') &&
config.get('emailMiddleware.service')
if (isEmpty(emailService)) {
throw new Error('Email middleware: No email service provided!')
}
/**
* Email notifications are only applicable on mutations
*/
const typeMap = schema.getTypeMap()
const { Mutation } = typeMap
const mutationFields = Mutation.getFields()
const mutationEntries = Object.keys(mutationFields).reduce(
(middlewareSchema, field) => {
const serviceFieldEntry = emailService[field]
if (!serviceFieldEntry) return middlewareSchema
/* eslint-disable-next-line no-param-reassign */
middlewareSchema[field] = serviceFieldEntry
return middlewareSchema
},
{},
)
return {
Mutation: mutationEntries,
}
})
module.exports = generator