aws / aws/aws-appsync-community

[Feature Request] Add datasource name to context in Javascript functions

Open
#258 0 comments 0 reactions 0 assignees View on GitHub
feature-request
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

Hey there, I've been using the new JS functions in appsync and I'm a fan.
I'm in the middle of building an API and I think the following feature would add value to the service.
I'm not sure if this is the right place to request, but I thought I would give it a shot :)

### Context
I use some generic function code for Creating/Updating my dynamodb datasources.
I keep naming consistent, i,e
- Table Name: `Foo`
- Datasource Name: `Foo`
- Function Name: `createFoo`

Due to some version control requirements, on every `createFoo` I write two items to the dynamo table.
- The first has a `version` attribute set to `current`
- The second has `version` set to the current RFC datetime

### Problem
My issue is that when I go to use `BatchPutItem`, [documented here](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-batch-put-item), I am required to provide the Table Name, e.g, `Foo`, which is not available to me as a variable.

To workaround the issue, and to keep my CRUD code generic, I am now keeping a map of all Queries to Datasources.
```
function dynamoTable(ctx){
const src = ctx?.prev?.result?.info;
const map = {
"Mutation": {
"createFoo": "Foo",
"createBar": "Bar"
}
};
return map[src?.parentTypeName][src?.fieldName];
}
```

### Expectation
I would prefer not have to maintain the map in my function code.
At run-time, I would like to get the datasource name that a function is attached to.

- Given a JS function is executing in Appsync on a Datasource named `Foo`
- When the developer has declared and set a variable as `let bar = ctx.target.name;`
- Then the `bar` variable will be set to the name of the datasource, `Foo`

Contributor guide

Open the contributing guide

Research direction

Start with the issue's AppSync JavaScript resolver context request and the linked BatchPutItem documentation. Determine whether a function can expose the attached datasource name through ctx.target.name, and define the runtime behavior for a function attached to the Foo datasource. Done means the declared variable receives Foo without a manually maintained mapping.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, graphql, javascript
Domain
api, backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.