ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.25k stars 745 forks source link

Replace argument/variable in the field middleware (IMiddlewareContext) #5946

Closed Deathrage closed 1 year ago

Deathrage commented 1 year ago

Product

Hot Chocolate

Is your feature request related to a problem?

First of all what is difference between ctx.Variables and ctx.Argument<T>()? Both can be used to acquir parameters of the resolver method.

Now, how to replace an argument or variable with another instance?

Use case Front-end sends an input DTO. There are optional properites. We would like to be able to fill in these properties by default values (which are dynamic and thus cannot be set as default values in the InputObjectType class). All of our DTOs are immutable, thus using setter is not an option. We need to create a new instance and replace it in the pipeline.

There is method ctx.ReplaceArguments(). However, this method requires the whole dictionary of arguments and there is no method or property that would yield the current dictionary. The only solution that came to my mind is to use ctx.Variables to build a new dictionary with one argument replaced.

The solution you'd like

Add property Arguments containing the dictionary to the IResolverContext.

Alternative solution Alternative solution could be being able to manually construct the DTO using a delegate. However this delegate needs to have access to the HttpContext.

michaelstaib commented 1 year ago

The difference is that ctx.Variable represents the variables for the operation as ctx.Arguments gets access to the argument values. Argument values might not be set with a variable but also could be the default value of the argument or a value literal.

The variables are immutable and cannot be changed.

The argument values can be transformed. There are actually 4 or 5 ways to do it.

Here are some youtube episodes that dive into this topic: https://www.youtube.com/watch?v=4n0dYBJ4SDA https://www.youtube.com/watch?v=yJ-2kxrs2TU

michaelstaib commented 1 year ago

There is also the following method in version 13: public ArgumentValue ReplaceArgument(string argumentName, ArgumentValue newArgumentValue)