GraphQL:订阅查询未触发字段解析器?

时间:2019-03-26 01:49:03

标签: graphql apollo-server apollostack graphql-subscriptions

我有一个订阅查询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...]
            }
        },
    }
`;

这是它的架构:

SCHEMA

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;
                }
            )
        }
}

当我对相关项目进行突变时,pubsub将按预期触发MessageFolder_Subscription查询。追溯,我看到它返回true

但是由于某些原因,remoteUserData上的字段MessageFolder_Subscription的字段解析器永远不会触发。

我想念什么?

1 个答案:

答案 0 :(得分:0)

已解决。我必须添加__typename:字段:

const messageFolder_Subscription = {
    __typename: 'messageFolder_Subscription',
    id: userID,
}

...到MessageFolder_Subscription订阅对象,该对象在传递给pubsub之前在变异解析器中创建。