@schamane/graphql-middleware
TypeScript icon, indicating that this package has built-in type declarations

3.3.3 • Public • Published

Graphql Middleware

@schamane/graphql-middleware

middleware for Apollo grapqhl server => 3.3.0 with websocket support for subscriptions based on express/polka

Todo

  • Authentication get working for context with WS and Graphql (typings)

Add dependancy to project

npm i @schamane/@schamane/graphql-middleware

Define app middleware

import { ApolloServer } from 'apollo-server-express';
import { TestBL } from '../bl/test';
import * as schemasDefs from '../schemas';
import { makeSchema, createGraphQlServer, initSubscriptionWSWihtCompatibility, DataSources, GrapqhContext } from '@schamane/graphql-middleware';
import type { Server } from 'http';


const schema = makeSchema(schemasDefs as any) as any;

const createDataSources = (): DataSources<GrapqhContext> => ({
  testApi: new TestBL()
});

export const initGraphQl = async (app: any): Promise<ApolloServer> => {
  const dataSources = createDataSources;

  const graphqlServer = createGraphQlServer(app, {
    schema,
    dataSources,
  }, {path: `/graphql`,});

  return graphqlServer;
};

export const initSubscriptions = (server: Server) => {
  const schema = makeSchema(schemasDefs as any) as any;
  return initSubscriptionWSWihtCompatibility(server, schema);
}

Use middleware with express

import express, { Application } from 'express';

const PORT: string = process.env.PORT || '8081';

const app: Application = express();

const serverInitialize = async () => {
    app.get('/', (_, res) => {
    res.send({ msg: 'set api version for request' });
  });

  await initGraphQl(app);
  const server = app.listen(PORT, async () => {
    initSubscriptions(server);
    console.info(`Listening to port ${PORT}`);
  });
  return server;
};

export const server = serverInitialize();

Use middleware with polka

import polka from 'polka';
import type { Polka } from 'polka';

const PORT: string = process.env.PORT || '8081';

const app: Polka = polka();

const serverInitialize = async () => {
    app.get('/', (_, res) => {
    res.send({ msg: 'set api version for request' });
  });

  await initGraphQl(app as any);
  const server = app.listen(PORT, async () => {
    initSubscriptions(server);
    console.info(`Listening to port ${PORT}`);
  }).server;
  return server;
};

export const server = serverInitialize();

Package Sidebar

Install

npm i @schamane/graphql-middleware

Weekly Downloads

0

Version

3.3.3

License

MIT

Unpacked Size

31.8 kB

Total Files

17

Last publish

Collaborators

  • schamane