@envelop/live-query
Version:
The easiest way of adding live queries to your GraphQL server!
33 lines (32 loc) • 1.49 kB
JavaScript
import { print } from 'graphql';
import { NoLiveMixedWithDeferStreamRule, GraphQLLiveDirective } from '@n1ru4l/graphql-live-query';
import { astFromDirective } from '@graphql-tools/utils';
export { GraphQLLiveDirective };
export const GraphQLLiveDirectiveAST = astFromDirective(GraphQLLiveDirective);
export const GraphQLLiveDirectiveSDL = print(GraphQLLiveDirectiveAST);
export const useLiveQuery = (opts) => {
return {
onExecute: ({ executeFn, setExecuteFn }) => {
const execute = opts.liveQueryStore.makeExecute(executeFn);
if (opts.applyLiveQueryPatchGenerator) {
const { applyLiveQueryPatchGenerator } = opts;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore execute typings do not include AsyncIterable return right now
setExecuteFn((...args) => applyLiveQueryPatchGenerator(execute(...args)));
}
else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore execute typings do not include AsyncIterable return right now
setExecuteFn(execute);
}
},
onValidate: ({ addValidationRule }) => {
addValidationRule(NoLiveMixedWithDeferStreamRule);
},
onContextBuilding: ({ extendContext }) => {
extendContext({
liveQueryStore: opts.liveQueryStore,
});
},
};
};