Skip to content

Commit

Permalink
feat: make fields optional for ObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
yuth committed Jan 20, 2021
1 parent b8c82fd commit 5d6922b
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 470 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function structDeclarationForInputObjectType(generator: CodeGenerator, type: Gra
},
() => {
const properties = propertiesFromFields(generator.context, Object.values(type.getFields()));
properties.forEach(property => propertyDeclaration(generator, { ...property, isOptional: true }));
properties.forEach(property => propertyDeclaration(generator, { ...property }));
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function propertyDeclaration(
if (closure) {
generator.printOnNewline(name);

if (isNullable && isOptional) {
if (isNullable || isOptional) {
generator.print('?');
}
generator.print(': ');
Expand All @@ -84,10 +84,26 @@ export function propertyDeclaration(
}
} else {
generator.printOnNewline(name);
if (isOptional && isNullable) {
if (isOptional || isNullable) {
generator.print('?');
}
generator.print(`: ${typeName || (type && typeNameFromGraphQLType(generator.context, type))}`);
generator.print(': ');

if (isArray) {
generator.print(' Array<');
}
generator.print(`${typeName || (type && typeNameFromGraphQLType(generator.context, type))}`);
if (isArray) {
if (isArrayElementNullable) {
generator.print(' | null');
}
generator.print(' >');
}

// When typeName is passed as string it includes <typename> | null for nullable types.
if (isNullable && (!typeName || isArray)) {
generator.print(' | null');
}
}
generator.print(',');
}
Expand Down

0 comments on commit 5d6922b

Please sign in to comment.