-
Notifications
You must be signed in to change notification settings - Fork 5
/
Sighting.js
158 lines (143 loc) · 4.14 KB
/
Sighting.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Parse from 'parse/node';
import {
GraphQLID,
GraphQLObjectType,
GraphQLInt,
GraphQLFloat,
GraphQLNonNull,
GraphQLList,
} from 'graphql';
import request from 'request';
import Pokemon from './Pokemon';
import { pokemons } from '../fixtures/pokemons';
const Sighting = Parse.Object.extend('Sighting');
const getOrAttr = (item, key) => (
typeof item.get !== 'undefined' && item.get(key)
) || item[key];
const SightingType = new GraphQLObjectType({
name: 'Sighting',
description: 'A concise description of what Sighting is',
fields: () => ({
id: {
type: GraphQLID,
resolve: sighting => getOrAttr(sighting, 'id'),
},
latitude: {
type: GraphQLFloat,
resolve: sighting => getOrAttr(sighting, 'latitude'),
},
longitude: {
type: GraphQLFloat,
resolve: sighting => getOrAttr(sighting, 'longitude'),
},
pokemon_id: {
type: GraphQLInt,
resolve: sighting => getOrAttr(sighting, 'pokemon_id'),
},
pokemon: {
type: Pokemon.SchemaType,
resolve: sighting =>
new Pokemon(
pokemons.find(p => p.pokenumber === getOrAttr(sighting, 'pokemon_id'))
),
},
}),
});
Sighting.SchemaType = SightingType;
Sighting.RootQuery = {
type: new GraphQLList(Sighting.SchemaType),
args: {
center_latitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
center_longitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
northeast_latitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
northeast_longitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
southwest_latitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
southwest_longitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
},
resolve: (_, args) => {
const defaultArgs = {
live: false,
minimal: true,
};
// pokemon_id=
// center_latitude=38.71713783905443
// center_longitude=-9.13937951534421
// northeast_latitude=38.81324035802483
// northeast_longitude=-9.037240996057108
// southwest_latitude=38.62090592679008
// southwest_longitude=-9.241518034631326
// const query = new Query(Sighting);
// return query.find();
const pokecrewData = new Parse.Promise();
request({
url: 'https://api.pokecrew.com/api/v1/seens',
qs: Object.assign({}, defaultArgs, args),
json: true,
}, (error, response, body) => {
if (!error && response.statusCode === 200) {
console.log('resolving with', body.seens);
pokecrewData.resolve(body.seens);
} else {
pokecrewData.reject(error);
}
});
const sightingQuery = new Parse.Query(Sighting);
sightingQuery.lessThanOrEqualTo('latitude', args.northeast_latitude);
sightingQuery.greaterThan('latitude', args.southwest_latitude);
sightingQuery.lessThanOrEqualTo('longitude', args.northeast_longitude);
sightingQuery.greaterThan('longitude', args.southwest_longitude);
return Parse.Promise.when(pokecrewData, sightingQuery.find()).then((pcData, internalData) =>
[...pcData, ...internalData.map(d => Object.assign({}, d.toJSON(), { id: d.id }))]
);
},
};
Sighting.Mutations = {
addSighting: {
type: Sighting.SchemaType,
description: 'Create a new instance of Sighting',
args: {
latitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
longitude: {
type: new GraphQLNonNull(GraphQLFloat),
},
pokemon_id: {
type: new GraphQLNonNull(GraphQLInt),
},
},
resolve: (_, { latitude, longitude, pokemon_id }) => {
const sighting = new Sighting({
latitude, longitude, pokemon_id,
});
return sighting.save().then(s => s);
},
},
// deleteSighting: {
// type: Sighting.SchemaType,
// description: 'Delete an instance of Sighting',
// args: {
// id: { type: new GraphQLNonNull(GraphQLID) },
// },
// resolve: (_, { id }, { Query }) =>
// new Query(Sighting).get(id).then((sighting) => {
// if (sighting) {
// return sighting.destroy();
// }
// return sighting;
// }),
// },
};
export default Sighting;