代码之家  ›  专栏  ›  技术社区  ›  kkesley

用Golang从Lambda调用AppSync突变

  •  1
  • kkesley  · 技术社区  · 6 年前

    我试图从lambda调用一个突变(特别是使用golang)。我曾经 AWS_IAM 作为我的AppSync API的身份验证方法。我也给 appsync:GraphQL 允许我的lambda。

    https://docs.aws.amazon.com/sdk-for-go/api/service/appsync/

    我找不到任何关于如何从库中调用appsync的文档。有人能给我指一下这里的正确方向吗?

    另外,我不想向lambda查询或订阅任何内容。只是一种变异

    谢谢!

    ------ -------

    感谢@thomasmichaelwallace通知我使用 https://godoc.org/github.com/machinebox/graphql

    2 回复  |  直到 6 年前
        1
  •  2
  •   kkesley    6 年前

    我找到了一种简单的方法 http.Request

    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)
    
        2
  •  1
  •   thomasmichaelwallace    6 年前

    问题在于,该API/库旨在帮助您创建/更新应用程序同步实例。

    最简单的测试方法是登录到AWS AppSync控制台,按下侧栏中的“查询”按钮,然后输入并运行您的程序。

    我对go不是很在行,但从我所看到的情况来看,golang中有GraphQL的客户机库(例如。 https://godoc.org/github.com/machinebox/graphql ).

    如果您使用的是IAM,则需要使用v4签名对请求进行签名(有关详细信息,请参阅本文: https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html )

    推荐文章