-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.graphql
60 lines (57 loc) · 2.29 KB
/
api.graphql
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
# The second query in a sequence that has arguments arg1, arg2, arg3 gets data for these:
# (i) the arguments explicitly passed to it from the enclosing query (see arguments.graphql)
# (ii) output of the first query as a type, and all the fields of that type
# Here we give example of (ii)
# However, what that means is that you have to define types for intermediate steps.
# Keeping your type system simple means using JSON as the return type of all but the last step.
type Weather {
temp: Float
}
type Query {
# Queries `weatherByJSON`,`convertCityOrLocaleToLatLon`, and `weatherByCityOrLocale` use an ecmascript return,
# which is included in the `@rest` directive.
# See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`.
# We are picturing a real-life scenario in which you call a weather API using the `endpoint` argument.
# See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`.
weatherByJSON(input: JSON): Weather
@rest(
endpoint: "stepzen:empty"
ecmascript: """
function transformREST(s) {
var input = get('input')
var lat = input['lat']
var lon = input['lon']
var temp
if (lat=='25.76' && lon=='-80.19')
temp = 100.0
else
temp = 60.4
return (JSON.stringify({"temp":temp}))
}
"""
)
# An ecmascript converter from `cityOrLocale` to its lat/lon.
# Here, since this will be called first, we declare it to return JSON.
convertCityOrLocaleToLatLon(cityOrLocale: String): JSON
@rest(
endpoint: "stepzen:empty"
ecmascript: """
function transformREST(s) {
var city = get('city')
if (city == 'Miami')
return (JSON.stringify({"lat":"25.76","lon":"-80.19"}))
else
return (JSON.stringify({"lat":"37.2","lon":"-121.19"}))
}
"""
)
# A `@sequence` directive that sequences `converCityOrLocaleToLatLong` and weather.
# Now, a cityOrLocale argument can be used to fetch weather data by lat/lon.
weatherByCityOrLocale(cityOrLocale: String): Weather
@sequence(
steps: [
{ query: "convertCityOrLocaleToLatLon" }
{ query: "weatherByJSON" }
]
)
}