Skip to content

fix: 6560 nullable required fields #6561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions packages/loaders/json-schema/src/getComposerFromJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,28 +1483,12 @@ export function getComposerFromJSONSchema({
}
}
(output as ObjectTypeComposer).addFields(fieldMap);
// TODO: Improve this later
for (const requiredFieldName of subSchemaAndTypeComposers.required || []) {
const sanitizedFieldName = sanitizeNameForGraphQL(requiredFieldName);
const fieldType = (output as ObjectTypeComposer).getFieldType(sanitizedFieldName);
if (!isNonNullType(fieldType)) {
(output as ObjectTypeComposer).makeFieldNonNull(requiredFieldName);
}
}
}
let input = subSchemaAndTypeComposers.input;
if (Object.keys(inputFieldMap).length === 0) {
input = schemaComposer.getAnyTC(GraphQLJSON);
} else if (input != null && 'addFields' in input) {
(input as InputTypeComposer).addFields(inputFieldMap);
// TODO: Improve this later
for (const requiredFieldName of subSchemaAndTypeComposers.required || []) {
const sanitizedFieldName = sanitizeNameForGraphQL(requiredFieldName);
const fieldType = (input as InputTypeComposer).getFieldType(sanitizedFieldName);
if (!isNonNullType(fieldType)) {
(input as InputTypeComposer).makeFieldNonNull(requiredFieldName);
}
}
}

if (isList) {
Expand Down
19 changes: 19 additions & 0 deletions packages/loaders/json-schema/test/getComposerFromSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1406,4 +1406,23 @@ ${printType(GraphQLString)}
});
expect((output as UnionTypeComposer).getType().toString()).toBe('[String]');
});
it('should handle objects with required but nullable fields as optional fields in GraphQL', async () => {
const schema: JSONSchema = {
title: 'Required Optional Test',
type: 'object',
required: ['bar'],
properties: {
bar: {
type: 'string',
nullable: true,
},
},
};
const { output } = await getComposerFromJSONSchema({
subgraphName: 'Test',
schema,
logger,
});
expect((output as ObjectTypeComposer).getField('bar').type.getTypeName()).toEqual('String');
});
});
Loading