-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.ts
63 lines (59 loc) · 1.54 KB
/
fetch.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
import { createRequest, Options, Params } from "./requests.ts";
import { jsonObject } from "./deps.ts";
import { mergeHeaders } from "./utils.ts";
import { Result } from "./types.ts";
import { resolveResponse } from "./responses.ts";
/** GraphQL client with HTTP.
* @param params parameters
* @param options Options
* @param requestInit Request init for customize HTTP request.
* @throw Error
* @throws TypeError
* @throw SyntaxError
* @throws DOMException
* @throws AggregateError
* ```ts
* import { gqlFetch } from "https://deno.land/x/graphql_http@$VERSION/mod.ts";
*
* const { data, errors, extensions } = await gqlFetch({
* url: `<graphql-endpoint>`,
* query: `query Greet(name: $name) {
* hello(name: $name)
* }
* `,
* }, {
* variables: {
* name: "Bob",
* },
* operationName: "Greet",
* method: "GET",
* });
* ```
*/
export default async function gqlFetch<T extends jsonObject>(
{ url, query }: Readonly<Params>,
{ method: _method = "POST", variables, operationName }: Readonly<
Partial<Options>
> = {},
requestInit?: RequestInit,
): Promise<Result<T>> {
const method = requestInit?.method ?? _method;
const [data, err] = createRequest(
{
url,
query,
method,
},
{ variables, operationName },
);
if (!data) {
throw err;
}
const [headers, error] = mergeHeaders(data.headers, requestInit?.headers);
if (!headers) {
throw error;
}
const req = new Request(data, { ...requestInit, headers });
const res = await fetch(req);
return resolveResponse(res);
}