/
index.js
87 lines (80 loc) · 1.98 KB
/
index.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
const { ApolloServer, gql } = require('apollo-server')
const { RESTDataSource } = require('apollo-datasource-rest')
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
type geoData {
language: String
ip: String
city: String
countryCode: String
countryName: String
continentCode: String
continentName: String
postCode: String
latitude: String
longitude: String
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each.
type Query {
# postIP: [geoData]
postIP(ip: String!, lang: String!):[geoData]
}
schema {
query: Query
}
`
const resolvers = {
Query: {
postIP: (parent, {ip, lang}, context) => {
// https://ipstack.com/dashboard
apiKey = '8d79df654365e2f23c619410d981eba6'
return context.dataSources.apiFunc.getIP(ip, apiKey, lang)
},
},
}
class apiFunc extends RESTDataSource {
constructor() {
super()
}
async getIP(ip, apiKey, lang) {
return this.get(`http://api.ipstack.com/${ip}?access_key=${apiKey}&language=${lang}`)
.then(val => {
console.log(val)
console.log(lang)
return [{
language: lang,
ip: val.ip,
countryCode: val.country_code,
countryName: val.country_name,
continentCode: val.continent_code,
continentName: val.continent_name,
city: val.city,
postCode: val.zip,
latitude: val.latitude,
longitude: val.longitude
}]
})
}
}
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => {
return {
apiFunc: new apiFunc(),
}
},
context: () => {
return {
// token: '###',
}
},
})
server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
console.log(`Server ready at ${url}`);
});