fix flow + typescript input object nullable properties

This commit is contained in:
lewisf committed 2017-11-25 14:42:05 -08:00
1 parent 4724a84466
commit 4d6f736be8
5 files changed
+47 -23

No files matched your search

@@ -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
+17 -5
View File
@@ -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
})
);
}
@@ -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
@@ -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) {
+17 -5
View File
@@ -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
})
);
}