-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.js
41 lines (35 loc) · 1.09 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { PubSub } = require('apollo-server-express');
const pubsub = new PubSub(); //create a PubSub instance
const CHANNEL_ADDED_TOPIC = 'newChannel';
const channels = [{
id: 1,
name: 'soccer',
}, {
id: 2,
name: 'baseball',
}];
let nextId = 3;
const resolvers = {
Query: {
channels: () => {
return channels;
},
channel: (root, { id }) => {
return channels.find(channel => channel.id == id);
},
},
Mutation: {
addChannel: (root, args) => { //Create a mutation to add a new channel.
const newChannel = { id: String(nextId++), messages: [], name: args.name };
channels.push(newChannel);
pubsub.publish(CHANNEL_ADDED_TOPIC, { channelAdded: newChannel }); // publish to a topic
return newChannel;
}
},
Subscription: {
channelAdded: { // create a channelAdded subscription resolver function.
subscribe: () => pubsub.asyncIterator(CHANNEL_ADDED_TOPIC) // subscribe to changes in a topic
}
}
};
module.exports = resolvers;