GraphQL:字段解析器未在订阅查询中触发?
Posted
技术标签:
【中文标题】GraphQL:字段解析器未在订阅查询中触发?【英文标题】:GraphQL: Field Resolver Not Firing on Subscription Query? 【发布时间】:2019-08-16 08:10:07 【问题描述】:我有一个订阅查询,MessageFolder_Subscription
,看起来像这样:
查询
const MESSAGEFOLDER_SUBSCRIPTION_QUERY = gql`
subscription ($localUserID: String!)
MessageFolder_Subscription(userID: $localUserID)
id
remoteUserData
id
name_title
name_first
name_last
[...more fields...]
,
`;
这是它的架构:
架构
type myUserData
id: String
gender: String
name_title: String
name_first: String
*[...more fields...]*
type messageFolder
id: String
remoteUserData: myUserData
type Subscription
MessageFolder_Subscription(userID: String!): messageFolder
我是这样处理解析器的:
解决者
const resolvers =
//FIELD RESOLVER
MessageFolder_Subscription:
subscribe: withFilter(
() => pubsub.asyncIterator(MSGFOLDER_ADDED_CHANNEL),
(payload, args) =>
debugger; <=== NEVER FIRES
if (typeof (payload) === 'undefined')
return false;
let result = false;
const userId = Meteor.userId();
// let messageFolder = MessageFolder_Subscription.messageFolder;
result = (userId === args.fromID || args === MSGFOLDERargs.toID);
return result;
)
,
//ROOT RESOLVER
*[......more resolvers here.....]*
Subscription:
MessageFolder_Subscription:
subscribe: withFilter(
() => pubsub.asyncIterator(MSGFOLDER_ADDED_CHANNEL),
(payload, args) =>
debugger;
if (typeof (payload) === 'undefined')
return false;
let result = false;
const userId = Meteor.userId();
// let messageFolder = MessageFolder_Subscription.messageFolder;
result = (userId === args.fromID || args === MSGFOLDERargs.toID);
return result;
)
当我改变相关项目时,MessageFolder_Subscription
查询会按预期由 pubsub 触发。追溯,我可以看到它返回true
。
但由于某种原因,MessageFolder_Subscription
上的字段 remoteUserData
的字段解析器永远不会触发。
我错过了什么?
【问题讨论】:
【参考方案1】:解决了。我必须添加 __typename:
字段:
const messageFolder_Subscription =
__typename: 'messageFolder_Subscription',
id: userID,
...到MessageFolder_Subscription
订阅对象,当它在突变解析器中创建时,在传递给pubsub
之前。
【讨论】:
以上是关于GraphQL:字段解析器未在订阅查询中触发?的主要内容,如果未能解决你的问题,请参考以下文章