【问题标题】:Use Postman to test a service with GraphQL in AWS (AppSync, Apollo)使用 Postman 在 AWS(AppSync、Apollo)中使用 GraphQL 测试服务
【发布时间】:2020-04-04 20:18:55
【问题描述】:

我正在尝试使用 Postman 通过 Graphql 调用服务。从我的应用程序 (React) 中,我可以毫无问题地使用所有服务(因此,它们运行正常),但我想单独使用 Postman 测试它们。

我正在使用 AppSync,这是我的配置:

const AppSyncConfig = {
  url: environments.configAws.graphql.aws_appsync_graphqlEndpoint,
  region: environments.configAws.graphql.aws_appsync_region,
  auth: {
    type: AUTH_TYPE.API_KEY,
    apiKey: environments.configAws.graphql.aws_appsync_apiKey,
  }
};

环境变量是这样的:

graphql: {
    aws_project_region: "us-east-1",

    aws_appsync_graphqlEndpoint: "https://XXXXXXXXXXXXX.appsync-api.us-east-1.amazonaws.com/graphql",
    aws_appsync_region: "us-east-1",
    aws_appsync_authenticationType: "API_KEY",
    aws_appsync_apiKey: "da2-XXXXXXXXXXXX"
}

应用程序使用它来进行查询:

const client = new AWSAppSyncClient(AppSyncConfig, {
  link: createAppSyncLink({ ...AppSyncConfig,
    resultsFetcherLink: ApolloLink.from([
      setContext((request, previousContext) => ({
        headers: { ...previousContext.headers,
          Authorization: localStorage.getItem('token') ? localStorage.getItem('token') : ''
        }
      })),
      createHttpLink({
        uri: AppSyncConfig.url
      })
    ])
  })
});

const data_configs = client.readQuery({ query: query_configs });

在 Postman 中我有这个配置,但我收到一个错误:

我今天拿到了这段代码,我之前没有使用过 Graphql(所以也没有使用过 AppSync、ApolloLink 等),但我问它是否有一个简单的解决方案,或者至少提示我应该检查什么。谢谢。

【问题讨论】:

    标签: react-apollo graphql-js aws-appsync


    【解决方案1】:

    我能够使用 Postman 使用以下步骤调用我的 AWS Amplify GraphQL 端点:

    1. 创建一个新的 Postman 请求
    2. 将请求类型设置为“POST”
    3. 在请求 URL 中输入 API URL
    4. 在“标题”选项卡中添加“x-api-key”标题:{{API KEY}}
    5. 添加由格式良好的 GraphQL 查询组成的正文
    6. 点击发送

    【讨论】:

      【解决方案2】:

      在请求标头中放入“x-api-id”对我有用。

      【讨论】:

        【解决方案3】:

        我认为 API KEY 的标头名称不是 API_KEY 而是 x-api-key。

        【讨论】:

          猜你喜欢
          • 2022-01-21
          • 2020-11-01
          • 2020-08-01
          • 2020-07-27
          • 2017-11-29
          • 2020-10-05
          • 2022-12-29
          • 2019-06-21
          • 1970-01-01
          相关资源
          最近更新 更多