Skip to content

Commit a128cd4

Browse files
committed
Adds typescript
Signed-off-by: sirugh <rugh@adobe.com>
1 parent 172fc4b commit a128cd4

File tree

6 files changed

+94
-23
lines changed

6 files changed

+94
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
{
22
"name": "apollo-link-mutation-queue",
33
"version": "0.0.1",
4-
"description": "An apollo link that queues mutations.",
5-
"main": "src/index.js",
4+
"description": "An apollo link that queues mutations",
5+
"main": "build/dist/index.js",
6+
"module": "build/dist/index.js",
7+
"typings": "build/dist/index.d.ts",
68
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
9+
"build": "tsc",
10+
"prepare": "yarn build"
811
},
912
"keywords": [
1013
"apollo",
1114
"link",
1215
"mutation",
1316
"queue"
1417
],
15-
"author": "Stephen Rugh",
18+
"author": "Stephen Rugh <rugh@adobe.com>",
1619
"license": "ISC",
1720
"dependencies": {
1821
"apollo-link": "~1.2.13"
22+
},
23+
"devDependencies": {
24+
"@types/graphql": "^14.5.0",
25+
"@types/node": "^13.9.0",
26+
"typescript": "^3.8.3"
1927
}
2028
}

src/index.js renamed to src/MutationQueueLink.ts

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
import { ApolloLink, Observable } from "apollo-link";
1+
import {
2+
ApolloLink,
3+
Observable,
4+
Operation,
5+
FetchResult,
6+
NextLink
7+
} from "apollo-link";
8+
import { Observer } from "zen-observable-ts";
29

