Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom query functions to accept an array of arguments #2913

Open
2 tasks
ataylorme opened this issue Sep 27, 2024 · 8 comments
Open
2 tasks

Allow custom query functions to accept an array of arguments #2913

ataylorme opened this issue Sep 27, 2024 · 8 comments
Labels
feature-request New feature or request Gen 2

Comments

@ataylorme
Copy link

Describe the feature you'd like to request

Allow a.query().arguments and .returns() to both accept arrays. This will allow custom query arguments mapped to Lambda handlers to allow the Lambda to process multiple requests in a single invocation, reducing network calls and cold starts.

In my specific case the user's identity is used to fetch an external API key and instantiate an external API client. Batching requests from the same user will make this much more efficient.

Currently the example below produces the error Property 'array' does not exist on type 'CustomType<{ fields: { action: ModelField<string, "required", undefined>; parameters: ModelField<string | number | boolean | object | any[], "required", undefined>; paginate: ModelField<...>; }; }>'

Notice how the return value may be an array, and does not produce an error, but the arguments cannot be an array.

MyCustomApiRequestResponse: a.customType({
	data: a.string(),
	rawResponse: a.string(),
	errors: a.string().array(),
}),

MyCustomApiRequest: a
	.query()
	.arguments({
		requests: a.customType({
			action: a.string().required(),
			parameters: a.json().required(),
			paginate: a.boolean().required(),
		}).array().required(),
	})
	.returns(a.ref('MyCustomApiRequestResponse').array())
	.handler(a.handler.function(myCustomApiRequest))
	.authorization(allow => [allow.authenticated()]),

Describe the solution you'd like

Support for custom query functions to accept an array of arguments modeled with customType

Describe alternatives you've considered

I have explored sending arguments as JSON or a string which gets parsed in the Lambda but in both cases validation and strict typing are lost.

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request

Would this feature include a breaking change?

  • ⚠️ This feature might incur a breaking change
@anni1236012
Copy link

+1
This is an important feature to have.

@AnilMaktala AnilMaktala added Gen 2 feature-request New feature or request and removed pending-triage labels Sep 29, 2024
@AnilMaktala
Copy link
Member

Hey @ataylorme, Thank you for bringing this to our attention. We have noted it as a feature request for the team to review in more detail.

@jmarshall9120
Copy link

I don't even think you can do this:

MyCustomApiRequest: a
	.query()
	.arguments({
		requests: a.customType({
			action: a.string().required(),
			parameters: a.json().required(),
			paginate: a.boolean().required(),
		}),
	})
	.returns(a.ref('MyCustomApiRequestResponse').array())
	.handler(a.handler.function(myCustomApiRequest))
	.authorization(allow => [allow.authenticated()]),

A customType returns type in the arguments field returns this error:

Type 'RefType<SetTypeSubArg<RefTypeArgFactory<"MyCustomType">, "valueRequired", true>, "required", undefined>' is not assignable to type 'BaseModelField | EnumType<readonly string[]>'.
  Type 'RefType<SetTypeSubArg<RefTypeArgFactory<"MyCustomType">, "valueRequired", true>, "required", undefined>' is not assignable to type 'BaseModelField'.
    Types of property '[brandSymbol]' are incompatible.
      Type '"ref"' is not assignable to type '"modelField"'

This makes the arguments field even more useless in it's current for.

@bartwp
Copy link

bartwp commented Dec 4, 2024

+1
This is an important feature to have.

@jtweeks
Copy link

jtweeks commented Dec 20, 2024

+999 , everyone needs this.

@jtweeks
Copy link

jtweeks commented Dec 20, 2024

I am going to try the json type, instead of customtype array

@ataylorme
Copy link
Author

It works okay but you have to stringify and decode yourself all over so it gets messy

@jmarshall9120
Copy link

I am going to try the json type, instead of customtype array

The big issue here, is that you won't end up creating types on the app sync side. So everything that arrives at appsync will be a json string, and it'll be the wild west over on the server side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request Gen 2
Projects
None yet
Development

No branches or pull requests

6 participants