-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
27 lines (23 loc) · 752 Bytes
/
index.ts
File metadata and controls
27 lines (23 loc) · 752 Bytes
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
import { DocumentNode } from 'graphql'
import { Client } from 'urql'
export const createUrqlRequester = <Context, E = unknown>(client: Client) => {
return async <Result, Variables>(
document: DocumentNode,
parameters?: Variables,
context?: Context
): Promise<Result> => {
let op = client.query
if (document.kind === 'Document') {
for (const def of document.definitions) {
if (def.kind === 'OperationDefinition' && def.operation === 'mutation')
op = client.mutation
}
}
return op(document, parameters as Record<string, unknown>, context)
.toPromise()
.then(data => {
if (data.error) return Promise.reject(data.error)
return data.data as Result
})
}
}