@graphprotocol/client-polling-live 1.0.0-alpha-20220815093611-dade325
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +84 -0
- package/cjs/package.json +1 -0
- package/esm/index.js +81 -0
- package/package.json +50 -0
- package/typings/index.d.cts +6 -0
- package/typings/index.d.ts +6 -0
package/cjs/index.js
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const core_1 = require("@envelop/core");
|
4
|
+
const graphql_1 = require("graphql");
|
5
|
+
const repeater_1 = require("@repeaterjs/repeater");
|
6
|
+
const schema_1 = require("@graphql-tools/schema");
|
7
|
+
function usePollingLive({ config: { defaultInterval = 1000 } = {} } = {}) {
|
8
|
+
return {
|
9
|
+
onSchemaChange({ schema, replaceSchema }) {
|
10
|
+
if (!schema.getDirective('live')) {
|
11
|
+
replaceSchema((0, schema_1.mergeSchemas)({
|
12
|
+
schemas: [schema],
|
13
|
+
typeDefs: /* GraphQL */ `
|
14
|
+
directive @live(interval: Int) on QUERY
|
15
|
+
`,
|
16
|
+
}));
|
17
|
+
}
|
18
|
+
},
|
19
|
+
onExecute({ args, executeFn, setExecuteFn }) {
|
20
|
+
var _a, _b;
|
21
|
+
let liveDirectiveNode;
|
22
|
+
args.document = (0, graphql_1.visit)(args.document, {
|
23
|
+
OperationDefinition(node) {
|
24
|
+
var _a;
|
25
|
+
if (args.operationName != null && ((_a = node.name) === null || _a === void 0 ? void 0 : _a.value) !== args.operationName) {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const directives = [];
|
29
|
+
if (node.directives && node.operation === 'query') {
|
30
|
+
for (const directive of node.directives) {
|
31
|
+
if (directive.name.value === 'live') {
|
32
|
+
liveDirectiveNode = directive;
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
directives.push(directive);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
return {
|
39
|
+
...node,
|
40
|
+
directives,
|
41
|
+
};
|
42
|
+
}
|
43
|
+
return node;
|
44
|
+
},
|
45
|
+
});
|
46
|
+
if (liveDirectiveNode) {
|
47
|
+
const intervalArgNode = (_a = liveDirectiveNode.arguments) === null || _a === void 0 ? void 0 : _a.find((argNode) => argNode.name.value === 'interval');
|
48
|
+
let intervalMs = defaultInterval;
|
49
|
+
if (((_b = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _b === void 0 ? void 0 : _b.kind) === graphql_1.Kind.INT) {
|
50
|
+
intervalMs = parseInt(intervalArgNode.value.value);
|
51
|
+
}
|
52
|
+
setExecuteFn((args) => new repeater_1.Repeater((push, stop) => {
|
53
|
+
let finished = false;
|
54
|
+
async function pump() {
|
55
|
+
if (finished) {
|
56
|
+
return;
|
57
|
+
}
|
58
|
+
const result = await executeFn(args);
|
59
|
+
if ((0, core_1.isAsyncIterable)(result)) {
|
60
|
+
push({
|
61
|
+
data: null,
|
62
|
+
errors: [new graphql_1.GraphQLError('Execution returned AsyncIterable which is not supported!')],
|
63
|
+
isLive: true,
|
64
|
+
});
|
65
|
+
stop();
|
66
|
+
return;
|
67
|
+
}
|
68
|
+
result.isLive = true;
|
69
|
+
if (finished) {
|
70
|
+
return;
|
71
|
+
}
|
72
|
+
push(result);
|
73
|
+
setTimeout(pump, intervalMs);
|
74
|
+
}
|
75
|
+
pump();
|
76
|
+
stop.then(() => {
|
77
|
+
finished = true;
|
78
|
+
});
|
79
|
+
}));
|
80
|
+
}
|
81
|
+
},
|
82
|
+
};
|
83
|
+
}
|
84
|
+
exports.default = usePollingLive;
|
package/cjs/package.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"type":"commonjs"}
|
package/esm/index.js
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
import { isAsyncIterable } from '@envelop/core';
|
2
|
+
import { GraphQLError, Kind, visit } from 'graphql';
|
3
|
+
import { Repeater } from '@repeaterjs/repeater';
|
4
|
+
import { mergeSchemas } from '@graphql-tools/schema';
|
5
|
+
export default function usePollingLive({ config: { defaultInterval = 1000 } = {} } = {}) {
|
6
|
+
return {
|
7
|
+
onSchemaChange({ schema, replaceSchema }) {
|
8
|
+
if (!schema.getDirective('live')) {
|
9
|
+
replaceSchema(mergeSchemas({
|
10
|
+
schemas: [schema],
|
11
|
+
typeDefs: /* GraphQL */ `
|
12
|
+
directive @live(interval: Int) on QUERY
|
13
|
+
`,
|
14
|
+
}));
|
15
|
+
}
|
16
|
+
},
|
17
|
+
onExecute({ args, executeFn, setExecuteFn }) {
|
18
|
+
var _a, _b;
|
19
|
+
let liveDirectiveNode;
|
20
|
+
args.document = visit(args.document, {
|
21
|
+
OperationDefinition(node) {
|
22
|
+
var _a;
|
23
|
+
if (args.operationName != null && ((_a = node.name) === null || _a === void 0 ? void 0 : _a.value) !== args.operationName) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
const directives = [];
|
27
|
+
if (node.directives && node.operation === 'query') {
|
28
|
+
for (const directive of node.directives) {
|
29
|
+
if (directive.name.value === 'live') {
|
30
|
+
liveDirectiveNode = directive;
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
directives.push(directive);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return {
|
37
|
+
...node,
|
38
|
+
directives,
|
39
|
+
};
|
40
|
+
}
|
41
|
+
return node;
|
42
|
+
},
|
43
|
+
});
|
44
|
+
if (liveDirectiveNode) {
|
45
|
+
const intervalArgNode = (_a = liveDirectiveNode.arguments) === null || _a === void 0 ? void 0 : _a.find((argNode) => argNode.name.value === 'interval');
|
46
|
+
let intervalMs = defaultInterval;
|
47
|
+
if (((_b = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _b === void 0 ? void 0 : _b.kind) === Kind.INT) {
|
48
|
+
intervalMs = parseInt(intervalArgNode.value.value);
|
49
|
+
}
|
50
|
+
setExecuteFn((args) => new Repeater((push, stop) => {
|
51
|
+
let finished = false;
|
52
|
+
async function pump() {
|
53
|
+
if (finished) {
|
54
|
+
return;
|
55
|
+
}
|
56
|
+
const result = await executeFn(args);
|
57
|
+
if (isAsyncIterable(result)) {
|
58
|
+
push({
|
59
|
+
data: null,
|
60
|
+
errors: [new GraphQLError('Execution returned AsyncIterable which is not supported!')],
|
61
|
+
isLive: true,
|
62
|
+
});
|
63
|
+
stop();
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
result.isLive = true;
|
67
|
+
if (finished) {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
push(result);
|
71
|
+
setTimeout(pump, intervalMs);
|
72
|
+
}
|
73
|
+
pump();
|
74
|
+
stop.then(() => {
|
75
|
+
finished = true;
|
76
|
+
});
|
77
|
+
}));
|
78
|
+
}
|
79
|
+
},
|
80
|
+
};
|
81
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
{
|
2
|
+
"name": "@graphprotocol/client-polling-live",
|
3
|
+
"version": "1.0.0-alpha-20220815093611-dade325",
|
4
|
+
"description": "",
|
5
|
+
"sideEffects": false,
|
6
|
+
"peerDependencies": {
|
7
|
+
"graphql": "^15.2.0 || ^16.0.0",
|
8
|
+
"@graphql-tools/merge": "^8.3.1",
|
9
|
+
"@envelop/core": "^2.4.2"
|
10
|
+
},
|
11
|
+
"dependencies": {
|
12
|
+
"@repeaterjs/repeater": "^3.0.4",
|
13
|
+
"tslib": "^2.4.0"
|
14
|
+
},
|
15
|
+
"repository": {
|
16
|
+
"type": "git",
|
17
|
+
"url": "https://github.com/graphprotocol/graph-client.git",
|
18
|
+
"directory": "packages/polling-live"
|
19
|
+
},
|
20
|
+
"keywords": [
|
21
|
+
"thegraph",
|
22
|
+
"graphql",
|
23
|
+
"client"
|
24
|
+
],
|
25
|
+
"license": "MIT",
|
26
|
+
"main": "cjs/index.js",
|
27
|
+
"module": "esm/index.js",
|
28
|
+
"typings": "typings/index.d.ts",
|
29
|
+
"typescript": {
|
30
|
+
"definition": "typings/index.d.ts"
|
31
|
+
},
|
32
|
+
"type": "module",
|
33
|
+
"exports": {
|
34
|
+
".": {
|
35
|
+
"require": {
|
36
|
+
"types": "./typings/index.d.cts",
|
37
|
+
"default": "./cjs/index.js"
|
38
|
+
},
|
39
|
+
"import": {
|
40
|
+
"types": "./typings/index.d.ts",
|
41
|
+
"default": "./esm/index.js"
|
42
|
+
},
|
43
|
+
"default": {
|
44
|
+
"types": "./typings/index.d.ts",
|
45
|
+
"default": "./esm/index.js"
|
46
|
+
}
|
47
|
+
},
|
48
|
+
"./package.json": "./package.json"
|
49
|
+
}
|
50
|
+
}
|