Skip to content

Commit a3246c3

Browse files
committed
Non-nullable properties in GraphQL object types
Signed-off-by: Alan Cha <[email protected]>
1 parent 2527d61 commit a3246c3

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

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

Lines changed: 14 additions & 7 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/utils.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export declare const mitigations: {
1616
OBJECT_MISSING_PROPERTIES: string;
1717
UNION_MEMBER_NON_OBJECT: string;
1818
AMBIGUOUS_UNION_MEMBERS: string;
19+
CANNOT_GET_FIELD_TYPE: string;
1920
UNRESOLVABLE_LINK: string;
2021
AMBIGUOUS_LINK: string;
2122
LINK_NAME_COLLISION: string;

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

Lines changed: 1 addition & 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/utils.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/schema_builder.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ function createFields({
585585
// Create fields for properties
586586
for (let fieldTypeKey in fieldTypeDefinitions) {
587587
const fieldTypeDefinition = fieldTypeDefinitions[fieldTypeKey]
588-
const schema = fieldTypeDefinition.schema
588+
const fieldSchema = fieldTypeDefinition.schema
589589

590590
// Get object type describing the property
591591
const objectType = getGraphQLType({
@@ -596,11 +596,8 @@ function createFields({
596596
isInputObjectType
597597
})
598598

599-
// Determine if this property is required in mutations
600-
const reqMutationProp =
601-
isInputObjectType &&
602-
'required' in def.schema && // The full schema, not subschema, will contain the required property
603-
def.schema.required.includes(fieldTypeKey)
599+
const requiredProperty =
600+
'required' in def.schema && def.schema.required.includes(fieldTypeKey) // The full schema, not field schema, will contain the required property
604601

605602
// Finally, add the object type to the fields (using sanitized field name)
606603
if (objectType) {
@@ -609,15 +606,24 @@ function createFields({
609606
data.saneMap
610607
)
611608
fields[sanePropName] = {
612-
type: reqMutationProp
609+
type: requiredProperty
613610
? new GraphQLNonNull(objectType)
614611
: (objectType as GraphQLOutputType),
615612

616613
description:
617-
typeof schema.description === 'undefined'
614+
typeof fieldSchema.description === 'undefined'
618615
? 'No description available.'
619-
: schema.description
616+
: fieldSchema.description
620617
}
618+
} else {
619+
handleWarning({
620+
typeKey: 'CANNOT_GET_FIELD_TYPE',
621+
message:
622+
`Cannot obtain GraphQL type for field '${fieldTypeKey}' in ` +
623+
`GraphQL type '${JSON.stringify(def.schema)}'.`,
624+
data,
625+
log: translationLog
626+
})
621627
}
622628
}
623629

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const mitigations = {
2525
OBJECT_MISSING_PROPERTIES: `The (sub-)object will be stored in an arbitray JSON type.`,
2626
UNION_MEMBER_NON_OBJECT: `Ignore union member type and continue.`,
2727
AMBIGUOUS_UNION_MEMBERS: `Ignore issue and continue.`,
28+
CANNOT_GET_FIELD_TYPE: `Ignore field and continue.`,
2829

2930
// Links
3031
UNRESOLVABLE_LINK: `Ignore link.`,

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,6 @@ test('Union type', () => {
11101110
}`
11111111

11121112
return graphql(createdSchema, query, null, {}).then(result => {
1113-
console.log(JSON.stringify(result, null, 2))
11141113
expect(result).toEqual({
11151114
data: {
11161115
assets: [
@@ -1846,8 +1845,11 @@ test('Option idFormats', () => {
18461845
fields {
18471846
name
18481847
type {
1849-
name
18501848
kind
1849+
ofType {
1850+
name
1851+
kind
1852+
}
18511853
}
18521854
}
18531855
}
@@ -1867,8 +1869,11 @@ test('Option idFormats', () => {
18671869
).toEqual({
18681870
name: 'patentId',
18691871
type: {
1870-
name: 'ID',
1871-
kind: 'SCALAR'
1872+
kind: 'NON_NULL',
1873+
ofType: {
1874+
name: 'ID',
1875+
kind: 'SCALAR'
1876+
}
18721877
}
18731878
})
18741879
})
@@ -1878,11 +1883,19 @@ test('Option idFormats', () => {
18781883
test('Required properties for input object types', () => {
18791884
const userInputType = createdSchema.getType('UserInput')
18801885

1881-
// The exclamation mark shows that it is a required property
1886+
// The exclamation mark shows that it is a required (non-nullable) property
18821887
expect(userInputType.toConfig().fields.address.type.toString()).toEqual(
18831888
'AddressInput!'
18841889
)
18851890
expect(userInputType.toConfig().fields.address2.type.toString()).toEqual(
18861891
'AddressInput'
18871892
)
18881893
})
1894+
1895+
test('Non-nullable properties for object types', () => {
1896+
const coordinates = createdSchema.getType('Coordinates')
1897+
1898+
// The exclamation mark shows that it is a required (non-nullable) property
1899+
expect(coordinates.toConfig().fields.lat.type.toString()).toEqual('Float!')
1900+
expect(coordinates.toConfig().fields.long.type.toString()).toEqual('Float!')
1901+
})

0 commit comments

Comments
 (0)