@graphql-yoga/plugin-graphql-sse
Version:
GraphQL over Server-Sent Events Protocol plugin for GraphQL Yoga.
57 lines (56 loc) • 2.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGraphQLSSE = void 0;
const graphql_1 = require("graphql");
const fetch_1 = require("graphql-sse/lib/use/fetch");
/**
* Get [GraphQL over Server-Sent Events Protocol](https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md) integration with GraphQL Yoga by simply installing this plugin!
*
* Note that the endpoint defaults to `/graphql/stream`, this is where your [graphql-sse](https://github.com/enisdenjo/graphql-sse) client should connect.
*/
function useGraphQLSSE(options = {}) {
const { endpoint = '/graphql/stream', ...handlerOptions } = options;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ctxForReq = new WeakMap();
let handler;
return {
onYogaInit({ yoga }) {
handler = (0, fetch_1.createHandler)({
...handlerOptions,
async onSubscribe(req, params) {
const enveloped = yoga.getEnveloped({
...ctxForReq.get(req.raw),
request: req.raw,
params,
});
const document = enveloped.parse(params.query);
const errors = enveloped.validate(enveloped.schema, document);
if (errors.length > 0) {
return { errors };
}
const contextValue = await enveloped.contextFactory();
const executionArgs = {
schema: enveloped.schema,
document,
contextValue,
variableValues: params.variables,
operationName: params.operationName,
};
const operation = (0, graphql_1.getOperationAST)(document, params.operationName);
const executeFn = operation?.operation === 'subscription'
? enveloped.subscribe
: enveloped.execute;
return executeFn(executionArgs);
},
}, yoga.fetchAPI);
},
async onRequest({ request, endResponse, serverContext }) {
const [path, _search] = request.url.split('?');
if (path.endsWith(endpoint)) {
ctxForReq.set(request, serverContext);
endResponse(await handler(request));
}
},
};
}
exports.useGraphQLSSE = useGraphQLSSE;
;