From 4d6f736be87adb3936ded355d12fda11cd6d44d0 Mon Sep 17 00:00:00 2001 From: lewisf Date: Sat, 25 Nov 2017 14:42:05 -0800 Subject: [PATCH] fix flow + typescript input object nullable properties --- .../__snapshots__/codeGeneration.ts.snap | 20 ++++++++--------- src/javascript/flow/language.ts | 22 ++++++++++++++----- .../__snapshots__/codeGeneration.ts.snap | 4 ++-- .../typescript/__tests__/codeGeneration.ts | 2 +- src/javascript/typescript/language.ts | 22 ++++++++++++++----- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/javascript/flow/__tests__/__snapshots__/codeGeneration.ts.snap b/src/javascript/flow/__tests__/__snapshots__/codeGeneration.ts.snap index 2fefdcd..bd77725 100644 --- a/src/javascript/flow/__tests__/__snapshots__/codeGeneration.ts.snap +++ b/src/javascript/flow/__tests__/__snapshots__/codeGeneration.ts.snap @@ -435,8 +435,8 @@ export type Episode = \\"NEWHOPE\\" | \\"EMPIRE\\" | \\"JEDI\\"; // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: ?string, - favorite_color: ?ColorInput, + commentary?: ?string, + favorite_color?: ?ColorInput, }; // The input object sent when passing in a color @@ -488,8 +488,8 @@ export type Episode = \\"NEWHOPE\\" | \\"EMPIRE\\" | \\"JEDI\\"; // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: ?string, - favorite_color: ?ColorInput, + commentary?: ?string, + favorite_color?: ?ColorInput, }; // The input object sent when passing in a color @@ -541,8 +541,8 @@ export type Episode = \\"NEWHOPE\\" | \\"EMPIRE\\" | \\"JEDI\\"; // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: ?string, - favorite_color: ?ColorInput, + commentary?: ?string, + favorite_color?: ?ColorInput, }; // The input object sent when passing in a color @@ -589,8 +589,8 @@ export type Episode = \\"NEWHOPE\\" | \\"EMPIRE\\" | \\"JEDI\\"; // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: ?string, - favorite_color: ?ColorInput, + commentary?: ?string, + favorite_color?: ?ColorInput, }; // The input object sent when passing in a color @@ -779,8 +779,8 @@ export type Episode = \\"NEWHOPE\\" | \\"EMPIRE\\" | \\"JEDI\\"; // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: ?string, - favorite_color: ?ColorInput, + commentary?: ?string, + favorite_color?: ?ColorInput, }; // The input object sent when passing in a color diff --git a/src/javascript/flow/language.ts b/src/javascript/flow/language.ts index 0bb6ff3..369a6e8 100644 --- a/src/javascript/flow/language.ts +++ b/src/javascript/flow/language.ts @@ -70,7 +70,9 @@ export default class FlowGenerator { } }); - const typeAlias = this.typeAliasObject(name, fields); + const typeAlias = this.typeAliasObject(name, fields, { + keyInheritsNullability: true + }); typeAlias.leadingComments = [{ type: 'CommentLine', @@ -80,7 +82,11 @@ export default class FlowGenerator { return typeAlias; } - public objectTypeAnnotation(fields: ObjectProperty[], isInputObject: boolean = false) { + public objectTypeAnnotation(fields: ObjectProperty[], { + keyInheritsNullability = false + } : { + keyInheritsNullability?: boolean + } = {}) { const objectTypeAnnotation = t.objectTypeAnnotation( fields.map(({name, description, annotation}) => { @@ -88,7 +94,7 @@ export default class FlowGenerator { t.identifier( // Nullable fields on input objects do not have to be defined // as well, so allow these fields to be "undefined" - (isInputObject && annotation.type === "NullableTypeAnnotation") + (keyInheritsNullability && annotation.type === "NullableTypeAnnotation") ? name + '?' : name ), @@ -113,11 +119,17 @@ export default class FlowGenerator { return objectTypeAnnotation; } - public typeAliasObject(name: string, fields: ObjectProperty[]) { + public typeAliasObject(name: string, fields: ObjectProperty[], { + keyInheritsNullability = false + }: { + keyInheritsNullability?: boolean + } = {}) { return t.typeAlias( t.identifier(name), undefined, - this.objectTypeAnnotation(fields) + this.objectTypeAnnotation(fields, { + keyInheritsNullability + }) ); } diff --git a/src/javascript/typescript/__tests__/__snapshots__/codeGeneration.ts.snap b/src/javascript/typescript/__tests__/__snapshots__/codeGeneration.ts.snap index 2b5fdc9..8a3e24a 100644 --- a/src/javascript/typescript/__tests__/__snapshots__/codeGeneration.ts.snap +++ b/src/javascript/typescript/__tests__/__snapshots__/codeGeneration.ts.snap @@ -843,8 +843,8 @@ export enum Episode { // The input object sent when someone is creating a new review type ReviewInput = { stars: number, - commentary: string | undefined | null, - favorite_color: ColorInput | undefined | null, + commentary?: string | undefined | null, + favorite_color?: ColorInput | undefined | null, }; // The input object sent when passing in a color diff --git a/src/javascript/typescript/__tests__/codeGeneration.ts b/src/javascript/typescript/__tests__/codeGeneration.ts index 9a34640..1879d78 100644 --- a/src/javascript/typescript/__tests__/codeGeneration.ts +++ b/src/javascript/typescript/__tests__/codeGeneration.ts @@ -76,7 +76,7 @@ describe('Typescript codeGeneration', () => { expect(output).toMatchSnapshot(); }); - test('simple mutation', () => { + test.only('simple mutation', () => { const context = compile(` mutation ReviewMovie($episode: Episode, $review: ReviewInput) { createReview(episode: $episode, review: $review) { diff --git a/src/javascript/typescript/language.ts b/src/javascript/typescript/language.ts index b660b95..013ea26 100644 --- a/src/javascript/typescript/language.ts +++ b/src/javascript/typescript/language.ts @@ -71,7 +71,9 @@ export default class TypescriptGenerator { } }); - const typeAlias = this.typeAliasObject(name, fields); + const typeAlias = this.typeAliasObject(name, fields, { + keyInheritsNullability: true + }); typeAlias.leadingComments = [{ type: 'CommentLine', @@ -81,14 +83,18 @@ export default class TypescriptGenerator { return typeAlias; } - public objectTypeAnnotation(fields: ObjectProperty[], isInputObject: boolean = false) { + public objectTypeAnnotation(fields: ObjectProperty[], { + keyInheritsNullability = false + } : { + keyInheritsNullability?: boolean + } = {}) { const objectTypeAnnotation = t.objectTypeAnnotation( fields.map(({name, description, annotation}) => { const objectTypeProperty = t.objectTypeProperty( t.identifier( // Nullable fields on input objects do not have to be defined // as well, so allow these fields to be "undefined" - (isInputObject && annotation.type === "NullableTypeAnnotation") + (keyInheritsNullability && annotation.type === "NullableTypeAnnotation") ? name + '?' : name ), @@ -111,11 +117,17 @@ export default class TypescriptGenerator { return objectTypeAnnotation; } - public typeAliasObject(name: string, fields: ObjectProperty[]) { + public typeAliasObject(name: string, fields: ObjectProperty[], { + keyInheritsNullability = false + }: { + keyInheritsNullability?: boolean + } = {}) { return t.typeAlias( t.identifier(name), undefined, - this.objectTypeAnnotation(fields) + this.objectTypeAnnotation(fields, { + keyInheritsNullability + }) ); }