Skip to content

Commit c8926d8

Browse files
committed
Docs and typing improvements
Signed-off-by: Alan Cha <[email protected]>
1 parent c2dccc3 commit c8926d8

File tree

11 files changed

+44
-494
lines changed

11 files changed

+44
-494
lines changed

docs/tutorials/watson.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ The following code shows all that's needed to create and run a GraphQL wrapper a
1515
```javascript
1616
const express = require('express')
1717
const graphqlHTTP = require('express-graphql')
18-
const OTG = require('openapi-to-graphql')
18+
const OtG = require('openapi-to-graphql')
1919
const bodyParser = require('body-parser')
2020

2121
async function startServer () {
2222
// use OpenAPI-to-GraphQL to create a GraphQL schema:
2323
const oas = require('path/to/language-translator-v2.json')
24-
const {schema} = await OTG.createGraphQLSchema(oas)
24+
const {schema} = await OtG.createGraphQLSchema(oas)
2525

2626
// setup Express.js app and serve the schema:
2727
const app = express()

packages/openapi-to-graphql/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ Schema options:
160160

161161
- `genericPayloadArgName` (type: `boolean`, default: `false`): Set the default argument name for the payload of a mutation to `requestBody`. Otherwise, the name will default to the camelCased pathname.
162162

163-
- `simpleNames` (type: `boolean`, default: `false`): By default, field names are sanitized to conform with GraphQL conventions, i.e. types should be in PascalCase, fields should be in camelCase, and enum values should be in ALL_CAPS. This option will prevent OTG from enforcing camelCase field names and PascalCase type names, only removing illegal characters and staying as true to the provided names in the OAS as possible.
163+
- `simpleNames` (type: `boolean`, default: `false`): By default, field names are sanitized to conform with GraphQL conventions, i.e. types should be in PascalCase, fields should be in camelCase, and enum values should be in ALL_CAPS. This option will prevent OpenAPI-to-GraphQL from enforcing camelCase field names and PascalCase type names, only removing illegal characters and staying as true to the provided names in the OAS as possible.
164164

165-
- `singularNames` (type: `boolean`, default: `false`): Experimental feature that will try to create more meaningful names from the operation path than the response object by leveraging common conventions. For example, given the operation `GET /users/{userId}/car`, OtG will create a `Query` field `userCar`. Note that because `users` is followed by the parameter `userId`, it insinuates that this operation will get the car that belongs to a singular user. Hence, the name `userCar` is more fitting than `usersCar` so the pluralizing 's' is dropped. This option will also consider irregular plural forms.
165+
- `singularNames` (type: `boolean`, default: `false`): Experimental feature that will try to create more meaningful names from the operation path than the response object by leveraging common conventions. For example, given the operation `GET /users/{userId}/car`, OpenAPI-to-GraphQL will create a `Query` field `userCar`. Note that because `users` is followed by the parameter `userId`, it insinuates that this operation will get the car that belongs to a singular user. Hence, the name `userCar` is more fitting than `usersCar` so the pluralizing 's' is dropped. This option will also consider irregular plural forms.
166166

167167
- `createSubscriptionsFromCallbacks` (type: `boolean`, default: `false`): Generates subscription fields from [callback objects](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#CallbackObject). The keys ([runtime expressions](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#runtimeExpression)) of the callback objects will be interpolated as the topic of a publish/subscription connection using [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions). Read the [doc](./docs/subscriptions.md) for explanations and examples regarding its usage.
168168

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/oas_3_tools.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/schema_builder.js

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

packages/openapi-to-graphql/lib/schema_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: 11 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -22,205 +22,14 @@ export declare type Report = {
2222
export declare type ConnectOptions = {
2323
[key: string]: boolean | number | string;
2424
};
25-
export declare type Options = {
26-
/**
27-
* Adhere to the OAS as closely as possible. If set to true, any deviation
28-
* from the OAS will lead OpenAPI-to-GraphQL to throw.
29-
*/
30-
strict?: boolean;
31-
/**
32-
* Field names can only be sanitized operationIds
33-
*
34-
* By default, query field names are based on the return type type name and
35-
* mutation field names are based on the operationId, which may be generated
36-
* if it does not exist.
37-
*
38-
* This option forces OpenAPI-to-GraphQL to only create field names based on the
39-
* operationId.
40-
*/
41-
operationIdFieldNames?: boolean;
42-
/**
43-
* Under certain circumstances (such as response code 204), some RESTful
44-
* operations should not return any data. However, GraphQL objects must have
45-
* a data structure. Normally, these operations would be ignored but for the
46-
* sake of completeness, the following option will give these operations a
47-
* placeholder data structure. Even though the data structure will not have
48-
* any practical use, at least the operations will show up in the schema.
49-
*/
50-
fillEmptyResponses?: boolean;
51-
/**
52-
* Auto-generate a 'limit' argument for all fields that return lists of
53-
* objects, including ones produced by links
54-
*
55-
* Allows to constrain the return size of lists of objects
56-
*
57-
* Returns the first n number of elements in the list
58-
*/
59-
addLimitArgument?: boolean;
60-
/**
61-
* If a schema is of type string and has format UUID, it will be translated
62-
* into a GraphQL ID type. To allow for more customzation, this option allows
63-
* users to specify other formats that should be interpreted as ID types.
64-
*/
65-
idFormats?: string[];
66-
/**
67-
* Allows to define the root operation type (Query or Mutation type) of any
68-
* OAS operation explicitly.
69-
*
70-
* OtG will by default make all GET operations Query fields and all other
71-
* operations into Mutation fields.
72-
*
73-
* The field is identifed first by the title of the OAS, then the path of the
74-
* operation, and lastly the method of the operation.
75-
*/
76-
selectQueryOrMutationField?: selectQueryOrMutationFieldType;
77-
/**
78-
* Sets argument name for the payload of a mutation to 'requestBody'
79-
*/
80-
genericPayloadArgName?: boolean;
81-
/**
82-
* By default, field names are sanitized to conform with GraphQL conventions,
83-
* i.e. types should be in PascalCase, fields should be in camelCase, and
84-
* enum values should be in ALL_CAPS.
85-
*
86-
* This option will prevent OtG from enforcing camelCase field names and
87-
* PascalCase type names, only removing illegal characters and staying as true
88-
* to the provided names in the OAS as possible.
89-
*/
90-
simpleNames?: boolean;
91-
/**
92-
* Experimental feature that will try to create more meaningful names from
93-
* the operation path than the response object by leveraging common
94-
* conventions.
95-
*
96-
* For example, given the operation 'GET /users/{userId}/car', OtG will
97-
* create a Query field 'userCar'. Note that because 'users' is followed by
98-
* the parameter 'userId', it insinuates that this operation will get the car
99-
* that belongs to a singular user. Hence, the name 'userCar' is more fitting
100-
* than 'usersCar' so the pluralizing 's' is dropped.
101-
*
102-
* This option will also consider irregular plural forms.
103-
*/
104-
singularNames?: boolean;
105-
/**
106-
* Allow to generate subscription fields from callback objects in the OAS.
107-
*
108-
* The keys (runtime expressions) of the callback object will be interpolated
109-
* as the topic of publish/subscription connection.
110-
*/
111-
createSubscriptionsFromCallbacks?: boolean;
112-
/**
113-
* Custom headers to send with every request made by a resolve function.
114-
*/
115-
headers?: {
116-
[key: string]: string;
117-
};
118-
/**
119-
* Custom query parameters to send with every reqeust by a resolve function.
120-
*/
121-
qs?: {
122-
[key: string]: string;
123-
};
124-
/**
125-
* Allows to override or add options to the node's request object used to make
126-
* calls to the API backend.
127-
* e.g. Setup the web proxy to use.
128-
*/
129-
requestOptions?: NodeRequest.OptionsWithUrl;
130-
/**
131-
* Allows to override or add options to the PubSub connect object used to make
132-
* publish/subscribe to the API backend.
133-
* e.g. Setup the web proxy to use.
134-
*/
135-
connectOptions?: ConnectOptions;
136-
/**
137-
* Specifies the URL on which all paths will be based on.
138-
* Overrides the server object in the OAS.
139-
*/
140-
baseUrl?: string;
141-
/**
142-
* Allows to define custom resolvers for fields on the Query/Mutation root
143-
* operation type.
144-
*
145-
* In other words, instead of resolving on an operation (REST call) defined in
146-
* the OAS, the field will resolve on the custom resolver. Note that this will
147-
* also affect the behavior of links.
148-
*
149-
* The field is identifed first by the title of the OAS, then the path of the
150-
* operation, and lastly the method of the operation.
151-
*
152-
* Use cases include the resolution of complex relationships between types,
153-
* implementing performance improvements like caching, or dealing with
154-
* non-standard authentication requirements.
155-
*/
156-
customResolvers?: {
157-
[title: string]: {
158-
[path: string]: {
159-
[method: string]: ResolveFunction;
160-
};
161-
};
162-
};
163-
/**
164-
* Allows to define custom resolvers and subscribe functions for fields on the
165-
* Subscription root operation type.
166-
*
167-
* In other words, instead of resolving on an operation (REST call) defined in
168-
* the OAS, the field will resolve on the custom resolver. Note that this will
169-
* also affect the behavior of links.
170-
*
171-
* The field is identifed first by the title of the OAS, then the path of the
172-
* operation, and lastly the method of the operation.
173-
*
174-
* Use cases include the resolution of complex relationships between types,
175-
* implementing performance improvements like caching, or dealing with
176-
* non-standard authentication requirements.
177-
*
178-
* Note: Subscription fields will only be generated if the
179-
* createSubscriptionsFromCallbacks option is enabled.
180-
*/
181-
customSubscriptionResolvers?: {
182-
[title: string]: {
183-
[path: string]: {
184-
[method: string]: ResolveObject;
185-
};
25+
export declare type OasTitlePathMethodObject<T> = {
26+
[title: string]: {
27+
[path: string]: {
28+
[method: string]: T;
18629
};
18730
};
188-
/**
189-
* Determines whether OpenAPI-to-GraphQL should create viewers that allow users to pass
190-
* basic auth and API key credentials.
191-
*/
192-
viewer?: boolean;
193-
/**
194-
* JSON path to OAuth 2 token contained in GraphQL context. Tokens will per
195-
* default be sent in "Authorization" header.
196-
*/
197-
tokenJSONpath?: string;
198-
/**
199-
* Determines whether to send OAuth 2 token as query parameter instead of in
200-
* header.
201-
*/
202-
sendOAuthTokenInQuery?: boolean;
203-
/**
204-
* The error extensions is part of the GraphQLErrors that will be returned if
205-
* the query cannot be fulfilled. It provides information about the failed
206-
* REST call(e.g. the method, path, status code, response
207-
* headers, and response body). It can be useful for debugging but may
208-
* unintentionally leak information.
209-
*
210-
* This option prevents the extensions from being created.
211-
*/
212-
provideErrorExtensions?: boolean;
213-
/**
214-
* Appends a small statement to the end of field description that clarifies
215-
* the operation that the field will trigger.
216-
*
217-
* Will affect query and mutation fields as well as fields created from links
218-
*
219-
* In the form of: 'Equivalent to {title of OAS} {method in ALL_CAPS} {path}'
220-
* Will forgo the title is only one OAS is provided
221-
*/
222-
equivalentToMessages?: boolean;
22331
};
32+
export declare type Options = Partial<InternalOptions>;
22433
export declare type InternalOptions = {
22534
/**
22635
* Adhere to the OAS as closely as possible. If set to true, any deviation
@@ -270,13 +79,13 @@ export declare type InternalOptions = {
27079
* Allows to define the root operation type (Query or Mutation type) of any
27180
* OAS operation explicitly.
27281
*
273-
* OtG will by default make all GET operations Query fields and all other
82+
* OpenAPI-to-GraphQL will by default make all GET operations Query fields and all other
27483
* operations into Mutation fields.
27584
*
27685
* The field is identifed first by the title of the OAS, then the path of the
27786
* operation, and lastly the method of the operation.
27887
*/
279-
selectQueryOrMutationField?: selectQueryOrMutationFieldType;
88+
selectQueryOrMutationField?: OasTitlePathMethodObject<GraphQLOperationType>;
28089
/**
28190
* Sets argument name for the payload of a mutation to 'requestBody'
28291
*/
@@ -286,7 +95,7 @@ export declare type InternalOptions = {
28695
* i.e. types should be in PascalCase, fields should be in camelCase, and
28796
* enum values should be in ALL_CAPS.
28897
*
289-
* This option will prevent OtG from enforcing camelCase field names and
98+
* This option will prevent OpenAPI-to-GraphQL from enforcing camelCase field names and
29099
* PascalCase type names, only removing illegal characters and staying as true
291100
* to the provided names in the OAS as possible.
292101
*/
@@ -296,7 +105,7 @@ export declare type InternalOptions = {
296105
* the operation path than the response object by leveraging common
297106
* conventions.
298107
*
299-
* For example, given the operation 'GET /users/{userId}/car', OtG will
108+
* For example, given the operation 'GET /users/{userId}/car', OpenAPI-to-GraphQL will
300109
* create a Query field 'userCar'. Note that because 'users' is followed by
301110
* the parameter 'userId', it insinuates that this operation will get the car
302111
* that belongs to a singular user. Hence, the name 'userCar' is more fitting
@@ -356,13 +165,7 @@ export declare type InternalOptions = {
356165
* implementing performance improvements like caching, or dealing with
357166
* non-standard authentication requirements.
358167
*/
359-
customResolvers?: {
360-
[title: string]: {
361-
[path: string]: {
362-
[method: string]: ResolveFunction;
363-
};
364-
};
365-
};
168+
customResolvers?: OasTitlePathMethodObject<ResolveFunction>;
366169
/**
367170
* Allows to define custom resolvers and subscribe functions for fields on the
368171
* Subscription root operation type.
@@ -381,13 +184,7 @@ export declare type InternalOptions = {
381184
* Note: Subscription fields will only be generated if the
382185
* createSubscriptionsFromCallbacks option is enabled.
383186
*/
384-
customSubscriptionResolvers?: {
385-
[title: string]: {
386-
[path: string]: {
387-
[method: string]: ResolveObject;
388-
};
389-
};
390-
};
187+
customSubscriptionResolvers?: OasTitlePathMethodObject<ResolveObject>;
391188
/**
392189
* Determines whether OpenAPI-to-GraphQL should create viewers that allow users to pass
393190
* basic auth and API key credentials.
@@ -424,10 +221,3 @@ export declare type InternalOptions = {
424221
*/
425222
equivalentToMessages: boolean;
426223
};
427-
export declare type selectQueryOrMutationFieldType = {
428-
[title: string]: {
429-
[path: string]: {
430-
[method: string]: GraphQLOperationType;
431-
};
432-
};
433-
};

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,8 @@ function createOrReuseUnion({
326326
types,
327327
resolveType: (source, context, info) => {
328328
const properties = Object.keys(source)
329-
330-
// Remove custom _openAPIToGraphQL property used to pass data
331-
const otgIndex = properties.indexOf('_openAPIToGraphQL')
332-
if (otgIndex !== -1) {
333-
properties.splice(otgIndex, 1)
334-
}
329+
// Remove custom _openAPIToGraphQL property used to pass data
330+
.filter(property => property !== '_openAPIToGraphQL')
335331

336332
/**
337333
* Find appropriate member type
@@ -347,13 +343,9 @@ function createOrReuseUnion({
347343
return types.find(type => {
348344
const typeFields = Object.keys(type.getFields())
349345

346+
// The type should be a superset of the properties
350347
if (properties.length <= typeFields.length) {
351-
for (let i = 0; i < properties.length; i++) {
352-
if (!typeFields.includes(properties[i])) {
353-
return false
354-
}
355-
}
356-
return true
348+
return properties.every(property => typeFields.includes(property))
357349
}
358350

359351
return false

0 commit comments

Comments
 (0)