Skip to content

Commit 55ef609

Browse files
committed
Passing the authHeader correctly to the runner,
also allowing you to explicitly pass ah to graphql
1 parent fa51951 commit 55ef609

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ declare module "@slash-graphql/lambda-types" {
2020

2121
type GraphQLEvent = GraphQLEventFields & {
2222
respondWith: (r: ResolverResponse) => void,
23-
graphql: (s: string, vars: Record<string, any> | undefined) => Promise<GraphQLResponse>,
23+
graphql: (s: string, vars: Record<string, any> | undefined, ah?: AuthHeaderField) => Promise<GraphQLResponse>,
2424
dql: {
2525
query: (s: string, vars: Record<string, any> | undefined) => Promise<GraphQLResponse>
2626
mutate: (s: string) => Promise<GraphQLResponse>

src/evaluate-script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventTarget } from 'event-target-shim';
22
import vm from 'vm';
3-
import { GraphQLEvent, GraphQLEventWithParent, GraphQLEventFields, ResolverResponse } from '@slash-graphql/lambda-types'
3+
import { GraphQLEvent, GraphQLEventWithParent, GraphQLEventFields, ResolverResponse, AuthHeaderField } from '@slash-graphql/lambda-types'
44

55
import fetch, { Request, Response, Headers } from "node-fetch";
66
import { URL } from "url";
@@ -84,7 +84,7 @@ export function evaluateScript(source: string) {
8484
const event = {
8585
...e,
8686
respondWith: (x: ResolverResponse) => { retPromise = x },
87-
graphql: (query: string, variables: Record<string, any>) => graphql(query, variables, e.authHeader),
87+
graphql: (query: string, variables: Record<string, any>, ah?: AuthHeaderField) => graphql(query, variables, ah || e.authHeader),
8888
dql,
8989
}
9090
target.dispatchEvent(event)

src/script-to-express.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,17 @@ describe(scriptToExpress, () => {
4242
.expect(400);
4343
expect(response.body).toEqual("");
4444
})
45+
46+
it("gets the auth header as a key", async () => {
47+
const app = scriptToExpress(`addGraphQLResolvers({
48+
"Query.authHeader": ({authHeader}) => authHeader.key + authHeader.value
49+
})`)
50+
const response = await supertest(app)
51+
.post('/graphql-worker')
52+
.send({ resolver: "Query.authHeader", parents: [{ n: 41 }], authHeader: {key: "foo", value: "bar"} })
53+
.set('Accept', 'application/json')
54+
.expect('Content-Type', /json/)
55+
.expect(200);
56+
expect(response.body).toEqual(["foobar"]);
57+
})
4558
})

src/script-to-express.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function bodyToEvent(b: any): GraphQLEventFields {
77
type: b.resolver,
88
parents: b.parents || null,
99
args: b.args || {},
10+
authHeader: b.authHeader,
1011
}
1112
}
1213

0 commit comments

Comments
 (0)