forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.tsx
55 lines (51 loc) · 1.94 KB
/
graphql.tsx
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
import { Observable } from 'rxjs'
import { GraphQLResult, requestGraphQL as requestGraphQLCommon } from '../../../shared/src/graphql/graphql'
import * as GQL from '../../../shared/src/graphql/schema'
const getHeaders = (): { [header: string]: string } => ({
...window.context.xhrHeaders,
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Sourcegraph-Should-Trace': new URLSearchParams(window.location.search).get('trace') || 'false',
})
/**
* Does a GraphQL request to the Sourcegraph GraphQL API running under `/.api/graphql`
*
* @param request The GraphQL request (query or mutation)
* @param variables A key/value object with variable values
* @returns Observable That emits the result or errors if the HTTP request failed
*/
export const requestGraphQL = <T extends GQL.IQuery | GQL.IMutation>(
request: string,
variables?: {}
): Observable<GraphQLResult<T>> =>
requestGraphQLCommon({
request,
variables,
headers: getHeaders(),
})
/**
* Does a GraphQL query to the Sourcegraph GraphQL API running under `/.api/graphql`
*
* @param request The GraphQL query
* @param variables A key/value object with variable values
* @returns Observable That emits the result or errors if the HTTP request failed
*/
export const queryGraphQL = (request: string, variables?: {}): Observable<GraphQLResult<GQL.IQuery>> =>
requestGraphQLCommon({
request,
variables,
headers: getHeaders(),
})
/**
* Does a GraphQL mutation to the Sourcegraph GraphQL API running under `/.api/graphql`
*
* @param request The GraphQL mutation
* @param variables A key/value object with variable values
* @returns Observable That emits the result or errors if the HTTP request failed
*/
export const mutateGraphQL = (request: string, variables?: {}): Observable<GraphQLResult<GQL.IMutation>> =>
requestGraphQLCommon({
request,
variables,
headers: getHeaders(),
})