UNPKG

7.34 kBJavaScriptView Raw
1import {
2 GraphQLID,
3 GraphQLList,
4 GraphQLNonNull,
5 GraphQLObjectType,
6 GraphQLSchema,
7 GraphQLString,
8} from 'graphql';
9import {
10 fromGlobalId,
11 nodeDefinitions,
12} from 'graphql-relay';
13
14import { getUserInputType, getUserType } from './Types/User';
15import {
16 getGeolocationType,
17 getGeolocationInputType,
18 getGeolocationsType,
19 getGeolocationsDistanceType,
20} from './Types/Geolocations';
21import {
22 getActivityType,
23 getActivityInputType,
24} from './Types/Activity';
25import {
26 getScoreType,
27 getScoreInputType,
28} from './Types/Score';
29import pubsub from './Services/PubSub';
30
31const {
32 nodeField,
33 nodeInterface,
34} = nodeDefinitions(
35 (globalId, { loaders }) => {
36 const { id, type } = fromGlobalId(globalId);
37 if (type === 'User') {
38 return loaders.user.load(id);
39 }
40 return null;
41 },
42 (obj) => {
43 if (obj.hasOwnProperty('email')) {
44 return UserType;
45 }
46 return null;
47 }
48);
49
50const UserType = getUserType(nodeInterface);
51const UserInputType = getUserInputType();
52
53const ScoreType = getScoreType(nodeInterface);
54const ScoreInputType = getScoreInputType();
55
56const ActivityType = getActivityType('Activities');
57const ActivityInputType = getActivityInputType();
58
59const GeolocationType = getGeolocationType(nodeInterface);
60const GeolocationInputType = getGeolocationInputType();
61
62const GeolocationsType = getGeolocationsType(nodeInterface);
63
64const GeolocationDistanceType = getGeolocationsDistanceType(nodeInterface);
65
66const QueryType = new GraphQLObjectType({
67 name: 'Query',
68 description: 'Query root',
69 fields: () => ({
70 users: {
71 type: new GraphQLList(UserType),
72 description: 'get all users',
73 resolve: (root, args, { loaders }) => loaders.user.loadAll(),
74 },
75 node: nodeField,
76 user: {
77 type: UserType,
78 description: 'get an user',
79 args: {
80 id: { type: new GraphQLNonNull(GraphQLID) },
81 },
82 resolve: (root, args, { loaders }) => loaders.user.load(args.id),
83 },
84 activities: {
85 type: new GraphQLList(ActivityType),
86 description: 'get all activity',
87 resolve: (root, args, { loaders }) => loaders.activity.loadAll(),
88 },
89 activity: {
90 type: ActivityType,
91 description: 'get an activity',
92 args: {
93 id: { type: new GraphQLNonNull(GraphQLID) },
94 },
95 resolve: (root, args, { loaders }) => loaders.activity.load(args.id),
96 },
97 scores: {
98 type: new GraphQLList(ScoreType),
99 description: 'get all scores',
100 resolve: (root, args, { loaders }) => loaders.score.loadAll(),
101 },
102 score: {
103 type: ScoreType,
104 description: 'get a score',
105 args: {
106 id: { type: new GraphQLNonNull(GraphQLID) },
107 },
108 resolve: (root, args, { loaders }) => loaders.score.load(args.id),
109 },
110 geolocation: {
111 type: GeolocationType,
112 description: 'get user geolocation',
113 args: {
114 id: { type: new GraphQLNonNull(GraphQLString) },
115 key: { type: new GraphQLNonNull(GraphQLString) },
116 },
117 resolve: (root, args, { loaders }) => loaders.geolocation.load(args),
118 },
119 geolocations: {
120 type: new GraphQLList(GeolocationsType),
121 description: 'get user geolocation',
122 args: {
123 id: { type: new GraphQLNonNull(GraphQLString) },
124 key: { type: new GraphQLNonNull(GraphQLString) },
125 distance: { type: new GraphQLNonNull(GraphQLString) },
126 unit: { type: new GraphQLNonNull(GraphQLString) },
127 },
128 resolve: (root, args, { loaders }) => loaders.geolocation.loadCloseUsers(args),
129 },
130 geolocationDistance: {
131 type: GeolocationDistanceType,
132 description: 'get user geolocation',
133 args: {
134 id: { type: new GraphQLNonNull(GraphQLString) },
135 key: { type: new GraphQLNonNull(GraphQLString) },
136 foreignKey: { type: new GraphQLNonNull(GraphQLString) },
137 },
138 resolve: (root, args, { loaders }) => loaders.geolocation.loadDistanceFromUser(args),
139 },
140 userClearCache: {
141 type: GraphQLString,
142 description: 'clear user cache',
143 resolve: (root, args, { loaders }) => loaders.user.clearAll(),
144 },
145 geolocationClearCache: {
146 type: GraphQLString,
147 description: 'clear geolocation cache',
148 resolve: (root, args, { loaders }) => loaders.geolocation.clearAll(),
149 },
150 activityClearCache: {
151 type: GraphQLString,
152 description: 'clear activity cache',
153 resolve: (root, args, { loaders }) => loaders.activity.clearAll(),
154 },
155 scoresClearCache: {
156 type: GraphQLString,
157 description: 'clear score cache',
158 resolve: (root, args, { loaders }) => loaders.score.clearAll(),
159 },
160 }),
161});
162
163const MutationType = new GraphQLObjectType({
164 name: 'Mutation',
165 description: 'Mutation query root',
166 fields: () => ({
167 addUser: {
168 type: UserType,
169 description: 'add an user',
170 args: {
171 user: {
172 type: UserInputType,
173 },
174 },
175 resolve: (root, { user }, { loaders }) => loaders.user.addUser(user),
176 },
177 updateUser: {
178 type: UserType,
179 description: 'update an user',
180 args: {
181 user: {
182 type: UserInputType,
183 },
184 },
185 resolve: (root, { user }, { loaders }) => loaders.user.updateUser(user),
186 },
187 updateGeolocation: {
188 type: new GraphQLList(GeolocationsType),
189 description: 'update an geoloaction',
190 args: {
191 geolocation: {
192 type: GeolocationInputType,
193 },
194 },
195 resolve: async (root, { geolocation }, { loaders }) => {
196 const matchs = await loaders.geolocation.updateGeolocation(geolocation);
197 pubsub.publish(`MATCH_${geolocation.key}`, matchs);
198 return matchs;
199 },
200 },
201 addActivity: {
202 type: ActivityType,
203 description: 'add an activity',
204 args: {
205 activity: {
206 type: ActivityInputType,
207 },
208 },
209 resolve: (root, { activity }, { loaders }) => loaders.activity.addActivity(activity),
210 },
211 updateActivity: {
212 type: ActivityType,
213 description: 'update an activity',
214 args: {
215 activity: {
216 type: ActivityInputType,
217 },
218 },
219 resolve: (root, { activity }, { loaders }) => loaders.activity.updateActivity(activity),
220 },
221 addScore: {
222 type: ScoreType,
223 description: 'add a score',
224 args: {
225 score: {
226 type: ScoreInputType,
227 },
228 },
229 resolve: (root, { score }, { loaders }) => loaders.score.addScore(score),
230 },
231 updateScore: {
232 type: ScoreType,
233 description: 'update a score',
234 args: {
235 score: {
236 type: ScoreInputType,
237 },
238 },
239 resolve: (root, { score }, { loaders }) => loaders.score.updateScore(score),
240 },
241 }),
242});
243
244const SubscriptionType = new GraphQLObjectType({
245 name: 'Subscription',
246 description: 'Subs query root',
247 fields: () => ({
248 subMatch: {
249 type: new GraphQLList(GeolocationsType),
250 args: {
251 id: { type: new GraphQLNonNull(GraphQLID) },
252 },
253 resolve: (payload, args, context, info) => payload,
254 subscribe: (root, { id }, { loaders }) => pubsub.asyncIterator(`MATCH_${id}`),
255 },
256 }),
257});
258
259export default new GraphQLSchema({
260 query: QueryType,
261 mutation: MutationType,
262 subscription: SubscriptionType,
263});