gpt4 book ai didi

apollo - AWS AppSync 订阅在握手期间失败

转载 作者:行者123 更新时间:2023-12-02 01:35:58 27 4
gpt4 key购买 nike

我正在尝试设置 graphql 订阅,但收到错误:

Unhandled GraphQL subscription error Error: Error during subscription handshake

我正在使用 AWS Cognito 用户池进行授权。

创建我正在使用的订阅:

this.subscription = this.props.client.subscribe({ query: gql(onCreateVehicle) }).subscribe({
next: response => {
console.log(response.data.onCreateVehicle);
},
error: error => {
console.warn(error);
}
});

“onCreateVehicle”由 Amplify 自动生成,如下所示:

export const onCreateVehicle = `subscription OnCreateVehicle($owner: String!) {
onCreateVehicle(owner: $owner) {
id
name
events {
items {
id
name
date
isDeleted
owner
}
nextToken
}
sessions {
items {
id
name
isDeleted
createdAt
owner
}
nextToken
}
isDeleted
owner
}
}
`;

请求解析器:

{
"version": "2018-05-29",
"payload": {}
}

响应解析器:

## [Start] Determine request authentication mode **
#if( $util.isNullOrEmpty($authMode) && !$util.isNull($ctx.identity) && !$util.isNull($ctx.identity.sub) && !$util.isNull($ctx.identity.issuer) && !$util.isNull($ctx.identity.username) && !$util.isNull($ctx.identity.claims) && !$util.isNull($ctx.identity.sourceIp) && !$util.isNull($ctx.identity.defaultAuthStrategy) )
#set( $authMode = "userPools" )
#end
## [End] Determine request authentication mode **
## [Start] Check authMode and execute owner/group checks **
#if( $authMode == "userPools" )
## No Static Group Authorization Rules **


## [Start] Owner Authorization Checks **
#set( $isOwnerAuthorized = false )
## Authorization rule: { allow: owner, ownerField: "owner", identityClaim: "cognito:username" } **
#set( $allowedOwners0 = $util.defaultIfNull($ctx.args.owner, null) )
#set( $identityValue = $util.defaultIfNull($ctx.identity.claims.get("username"),
$util.defaultIfNull($ctx.identity.claims.get("cognito:username"), "___xamznone____")) )
#if( $util.isList($allowedOwners0) )
#foreach( $allowedOwner in $allowedOwners0 )
#if( $allowedOwner == $identityValue )
#set( $isOwnerAuthorized = true )
#end
#end
#end
#if( $util.isString($allowedOwners0) )
#if( $allowedOwners0 == $identityValue )
#set( $isOwnerAuthorized = true )
#end
#end
## [End] Owner Authorization Checks **


## [Start] Throw if unauthorized **
#if( !($isStaticGroupAuthorized == true || $isOwnerAuthorized == true) )
$util.unauthorized()
#end
## [End] Throw if unauthorized **
#end
## [End] Check authMode and execute owner/group checks **

$util.toJson(null)

我知道它与授权有关,因为当我从 schema.graphql 中删除 @auth 时,它没有引发握手错误。请求解析器中是否应该有一些东西来处理授权?

亚当

最佳答案

我只需通过变量对象传递“所有者”值。

subscribeToVehicles: async () => props.data.subscribeToMore({
document: gql(onCreateVehicle),
variables: {
owner: (await Auth.currentSession()).getIdToken().payload.sub
},
...

关于apollo - AWS AppSync 订阅在握手期间失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com