Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requestOptions accepting function so that we can change options depending on the request #381

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/openapi-to-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,13 @@ Schema options:
Resolver options:

- `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.
_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._

- `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.

- `requestOptions` (type: `object`, 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.
- `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._

- `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.

- `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.

Expand Down
1 change: 1 addition & 0 deletions packages/openapi-to-graphql/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/index.js.map

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions packages/openapi-to-graphql/lib/resolver_builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ import { RequestOptions } from './types/options';
import { GraphQLFieldResolver } from 'graphql';
import { IncomingHttpHeaders } from 'http';
export declare const OPENAPI_TO_GRAPHQL = "_openAPIToGraphQL";
declare type GetResolverParams<TSource, TContext, TArgs> = {
declare type GetSubscribeParams<TSource, TContext, TArgs> = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just cleaning up old code. Just ignore.

operation: Operation;
argsFromLink?: {
[key: string]: string;
};
payloadName?: string;
responseName?: string;
data: PreprocessingData<TSource, TContext, TArgs>;
baseUrl?: string;
requestOptions?: Partial<RequestOptions<TSource, TContext, TArgs>>;
connectOptions: ConnectOptions;
};
declare type GetSubscribeParams<TSource, TContext, TArgs> = {
declare type GetPublishResolverParams<TSource, TContext, TArgs> = {
operation: Operation;
responseName?: string;
data: PreprocessingData<TSource, TContext, TArgs>;
};
declare type GetResolverParams<TSource, TContext, TArgs> = {
operation: Operation;
argsFromLink?: {
[key: string]: string;
};
payloadName?: string;
responseName?: string;
data: PreprocessingData<TSource, TContext, TArgs>;
baseUrl?: string;
connectOptions?: ConnectOptions;
requestOptions: RequestOptions<TSource, TContext, TArgs>;
};
declare type ResolveData<TSource, TContext, TArgs> = {
/**
Expand Down Expand Up @@ -61,7 +66,7 @@ declare type OpenAPIToGraphQLSource<TSource, TContext, TArgs> = {
_openAPIToGraphQL: OpenAPIToGraphQLRoot<TSource, TContext, TArgs>;
};
export declare function getSubscribe<TSource, TContext, TArgs>({ operation, payloadName, data, baseUrl, connectOptions }: GetSubscribeParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, SubscriptionContext, TArgs>;
export declare function getPublishResolver<TSource, TContext, TArgs>({ operation, responseName, data }: GetResolverParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, TContext, TArgs>;
export declare function getPublishResolver<TSource, TContext, TArgs>({ operation, responseName, data }: GetPublishResolverParams<TSource, TContext, TArgs>): GraphQLFieldResolver<TSource, TContext, TArgs>;
/**
* If the operation type is Query or Mutation, create and return a resolver
* function that performs API requests for the given GraphQL query
Expand Down
153 changes: 61 additions & 92 deletions packages/openapi-to-graphql/lib/resolver_builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/resolver_builder.js.map

Large diffs are not rendered by default.

Loading