gpt4 book ai didi

javascript - 如何将签名的 HTTP 请求从 AWS Lambda 发送到 AppSync GraphQL?

转载 作者:行者123 更新时间:2023-11-29 10:56:07 25 4
gpt4 key购买 nike

我不确定如何向 AppSync GraphQL 端点发送签名的 http 请求。 AWS 中没有用于执行此操作的库。

  • aws-amplify 不工作,因为只在浏览器中工作,而不在 Lambda 函数中工作。
  • aws-sdk AppSync 仅供管理员使用,它没有调用用户端 api 的方法

可以从 AWS Lambda 发出 IAM 签名的 HTTP 请求吗? (以一些简单的方式)

最佳答案

我会推荐阅读这篇文章:Backend GraphQL: How to trigger an AWS AppSync mutation from AWS Lambda ,

引用作者,https://stackoverflow.com/users/1313441/adrian-hall ,我们有:

GraphQL is routed over HTTPS. That means we can simulate the GraphQL client libraries with a simple HTTPS POST. Since we are using IAM, we need to sign the request before we deliver it. Here is my code for this:

// ... more code here
// POST the GraphQL mutation to AWS AppSync using a signed connection
const uri = URL.parse(env.GRAPHQL_API);
const httpRequest = new AWS.HttpRequest(uri.href, env.REGION);
httpRequest.headers.host = uri.host;
httpRequest.headers['Content-Type'] = 'application/json';
httpRequest.method = 'POST';
httpRequest.body = JSON.stringify(post_body);

AWS.config.credentials.get(err => {
const signer = new AWS.Signers.V4(httpRequest, "appsync", true);
signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());

const options = {
method: httpRequest.method,
body: httpRequest.body,
headers: httpRequest.headers
};

fetch(uri.href, options)
// ... more code here

我一直将它用作我所有 Lambda->AppSync 通信的模板!

关于javascript - 如何将签名的 HTTP 请求从 AWS Lambda 发送到 AppSync GraphQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56952799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com