1
1
import fetch from 'node-fetch' ;
2
- import { GraphQLResponse } from '@slash-graphql/lambda-types' ;
2
+ import { GraphQLResponse , AuthHeaderField } from '@slash-graphql/lambda-types' ;
3
3
4
- export async function graphql ( query : string , variables : Record < string , any > = { } ) : Promise < GraphQLResponse > {
4
+ export async function graphql ( query : string , variables : Record < string , any > = { } , authHeader : AuthHeaderField ) : Promise < GraphQLResponse > {
5
+ const headers : Record < string , string > = { "Content-Type" : "application/json" } ;
6
+ if ( authHeader && authHeader . key && authHeader . value ) {
7
+ headers [ authHeader . key ] = headers [ authHeader . value ] ;
8
+ }
5
9
const response = await fetch ( `${ process . env . DGRAPH_URL } /graphql` , {
6
10
method : "POST" ,
7
- headers : { "Content-Type" : "application/json" } ,
11
+ headers,
8
12
body : JSON . stringify ( { query, variables} )
9
13
} )
10
14
if ( response . status !== 200 ) {
@@ -16,7 +20,10 @@ export async function graphql(query: string, variables: Record<string, any> = {}
16
20
export async function dql ( query : string , variables : Record < string , any > = { } ) : Promise < GraphQLResponse > {
17
21
const response = await fetch ( `${ process . env . DGRAPH_URL } /query` , {
18
22
method : "POST" ,
19
- headers : { "Content-Type" : "application/json" } ,
23
+ headers : {
24
+ "Content-Type" : "application/json" ,
25
+ "X-Auth-Token" : process . env . DGRAPH_TOKEN || ""
26
+ } ,
20
27
body : JSON . stringify ( { query, variables } )
21
28
} )
22
29
if ( response . status !== 200 ) {
0 commit comments