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