Skip to content

Commit 4e0ca05

Browse files
committed
fix: Added urlResolver on Resolver options
1 parent 784b89a commit 4e0ca05

File tree

10 files changed

+68
-4
lines changed

10 files changed

+68
-4
lines changed

packages/openapi-to-graphql/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ Resolver options:
180180

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

183+
- `urlResolver` (type: `object` | `function`, default: `{}`): URL used to send every request to the API. 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 url.
184+
183185
- `customResolvers` (type: `object`, default: `{}`): OpenAPI-to-GraphQL, by default, creates resolver functions that make REST calls to resolve Query and Mutation fields in the generated GraphQL interface. This option allows users to provide custom resolver functions to be used in place of said ones created by OpenAPI-to-GraphQL. The field that the custom resolver will affect is identifed first by the [title](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#infoObject) of the OAS, then the [path](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#paths-object) of the operation, and lastly the [method](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-item-object) of the operation. The `customResolvers` object is thus a triply nested object where the outer key is the title, followed by the path, and finally the method, which points to the [resolver function](https://graphql.org/learn/execution/#root-fields-resolvers) itself. The resolver function can use the parameters `obj`, `args`, `context`, and `info` in order to produce the proper data, as do standard [resolver functions](https://graphql.org/learn/execution/#root-fields-resolvers) in GraphQL. Use cases include the resolution of complex relationships between types, implementing performance improvements like caching, or dealing with non-standard authentication requirements. _Note: Because the arguments are provided by the GraphQL interface, they may look different from the [parameters](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterObject) defined by the OAS. For example, they will have [sanitized](https://github.com/IBM/openapi-to-graphql#characteristics) names. The [request body](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#requestBodyObject) will also be contained in the arguments as an [input object type](https://graphql.org/graphql-js/mutations-and-input-types/)._
184186

185187
- `customSubscriptionResolvers` (type: `object`, default: `{}`): If the `createSubscriptionsFromCallbacks` is enabled, OpenAPI-to-GraphQL will generate Subscription fields. This option allows users to provide custom resolver and subscribe functions to be used in place of said ones created by OpenAPI-to-GraphQL. The field that the custom resolver and subscribe functions will affect is identifed first by the [title](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#infoObject) of the OAS, then the [path](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#paths-object) of the operation, and lastly the [method](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#path-item-object) of the operation. The resolver is provided via the `resolver` field and the publish function is provided via the `publish` field. The `customSubscriptionResolvers` object is thus a quadruply nested object where the outer key is the title, followed by the path, then the method, and lastly either `resolver` or `publish` which points to the [resolver function](https://graphql.org/learn/execution/#root-fields-resolvers) itself or publish function. See the [Subscriptions tutorial](./docs/subscriptions.md) for more information. _Note: Because the arguments are provided by the GraphQL interface, they may look different from the [parameters](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterObject) defined by the OAS. For example, they will have [sanitized](https://github.com/IBM/openapi-to-graphql#characteristics) names. The [request body](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#requestBodyObject) will also be contained in the arguments as an [input object type](https://graphql.org/graphql-js/mutations-and-input-types/)._

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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference types="node" />
21
/**
32
* Functions to create resolve functions.
43
*/

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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/types/options.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ export declare type RequestHeadersFunction<TSource, TContext, TArgs> = (method:
4343
context: TContext;
4444
info: GraphQLResolveInfo;
4545
}) => Headers;
46+
/**
47+
* Given a set parameters corresponding to a specific operation in the OAS,
48+
* provide the appropriate url
49+
*/
50+
export declare type RequestURLFunction<TSource, TContext, TArgs> = (method: string, path: string, title: string, resolverParams?: {
51+
source: TSource;
52+
args: TArgs;
53+
context: TContext;
54+
info: GraphQLResolveInfo;
55+
}) => string;
4656
/**
4757
* We rely on the Request library in order to make resolver API calls.
4858
*
@@ -187,6 +197,11 @@ export declare type InternalOptions<TSource, TContext, TArgs> = {
187197
* Overrides the server object in the OAS.
188198
*/
189199
baseUrl?: string;
200+
/**
201+
* Specifies the function which returns a URL, which will be used for request made by a resolve function.
202+
* Overrides the server object in the OAS and baseUrl.
203+
*/
204+
urlResolver?: RequestURLFunction<TSource, TContext, TArgs>;
190205
/**
191206
* Allows to define custom resolvers for fields on the Query/Mutation root
192207
* operation type.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
203203
requestOptions,
204204
connectOptions,
205205
baseUrl,
206+
urlResolver,
206207
customResolvers,
207208
customSubscriptionResolvers,
208209

@@ -242,6 +243,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
242243
requestOptions,
243244
connectOptions,
244245
baseUrl,
246+
urlResolver,
245247
customResolvers,
246248
customSubscriptionResolvers,
247249

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,18 @@ export function getResolver<TSource, TContext, TArgs>({
644644
Object.assign(options.headers, oauthHeader)
645645
}
646646

647+
if (data.options.urlResolver) {
648+
const url = data.options.urlResolver(method, path, title, {
649+
source,
650+
args,
651+
context,
652+
info
653+
});
654+
if (typeof url === 'string') {
655+
options.url = url;
656+
}
657+
}
658+
647659
resolveData.usedRequestOptions = options
648660
resolveData.usedStatusCode = operation.statusCode
649661

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ export type RequestHeadersFunction<TSource, TContext, TArgs> = (
6060
}
6161
) => Headers
6262

63+
/**
64+
* Given a set parameters corresponding to a specific operation in the OAS,
65+
* provide the appropriate url
66+
*/
67+
export type RequestURLFunction<TSource, TContext, TArgs> = (
68+
method: string,
69+
path: string,
70+
title: string,
71+
resolverParams?: {
72+
source: TSource;
73+
args: TArgs;
74+
context: TContext;
75+
info: GraphQLResolveInfo;
76+
}
77+
) => string;
78+
6379
/**
6480
* We rely on the Request library in order to make resolver API calls.
6581
*
@@ -234,6 +250,12 @@ export type InternalOptions<TSource, TContext, TArgs> = {
234250
*/
235251
baseUrl?: string
236252

253+
/**
254+
* Specifies the function which returns a URL, which will be used for request made by a resolve function.
255+
* Overrides the server object in the OAS and baseUrl.
256+
*/
257+
urlResolver?: RequestURLFunction<TSource, TContext, TArgs>;
258+
237259
/**
238260
* Allows to define custom resolvers for fields on the Query/Mutation root
239261
* operation type.

0 commit comments

Comments
 (0)