aws-appsync-community
aws-appsync-community copied to clipboard
Add method to check if variable is of type array
Currently, there is no way to check whether a variable is an array in JS resolver functions.
Here are a couple of approaches as to how you would go about it in a browser or Nodejs environment:
- Use
Array.isArray
, which doesn't exist inAPPSYNC_JS
. - Use
arr.constructor === Array
. That doesn't work, becausearr.constructor
is undefined inAPPSYNC_JS
. - Check for multiple array functions. Ex:
This also doesn't work becausefunction isArray(x) { return ( typeof x.find === 'function' && typeof x.length === 'number' && typeof x.sort === 'function' ); }
typeof
any function is'undefined'
inAPPSYNC_JS
. This is also (IMO) the worst solution.
The functionality is present in vtl
resolvers with $util.isList
, so it would be nice if it was available in the JS runtime as well.
This would indeed be very helpful!
After some trial and error I went for this hack now. Probably does not cover all cases but seems to work fine for all our use cases:
function isArray(input) {
return typeof input === "object" && input !== null && typeof input.length === "number";
}
I have the same issue. In V1 of graphql we were able to work around this.
But in V2 the resolver lacks any way of detecting a json array.