Skip to content

Commit 59ee1d4

Browse files
committed
Add simpleEnumValues option
Signed-off-by: Alan Cha <[email protected]>
1 parent 73d75c1 commit 59ee1d4

16 files changed

+180
-104
lines changed

.gitignore

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
# visual code settings
2-
.vscode/
1+
# OpenAPI-to-GraphQL specific
2+
packages/openapi-to-graphql/APIs
33

4-
# Logs
5-
logs
6-
*.log
7-
npm-debug.log*
8-
9-
# Runtime data
10-
pids
11-
*.pid
12-
*.seed
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# node-waf configuration
27-
.lock-wscript
28-
29-
# Compiled binary addons (http://nodejs.org/api/addons.html)
30-
build/Release
4+
# Visual Code settings
5+
.vscode
316

327
# Dependency directories
338
node_modules
34-
jspm_packages
359

3610
# Optional npm cache directory
3711
.npm

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

+9-7
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.js

+9-9
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.

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

+3-2
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

+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/types/options.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ export declare type InternalOptions<TSource, TContext, TArgs> = {
113113
* to the provided names in the OAS as possible.
114114
*/
115115
simpleNames: boolean;
116+
/**
117+
* By default, field names are sanitized to conform with GraphQL conventions,
118+
* i.e. types should be in PascalCase, fields should be in camelCase, and
119+
* enum values should be in ALL_CAPS.
120+
*
121+
* This option will prevent OpenAPI-to-GraphQL from enforcing ALL_CAPS enum
122+
* values, only removing illegal characters and staying as true to the
123+
* provided enum values in the OAS as possible.
124+
*/
125+
simpleEnumValues: boolean;
116126
/**
117127
* Experimental feature that will try to create more meaningful names from
118128
* the operation path than the response object by leveraging common

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

+25-22
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export function createGraphQLSchema<TSource, TContext, TArgs>(
112112
: false
113113
options.simpleNames =
114114
typeof options.simpleNames === 'boolean' ? options.simpleNames : false
115+
options.simpleEnumValues =
116+
typeof options.simpleEnumValues === 'boolean'
117+
? options.simpleEnumValues
118+
: false
115119
options.singularNames =
116120
typeof options.singularNames === 'boolean' ? options.singularNames : false
117121
options.createSubscriptionsFromCallbacks =
@@ -153,36 +157,33 @@ export function createGraphQLSchema<TSource, TContext, TArgs>(
153157
spec.map((ele) => {
154158
return Oas3Tools.getValidOAS3(ele)
155159
})
156-
)
157-
.then((oass) => {
158-
resolve(
159-
translateOpenAPIToGraphQL(
160-
oass,
161-
options as InternalOptions<TSource, TContext, TArgs>
162-
)
160+
).then((oass) => {
161+
resolve(
162+
translateOpenAPIToGraphQL(
163+
oass,
164+
options as InternalOptions<TSource, TContext, TArgs>
163165
)
164-
})
165-
.catch((error) => {
166-
reject(error)
167-
})
166+
)
167+
}).catch((error) => {
168+
reject(error)
169+
})
168170
} else {
169171
/**
170172
* Check if the spec is a valid OAS 3
171173
* If the spec is OAS 2.0, attempt to translate it into 3, then try to
172174
* translate the spec into a GraphQL schema
173175
*/
174-
Oas3Tools.getValidOAS3(spec)
175-
.then((oas) => {
176-
resolve(
177-
translateOpenAPIToGraphQL(
178-
[oas],
179-
options as InternalOptions<TSource, TContext, TArgs>
180-
)
176+
Oas3Tools.getValidOAS3(spec).then((oas) => {
177+
resolve(
178+
translateOpenAPIToGraphQL(
179+
[oas],
180+
options as InternalOptions<TSource, TContext, TArgs>
181181
)
182-
})
183-
.catch((error) => {
184-
reject(error)
185-
})
182+
)
183+
})
184+
.catch((error) => {
185+
reject(error)
186+
})
186187
}
187188
})
188189
}
@@ -204,6 +205,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
204205
selectQueryOrMutationField,
205206
genericPayloadArgName,
206207
simpleNames,
208+
simpleEnumValues,
207209
singularNames,
208210
createSubscriptionsFromCallbacks,
209211

