【发布时间】:2017-09-01 17:41:40
【问题描述】:
我已经按照 Apollo 的文档在客户端和服务器上设置 GraphQL 订阅,虽然我已经完成了 90%,但我不知道如何设置订阅通道以及如何将突变连接到这些通道这样无论何时发生突变,服务器都会将新数据推送到客户端。 (对于内容,我正在制作一个 Reddit 克隆,人们在其中发布主题和其他人对其发表评论。因此,当您看到“主题”或“主题列表”时,将它们视为帖子。)
到目前为止,我已经成功设置了 Apollo Client 进行订阅:
const wsClient = new SubscriptionClient('ws://localhost:3001/subscriptions', {
reconnect: true
});
const networkInterface = createNetworkInterface({
uri: '/graphql',
opts: {
credentials: 'same-origin'
}
});
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient,
);
const client = new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions,
dataIdFromObject: o => o.id
});
我还为订阅设置了后端。这是我的 server.js 文件:
//===========================================================
//Subscription Managaer
//===========================================================
const pubsub = new PubSub();
const subscriptionManager = new SubscriptionManager({
schema: schema,
pubsub: pubsub
});
//=====================================
//WebSocket + Express Server
//=====================================
const server = createServer(app);
//setup listening port
server.listen(3001, ()=>{
new SubscriptionServer(
{
subscriptionManager: subscriptionManager,
onConnect: (connectionParams, webSocket) => {
console.log('Websocket connection established');
},
onSubscribe: (message, params, webSocket) => {
console.log("The client has been subscribed", message, params);
},
onUnsubsribe: (webSocket) => {
console.log("Now unsubscribed");
},
onDisconnect: (webSocket) => {
console.log('Now disconnected');
}
},
{
server: server,
path: '/subscriptions',
});
console.log('Server is hot my man!');
})
我知道这些都是成功的,因为我在终端中记录了“Websocket 连接已建立”消息。
接下来是实际订阅 - 我创建了订阅模式类型(就像查询和突变一样):
const SubscriptionType = new GraphQLObjectType({
name: 'Subscription',
fields: () => ({
topicAdded: {
type: TopicType,
args: {repoFullName: {type: GraphQLString}}, //I don't understand what repoFullName is - I was trying to follow the Apollo docs, but they never specified that
resolve(parentValue, args){
return parentValue;
}
}
})
});
module.exports = SubscriptionType;
并将其合并到我的根架构中。因此,当我查看 GraphiQL 时,我看到:Docs 侧菜单中提供了此订阅 My GraphiQIL UI showing the subscriptionSchema successfully
在我的 React 组件中,我使用 Apollo 的 subscribeToMore 方法成功地“订阅”了它:
const TOPICS_SUBSCRIPTION = gql`
subscription OnTopicAdded($repoFullName: String){
topicAdded(repoFullName: $repoFullName){
id
}
}
`;
class TopicList extends Component {
componentDidMount() {
this.createMessageSubscription = this.props.data.subscribeToMore({
document: TOPICS_SUBSCRIPTION,
// updateQuery: (previousState, {subscriptionData}) => {
// const newTopic = subscriptionData.data.Topic.node
// const topics = previousState.findTopics.concat([newTopic])
// return {
// findTopics: topics
// }
// },
onError: (err) => console.error(err)
})
} //...
我在终端中收到“客户已订阅”消息。但这就是我卡住的地方。我已经阅读了有关 SubscriptionManager 的 SetupFunction 的信息,但这不包含在 Apollo 的文档中。而且我找不到如何将“createTopic”突变映射到此订阅,以便每当有人添加新主题时,它都会在 TopicList 中弹出。
我意识到这真的很长,但我一直在努力弄清楚下一步是什么。任何帮助将非常感激!!感谢您的阅读!
【问题讨论】:
标签: reactjs express publish-subscribe graphql apollo