Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass default GraphQL arguments to AWS AppSync resolver

AWS AppSync allow to define default values in schema like that

type Foo {
  bar(
    arg: Int = 20
  ): Bar!
}

or that

type Foo {
  bar(
    arg: Int! = 20
  ): Bar!
}

But either way when the value is not explicitly defined, the default value is not passed to the resolver.

Is there something I should opt-in to activate the default value to be passed? Is that an AWS bug? If so, is there a workaround?

PS: In the GraphQL specs

If no value is provided for a defined input object field and that field definition provides a default value, the default value should be used. If no default value is provided and the input object field’s type is non‐null, an error should be thrown. Otherwise, if the field is not required, then no entry is added to the coerced unordered map.

like image 542
Yves M. Avatar asked Jul 12 '18 09:07

Yves M.


People also ask

How do I set the default value in a GraphQL schema?

In GraphQL you can assign Default values to the variables. It's done by by adding the default value after the type declaration in your query. If Default values are provided for all variables, you can call the query without passing any variables.

What is resolver in AppSync?

Data sources and resolvers are how AWS AppSync translates GraphQL requests and fetches information from your AWS resources. AWS AppSync has support for automatic provisioning and connections with certain data source types.

Is AppSync a GraphQL?

AppSync is a managed service that uses GraphQL to make it easy for applications to get exactly the data they need.


1 Answers

Default arguments are currently a known issue and the best way to get around them is to use the $util.defaultIfNull() velocity helper function. For example, you can default arguments for limit and next token doing something like this:

{
  "version": "2017-02-28",
  "operation": "Scan",
  "limit": $util.defaultIfNull($ctx.args.first, 20),
  "nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.after, null)),
}
like image 55
mparis Avatar answered Sep 28 '22 01:09

mparis