Skip to content

Commit 4d478ab

Browse files
committed
Modify requestOptions
Signed-off-by: Alan Cha <[email protected]>
1 parent 0b0f8e7 commit 4d478ab

19 files changed

+471
-441
lines changed

packages/openapi-to-graphql/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ Schema options:
173173
Resolver options:
174174

175175
- `headers` (type: `object` | `function`, default: `{}`): Headers to be sent in every request to the API. Parameters defined in the OpenAPI Specification and set by these headers will be ignored by OpenAPI-to-GraphQL. If used as a function, the following parameters will be exposed per-request: the operation's `method`, the operation's `path`, the API `title`, and `resolverParams` (the [GraphQL resolver's parameters](https://graphql.org/learn/execution/#root-fields-resolvers)). The function should return the desired headers.
176+
_Note: If used as a function, this will be used to modify resolvers AND the schema. For example, if a `foo` header is set, then the resolver call will append the `foo` header, and the `foo` parameter will be removed from the GraphQL schema. In `resolver_builder.ts`, the function will be called with all arguments, but in `schema_builder.ts`, only the `method`, `path`, and `title` will be provided (because `resolverParams` and `generatedRequestOptions` are only available during execution). Therefore, for query, path, header, and cookie parameters, a value must be provided without `resolverParam` and `generatedRequestOptions` or else the schema will not have the right parameters._
176177

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

179-
- `requestOptions` (type: `object` | `function`, default: `{}`): Additional [options](https://github.com/request/request#requestoptions-callback), provided by the [`Request` module](https://github.com/request/request), that can be used to configure the HTTP calls that powers the generated GraphQL resolvers. A common use case for this option is to set up a web proxy with the `proxy` field. Parameters defined in the OpenAPI Specification and set by this option will be ignored by OpenAPI-to-GraphQL. Additionally, the `headers` field has the feature of being used as a function. If used as a function, the following parameters will be exposed per-request: the operation's `method`, the operation's `path`, the API `title`, and `resolverParams` (the [GraphQL resolver's parameters](https://graphql.org/learn/execution/#root-fields-resolvers)). The function should return the desired headers. If `requestOptions` is used as a function, the following parameters will be exposed per-request: the operation's `method`, the operation's `path`, the API `title`, and `resolverParams` (the [GraphQL resolver's parameters](https://graphql.org/learn/execution/#root-fields-resolvers)). It should return object of type [options](https://github.com/request/request#requestoptions-callback). If `requestOptions` is used as a function `url` and `method` can also be set.
180+
- `requestOptions` (type: `object` | `function`, default: `{}`): Additional [options](https://github.com/request/request#requestoptions-callback), provided by the [`Request` module](https://github.com/request/request), that can be used to configure the HTTP calls that powers the generated GraphQL resolvers. A common use case for this option is to add headers or query string parameters to every request or to set up a web proxy with the `proxy` field. Parameters defined in the OpenAPI Specification and set by this option will be ignored by OpenAPI-to-GraphQL. If `requestOptions` is used as a function, the following parameters will be exposed per request: the operation's `method`, the operation's `path`, the API `title`, and `resolverParams` (the [GraphQL resolver's parameters](https://graphql.org/learn/execution/#root-fields-resolvers)). The function should return an [options](https://github.com/request/request#requestoptions-callback) object. _Note: If used as a function, this will be used to modify resolvers AND the schema. For example, if a `foo` header is set, then the resolver call will append the `foo` header, and the `foo` parameter will be removed from the GraphQL schema. In `resolver_builder.ts`, the function will be called with all arguments, but in `schema_builder.ts`, only the `method`, `path`, and `title` will be provided (because `resolverParams` and `generatedRequestOptions` are only available during execution). Therefore, for query, path, header, and cookie parameters, a value must be provided without `resolverParam` and `generatedRequestOptions` or else the schema will not have the right parameters._
181+
182+
- `connectOptions` (type: `object`, default: `{}`): Allows to override or add options to the PubSub connect object used to make publish/subscribe to the API backend, e.g. setup the web proxy to use.
180183

181184
- `baseUrl` (type: `string`): Used to manually specify the base URL which all paths will be built on. Normally, OpenAPI-to-GraphQL will select a base URL from the [server object](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#serverObject) defined in the OAS. However, if the server object contains multiple URLs, OpenAPI-to-GraphQL will randomly select one. The purpose of this option is to provide greater control over the base URL in these situations, especially when the OAS cannot be modified. This option may also prove to be useful in testing and development.
182185

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

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

packages/openapi-to-graphql/lib/index.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/lib/resolver_builder.d.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,35 @@ import { ConnectOptions } from './types/options';
77
import { Operation } from './types/operation';
88
import { SubscriptionContext } from './types/graphql';
99
import { PreprocessingData } from './types/preprocessing_data';
10-
import { RequestOptions, RequestOptionsFunction } from './types/options';
10+
import { RequestOptions } from './types/options';
1111
import { GraphQLFieldResolver } from 'graphql';
1212
import { IncomingHttpHeaders } from 'http';
1313
export declare const OPENAPI_TO_GRAPHQL = "_openAPIToGraphQL";
14-
declare type GetResolverParams<TSource, TContext, TArgs> = {
14+
declare type GetSubscribeParams<TSource, TContext, TArgs> = {
1515
operation: Operation;
1616
argsFromLink?: {
1717
[key: string]: string;
1818
};
1919
payloadName?: string;
20-
responseName?: string;
2120
data: PreprocessingData<TSource, TContext, TArgs>;
2221
baseUrl?: string;
23-
requestOptions?: Partial<RequestOptions<TSource, TContext, TArgs>> | RequestOptionsFunction<TSource, TContext, TArgs>;
22+
connectOptions: ConnectOptions;
2423
};
25-
declare type GetSubscribeParams<TSource, TContext, TArgs> = {
24+
declare type GetPublishResolverParams<TSource, TContext, TArgs> = {
25+
operation: Operation;
26+
responseName?: string;
27+
data: PreprocessingData<TSource, TContext, TArgs>;
28+
};
29+
declare type GetResolverParams<TSource, TContext, TArgs> = {
2630
operation: Operation;
2731
argsFromLink?: {
2832
[key: string]: string;
2933
};
3034
payloadName?: string;
35+
responseName?: string;
3136
data: PreprocessingData<TSource, TContext, TArgs>;
3237
baseUrl?: string;
33-
connectOptions?: ConnectOptions;
38+
requestOptions: RequestOptions<TSource, TContext, TArgs>;
3439
};
3540
declare type ResolveData<TSource, TContext, TArgs> = {
3641
/**
@@ -61,7 +66,7 @@ declare type OpenAPIToGraphQLSource<TSource, TContext, TArgs> = {
6166
_openAPIToGraphQL: OpenAPIToGraphQLRoot<TSource, TContext, TArgs>;
6267
};
6368
export declare function getSubscribe<TSource, TContext, TArgs>({ operation, payloadName, data, baseUrl, connectOptions }: GetSubscribeParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, SubscriptionContext, TArgs>;
64-
export declare function getPublishResolver<TSource, TContext, TArgs>({ operation, responseName, data }: GetResolverParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, TContext, TArgs>;
69+
export declare function getPublishResolver<TSource, TContext, TArgs>({ operation, responseName, data }: GetPublishResolverParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, TContext, TArgs>;
6570
/**
6671
* If the operation type is Query or Mutation, create and return a resolver
6772
* function that performs API requests for the given GraphQL query

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

+62-109
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.

0 commit comments

Comments
 (0)