Skip to the content.

Table of contents

  1. Getting started
  2. Documentation
  3. Contributing

GWH provides a flexible and simple tool to executes and manages webhooks in a GraphQL environement.

It is based on graphql-sequelize-generator and inspired by graphql-node-jobs.


Prerequisites

To use GWH, you need a project with:

A few modules need to be installed, like WebSocket, etc.


How to setup


Add GWH to your repository

yarn add graphql-web-hook

Migrate models


yarn run gnj migrate <configPath>

Add server apollo to your express server

const path = require("path");
const { getApolloServer } = require("./../lib/index");
const express = require("express");
const http = require("spdy");
const { PubSub } = require("graphql-subscriptions");
const { WebSocketServer } = require("ws");
const { expressMiddleware } = require("@apollo/server/express4");
const cors = require("cors");
const { json } = require("body-parser");

const config = require("./sqliteTestConfig.js");
const { getMetadataFromContext } = require("./tools");
const app = express();

var dbConfig = require(path.join(__dirname, "/sqliteTestConfig.js")).test;

async function startServer() {
  var options = {
    spdy: {
      plain: true,
    },
  };
  const httpServer = http.createServer(options, app);

  const pubSubInstance = new PubSub();

  const wsServer = new WebSocketServer({
    // This is the `httpServer` we created in a previous step.
    server: httpServer,
    // Pass a different path here if app.use
    // serves expressMiddleware at a different path
    path: "/graphql",
  });

  const server = await getApolloServer({
    dbConfig,
    pubSubInstance,
    getMetadataFromContext,
    playground: true,
    wsServer,
    apolloServerOptions: {},
  });

  await server.start();

  app.use(
    "/graphql",
    cors(),
    json(),
    expressMiddleware(server, {
      // THIS IS FOR TESTING PURPOSE, DO NOT DO THAT IN PRODUCTION
      context: ({ req }) => ({ userId: req.headers.userId }),
    })
  );

  const port = process.env.PORT || 8080;

  httpServer.listen(port, async () => {
    console.log(
      `🚀 http/https/h2 server runs on  http://localhost:${port}/graphql .`
    );
  });
}

startServer();

How to use

const { getCallWebhook } = require('graphql-web-hook')

const callWebhook = getCallWebhook(getMetadataFromContext)

await callWebhook({
  evenType: 'publish',
  context: {
    userId: 1,
    ...
  },
  data: {
    text: 'New post publish',
  },
})

Next: Documentation


Teamstarter’s other libraries