Skip to content
This repository was archived by the owner on Sep 5, 2025. It is now read-only.

Commit 39c3d63

Browse files
committed
Passing auth headers back to dgraph
1 parent 2496fb7 commit 39c3d63

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

slash-graphql-lambda-types/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ module "@slash-graphql/lambda-types" {
44
errors?: { message: string }[]
55
}
66

7+
type AuthHeaderField = {
8+
key: string | undefined,
9+
value: string | undefined
10+
}
11+
712
type GraphQLEventFields = {
813
type: string,
914
parents: (Record<string, any>)[] | null,
1015
args: Record<string, any>,
16+
authHeader?: AuthHeaderField
1117
}
1218

1319
type ResolverResponse = any[] | Promise<any>[] | Promise<any[]>;

src/dgraph.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import fetch from 'node-fetch';
2-
import { GraphQLResponse } from '@slash-graphql/lambda-types';
2+
import { GraphQLResponse, AuthHeaderField } from '@slash-graphql/lambda-types';
33

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+
}
59
const response = await fetch(`${process.env.DGRAPH_URL}/graphql`, {
610
method: "POST",
7-
headers: { "Content-Type": "application/json" },
11+
headers,
812
body: JSON.stringify({query, variables})
913
})
1014
if(response.status !== 200) {
@@ -16,7 +20,10 @@ export async function graphql(query: string, variables: Record<string, any> = {}
1620
export async function dql(query: string, variables: Record<string, any> = {}): Promise<GraphQLResponse> {
1721
const response = await fetch(`${process.env.DGRAPH_URL}/query`, {
1822
method: "POST",
19-
headers: { "Content-Type": "application/json" },
23+
headers: {
24+
"Content-Type": "application/json",
25+
"X-Auth-Token": process.env.DGRAPH_TOKEN || ""
26+
},
2027
body: JSON.stringify({ query, variables })
2128
})
2229
if (response.status !== 200) {

0 commit comments

Comments
 (0)