如何在自定义解析器 AWS Amplify CLI 中查询 API?

Posted

技术标签:

【中文标题】如何在自定义解析器 AWS Amplify CLI 中查询 API?【英文标题】:How to query to API in custom resolver AWS Amplify CLI? 【发布时间】:2021-06-08 09:53:45 【问题描述】:

我想使用graphql.schema 中定义的查询在我的自定义解析器中获取数据。可能吗?怎么查询?

【问题讨论】:

【参考方案1】:

我的解决方案:

    允许 lambda 解析器使用以下命令查询我的 API: amplify function update -> Lambda function -> <lambdaResolverfunctionName> -> Resource access permissions-> api -> Query 像典型的 graphQL 查询一样获取数据:
const myResolver = async (event) => 
  const endpoint = process.env.API_<apiName>_GRAPHQLAPIENDPOINTOUTPUT;
  const  authorization  = event.request.headers;
  const query = 'query ...';

  const response = await fetch(endpoint, 
    method: 'POST',
    headers: 
      'Content-Type': 'application/json',
      authorization
    ,
    body: query
  );

  const  data  = await response.json();

  return data;


有没有更好的方法来做到这一点?

【讨论】:

以上是关于如何在自定义解析器 AWS Amplify CLI 中查询 API?的主要内容,如果未能解决你的问题,请参考以下文章