Skip to content

Commit efc1c9a

Browse files
elsmrAlan-Cha
authored andcommitted
Implement error handling for enum mapping
Signed-off-by: Elias Meire <elias@meire.dev>
1 parent 2a1e5f5 commit efc1c9a

File tree

12 files changed

+279
-30
lines changed

12 files changed

+279
-30
lines changed

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

Lines changed: 6 additions & 4 deletions
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/preprocessor.js

Lines changed: 3 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/preprocessor.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: 15 additions & 9 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/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
323323
}
324324

325325
let fieldName =
326-
extensionFieldName ??
326+
extensionFieldName ||
327327
(!singularNames
328328
? Oas3Tools.uncapitalize(operation.responseDefinition.graphQLTypeName)
329329
: Oas3Tools.sanitize(
@@ -503,7 +503,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
503503
}
504504

505505
const saneFieldName =
506-
extensionFieldName ??
506+
extensionFieldName ||
507507
Oas3Tools.storeSaneName(saneOperationId, operationId, data.saneMap)
508508

509509
if (operation.inViewer) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ export function createDataDef<TSource, TContext, TArgs>(
694694
Object.keys(links).forEach((linkKey) => {
695695
const link = links[linkKey]
696696
const fromExtension = link[Oas3Tools.OAS_GRAPHQL_EXTENSIONS.FieldName]
697+
if (fromExtension in saneLinks) {
698+
throw new Error(
699+
`Cannot create link with name "${fromExtension}".\nYou provided "${fromExtension}" in ${Oas3Tools.OAS_GRAPHQL_EXTENSIONS.FieldName}, but it conflicts with another link called "${fromExtension}"`
700+
)
701+
}
697702
const linkSaneName = Oas3Tools.sanitize(
698703
fromExtension || linkKey,
699704
!data.options.simpleNames

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,23 @@ function createOrReuseEnum<TSource, TContext, TArgs>({
557557
const values = {}
558558
const mapping =
559559
def.schema[Oas3Tools.OAS_GRAPHQL_EXTENSIONS.EnumMapping] || {}
560-
def.schema.enum.forEach((e) => {
561-
values[
560+
def.schema.enum.forEach((enumValue) => {
561+
const enumValueString = enumValue.toString()
562+
const fromExtension = mapping[enumValueString]
563+
const saneEnumValue =
564+
fromExtension ||
562565
Oas3Tools.sanitize(
563-
mapping[e.toString()] || e.toString(),
566+
enumValueString,
564567
!data.options.simpleEnumValues
565568
? Oas3Tools.CaseStyle.ALL_CAPS
566569
: Oas3Tools.CaseStyle.simple
567570
)
568-
] = {
569-
value: e
571+
if (fromExtension in values) {
572+
throw new Error(
573+
`Cannot create enum value "${fromExtension}".\nYou provided "${fromExtension}" in ${Oas3Tools.OAS_GRAPHQL_EXTENSIONS.EnumMapping}, but it conflicts with another enum value "${fromExtension}"`
574+
)
570575
}
576+
values[saneEnumValue] = { value: enumValue }
571577
})
572578

573579
// Store newly created Enum Object Type
@@ -628,7 +634,7 @@ function createFields<TSource, TContext, TArgs>({
628634
}
629635

630636
const saneFieldTypeKey =
631-
fromExtension ??
637+
fromExtension ||
632638
Oas3Tools.sanitize(
633639
fieldTypeKey,
634640
!data.options.simpleNames

packages/openapi-to-graphql/test/extensions.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,24 @@ describe('GraphQL Extensions', () => {
137137
)
138138
})
139139

140-
test.skip('should throw when x-graphql-field-name causes naming conflicts on links', async () => {
141-
const oas = require('./fixtures/extensions_error3.json')
140+
test('should throw when x-graphql-field-name causes naming conflicts on links', async () => {
141+
const oas = require('./fixtures/extensions_error6.json')
142142
await expect(
143143
openAPIToGraphQL.createGraphQLSchema(oas)
144144
).rejects.toThrowError(
145145
new Error(
146-
'Cannot create field with name "name".\nYou provided "name" in x-graphql-field-name, but it conflicts with another field called "name"'
146+
'Cannot create link with name "group".\nYou provided "group" in x-graphql-field-name, but it conflicts with another link called "group"'
147147
)
148148
)
149149
})
150150

151-
test.skip('should throw when x-graphql-enum-mapping causes naming conflicts', async () => {
152-
const oas = require('./fixtures/extensions_error3.json')
151+
test('should throw when x-graphql-enum-mapping causes naming conflicts', async () => {
152+
const oas = require('./fixtures/extensions_error7.json')
153153
await expect(
154154
openAPIToGraphQL.createGraphQLSchema(oas)
155155
).rejects.toThrowError(
156156
new Error(
157-
'Cannot create field with name "name".\nYou provided "name" in x-graphql-field-name, but it conflicts with another field called "name"'
157+
'Cannot create enum value "CONFLICT".\nYou provided "CONFLICT" in x-graphql-enum-mapping, but it conflicts with another enum value "CONFLICT"'
158158
)
159159
)
160160
})

0 commit comments

Comments
 (0)