@@ -238,6 +240,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
238240
selectQueryOrMutationField,
239241
genericPayloadArgName,
240242
simpleNames,
243+
simpleEnumValues,
241244
singularNames,
242245
createSubscriptionsFromCallbacks,
243246

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,14 @@ export function getResolver<TSource, TContext, TArgs>({
402402
* the user.
403403
*/
404404
operation.parameters.forEach((param) => {
405-
const paramName = Oas3Tools.sanitize(
405+
const saneParamName = Oas3Tools.sanitize(
406406
param.name,
407407
!data.options.simpleNames
408408
? Oas3Tools.CaseStyle.camelCase
409409
: Oas3Tools.CaseStyle.simple
410410
)
411411
if (
412-
typeof args[paramName] === 'undefined' &&
412+
typeof args[saneParamName] === 'undefined' &&
413413
param.schema &&
414414
typeof param.schema === 'object'
415415
) {
@@ -422,7 +422,7 @@ export function getResolver<TSource, TContext, TArgs>({
422422
(schema as SchemaObject).default &&
423423
typeof (schema as SchemaObject).default !== 'undefined'
424424
) {
425-
args[paramName] = (schema as SchemaObject).default
425+
args[saneParamName] = (schema as SchemaObject).default
426426
}
427427
}
428428
})
@@ -1282,28 +1282,28 @@ export function extractRequestDataFromArgs<TSource, TContext, TArgs>(
12821282

12831283
// Iterate parameters:
12841284
for (const param of parameters) {
1285-
const sanitizedParamName = Oas3Tools.sanitize(
1285+
const saneParamName = Oas3Tools.sanitize(
12861286
param.name,
12871287
!data.options.simpleNames
12881288
? Oas3Tools.CaseStyle.camelCase
12891289
: Oas3Tools.CaseStyle.simple
12901290
)
12911291

1292-
if (sanitizedParamName && sanitizedParamName in args) {
1292+
if (saneParamName && saneParamName in args) {
12931293
switch (param.in) {
12941294
// Path parameters
12951295
case 'path':
1296-
path = path.replace(`{${param.name}}`, args[sanitizedParamName])
1296+
path = path.replace(`{${param.name}}`, args[saneParamName])
12971297
break
12981298

12991299
// Query parameters
13001300
case 'query':
1301-
qs[param.name] = args[sanitizedParamName]
1301+
qs[param.name] = args[saneParamName]
13021302
break
13031303

13041304
// Header parameters
13051305
case 'header':
1306-
headers[param.name] = args[sanitizedParamName]
1306+
headers[param.name] = args[saneParamName]
13071307
break
13081308

13091309
// Cookie parameters
@@ -1312,7 +1312,7 @@ export function extractRequestDataFromArgs<TSource, TContext, TArgs>(
13121312
headers['cookie'] = ''
13131313
}
13141314

1315-
headers['cookie'] += `${param.name}=${args[sanitizedParamName]}; `
1315+
headers['cookie'] += `${param.name}=${args[saneParamName]}; `
13161316
break
13171317

13181318
default:

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,12 @@ function createOrReuseEnum<TSource, TContext, TArgs>({
537537

538538
const values = {}
539539
def.schema.enum.forEach((e) => {
540-
// Force enum values to string and value should be in ALL_CAPS
541540
values[
542541
Oas3Tools.sanitize(
543542
e.toString(),
544-
data.options.simpleNames
545-
? Oas3Tools.CaseStyle.simple
546-
: Oas3Tools.CaseStyle.ALL_CAPS
543+
!data.options.simpleEnumValues
544+
? Oas3Tools.CaseStyle.ALL_CAPS
545+
: Oas3Tools.CaseStyle.simple
547546
)
548547
] = {
549548
value: e

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ export type RequestOptions<TSource, TContext, TArgs> = Omit<
6666
> & {
6767
headers?: Headers | RequestHeadersFunction<TSource, TContext, TArgs>
6868
}
69-
70-
export type Options<TSource, TContext, TArgs> = Partial<InternalOptions<TSource, TContext, TArgs>>
69+
70+
export type Options<TSource, TContext, TArgs> = Partial<
71+
InternalOptions<TSource, TContext, TArgs>
72+
>
7173

7274
export type InternalOptions<TSource, TContext, TArgs> = {
7375
/*
@@ -150,6 +152,17 @@ export type InternalOptions<TSource, TContext, TArgs> = {
150152
*/
151153
simpleNames: boolean
152154

155+
/**
156+
* By default, field names are sanitized to conform with GraphQL conventions,
157+
* i.e. types should be in PascalCase, fields should be in camelCase, and
158+
* enum values should be in ALL_CAPS.
159+
*
160+
* This option will prevent OpenAPI-to-GraphQL from enforcing ALL_CAPS enum
161+
* values, only removing illegal characters and staying as true to the
162+
* provided enum values in the OAS as possible.
163+
*/
164+
simpleEnumValues: boolean
165+
153166
/**
154167
* Experimental feature that will try to create more meaningful names from
155168
* the operation path than the response object by leveraging common
@@ -220,7 +233,9 @@ export type InternalOptions<TSource, TContext, TArgs> = {
220233
* implementing performance improvements like caching, or dealing with
221234
* non-standard authentication requirements.
222235
*/
223-
customResolvers?: OasTitlePathMethodObject<GraphQLFieldResolver<TSource, TContext, TArgs>>
236+
customResolvers?: OasTitlePathMethodObject<
237+
GraphQLFieldResolver<TSource, TContext, TArgs>
238+
>
224239

225240
/**
226241
* Allows to define custom resolvers and subscribe functions for fields on the

0 commit comments

Comments
 (0)