https://github.com/schamane/graphql-middleware
Middleware helper for graphql
https://github.com/schamane/graphql-middleware
apollo-server-express express extension graphql middleware npmjs subscriptions websocket
Last synced: 3 months ago
JSON representation
Middleware helper for graphql
- Host: GitHub
- URL: https://github.com/schamane/graphql-middleware
- Owner: schamane
- License: mit
- Created: 2021-09-03T07:47:01.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2022-10-26T08:54:35.000Z (over 3 years ago)
- Last Synced: 2025-03-01T03:32:05.219Z (over 1 year ago)
- Topics: apollo-server-express, express, extension, graphql, middleware, npmjs, subscriptions, websocket
- Language: TypeScript
- Homepage:
- Size: 750 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Graphql Middleware
@schamane/graphql-middleware
middleware for [Apollo grapqhl server](https://www.apollographql.com/docs/apollo-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
```bash
npm i @schamane/@schamane/graphql-middleware
```
## Define app middleware
```ts
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 => ({
testApi: new TestBL()
});
export const initGraphQl = async (app: any): Promise => {
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
```ts
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
```ts
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();
```