Skip to content

AppSync event looks like a map and not like AppSyncResolverTemplate #150

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

Open
sashker opened this issue Dec 9, 2018 · 5 comments
Open

Comments

@sashker
Copy link

sashker commented Dec 9, 2018

Hello.

I'm awaiting events.AppSyncResolverTemplate having all usual fields like Operation, Version, Payload from AppSync.

My mapping template produced from the original Invoke and forward arguments

{
    "version" : "2017-02-28",
    "operation": "Invoke",
    "payload": $util.toJson($context.arguments)
}

I thought I have to recieve a JSON with all those fields. But when I eventually decided to specify an event like an interface{} it turned out that I recieve a map[] of values

map[input:map[someField:1922 locationCountry:US]]

Can somebody clarify, please, if it works as expected or not?

@daviskoh
Copy link
Contributor

daviskoh commented Feb 12, 2019

I can confirm this

mapping template:

{
  "version": "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "field": "correctField",
    "arguments":  $utils.toJson($context.arguments)
  }
}
func(event events.AppSyncResolverTemplate) error {
...
}

does not populate event. Logging event will output:

{ []}

Edit 1:

Following the README format with:

func(ctx context.Context, event events.AppSyncResolverTemplate) error {
...
}

populates ctx, but again event is an empty { []}

Edit 2:

Doesn't seem to be like events.APIGatewayProxyRequest either this guy is suggesting...

func(req events.APIGatewayProxyRequest) error {
...
}

Printing req outputs:

{ map[] map[] map[] map[] map[] map[] { { } map[] } false}

Edit 3:

Also tried changing the mapping template:

{
  "version": "2017-02-28",
  "operation": "Invoke",
  "payload": $utils.toJson($context.arguments)
}

and get the same results.

Are Go handlers for AppSync just not supported? This issue makes them unusable. Documentation only exists for NodeJS, so maybe the inclusion of AppSyncResolverTemplate is just a preview as Go is not actually supported for AppSync? What are the actual valid arg types for Handler?

@daviskoh
Copy link
Contributor

Are there any updates on this? This issue has been open for a few months...

@daviskoh
Copy link
Contributor

@sashker did you figure out this issue?

@daviskoh
Copy link
Contributor

I managed to get everything working. My mistake was a misunderstanding of what exactly is passed into the Handlers.

AppSyncResolverTemplate is not what is passed in. Rather, what is defined as the payload field in the mapping template is what is passed into the Handler.

For ex:

mapping template:

{
  "version": "2017-02-28",
  "operation": "Invoke",
  "payload": $utils.toJson($context.args) // <-- note this is args not arguments
}

sample query:

query Post {
  singlePost(id: "meowww") {
    id
  }
}

Type:

type Post {
  Id string `json:"id"` // tags are required if you plan on reusing the type as a response as well
}

Handler:

func Handler(post Post) (Post, error) {
  ...
}

I'm unsubscribing to this thread. @ me if u have any questions.

@cduggn
Copy link

cduggn commented Jul 25, 2022

@daviskoh I think the map type might be more flexible in these situations. Map can adjust to new arguments being added, and usage can be replicated across other handlers. The strict format of the struct you mentioned above will also work, just not as flexible

type Event struct {
	Arguments map[string]string `json:"arguments"`
	Field     string            `json:"field"`
}

func HandleRequest(ctx context.Context, e Event) (string, error) {`
        id := e.Arguments["id"]
	fmt.Println("id: ", id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants