2017-06-29 21 views
0

我的组件正在调用订阅查询,但由于某种原因订阅解析器未被访问:其中的断点永远不会被激活。但在客户端上,我收到GraphQL订阅错误:阿波罗订阅解析器永不激活?

"Subscription must return Async Iterable. Received: undefined"

什么可能导致这种情况?

在此先感谢所有的任何信息。

认购QUERY

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

RESOLVER

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

COMPONENT

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], 
          }, 
         }); 
        } 
       }); 
      } 
     }; 
    }, 
}) 
; 
+0

你是否初始化'pubsub'服务的一个实例? 您是否在服务器订阅功能中尝试使用console.log? –

+0

感谢您的输入。事实上,它变成了需要改变的服务器设置。我将发布修复它的服务器端代码。 – VikR

回答

0

服务器-SID e设置和突变/订阅解析器已经发生了很大的变化,因为我的帖子中的代码是基于去年年底Apollo libs的。下面是代码,与当前的阿波罗库工作,通过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 

突变RESOLVER

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

认购RESOLVER

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