-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
GQLEndpoint.ts
116 lines (106 loc) · 3.05 KB
/
GQLEndpoint.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
import { Endpoint, EndpointOptions } from '@rest-hooks/endpoint';
import type { Schema } from '@rest-hooks/endpoint';
import GQLNetworkError from './GQLNetworkError.js';
export default class GQLEndpoint<
Variables,
S extends Schema | undefined = Schema | undefined,
M extends true | undefined = true | undefined,
> extends Endpoint<(v: Variables) => Promise<any>, S, M> {
declare url: string;
declare signal?: AbortSignal;
constructor(
url: string,
options?: EndpointOptions<(v: Variables) => Promise<any>, S, M>,
) {
const args = url ? { ...options, url } : options;
super(async function (this: GQLEndpoint<Variables, S>, variables: any) {
return fetch(
this.url,
this.getFetchInit({
body: JSON.stringify({
query: this.getQuery(variables),
variables,
}),
method: 'POST',
signal: this.signal ?? null,
headers: this.getHeaders({ 'Content-Type': 'application/json' }),
}),
)
.then(async res => {
const json = await res.json();
if (json.errors) throw new GQLNetworkError(json.errors);
if (!res.ok) throw new GQLNetworkError([json]);
return json.data;
})
.catch(error => {
// ensure CORS, network down, and parse errors are still caught by NetworkErrorBoundary
if (error instanceof TypeError) {
(error as any).status = 400;
}
throw error;
});
}, args);
return this;
}
key(variables: Variables): string {
// TODO: make this faster
return `${this.getQuery(variables)} ${JSON.stringify(variables)}`;
}
getFetchInit(init: RequestInit): RequestInit {
return init;
}
getQuery(variables: Variables): string {
throw new Error('You must include a query');
}
getHeaders(headers: HeadersInit): HeadersInit {
return headers;
}
errorPolicy(error: any) {
return error.status >= 500 ? ('soft' as const) : undefined;
}
query<
Q extends string | ((variables: any) => string),
S extends Schema | undefined,
E extends GQLEndpoint<any, any> = GQLEndpoint<any, any>,
>(
this: E,
queryOrGetQuery: Q,
schema?: S,
): GQLEndpoint<
Q extends (variables: infer V) => string ? V : any,
S,
undefined
> {
const options: any = {
schema,
getQuery:
typeof queryOrGetQuery === 'function'
? queryOrGetQuery
: () => queryOrGetQuery,
};
return this.extend(options) as any;
}
mutation<
Q extends string | ((variables: any) => string),
S extends Schema | undefined,
E extends GQLEndpoint<any, any> = GQLEndpoint<any, any>,
>(
this: E,
queryOrGetQuery: Q,
schema?: S,
): GQLEndpoint<
Q extends (variables: infer V) => string ? V : any,
S,
undefined
> {
const options: any = {
sideEffect: true,
schema,
getQuery:
typeof queryOrGetQuery === 'function'
? queryOrGetQuery
: () => queryOrGetQuery,
};
return this.extend(options) as any;
}
}