3-
const toRequestKey = operation => {
10+
interface OperationQueueEntry {
11+
operation: Operation;
12+
forward: NextLink;
13+
observer: Observer<FetchResult>;
14+
subscription?: { unsubscribe: () => void };
15+
}
16+
17+
const toRequestKey = (operation: Operation) => {
418
return operation.operationName;
519
};
620

@@ -10,23 +24,25 @@ const toRequestKey = operation => {
1024
* To skip the queue pass `{ context: { skipQueue: true } }` to your mutation.
1125
*/
1226
export default class MutationQueueLink extends ApolloLink {
27+
private opQueue: OperationQueueEntry[] = [];
28+
private inProcess: Boolean = false;
29+
private debug: Boolean = false;
30+
1331
/**
14-
* @param {Boolean} props.debug - set to true to enable logging
32+
* @param {Boolean} debug - set to true to enable logging
1533
*/
16-
constructor({ debug = true } = {}) {
34+
constructor({ debug = false } = {}) {
1735
super();
18-
this.opQueue = [];
19-
this.inProcess = false;
2036
this.debug = debug;
2137
}
2238

23-
log(message, ...rest) {
39+
private log(message: String, ...rest: String[]) {
2440
if (this.debug) {
2541
console.log(message, ...rest);
2642
}
2743
}
2844

29-
processOperation(entry) {
45+
private processOperation(entry: OperationQueueEntry) {
3046
const { operation, forward, observer } = entry;
3147
this.inProcess = true;
3248
this.log("[PROCESSING] -", toRequestKey(operation));
@@ -53,7 +69,16 @@ export default class MutationQueueLink extends ApolloLink {
5369
});
5470
}
5571

56-
request(operation, forward) {
72+
private cancelOperation(entry: OperationQueueEntry) {
73+
this.opQueue = this.opQueue.filter(e => e !== entry);
74+
}
75+
76+
private enqueue(entry: OperationQueueEntry) {
77+
this.log("[ENQUEUE] -", toRequestKey(entry.operation));
78+
this.opQueue.push(entry);
79+
}
80+
81+
public request(operation: Operation, forward: NextLink) {
5782
// Enqueue all mutations unless manually skipped.
5883
if (
5984
operation.toKey().includes('"operation":"mutation"') &&
@@ -64,21 +89,12 @@ export default class MutationQueueLink extends ApolloLink {
6489
if (!this.inProcess) {
6590
this.processOperation(operationEntry);
6691
} else {
67-
this.log("[ENQUEUE] -", toRequestKey(operation));
68-
this.opQueue.push(operationEntry);
92+
this.enqueue(operationEntry);
6993
}
7094
return () => this.cancelOperation(operationEntry);
7195
});
7296
} else {
7397
return forward(operation);
7498
}
7599
}
76-
77-
cancelOperation(entry) {
78-
this.opQueue = this.opQueue.filter(e => e !== entry);
79-
}
80-
81-
enqueue(entry) {
82-
this.opQueue.push(entry);
83-
}
84100
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import MutationQueueLink from "./MutationQueueLink";
2+
export default MutationQueueLink;

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"noImplicitAny": true,
7+
"lib": ["esnext"],
8+
"removeComments": true,
9+
"preserveConstEnums": true,
10+
"outDir": "build/dist",
11+
"sourceMap": true
12+
},
13+
"include": ["src/**/*"],
14+
"exclude": ["node_modules", "**/*.test.ts"]
15+
}

yarn.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
# yarn lockfile v1
33

44

5+
"@types/graphql@^14.5.0":
6+
version "14.5.0"
7+
resolved "https://registry.yarnpkg.com/@types/graphql/-/graphql-14.5.0.tgz#a545fb3bc8013a3547cf2f07f5e13a33642b75d6"
8+
integrity sha512-MOkzsEp1Jk5bXuAsHsUi6BVv0zCO+7/2PTiZMXWDSsMXvNU6w/PLMQT2vHn8hy2i0JqojPz1Sz6rsFjHtsU0lA==
9+
dependencies:
10+
graphql "*"
11+
12+
"@types/node@^13.9.0":
13+
version "13.9.0"
14+
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.0.tgz#5b6ee7a77faacddd7de719017d0bc12f52f81589"
15+
integrity sha512-0ARSQootUG1RljH2HncpsY2TJBfGQIKOOi7kxzUY6z54ePu/ZD+wJA8zI2Q6v8rol2qpG/rvqsReco8zNMPvhQ==
16+
517
"@wry/equality@^0.1.2":
618
version "0.1.9"
719
resolved "https://registry.yarnpkg.com/@wry/equality/-/equality-0.1.9.tgz#b13e18b7a8053c6858aa6c85b54911fb31e3a909"
@@ -34,6 +46,18 @@ fast-json-stable-stringify@^2.0.0:
3446
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
3547
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
3648

49+
graphql@*:
50+
version "14.6.0"
51+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.6.0.tgz#57822297111e874ea12f5cd4419616930cd83e49"
52+
integrity sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==
53+
dependencies:
54+
iterall "^1.2.2"
55+
56+
iterall@^1.2.2:
57+
version "1.3.0"
58+
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
59+
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==
60+
3761
ts-invariant@^0.4.0:
3862
version "0.4.4"
3963
resolved "https://registry.yarnpkg.com/ts-invariant/-/ts-invariant-0.4.4.tgz#97a523518688f93aafad01b0e80eb803eb2abd86"
@@ -46,6 +70,11 @@ tslib@^1.10.0, tslib@^1.9.3:
4670
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.11.1.tgz#eb15d128827fbee2841549e171f45ed338ac7e35"
4771
integrity sha512-aZW88SY8kQbU7gpV19lN24LtXh/yD4ZZg6qieAJDDg+YBsJcSmLGK9QpnUjAKVG/xefmvJGd1WUmfpT/g6AJGA==
4872

73+
typescript@^3.8.3:
74+
version "3.8.3"
75+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
76+
integrity sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==
77+
4978
zen-observable-ts@^0.8.20:
5079
version "0.8.20"
5180
resolved "https://registry.yarnpkg.com/zen-observable-ts/-/zen-observable-ts-0.8.20.tgz#44091e335d3fcbc97f6497e63e7f57d5b516b163"

0 commit comments

Comments
 (0)