Skip to content

Commit 2a3f01b

Browse files
Alan-ChaErikWittern
authored andcommitted
More code improvements
Signed-off-by: Alan Cha <[email protected]>
1 parent 9ab5012 commit 2a3f01b

File tree

9 files changed

+82
-46
lines changed

9 files changed

+82
-46
lines changed

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

Lines changed: 0 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/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/preprocessor.js

Lines changed: 19 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/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: 12 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/oas_3_tools.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,6 @@ export function getSchemaType(
453453
return schema.type
454454
}
455455

456-
// CASE: nullable - default to string
457-
if (typeof schema.nullable !== 'undefined') {
458-
return 'string'
459-
}
460-
461456
return null
462457
}
463458

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,17 @@ export function createDataDef(
487487

488488
const def: DataDefinition = {
489489
preferredName,
490+
491+
/**
492+
* Note that schema may contain $ref or schema composition (e.g. allOf)
493+
*
494+
* TODO: the schema is used in getSchemaIndex, which allows us to check
495+
* whether a dataDef has already been created for that particular
496+
* schema and name pair. The look up should resolve references but
497+
* currently, it does not.
498+
*/
490499
schema,
500+
491501
type,
492502
subDefinitions: undefined,
493503
links: saneLinks,
@@ -572,16 +582,23 @@ function getSchemaIndex(
572582
schema: SchemaObject,
573583
dataDefs: DataDefinition[]
574584
): number {
575-
let index = -1
576-
for (let def of dataDefs) {
577-
index++
585+
/**
586+
* TODO: instead of iterating through the whole list every time, create a
587+
* hashing function and store all of the DataDefinitions in a hashmap.
588+
*/
589+
for (let index = 0; index < dataDefs.length; index++) {
590+
const def = dataDefs[index]
578591

592+
/**
593+
* TODO: deepEquals is not sufficient. We also need to resolve references.
594+
* However, deepEquals should work for vast majority of cases.
595+
*/
579596
if (preferredName === def.preferredName && deepEqual(schema, def.schema)) {
580597
return index
581598
}
582599
}
583600

584-
// If the schema could not be found in the master list
601+
// The schema could not be found in the master list
585602
return -1
586603
}
587604

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ import {
1515
SchemaObject,
1616
ParameterObject,
1717
ReferenceObject,
18-
LinkObject,
19-
LinksObject
18+
LinkObject
2019
} from './types/oas3'
21-
import { Args, Field, GraphQLType } from './types/graphql'
20+
import { Args, GraphQLType } from './types/graphql'
2221
import {
2322
GraphQLScalarType,
2423
GraphQLObjectType,
@@ -67,20 +66,20 @@ type CreateOrReuseOtParams = {
6766
data: PreprocessingData
6867
}
6968

70-
type ReuseOrCreateListParams = {
69+
type CreateOrReuseListParams = {
7170
def: DataDefinition
7271
operation?: Operation
7372
iteration: number
7473
isInputObjectType: boolean
7574
data: PreprocessingData
7675
}
7776

78-
type ReuseOrCreateEnum = {
77+
type CreateOrReuseEnumParams = {
7978
def: DataDefinition
8079
data: PreprocessingData
8180
}
8281

83-
type ReuseOrCreateScalar = {
82+
type CreateOrReuseScalarParams = {
8483
def: DataDefinition
8584
data: PreprocessingData
8685
}
@@ -134,7 +133,7 @@ export function getGraphQLType({
134133

135134
// CASE: array - create ArrayType
136135
} else if (type === 'array') {
137-
return reuseOrCreateList({
136+
return createOrReuseList({
138137
def,
139138
operation,
140139
data,
@@ -144,7 +143,7 @@ export function getGraphQLType({
144143

145144
// CASE: enum - create EnumType
146145
} else if (type === 'enum') {
147-
return reuseOrCreateEnum({
146+
return createOrReuseEnum({
148147
def,
149148
data
150149
})
@@ -229,6 +228,7 @@ function createOrReuseOt({
229228
if (
230229
typeof def.schema.properties === 'undefined' &&
231230
typeof def.schema.allOf === 'undefined' // allOf can provide all the properties
231+
// TODO: Add oneOf and anyOf
232232
) {
233233
handleWarning({
234234
typeKey: 'OBJECT_MISSING_PROPERTIES',
@@ -305,13 +305,13 @@ function createOrReuseOt({
305305
/**
306306
* Returns an existing List or creates a new one, and stores it in data
307307
*/
308-
function reuseOrCreateList({
308+
function createOrReuseList({
309309
def,
310310
operation,
311311
iteration,
312312
isInputObjectType,
313313
data
314-
}: ReuseOrCreateListParams): GraphQLList<any> {
314+
}: CreateOrReuseListParams): GraphQLList<any> {
315315
const name = isInputObjectType ? def.iotName : def.otName
316316

317317
// Try to reuse existing Object Type
@@ -359,15 +359,23 @@ function reuseOrCreateList({
359359
}
360360

361361
/**
362-
* Returns an existing Enum Type or creates a new one, and stores it in data
362+
* Returns an existing enum type or creates a new one, and stores it in data
363363
*/
364-
function reuseOrCreateEnum({ def, data }: ReuseOrCreateEnum): GraphQLEnumType {
365-
// Try to reuse existing Enum Type
364+
function createOrReuseEnum({
365+
def,
366+
data
367+
}: CreateOrReuseEnumParams): GraphQLEnumType {
368+
/**
369+
* Try to reuse existing enum type
370+
*
371+
* Enum types do not have an input variant so only check def.ot
372+
*/
366373
if (def.ot && typeof def.ot !== 'undefined') {
367374
translationLog(`Reuse GraphQLEnumType '${def.otName}'`)
368375
return def.ot as GraphQLEnumType
369376
} else {
370377
translationLog(`Create GraphQLEnumType '${def.otName}'`)
378+
371379
const values = {}
372380
def.schema.enum.forEach(e => {
373381
// Force enum values to string
@@ -381,14 +389,18 @@ function reuseOrCreateEnum({ def, data }: ReuseOrCreateEnum): GraphQLEnumType {
381389
name: def.otName,
382390
values
383391
})
392+
384393
return def.ot
385394
}
386395
}
387396

388397
/**
389398
* Returns the GraphQL scalar type matching the given JSON schema type
390399
*/
391-
function getScalarType({ def, data }: ReuseOrCreateScalar): GraphQLScalarType {
400+
function getScalarType({
401+
def,
402+
data
403+
}: CreateOrReuseScalarParams): GraphQLScalarType {
392404
const type = def.type
393405

394406
switch (type) {
@@ -411,9 +423,7 @@ function getScalarType({ def, data }: ReuseOrCreateScalar): GraphQLScalarType {
411423
def.ot = GraphQLJSON
412424
break
413425
default:
414-
// If the type is not known, try to stringify
415-
def.ot = GraphQLString
416-
break
426+
throw new Error(`Cannot process schema type '${def.type}'.`)
417427
}
418428

419429
return def.ot as GraphQLScalarType

0 commit comments

Comments
 (0)