Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Alekhya-Polavarapu
Copy link
Contributor

@Alekhya-Polavarapu Alekhya-Polavarapu commented Jun 16, 2025

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. InputObject CreateNewTableInput has no fields declared. (HotChocolate.Types.InputObjectType)\r\n2. InputObject UpdateNewTableInput 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?

  • Unit tests
  • Manual testing

screenshots

For create:

image

for update:

image

Copy link
Contributor

@Copilot Copilot AI left a 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!)

@Alekhya-Polavarapu Alekhya-Polavarapu changed the title Dev/alpolava/primarykey [Draft] Prevent empty input node generation in mutation builder. Jun 16, 2025
@Alekhya-Polavarapu
Copy link
Contributor Author

/azp

Copy link

Supported commands
  • help:
    • Get descriptions, examples and documentation about supported commands
    • Example: help "command_name"
  • list:
    • List all pipelines for this repository using a comment.
    • Example: "list"
  • run:
    • Run all pipelines or specific pipelines for this repository using a comment. Use this command by itself to trigger all related pipelines, or specify specific pipelines to run.
    • Example: "run" or "run pipeline_name, pipeline_name, pipeline_name"
  • where:
    • Report back the Azure DevOps orgs that are related to this repository and org
    • Example: "where"

See additional documentation.

@Alekhya-Polavarapu
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@Alekhya-Polavarapu Alekhya-Polavarapu changed the title [Draft] Prevent empty input node generation in mutation builder. Prevent empty input node generation in mutation builder. Jun 24, 2025
@RubenCerna2079
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@RubenCerna2079 RubenCerna2079 added this to the Backlog milestone Jun 25, 2025
Comment on lines +78 to +79
inputFields!
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
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(
Copy link
Contributor

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>()));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove line

Comment on lines +219 to +220
entityPermissionsMap: _entityPermissions
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
entityPermissionsMap: _entityPermissions
);
entityPermissionsMap: _entityPermissions);

DocumentNode mutationRoot = MutationBuilder.Build(
root,
entityNameToDatabasetype,
new(new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
new(new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }),
new (new Dictionary<string, Entity> { { "Foo", GenerateEmptyEntity() } }),

@RubenCerna2079
Copy link
Contributor

@Alekhya-Polavarapu could you update the description for the PR? I see that you wanted to create some new lines but it shows as \r\n\r\n

if (inputFields.Any())
{
List<InputValueDefinitionNode> inputFieldsList = inputFields
.Select(i => i!)
Copy link
Contributor

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! ?

Copy link
Contributor

@Aniruddh25 Aniruddh25 left a 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.

@Aniruddh25
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Invalid Empty Node generation for Create and Update Mutation Input
3 participants