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

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

【问题讨论】:

    标签: graphql aws-cli aws-amplify aws-appsync aws-amplify-cli


    【解决方案1】:

    我的解决方案:

    1. 允许 lambda 解析器使用以下命令查询我的 API: amplify function update -> Lambda function -> <lambdaResolverfunctionName> -> Resource access permissions-> api -> Query
    2. 像典型的 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;
    }
    
    

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

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 2019-08-06
      • 1970-01-01
      • 2020-05-09
      • 2018-09-20
      • 2020-07-17
      • 2019-02-04
      • 1970-01-01
      • 2021-02-21
      相关资源
      最近更新 更多