This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
/
link.ts
133 lines (116 loc) · 3.29 KB
/
link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as Observable from 'zen-observable';
import {
GraphQLRequest,
NextLink,
Operation,
RequestHandler,
FetchResult,
} from './types';
import {
validateOperation,
isTerminating,
LinkError,
transformOperation,
createOperation,
} from './linkUtils';
const passthrough = (op, forward) => (forward ? forward(op) : Observable.of());
const toLink = (handler: RequestHandler | ApolloLink) =>
typeof handler === 'function' ? new ApolloLink(handler) : handler;
export const empty = (): ApolloLink =>
new ApolloLink((op, forward) => Observable.of());
export const from = (links: ApolloLink[]): ApolloLink => {
if (links.length === 0) return empty();
return links.map(toLink).reduce((x, y) => x.concat(y));
};
export const split = (
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right: ApolloLink | RequestHandler = new ApolloLink(passthrough),
): ApolloLink => {
const leftLink = toLink(left);
const rightLink = toLink(right);
if (isTerminating(leftLink) && isTerminating(rightLink)) {
return new ApolloLink(operation => {
return test(operation)
? leftLink.request(operation) || Observable.of()
: rightLink.request(operation) || Observable.of();
});
} else {
return new ApolloLink((operation, forward) => {
return test(operation)
? leftLink.request(operation, forward) || Observable.of()
: rightLink.request(operation, forward) || Observable.of();
});
}
};
// join two Links together
export const concat = (
first: ApolloLink | RequestHandler,
second: ApolloLink | RequestHandler,
) => {
const firstLink = toLink(first);
if (isTerminating(firstLink)) {
console.warn(
new LinkError(
`You are calling concat on a terminating link, which will have no effect`,
firstLink,
),
);
return firstLink;
}
const nextLink = toLink(second);
if (isTerminating(nextLink)) {
return new ApolloLink(
operation =>
firstLink.request(
operation,
op => nextLink.request(op) || Observable.of(),
) || Observable.of(),
);
} else {
return new ApolloLink((operation, forward) => {
return (
firstLink.request(operation, op => {
return nextLink.request(op, forward) || Observable.of();
}) || Observable.of()
);
});
}
};
export class ApolloLink {
constructor(request?: RequestHandler) {
if (request) this.request = request;
}
public static empty = empty;
public static from = from;
public static split = split;
public split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right: ApolloLink | RequestHandler = new ApolloLink(passthrough),
): ApolloLink {
return this.concat(split(test, left, right));
}
public concat(next: ApolloLink | RequestHandler): ApolloLink {
return concat(this, next);
}
public request(
operation: Operation,
forward?: NextLink,
): Observable<FetchResult> | null {
throw new Error('request is not implemented');
}
}
export function execute(
link: ApolloLink,
operation: GraphQLRequest,
): Observable<FetchResult> {
return (
link.request(
createOperation(
operation.context,
transformOperation(validateOperation(operation)),
),
) || Observable.of()
);
}