如何使用appsync httpdatasource访问logz api搜索

问题描述 投票:0回答:1

我试图使用他们提供给我的api-search来访问我在logz.io上存储的日志。

实际上,我可以使用curl命令成功访问,因为我显示:

curl -X POST 'https://api.logz.io/v1/search'  
--header "X-API-TOKEN: API-TOKEN-GENERATED" 
--header "Content-Type: application/json" 
-d '{"query": {"term": {"_id": {"value": "Log Id Here"}}}}', 

就像https://github.com/logzio/public-api/tree/master/search说的那​​样。

但是,当我使用AWS AppSync api时,使用带有params名称的HttpResolver数据源:HttpDataSourceTest,键入:HTTP和endpoint:https://api.logz.io/v1/search,我定义了schema.grapqhl,请求和响应模板解析器:

schema.grapgql

type Query {
    GetLog(id: String): Log
} 

请求模板解析器:

{
    "version": "2018-05-29",
    "method": "POST",
    "params": {
        "headers": {
            "Content-Type: application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        },
    "body":{
        "query": {
            "term": {
                "_id": {
                    "value": "$context.arguments.id"
                }
             }
         }
    }
    },
    "resourcePath": "/"
}  

解决模板响应:

$utils.toJson({"TimeStamp":"$ctx.result.statusCode $ctx.result.body" })

经过多次尝试并失败后,我保持非常简单,只需要查询TimeStamp字段并显示状态,并在响应中返回所有内容。

完成所有这些配置后,我得到了这样的响

{
    "data": {
        "GetLog": {
            "TimeStamp": "403 {\"message\":\"Forbidden\"}"
        }
    }
}

当我跳过X-API-TOKEN param标头时,同样的结果,就像HttpDatasource不发送那个参数。我是新用的所有技术,AWS服务和Logz.io,请告诉我,如果我在某个地方省略某些东西。

graphql aws-appsync logz.io
1个回答
0
投票

单个http数据源可以用于我的许多解析器,并且相对于同一个根命中不同的路径。因此,在设置HTTP数据源时,将端点设置为https://api.logz.io,然后将其用于请求映射模板:

{
    "version": "2018-05-29",
    "method": "POST",
    ## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
    "resourcePath": "/v1/search",
    "params":{
        "body":{
          "query": {
            "term": {
              "_id": {
                "value": "$context.arguments.id"
              }
            }
          }
        },
        "headers":{
            "Content-Type": "application/json",
            "X-API-TOKEN":"API-TOKEN-GENERATED"
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.