duan7772 2018-10-08 02:35
浏览 66
已采纳

使用Golang从Lambda调用AppSync突变

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?

  • 写回答

2条回答 默认 最新

  • doulin3510 2018-10-09 00:38
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?