Apollo 订阅解析器永远不会激活? [英] Apollo Subscription Resolver Never Activates?

查看:33
本文介绍了Apollo 订阅解析器永远不会激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组件正在调用订阅查询,但由于某种原因订阅解析器未被访问:其中的断点从未被激活.然而在客户端,我收到了 GraphQL 订阅错误:

My component is calling a subscription query, but for some reason the subscription resolver isn't being accessed: a breakpoint in it is never activated. And yet on the client I get the GraphQL subscription error:

订阅必须返回异步迭代.收到:未定义"

"Subscription must return Async Iterable. Received: undefined"

这可能是什么原因造成的?

What could be causing this?

提前感谢大家提供任何信息.

Thanks in advance to all for any info.

订阅查询

const IM_SUBSCRIPTION_QUERY = gql`
      subscription getIMsViaSubscription($fromID: String!, $toID: String!){
          IMAdded(fromID:$fromID, toID: $toID){
            id,
            fromID,
            toID,
            msgText,
            myUserData{
                id,
                name_first,
                name_last,
                picture_large,
                picture_medium,
                picture_thumbnail
                my_category
            }
          }
        } 
`;

解析器

Subscription: {
    IMAdded(IMThatWasAdded) {
        debugger; <== IS NEVER ACTIVATED
        subscribe: withFilter(() => SubscriptionServer.pubsub.asyncIterator(IM_ADDED_CHANNEL), (newIM, args) => {
            const callIsFromMsgFoldersComponent = args.toID == 0;
            var result;
            if (callIsFromMsgFoldersComponent){
                result = (newIM.fromID === args.fromID || newIM.toID === args.fromID);
            } else {
                result = ((newIM.fromID === args.fromID && newIM.toID === args.toID) || (newIM.fromID === args.toID && newIM.toID === args.fromID));
            }
            return result;
        })

组件

const withDataAndSubscription = graphql(GETIMS_QUERY, {
    options({toID}) {
        console.log(GETIMS_QUERY);
        const fromID = Meteor.userId();
        return {
            fetchPolicy: 'cache-and-network',
            variables: {fromID: `${fromID}`, toID: `${toID}`}
        };
    }
    ,
    props: props => {
        debugger;
        return {
            loading: props.data.loading,
            instant_message: props.data.instant_message,
            subscribeToMore: props.data.subscribeToMore,
            subscribeToNewIMs: params => {
                debugger;  <==IS ACTIVATED AS EXPECTED
                console.log(IM_SUBSCRIPTION_QUERY); <==IS OKAY
                const fromID = Meteor.userId();
                const toID = params.toID;
                return props.data.subscribeToMore({
                    document: IM_SUBSCRIPTION_QUERY,
                    variables: {fromID: `${fromID}`, toID: `${toID}`},
                    updateQuery: (previousResult, {subscriptionData}) => {
                        if (!subscriptionData.data) {
                            return previousResult;
                        }
                        const newMsg = subscriptionData.data.createIM;
                        return update(previousResult, {
                            instant_message: {
                                $push: [newMsg],
                            },
                        });
                    }
                });
            }
        };
    },
})
;

推荐答案

自从我的帖子中的代码以来,服务器端设置和突变/订阅解析器已经发生了很大的变化,这些代码基于去年年底的 Apollo 库.这是适用于当前 Apollo 库的代码,通过 本教程:

Server-side setup, and mutation/subscription resolvers have changed quite a bit since the code in my post, which was based on Apollo libs from late last year. Here is code that works with current Apollo libs, via this tutorial:

服务器设置

import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import bodyParser from 'body-parser';
import  cors  from 'cors';
import { execute, subscribe } from 'graphql';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';

import schema from '/imports/api/schema';

//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
const METEOR_PORT = 3000;
const GRAPHQL_PORT = 4000;
const server = express();

server.use('*', cors({ origin: 'http://localhost:${METEOR_PORT}' }));

server.use('/graphql', bodyParser.json(), graphqlExpress({
    schema
}));

server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    subscriptionsEndpoint: `ws://localhost:${GRAPHQL_PORT}/subscriptions`
}));

// Wrap the Express server
const ws = createServer(server);
ws.listen(GRAPHQL_PORT, () => {
    console.log(`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`);
    // Set up the WebSocket for handling GraphQL subscriptions
    new SubscriptionServer({
        execute,
        subscribe,
        schema
    }, {
        server: ws,
        path: '/subscriptions',
    });
});
//END OF SET UP APOLLO PUBSUB

突变解析器

   Mutation: {
        createIM(root, args, context) {
            var associatedUsers = [];
            associatedUsers.push(args.fromID);
            associatedUsers.push(args.toID);
            var IDofNewIM;

            return Promise.resolve()
                .then(() => {
                    const newIM = connectors.IMs.create(args);
                    return newIM;
                })
                .then(IMAdded => {
                    // publish subscription notification
                    pubsub.publish(IM_ADDED_CHANNEL, { IMAdded, args});
                    return IMAdded;
                })
                .catch((err)=> {
                    console.log(err);
                });
        },

订阅解析器

IMAdded: {
    subscribe: withFilter(
        () => pubsub.asyncIterator(IM_ADDED_CHANNEL),
        (IMAdded, args) => {
            IMAdded = IMAdded.IMAdded;
            var result = ((IMAdded.fromID === args.fromID && IMAdded.toID === args.toID) || (IMAdded.fromID === args.toID && IMAdded.toID === args.fromID));
            return result;
        }
    )
},

这篇关于Apollo 订阅解析器永远不会激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