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 347
/
link.ts
138 lines (122 loc) · 3.39 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
134
135
136
137
138
import Observable from 'zen-observable-ts';
import { invariant, InvariantError } from 'ts-invariant';
import {
GraphQLRequest,
NextLink,
Operation,
RequestHandler,
FetchResult,
} from './types';
import {
validateOperation,
isTerminating,
LinkError,
transformOperation,
createOperation,
} from './linkUtils';
function passthrough(op, forward) {
return forward ? forward(op) : Observable.of();
}
function toLink(handler: RequestHandler | ApolloLink) {
return typeof handler === 'function' ? new ApolloLink(handler) : handler;
}
export function empty(): ApolloLink {
return new ApolloLink(() => Observable.of());
}
export function from(links: ApolloLink[]): ApolloLink {
if (links.length === 0) return empty();
return links.map(toLink).reduce((x, y) => x.concat(y));
}
export function split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right?: ApolloLink | RequestHandler,
): ApolloLink {
const leftLink = toLink(left);
const rightLink = toLink(right || new ApolloLink(passthrough));
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)) {
invariant.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 {
public static empty = empty;
public static from = from;
public static split = split;
public static execute = execute;
constructor(request?: RequestHandler) {
if (request) this.request = request;
}
public split(
test: (op: Operation) => boolean,
left: ApolloLink | RequestHandler,
right?: ApolloLink | RequestHandler,
): ApolloLink {
return this.concat(split(test, left, right || new ApolloLink(passthrough)));
}
public concat(next: ApolloLink | RequestHandler): ApolloLink {
return concat(this, next);
}
public request(
operation: Operation,
forward?: NextLink,
): Observable<FetchResult> | null {
throw new InvariantError('request is not implemented');
}
}
export function execute(
link: ApolloLink,
operation: GraphQLRequest,
): Observable<FetchResult> {
return (
link.request(
createOperation(
operation.context,
transformOperation(validateOperation(operation)),
),
) || Observable.of()
);
}