-
Notifications
You must be signed in to change notification settings - Fork 249
Prevent empty input node generation in mutation builder. #2729
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the GraphQL mutation builders for update/patch and create operations by converting several input type generation methods to return nullable values and adding corresponding null checks. Key changes include updating method signatures to support nullable returns, inserting conditional blocks to handle cases where input types may not be generated, and adjusting related logic in both UpdateAndPatchMutationBuilder and CreateMutationBuilder.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/Service.GraphQLBuilder/Mutations/UpdateAndPatchMutationBuilder.cs | Updated method signatures (e.g. GenerateUpdateInputType and GetComplexInputType) and added null checks to improve null safety when generating update/patch input types. |
src/Service.GraphQLBuilder/Mutations/CreateMutationBuilder.cs | Updated method signatures (e.g. GenerateCreateInputTypeForRelationalDb and GenerateComplexInputTypeForRelationalDb) and added conditional checks to ensure that input objects are only used when successfully created. |
Comments suppressed due to low confidence (2)
src/Service.GraphQLBuilder/Mutations/UpdateAndPatchMutationBuilder.cs:95
- Consider defining 'inputFields' as a List rather than a List<InputValueDefinitionNode?> if null values are not expected. This change would eliminate the need to use the null-forgiving operator and improve type safety.
List<InputValueDefinitionNode> inputFieldsList = inputFields.Select(i => i!).ToList();
src/Service.GraphQLBuilder/Mutations/CreateMutationBuilder.cs:77
- Consider updating the declaration of 'inputFields' to use a non-nullable list if null entries are not expected. This revision would remove the need for the null-forgiving operator and enhance code clarity.
inputFields.AddRange(inputFields!)
/azp |
Supported commands
See additional documentation. |
/azp run |
Azure Pipelines successfully started running 6 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 6 pipeline(s). |
inputFields! | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
inputFields! | |
); | |
inputFields!); |
/// <returns>A GraphQL input type with all expected fields mapped as GraphQL inputs.</returns> | ||
private static InputObjectTypeDefinitionNode GenerateCreateInputTypeForRelationalDb( | ||
/// <returns>An optional GraphQL input type with all expected fields mapped as GraphQL inputs.</returns> | ||
private static InputObjectTypeDefinitionNode? GenerateCreateInputTypeForRelationalDb( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiosity, I am not entirely sure how the mutation builder works for non-relational databases. So do you think this might also be a bug that could be found there? If so then it would also be a good idea to change the GenerateCreateInputTypeForNonRelationalDb
function.
location: null, | ||
new NameNode(INPUT_ARGUMENT_NAME), | ||
new StringValueNode($"Input representing all the fields for updating {name}"), | ||
new NonNullTypeNode(new NamedTypeNode(input.Name)), | ||
defaultValue: null, | ||
new List<DirectiveNode>())); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: remove line
entityPermissionsMap: _entityPermissions | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
entityPermissionsMap: _entityPermissions | |
); | |
entityPermissionsMap: _entityPermissions); |
DocumentNode mutationRoot = MutationBuilder.Build( | ||
root, | ||
entityNameToDatabasetype, | ||
new(new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new(new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }), | |
new (new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }), |
@Alekhya-Polavarapu could you update the description for the PR? I see that you wanted to create some new lines but it shows as |
if (inputFields.Any()) | ||
{ | ||
List<InputValueDefinitionNode> inputFieldsList = inputFields | ||
.Select(i => i!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the inputFields IEnumerable contains some null and some non-null fields based on if GenerateSimpleType returns null or non-null? Can we still safely cast it to ignore the null by doing i => i!
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall, but waiting for clarification on one of the comments.
/azp run |
Azure Pipelines successfully started running 6 pipeline(s). |
Why make this change?
Currently today when the tables has only autogenerated fileds, then the schema generation is failing with an error saying that "Empty input for create and update mutation input".
Error trace:
For more details look at the
Errors
property.\r\n\r\n1. InputObjectCreateNewTableInput
has no fields declared. (HotChocolate.Types.InputObjectType)\r\n2. InputObjectUpdateNewTableInput
has no fields declared.#2739
What is this change?
This PR addresses the issue by conditionally generating create and update mutation input types only when the table contains at least one non-auto-generated field. This ensures that the schema remains valid and avoids generating empty input objects.
How was this tested?
screenshots
For create:
for update: