aws-mobile-appsync-sdk-ios icon indicating copy to clipboard operation
aws-mobile-appsync-sdk-ios copied to clipboard

Omitted optional values are converted to null when using lambda resolver

Open munibrahman opened this issue 5 years ago • 8 comments

Describe the bug Given that GraphQL arguments without ! are treated as optional values. If one is to use the sdk, and pass a nil value to the argument that allows optional values, it gets converted to null in the resolver.

If the same endpoint is called via the web console, and that argument is omitted completely, the value is never passed via resolver at all.

I am using a lambda datasource

To Reproduce

  1. Create a query or mutation with optional arguments. For example.

    listCars(limit: int, numWheels: int): [Car]

  2. Add a lambda resolver that simply prints the events as they are received.

  3. Call the endpoint using the Query Editor with the query listCars(limit: 10). The arguments passed to the lambda function will be.

    "arguments": { "limit": 10 }

  4. Call the same endpoint via the ios sdk, and only pass values for the limit and nil for numWheels or only set the value for limit, post ListCarsQuery.init().

    "arguments": { "limit": 10, "numWheels": null }

No matter how the query is instantiated using swift, either by passing the nil arguments in init or by calling init() and then setting the values later on, the output is the same regardless..

Expected behavior If an argument's value is omitted from a graphQL query or mutation. It shouldn't be null in the arguments passed to the lambda via resolvers.

munibrahman avatar Sep 29 '19 06:09 munibrahman

For now, one can basically go around this bug by including this at the beginning of the appsync lambda resolver.

#foreach ($arg in $ctx.arguments.entrySet())
	#if( $util.isNull($arg.value))
    	$util.qr($ctx.arguments.remove($arg.key))
    #end
#end

munibrahman avatar Jan 14 '20 17:01 munibrahman

Also having this issue. The above doesn't work because we sometimes will be setting things to null.

allisoninouye avatar Mar 13 '20 17:03 allisoninouye

@allisoninouye If you just starting out with Appsync and you are having issues. I assure you, this is just the beginning. If you plan on taking your idea/product to production. I would highly recommend you consider some other solution. Appsync is really nice, but it has a long way to go to become a production ready microservice.

munibrahman avatar Mar 14 '20 18:03 munibrahman

This is a huge issue, that's also apparently non-existent in the Android SDK. Really hoping there's plans on addressing this. This foundational.

dinnerdeal avatar May 14 '20 01:05 dinnerdeal

Probably the devil is hidden in the details. :thinking:
If we take a closer look at the docs we might notice the difference between arguments

arguments A map that contains all GraphQL arguments for this field.

and variables

variables A map which holds all variables that are passed into the GraphQL request.

Probably, we should just use variables if we don't want optional arguments to be injected with the null values. Really, the variables property contains only those variables that were provided by the client while arguments contains all of them according to the schema.

gnemtsov avatar Sep 22 '21 12:09 gnemtsov

It's been ages since I worked with appsync, so if you think this is a solution, i'd be happy to close this issue :)

munibrahman avatar Sep 22 '21 16:09 munibrahman

Well, at least it works for me. I am not sure if it's "best practices" or if there are some downsides in using variables instead of arguments. :man_shrugging:

gnemtsov avatar Sep 23 '21 07:09 gnemtsov

Probably, we should just use variables if we don't want optional arguments to be injected with the null values. Really, the variables property contains only those variables that were provided by the client while arguments contains all of them according to the schema.

Hi @gnemtsov , Encountered this thread while having same problem.

Variables work if the optional field does not present at all in the client's request payload but if it is present even with the empty value, AppSync allows it to be passed in context.info.variables.

z-u-a avatar Jul 19 '22 16:07 z-u-a

Thank you for opening this issue. AWS AppSync SDK for iOS entered maintenance mode in September 2023 and will receive no further updates as of September 2024.

Please use Amplify Swift going forward. For information on upgrading to Amplify Swift, refer to the Upgrade from AppSync SDK documentation.

atierian avatar Dec 07 '23 20:12 atierian