Skip to content

Commit bd7a921

Browse files
Yoon LeeAlan-Cha
Yoon Lee
authored andcommitted
Allow 'headers' resolver option to be a function
Signed-off-by: Yoon Lee <[email protected]>
1 parent c8926d8 commit bd7a921

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

packages/openapi-to-graphql/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Schema options:
170170

171171
Resolver options:
172172

173-
- `headers` (type: `object`, default: `{}`): OpenAPI-to-GraphQL, by default, will create arguments for header parameters. This option allows users to stop this behavior and manually inject headers into resolver calls.
173+
- `headers` (type: `object` | `function`, default: `{}`): Headers to be sent in every request to the API described by the given OAS. Parameters defined in the OpenAPI Specification to set these headers will be ignored by OpenAPI-to-GraphQL. If used as a function, the following parameters will be exposed per-request: `req` (the GraphQL resolver's context.request object), `method`, `path`, and `title`.
174174

175175
- `qs` (type: `object`, default: `{}`): Query parameters to be sent in every request to the API described by the given OAS. Parameters defined in the OpenAPI Specification to set these query parameters will be ignored by OpenAPI-to-GraphQL.
176176

packages/openapi-to-graphql/lib/resolver_builder.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { Operation } from './types/operation';
33
import { ResolveFunction, SubscriptionIterator } from './types/graphql';
44
import { PreprocessingData } from './types/preprocessing_data';
55
import * as NodeRequest from 'request';
6+
import { RequestHeadersFunction } from './types/options';
7+
declare type RequestOptions = Omit<NodeRequest.OptionsWithUrl, 'headers'> & {
8+
headers: {
9+
[key: string]: string;
10+
} | RequestHeadersFunction;
11+
};
612
declare type GetResolverParams = {
713
operation: Operation;
814
argsFromLink?: {
@@ -12,7 +18,7 @@ declare type GetResolverParams = {
1218
responseName?: string;
1319
data: PreprocessingData;
1420
baseUrl?: string;
15-
requestOptions?: NodeRequest.OptionsWithUrl;
21+
requestOptions?: RequestOptions;
1622
};
1723
declare type GetSubscribeParams = {
1824
operation: Operation;

packages/openapi-to-graphql/lib/resolver_builder.js

+13-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/resolver_builder.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/src/resolver_builder.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from './types/graphql'
1919
import { PreprocessingData } from './types/preprocessing_data'
2020
import * as NodeRequest from 'request'
21+
import { RequestHeadersFunction } from './types/options'
2122

2223
// Imports:
2324
import * as Oas3Tools from './oas_3_tools'
@@ -46,14 +47,18 @@ type AuthOptions = {
4647
authCookie: NodeRequest.Cookie
4748
}
4849

50+
type RequestOptions = Omit<NodeRequest.OptionsWithUrl, 'headers'> & {
51+
headers: { [key: string]: string } | RequestHeadersFunction
52+
}
53+
4954
type GetResolverParams = {
5055
operation: Operation
5156
argsFromLink?: { [key: string]: string }
5257
payloadName?: string
5358
responseName?: string
5459
data: PreprocessingData
5560
baseUrl?: string
56-
requestOptions?: NodeRequest.OptionsWithUrl
61+
requestOptions?: RequestOptions
5762
}
5863

5964
type GetSubscribeParams = {
@@ -458,8 +463,18 @@ export function getResolver({
458463
options = { ...requestOptions }
459464
options['method'] = operation.method
460465
options['url'] = url
461-
if (options.headers) {
462-
Object.assign(options.headers, headers)
466+
if (requestOptions.headers) {
467+
if (typeof requestOptions.headers === 'object') {
468+
Object.assign(requestOptions.headers, headers)
469+
} else if (typeof requestOptions.headers === 'function') {
470+
const val = requestOptions.headers({
471+
req: (ctx as any).request,
472+
method,
473+
path,
474+
title
475+
})
476+
Object.assign(options.headers, val)
477+
}
463478
} else {
464479
options['headers'] = headers
465480
}

packages/openapi-to-graphql/src/types/options.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Type imports:
77
import * as NodeRequest from 'request'
8-
import { ResolveFunction, ResolveObject, GraphQLOperationType } from './graphql'
8+
import { GraphQLOperationType } from './graphql'
99

1010
/**
1111
* Type definition of the options that users can pass to OpenAPI-to-GraphQL.
@@ -43,6 +43,13 @@ export type OasTitlePathMethodObject<T> = {
4343

4444
export type Options = Partial<InternalOptions>
4545

46+
export type RequestHeadersFunction = (params: {
47+
req: any
48+
method: string
49+
path: string
50+
title: string
51+
}) => object
52+
4653
export type InternalOptions = {
4754
/**
4855
* Adhere to the OAS as closely as possible. If set to true, any deviation
@@ -152,7 +159,7 @@ export type InternalOptions = {
152159
/**
153160
* Custom headers to send with every request made by a resolve function.
154161
*/
155-
headers?: { [key: string]: string }
162+
headers?: { [key: string]: string } | RequestHeadersFunction
156163

157164
/**
158165
* Custom query parameters to send with every reqeust by a resolve function.

0 commit comments

Comments
 (0)