2017-03-17 90 views
1

我想我的後端訂閱設置正確。我使用的角度在客戶端上,當我嘗試打電話subscribe我得到一個錯誤GraphQL訂閱:調用apolloClient.subscribe的錯誤

passwordUpdatedSubscription = gql` 
subscription passwordUpdated{passwordUpdated{name password}} 
`; 

// Apollo Subscription 
var subscription = this.apollo.subscribe({ 
    query: this.passwordUpdatedSubscription 
}); 

subscription.subscribe(
    { 
    next(data) { 
     console.log(data); 
    }, 
    error(err) { console.error('err', err); }, 
    } 
); 

然後這是錯誤控制檯出現 {"type":"subscription_fail","id":0,"payload":{"errors":[{"message":"Cannot read property 'subscribe' of undefined"}]}}

在後端也許我失去了一些東西?我是否需要在SubscriptionManager中定義setupFunctions

這是我SubscriptionManager

const sub = require('graphql-subscriptions'); 
const pubSub = new sub.PubSub(); 
const manager = new sub.SubscriptionManager({ 
    schema, 
    pubSub 
}); 

這是我在graphQL

const graphql = require('graphql'); 

var schema = graphql.buildSchema(` 
    type Subscription { 
     passwordUpdated: User 
    } 
    type Mutation { 
     setMessage(message: String): String, 
     updateUserPassword(userName: String, password: String): User! 
    } 

    type Query { 
     getMessage: String, 
     getUsers: [User], 
     findUsers(userName: String): [User] 
    } 

    type User { 
     name: String, 
     password: String 
    } 
`); 
+0

錯誤是告訴你,在這種情況下apollo客戶端是未定義的。你已經檢查過嗎? –

+0

@ Locco0_0,我已檢查過。它不是沒有定義的。其他操作如'watchQuery'和'mutation'正在工作,雖然 –

+0

好,所以請在服務器上顯示Subscription Manager。你使用哪個節點包?我想你錯過了設置功能。你使用什麼樣的發佈訂閱? –

回答

1

模式是你缺少的設置功能。你可以看看這個鏈接graphql subscription docuexample

你的訂閱管理器看起來是這樣的:

const manager = new sub.SubscriptionManager({ 
 
    schema, 
 
    pubSub, 
 

 
    setupFunctions: { 
 
    passwordUpdated: (options, args) => ({ // name of your graphQL subscription 
 
     passwordUpdatedChannel: { // name of your pubsub publish-tag 
 
     filter:() => { 
 
      return true 
 
     }, 
 
     }, 
 
    }), 
 
    }, 
 
});

當你調用PubSub的發佈,你必須把它寫這樣pubsub.publish("passwordUpdatedChannel")功能。

Sidenode:您可能想要將密碼已更改爲用戶的用戶的ID添加到訂閱中。如果你這樣做,你可以將它添加到過濾器選項,可能看起來像這樣filter: (user) => {return user.id === args.userId}

+0

謝謝!它解決了問題! –