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

ERROR when returning hasura variables - Auth Webhook - AWS Lambda

我正在尝试 return nodejs 12 AWS Lambda 中的 hasura 变量,但出现错误:

GraphQL error: Invalid response from authorization hook: Error in $.hasuraVariables: parsing Text failed, expected String, but encountered Object

我的代码:

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
}

如果我记录 body 它的:

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

如果我return变量没有先stringify:

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

我收到错误:

GraphQL error: Invalid response from authorization hook: Error in $: Failed reading: satisfy. Expecting object key at ''X-Hasura-Role':'user','X-Hasura-User-Id':'d61ea04f-421b-48a8-92a2-de6d00491425','X-Hasura-Pub'

return变量的正确方法是什么?

我相信你应该使用

const body = JSON.stringify(hasuraVariables);

而不是

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

来自 docs the response should be an object with the key/value header pairs directly, not below a hasuraVariables key like you're doing. This example 还说明了您应该如何做出此响应。