/
schema.js
124 lines (105 loc) · 2.57 KB
/
schema.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import _ from 'lodash';
import { makeExecutableSchema } from 'graphql-tools';
import debugCreator from 'debug';
import { authQuery, authMutation, authSchema, authResolver } from './auth-schema';
import { feedQuery, feedMutation, feedSchema, feedResolver } from './feed-schema';
import { showQuery, showMutation, showSchema, showResolver } from './show-schema';
import { subscriptionSchema, subscriptionResolver } from './subscriptions';
const debug = debugCreator('graphql/schema');
export const rootSchema = `
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Query {
chatMessages(showId: String!): [ChatMessage]!
chatUsers(showId: String!): [ChatUser]
${showQuery}
${authQuery}
${feedQuery}
}
type Mutation {
createChatMessage(
showId: String!
message: String!
): ChatMessage!
${authMutation}
${feedMutation}
${showMutation}
}
type ChatMessage {
id: ID!
user: User
show: Show
message: String!
timestamp: Float!
}
type ChatUser {
id: String!
user: User
show: Show
timestamp: Float!
}
type UpdatedChatUser {
chatUser: ChatUser!
added: Boolean!
}
`;
export const rootResolver = {
Query: {
chatMessages(root, { showId }, { Models }) {
const { ChatMessage } = Models;
debug('query chatMessages');
return ChatMessage
.find({ show: showId })
.populate('user show')
.limit(1000)
.sort({ timestamp: 1 });
},
chatUsers(root, { showId }, { Models }) {
const {
ChatUser,
} = Models;
debug('query chatUsers');
return ChatUser
.find({ show: showId })
.populate('user show')
.limit(1000)
.sort({ timestamp: 1 });
},
},
Mutation: {
async createChatMessage(root, args, { credentials, Models, pubsub }) {
if (credentials == null || credentials.id == null) {
throw new Error('Not logged in.');
}
const { ChatMessage } = Models;
debug('createChatMessage')
let chatMessage = await ChatMessage.create(_.extend({}, args, {
user: credentials.id,
show: args.showId,
}));
debug('createChatMessage publish');
chatMessage = await chatMessage.populate('user show').execPopulate();
pubsub.publish('newChatMessage', chatMessage);
return chatMessage;
},
},
};
export default makeExecutableSchema({
typeDefs: [
rootSchema,
subscriptionSchema,
authSchema,
feedSchema,
showSchema,
],
resolvers: _.merge(
rootResolver,
subscriptionResolver,
authResolver,
feedResolver,
showResolver
),
});