Skip to content

Add additional methods to interact with an ArgumentValue #1172

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

Conversation

JBodkin-Amphora
Copy link

No description provided.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 3, 2025
@bclozel bclozel self-assigned this Apr 3, 2025
@bclozel bclozel added type: enhancement A general enhancement in: data Issues related to working with data and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 3, 2025
@bclozel bclozel added this to the 1.4.0-RC1 milestone Apr 3, 2025
@bclozel bclozel closed this in 015058b Apr 3, 2025
@bclozel
Copy link
Member

bclozel commented Apr 3, 2025

Thanks very much @JBodkin-Amphora for your first contribution to the project.
I took the liberty to add some tests, fix the isEmpty implementation, and remove the ifPresentOrEmpty as I don't think it's worth adding to our public API at this stage.

bclozel added a commit that referenced this pull request Apr 3, 2025
@JBodkin-Amphora
Copy link
Author

JBodkin-Amphora commented Apr 3, 2025

Thank you for taking a look at this, the ifPresentOrEmpty method is the most important for us as it helps to reduce the verbosity of the code.

For example, using if else if is the most verbose:

if (message.isPresent()) {
	entity.setMessage(entityManager.find(...));
} else if (message.isEmpty()) {
	entity.setMessage(null);
}

The verbosity could be reduced by checking for not omitted, however we have an intermediate operation to look for an entity reference. In this instance, we're not using the data integration as the lookup for the entity has additional requirements that need to match but not demonstrated here. Also, using optionals potentially has overhead as they may not be optimised by the compiler.

For example:

if (!message.isOmitted()) {
	final var value = message.asOptional().map(entityManager.find()).orElse(null);
	entity.setMessage(value);
}

If we had the ifPresentOrEmpty method, it would look more readable:

message.ifPresentOrEmpty(
	value -> entity.setMessage(entityManager.find(...)),
	entity.setMessage(null)
);

What do you think?

@bclozel
Copy link
Member

bclozel commented Apr 4, 2025

Thanks for the feedback. I initially thought that the main driver for this contribution was the testing bits of your other PR.
The two other variants don't look bad to me and I don't understand the point about Optional and compiler optimization as ArgumentValue is likely to be the same (or worse if Optional has intrisics).

Overall I'm not sure we should expand the ArgumentValue API, especially if we're getting closer to the Optional API with subtle semantic differences. We can reconsider this if we get more demand from the community.

@JBodkin-Amphora
Copy link
Author

If we create a ArgumentValue and a Optional, then we're creating 2x the number of objects which could make it onto the heap and require garbage collection. We have some particularly large models which could end up with a few hundred fields in a mutation, so I'm trying to think ahead to keep the number of objects created to a minimum whilst ensuring the readability of the code. We're going through a transitional project to move to GraphQL (rather than transformational) which means we don't have the scope to make changes to the inputs, as we're keeping them as-is.

I understand that your not keen on supplementing the ArgumentValue API at the moment, so internally we can implement our own alternative and register our own ArgumentResolver.

@JBodkin-Amphora JBodkin-Amphora deleted the feat/argument-value branch April 7, 2025 07:58
@bclozel
Copy link
Member

bclozel commented Apr 7, 2025

I'll discuss this with the rest of the team.

@bclozel bclozel added the for: team-attention An issue we need to discuss as a team to make progress label Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention An issue we need to discuss as a team to make progress in: data Issues related to working with data type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants