简体   繁体   English

返回 hasura 变量时出错 - Auth Webhook - AWS Lambda

[英]ERROR when returning hasura variables - Auth Webhook - AWS Lambda

I am trying to return the hasura variables in a nodejs 12 AWS Lambda but I get the error:我正在尝试在nodejs 12 AWS Lambda 中返回 hasura 变量,但出现错误:

GraphQL error: Invalid response from authorization hook: Error in $.hasuraVariables: parsing Text failed, expected String, but encountered Object GraphQL 错误:来自授权挂钩的无效响应:$.hasuraVariables 中的错误:解析文本失败,预期字符串,但遇到 Object

My code:我的代码:

let hasuraVariables = {
    "X-Hasura-Role": "user", 
    "X-Hasura-User-Id": user.id, 
};

if (user.publisher && user.publisher.id) {
    Object.assign(hasuraVariables, {
        "X-Hasura-Publisher-Id": user.publisher.id.toString()
    }) 
}

const body = JSON.stringify({
    hasuraVariables
});
console.log("body", body)

return {
    "statusCode": "200",
    "body": body
}

If I log the body its:如果我记录body

{"hasuraVariables":{"X-Hasura-Role":"user","X-Hasura-User-Id":"d61ea04f-421b-48a8-92a2-de6d00491425","X-Hasura-Publisher-Id":"110"}} {"hasuraVariables":{"X-Hasura-Role":"user","X-Hasura-User-Id":"d61ea04f-421b-48a8-92a2-de6d00491425","X-Hasura-Publisher-Id": “110”}}

If I return the variables without stringify first:如果我先返回没有字符串化的变量:

return {
    "statusCode": "200",
    "body": hasuraVariables
}

I get the error:我得到错误:

GraphQL error: Invalid response from authorization hook: Error in $: Failed reading: satisfy. GraphQL 错误:来自授权挂钩的无效响应:$ 中的错误:读取失败:满足。 Expecting object key at ''X-Hasura-Role':'user','X-Hasura-User-Id':'d61ea04f-421b-48a8-92a2-de6d00491425','X-Hasura-Pub'期望 object 键位于 ''X-Hasura-Role':'user','X-Hasura-User-Id':'d61ea04f-421b-48a8-92a2-de6d00491425','X-Hasura-Pub'

What is the correct way to return the variables?返回变量的正确方法是什么?

I believe you should use我相信你应该使用

const body = JSON.stringify(hasuraVariables);

instead of代替

const body = JSON.stringify({
    hasuraVariables
});

From the docs the response should be an object with the key/value header pairs directly, not below a hasuraVariables key like you're doing.文档中,响应应该是一个 object 直接键/值 header 对,而不是像你正在做的那样在hasuraVariables键之下。 This example also illustrates how you should make this response. 此示例还说明了您应该如何做出此响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM