Skip to content

Commit 9c5f7d7

Browse files
Alan-ChaErikWittern
authored andcommitted
Add customResolvers option
Fix #139 Signed-off-by: Alan Cha <[email protected]>
1 parent fd32280 commit 9c5f7d7

31 files changed

+645
-199
lines changed

packages/oasgraph-cli/src/oasgraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ Promise.all(filePaths.map(filePath => {
8484
/**
8585
* Returns content of read JSON/YAML file.
8686
*
87-
* @param {String} path Path to file to read
88-
* @return {Object} Content of read file
87+
* @param {string} path Path to file to read
88+
* @return {object} Content of read file
8989
*/
9090
function readFile (path) {
9191
try {
@@ -127,7 +127,7 @@ function readFile (path) {
127127

128128
/**
129129
* generates a GraphQL schema and starts the GraphQL server on the specified port
130-
* @param {Object} oas the OAS specification file
130+
* @param {object} oas the OAS specification file
131131
* @param {number} port the port number to listen on on this server
132132
*/
133133
function startGraphQLServer(oas, port) {

packages/oasgraph/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,11 @@ The options object can contain the following properties:
155155

156156
* `operationIdFieldNames` (type: `boolean`, default: `false`): By default, query field names are based on the return type type name and mutation field names are based on the [`operationId`](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#operation-object), which may be generated if it does not exist. This option forces OASGraph to only create field names based on the operationId.
157157

158-
* `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 is to use this to up set a web proxy with the `proxy` field.
158+
* `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.
159159

160-
* `provideErrorExtensions` (type: `boolean`, default: `true`): If a query cannot fulfilled, GraphQL will provide a [list of error objects](https://graphql.github.io/graphql-spec/draft/#sec-Errors) for all fields that could not be resolved. OASGraph will add an extensions to all error objects resulting from REST calls that did not return successful HTTP codes (i.e. 200-299). These extensions contain data about the REST call (e.g. the method, path, status code, response headers, and response body). This data can be useful for debugging but it can also _unintentionally leak information_. This option prevents the error extensions from being created.
160+
* `provideErrorExtensions` (type: `boolean`, default: `true`): If a query cannot be fulfilled, GraphQL provides a [list of error objects](https://graphql.github.io/graphql-spec/draft/#sec-Errors) for all fields that could not be resolved. OASGraph will add an `extensions` object to all error objects resulting from REST calls that did not return successful HTTP codes (i.e. 200-299). Each `extensions` object contain data about the REST call (e.g. the method, path, status code, response headers, and response body). This data can be useful for debugging but it can also _unintentionally leak information_. If set to `false`, this option prevents the `extensions` objects from being created.
161+
162+
* `customResolvers` (type: `object`, default: `{}`): OASGraph, per default, creates resolver functions that make REST calls to resolve 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 OASGraph. 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/Alan-Cha/oasgraph#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/)._
161163

162164
Consider this example of passing options:
163165

@@ -173,6 +175,20 @@ OASGraph.createGraphQLSchema(oas, {
173175
addSubOperations: false,
174176
requestOptions: {
175177
proxy: "http://my-proxy:3128"
178+
},
179+
customResolvers: {
180+
'I <3 Books API': {
181+
'/favoriteBooks/{name}': {
182+
'get': (obj, args, context, info) => {
183+
return {
184+
books: [
185+
'A Guide to OASGraph',
186+
'Why OASGraph is Amazing',
187+
'Long Live OASGraph!'
188+
]
189+
}
190+
}
191+
}
176192
}
177193
})
178194
```

packages/oasgraph/lib/auth_builder.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare type Viewer = {
1818
* i.e. inside either rootQueryFields/rootMutationFields or inside
1919
* rootQueryFields/rootMutationFields for further processing
2020
*/
21-
export declare function createAndLoadViewer(queryFields: Object, data: PreprocessingData, isMutation: boolean, oass: Oas3[]): {
21+
export declare function createAndLoadViewer(queryFields: object, data: PreprocessingData, isMutation: boolean, oass: Oas3[]): {
2222
[key: string]: Viewer;
2323
};
2424
export {};

packages/oasgraph/lib/auth_builder.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/auth_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/oasgraph/lib/index.js

Lines changed: 10 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)