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 9461ffd
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 481 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,17 +355,6 @@ export function propertyDeclarations(generator, properties, isOptional) {
(property.inlineFragments && property.inlineFragments.length > 0) ||
(property.fragmentSpreads && property.fragmentSpreads.length > 0)
) {
const fields = property.fields.map(field => {
if (field.fieldName === '__typename') {
return {
...field,
typeName: `"${property.typeName}"`,
type: { name: `"${property.typeName}"` },
};
} else {
return field;
}
});
propertyDeclaration(generator, property, () => {
const properties = propertiesFromFields(generator.context, property.fields);
propertyDeclarations(generator, properties, isOptional);
Expand Down
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 9461ffd

Please sign in to comment.