amazon-web-servicesgoaws-lambdaaws-appsync

Calling AppSync Mutation from Lambda with Golang


I'm trying to invoke a mutation from lambda (specifically using golang). I used AWS_IAM as the authentication method of my AppSync API. I also give appsync:GraphQL permission to my lambda.

However, after looking at the docs here: https://docs.aws.amazon.com/sdk-for-go/api/service/appsync/

I can't find any documentation on how to invoke the appsync from the library. Can anyone point me to the right direction here?

P.S. I don't want to query or subscribe or anything else from lambda. It's just a mutation

Thanks!

------UPDATE-------

Thanks to @thomasmichaelwallace for informing me to use https://godoc.org/github.com/machinebox/graphql

Now the problem is how can I sign the request from that package using aws v4?


Solution

  • I found a way of using plain http.Request and AWS v4 signing. (Thanks to @thomasmichaelwallace for pointing this method out)

    client := new(http.Client)
    // construct the query
    query := AppSyncPublish{
        Query: `
            mutation ($userid: ID!) {
                publishMessage(
                    userid: $userid
                ){
                    userid
                }
            }
        `,
        Variables: PublishInput{
            UserID:     "wow",
        },
    }
    b, err := json.Marshal(&query)
    if err != nil {
        fmt.Println(err)
    }
    
    // construct the request object
    req, err := http.NewRequest("POST", os.Getenv("APPSYNC_URL"), bytes.NewReader(b))
    if err != nil {
        fmt.Println(err)
    }
    req.Header.Set("Content-Type", "application/json")
    
    // get aws credential
    config := aws.Config{
        Region: aws.String(os.Getenv("AWS_REGION")),
    }
    sess := session.Must(session.NewSession(&config))
    
    
    //sign the request
    signer := v4.NewSigner(sess.Config.Credentials)
    signer.Sign(req, bytes.NewReader(b), "appsync", "ap-southeast-1", time.Now())
    
    //FIRE!!
    response, _ := client.Do(req)
    
    //print the response
    buf := new(bytes.Buffer)
    buf.ReadFrom(response.Body)
    newStr := buf.String()
    
    fmt.Printf(newStr)