|
| 1 | +const { gql, ApolloServer, ApolloError, PubSub, withFilter } = require('apollo-server') |
| 2 | +const shortid = require('shortid') |
| 3 | + |
| 4 | +const typeDefs = gql` |
| 5 | +type Channel { |
| 6 | + id: ID! |
| 7 | + label: String! |
| 8 | + messages: [Message!]! |
| 9 | +} |
| 10 | +
|
| 11 | +type Message { |
| 12 | + id: ID! |
| 13 | + channel: Channel! |
| 14 | + text: String! |
| 15 | +} |
| 16 | +
|
| 17 | +type Query { |
| 18 | + channels: [Channel!]! |
| 19 | + channel (id: ID!): Channel |
| 20 | +} |
| 21 | +
|
| 22 | +type Mutation { |
| 23 | + addMessage (input: AddMessageInput!): Message |
| 24 | + updateMessage (input: UpdateMessageInput!): Message |
| 25 | +} |
| 26 | +
|
| 27 | +input AddMessageInput { |
| 28 | + channelId: ID! |
| 29 | + text: String! |
| 30 | +} |
| 31 | +
|
| 32 | +input UpdateMessageInput { |
| 33 | + id: ID! |
| 34 | + channelId: ID! |
| 35 | + text: String! |
| 36 | +} |
| 37 | +
|
| 38 | +type Subscription { |
| 39 | + messageAdded (channelId: ID!): Message! |
| 40 | + messageUpdated (channelId: ID!): Message! |
| 41 | +} |
| 42 | +` |
| 43 | + |
| 44 | +const pubsub = new PubSub() |
| 45 | + |
| 46 | +const channels = [ |
| 47 | + { |
| 48 | + id: 'general', |
| 49 | + label: 'General', |
| 50 | + messages: [], |
| 51 | + }, |
| 52 | + { |
| 53 | + id: 'random', |
| 54 | + label: 'Random', |
| 55 | + messages: [], |
| 56 | + }, |
| 57 | +] |
| 58 | + |
| 59 | +const resolvers = { |
| 60 | + Query: { |
| 61 | + channels: () => channels, |
| 62 | + channel: (root, { id }) => channels.find(c => c.id === id), |
| 63 | + }, |
| 64 | + |
| 65 | + Mutation: { |
| 66 | + addMessage: (root, { input }) => { |
| 67 | + const channel = channels.find(c => c.id === input.channelId) |
| 68 | + if (!channel) { |
| 69 | + throw new ApolloError(`Channel ${input.channelId} not found`, 'not-found') |
| 70 | + } |
| 71 | + const message = { |
| 72 | + id: shortid(), |
| 73 | + channel: channel, |
| 74 | + text: input.text, |
| 75 | + } |
| 76 | + channel.messages.push(message) |
| 77 | + pubsub.publish('messageAdded', { messageAdded: message }) |
| 78 | + return message |
| 79 | + }, |
| 80 | + |
| 81 | + updateMessage: (root, { input }) => { |
| 82 | + const channel = channels.find(c => c.id === input.channelId) |
| 83 | + if (!channel) { |
| 84 | + throw new ApolloError(`Channel ${input.channelId} not found`, 'not-found') |
| 85 | + } |
| 86 | + const message = channel.messages.find(m => m.id === input.id) |
| 87 | + Object.assign(message, { |
| 88 | + text: input.text, |
| 89 | + }) |
| 90 | + pubsub.publish('messageUpdated', { messageUpdated: message }) |
| 91 | + return message |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + Subscription: { |
| 96 | + messageAdded: { |
| 97 | + subscribe: withFilter( |
| 98 | + () => pubsub.asyncIterator('messageAdded'), |
| 99 | + (payload, variables) => payload.messageAdded.channel.id === variables.channelId, |
| 100 | + ), |
| 101 | + }, |
| 102 | + |
| 103 | + messageUpdated: { |
| 104 | + subscribe: withFilter( |
| 105 | + () => pubsub.asyncIterator('messageUpdated'), |
| 106 | + (payload, variables) => payload.messageUpdated.channel.id === variables.channelId, |
| 107 | + ), |
| 108 | + }, |
| 109 | + }, |
| 110 | +} |
| 111 | + |
| 112 | +const server = new ApolloServer({ |
| 113 | + typeDefs, |
| 114 | + resolvers, |
| 115 | +}) |
| 116 | + |
| 117 | +server.listen({ |
| 118 | + port: 4042, |
| 119 | +}).then(({ url }) => { |
| 120 | + console.log(`🚀 Server ready at ${url}`); |
| 121 | +}) |
0 commit comments