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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