Zero-dependency, HTTP/1 safe, simple, GraphQL over Server-Sent Events Protocol server and client.
MIT License
Bot releases are hidden (Show)
Published by enisdenjo about 1 year ago
Koa
import Koa from 'koa'; // yarn add koa
import mount from 'koa-mount'; // yarn add koa-mount
import { createHandler } from 'graphql-sse/lib/use/koa';
import { schema } from './my-graphql';
// Create a Koa app
const app = new Koa();
// Serve all methods on `/graphql/stream`
app.use(mount('/graphql/stream', createHandler({ schema })));
app.listen({ port: 4000 });
console.log('Listening to port 4000');
Published by enisdenjo over 1 year ago
import { createClient } from 'graphql-sse';
const client = createClient({
// singleConnection: true, preferred for HTTP/1 enabled servers and subscription heavy apps
url: 'http://localhost:4000/graphql/stream',
});
// query
(async () => {
const query = client.iterate({
query: '{ hello }',
});
const { value } = await query.next();
expect(value).toEqual({ hello: 'world' });
})();
// subscription
(async () => {
const subscription = client.iterate({
query: 'subscription { greetings }',
});
for await (const event of subscription) {
expect(event).toEqual({ greetings: 'Hi' });
// complete a running subscription by breaking the iterator loop
break;
}
})();
Published by enisdenjo almost 2 years ago
graphql-sse
is now server agnostic and as such offers a handler that implements a generic request/response modelonSubscribe
hook can now return an execution result too (useful for caching for example)onNext
and onComplete
hooks will bubble the error to the returned iteratorEven though the core of graphql-sse is now completely server agnostic, there are adapters to ease the integration with existing solutions. Migrating is actually not a headache!
Beware that the adapters don't handle internal errors, it's your responsibility to take care of that and behave accordingly.
http
import http from 'http';
- import { createHandler } from 'graphql-sse';
+ import { createHandler } from 'graphql-sse/lib/use/http';
// Create the GraphQL over SSE handler
const handler = createHandler({ schema });
// Create an HTTP server using the handler on `/graphql/stream`
const server = http.createServer((req, res) => {
if (req.url.startsWith('/graphql/stream')) {
return handler(req, res);
}
res.writeHead(404).end();
});
server.listen(4000);
console.log('Listening to port 4000');
http2
import fs from 'fs';
import http2 from 'http2';
- import { createHandler } from 'graphql-sse';
+ import { createHandler } from 'graphql-sse/lib/use/http2';
// Create the GraphQL over SSE handler
const handler = createHandler({ schema });
// Create an HTTP server using the handler on `/graphql/stream`
const server = http.createServer((req, res) => {
if (req.url.startsWith('/graphql/stream')) {
return handler(req, res);
}
res.writeHead(404).end();
});
server.listen(4000);
console.log('Listening to port 4000');
express
import express from 'express'; // yarn add express
- import { createHandler } from 'graphql-sse';
+ import { createHandler } from 'graphql-sse/lib/use/express';
// Create the GraphQL over SSE handler
const handler = createHandler({ schema });
// Create an express app
const app = express();
// Serve all methods on `/graphql/stream`
app.use('/graphql/stream', handler);
server.listen(4000);
console.log('Listening to port 4000');
fastify
import Fastify from 'fastify'; // yarn add fastify
- import { createHandler } from 'graphql-sse';
+ import { createHandler } from 'graphql-sse/lib/use/fastify';
// Create the GraphQL over SSE handler
const handler = createHandler({ schema });
// Create a fastify app
const fastify = Fastify();
// Serve all methods on `/graphql/stream`
fastify.all('/graphql/stream', handler);
fastify.listen({ port: 4000 });
console.log('Listening to port 4000');