Giter VIP home page Giter VIP logo

postgraphql-hook-demo's Introduction

postgraphql-hook-demo

setup and demo

This demo builds upon postgraphql ^4.0 to show how to send emails via mailgun.

For now, postgraphql-hook is used to help setup the hook. This library may or may not be subsumed into the postgraphql library in some way.

After setting up your mailgun account, follow these steps to send an email through postgraphql:

git clone https://github.com/stlbucket/postgraphql-hook-demo.git

Create a new database to use with the demo - 'pgql_hook' is a good name, but use whatever you like.

Copy 'example_config.js' to 'config.js' in root directory and update the following settings:

  • DB_CONNECTION_STRING
  • MAILGUN_API_KEY
  • MAILGUN_DOMAIN
npm install

If you do not already have knex-migrate installed globally:

npm install -g knex-migrate
knex-migrate up
npm start

Navigate to http://localhost:3000/graphiql and execute this mutation:

mutation {
  sendEmail(input: {
    _fromAddress: "YOUR VALID FROM EMAIL"
    _toAddress: "YOUR VALID TO EMAIL"
    _subject: "Mailgun Test"
    _body: "You got it, you get it, you're gonna get it!"
  }) {
    emailInfo {
      id
      fromAddress
      toAddress
      subject
      body
    }
  }
}

the code that matters

sendEmail.js

const apiKey = process.env.MAILGUN_API_KEY
const domain = process.env.MAILGUN_DOMAIN

const mailgun = require('mailgun-js')({apiKey: apiKey, domain: domain})
const appendMutation = require('postgraphql-hook').appendMutation

async function handler (args, result) {
  const emailInfo = result.data

  const mailgunData = {
    from: `Postgraphql-Mailgun Demo <${emailInfo.fromAddress}>`,
    to: emailInfo.toAddress,
    subject: emailInfo.subject,
    text: emailInfo.body
  }

  console.log('SENDING EMAIL', emailInfo)

  mailgun
    .messages()
    .send(mailgunData, function (error, body) {
      if (error) {
        console.log('MAILGUN ERROR', error)
      } else {
        console.log('MAILGUN RESULT', body)
      }
    })
}

const plugin = appendMutation({
  mutationName: 'sendEmail',
  handler: handler
})

module.exports = plugin

postgraphql-hook-demo's People

Contributors

stlbucket avatar

Watchers

 avatar

postgraphql-hook-demo's Issues

needs babel?

$ yarn start

/Users/cameronellis/work/postgraphql-hook-demo/src/mutationHooks/sendEmail.js:7
async function handler (args, result) {
      ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:78:16)
    at Module._compile (module.js:545:28)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/cameronellis/work/postgraphql-hook-demo/src/mutationHooks/index.js:2:3)
    at Module._compile (module.js:573:32)

Edit: I know this is super early stuff and you probably didn't expect anyone to find this. I found it in the V4 announcement as I follow postgraphql pretty closely. The npm package postgraphql-hook probably should be transpiled into ES5 in the package and republished.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.