Sunrise API
StepZen's custom directive @sequence allows for multiple queries to be executed in a sequence, one after the other to return a single result. Each step in the sequence passes data to the next, allowing data from step 1 in a sequence to be used as arguments to step 2 in a sequence and so on.
This can allow you to create complex queries without having to manually orchestrate APIs, write lots of server-side logic, or handle asynchronous calls and database queries. In this example we will see how to use @sequence to query three APIs to get the sunrise based on the users's location (latitude and longitude) which we get using their IP address.
Our three APIs
We'll use three APIs to get the sunrise based on the location (latitude and longitude) which we get using the IP address:
This requires executing a sequence of steps:
- Get the
locationgiven an IP address. - Get
sunrisefrom thelatitudeandlongitudederived from thelocation.
SunriseSunset contains properties about the sunrise and sunset
type SunriseSunset {
sunrise: String!
sunset: String!
solar_noon: String!
day_length: String!
civil_twilight_begin: String!
civil_twilight_end: String!
nautical_twilight_begin: String!
nautical_twilight_end: String!
astronomical_twilight_begin: String!
astronomical_twilight_end: String!
sunrise_unix: Int
sunset_unix: Int
abbreviation: String
}Geolocation contains properties about the location (i.e. longitude and latitude)
type Geolocation {
ip_address: String!
city: String
city_geoname_id: Int
region: String
region_iso_code: String
region_geoname_id: Int
postal_code: String
country: String
country_code: String
country_geoname_id: Int
country_is_eu: Boolean
continent: String
continent_code: String
continent_geoname_id: Int
longitude: Float
latitude: Float
abbreviation: String
}Timezone contains properties about the timezone
type Timezone {
countryCode: String
countryName: String
zoneName: String!
abbreviation: String
gmtOffset: Int!
dst: Int
zoneStart: Int
zoneEnd: Int
nextAbbreviation: String
timestamp: Int
timestring: String
formatted: Date
}Queries
getLocationByIpAddress gets a location using IP Geolocation API based upon a provided IP address
type Query {
getLocationByIpAddress(
ip_address: String!
): Geolocation
@rest(
endpoint: "https://ipgeolocation.abstractapi.com/v1/?api_key=$api_key&ip_address=$ip_address"
configuration: "abstractapi_config"
)
}getTimezoneByLatLong gets the timezone from TimeZoneDB based upon a provided latitude and longitude
type Query {
getTimezoneByLatLong(
latitude: Float!,
longitude: Float!
): Timezone
@rest(
endpoint: "http://api.timezonedb.com/v2.1/get-time-zone?format=json&key=$api_key&by=position&lat=$latitude&lng=$longitude"
configuration: "timezone_config"
)
}getSunriseByLatLong gets the sunrise from the Sunrise Sunset API based upon a provided latitude and longitude
type Query {
getSunriseByLatLong(latitude: Float!, longitude: Float!): SunriseSunsetFoo
@rest(
endpoint: "https://api.sunrise-sunset.org/json?lat=$latitude&lng=$longitude"
resultroot: "results"
cel: """
function transformREST(json)
{ ... }
"""
)
}collectSunrise uses the echo connector to return the final result
type Query {
collectSunrise(
sunrise: String!
sunset: String!
solar_noon: String!
day_length: String!
civil_twilight_begin: String!
civil_twilight_end: String!
nautical_twilight_begin: String!
nautical_twilight_end: String!
astronomical_twilight_begin: String!
astronomical_twilight_end: String!
sunrise_unix: Int
sunrise_local: String
sunset_unix: Int
sunset_local: String
abbreviation: String
): SunriseSunset
@connector(type: "echo")
}getSunriseByIp sequence
getSunriseByIp(ip_address: String!): SunriseSunset
@sequence(
steps: [
{ query: "getLocationByIpAddress" }
{ query: "getTimezoneByLatLong" }
{ query: "getSunriseByLatLong" }
{ query: "collectSunrise" }
]
)Deploy the schema with stepzen start
Create a config.yaml file with your API keys included where it says xxxxx.
configurationset:
- configuration:
name: abstractapi_config
api_key: xxxx
- configuration:
name: timezone_config
api_key: xxxxDeploy the schema using the stepzen start command and issue a query in the StepZen Schema explorer. You can find your own IP address by Googling, "get my IP address."
query MyQuery {
getSunriseByIp(ip_address: "") {
abbreviation
sunrise
sunset
}
}getSunriseByIp(ip_address: String!): SunriseSunset
@sequence(
steps: [
{ query: "getLocationByIpAddress" }
{ query: "getTimezoneByLatLong" }
{ query: "getSunriseByLatLong" }
{ query: "collectSunrise" }
]
)Sign Up
You can sign up for StepZen at the following link.