1

Consider below query:

const fetchItemsQuery = `query(
  $paginationPageSize: Int! 
  $paginationPageNumber: Int!
  $searchByNameRequest: String
  $categoryID: String
) {
  getItems(
    paginationPageSize: $paginationPageSize
    paginationPageNumber: $paginationPageNumber
    searchByNameRequest: $searchByNameRequest
    categoryID: $categoryID
  ) {
    // ...
  }
};

If to submit it from client side with variables:

{
  paginationPageSize: 20,
  paginationPageNumber: 1
}

in AWS App Sync logs, it reads:

{
  paginationPageSize: 20,
  paginationPageNumber: 1,
  searchByNameRequest: null,
  categoryID: null
}

I checked the request payload from browser - no nulls, just

{
  paginationPageSize: 20,
  paginationPageNumber: 1
}

So the cause is not in frontend side.

I found out that the null substitution occurring in request resolving template:

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "field": "listProduct",
    "arguments": $util.toJson($context.args) // <- here
  }
}

$util.toJson uses Apache Velocity.

Can I avoid $util.toJson($context.args) will substitute the nulls in my request variables?

Important: the solution should not touch the explicitly submitted nulls .

1
  • did you end up resolving this? It seems pretty bad of appsync to not think that null has meaning in GraphQL compared to undefined or non-present. The defaults option below seems like a hacky workaround Commented Aug 26, 2021 at 15:22

1 Answer 1

1

You could try to filter out null arguments from the $context.args map.

#set( $args = {} )
#foreach( $entry in $context.args.entrySet())
  #if( $entry.value != $null )
    #set( $junk = $args.put($entry.key, $entry.value) )
  #end
#end

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "field": "listProduct",
    "arguments": $util.toJson($args)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer! But what if null has been submitted explicitly?
@GurebuBokofu You could assign default values like ` = "DEFAULT"` to GraphQL arguments, and check for this value to spot unspecified arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.