Finding the nearest locations around you using AWS Amplify — Part 1

Before you continue

Step 1: change your GraphQL Schema to include location coordinates

LondonCycles app showing live status for nearby Santander Cycles bike stations.

A GraphQL Schema allows you to clearly specify your application data types, its relationships, how to query data, make changes and notify subscribers. Learn the basics of GraphQL at graphql.org.

(-0.109971, 51.529163)   // GPS/WGS84 (long, lat) 
[-0.109971, 51.529163] // GeoJSON [long, lat]
51°31'44''N, 0°6'35''W // DMS Degrees Minutes Seconds
gcpvjkxgjkpwmsp // Geohash
(-0.1099 , 51.5291) // Use 4 digits for 10m precision
(-0.10997 , 51.52916) // Use 5 digits for 1m precision
(-0.109971, 51.529163) // Use 6 digits for .1m precision

Note: remember to double check the order for longitude and latitude when using location coordinates in WGS84 or GeoJSON as is a common mistake.

type BikePoint @model {   
location: Location!
}
type Location {
lat: Float!
lon: Float!
}

Types using @model are connected to a table in Amazon DynamoDB. Learn more about how it works at @model GraphQL transform.

Learn the basics of GraphQL including Schema definition at graphql.org.

Step 2: enable distance-aware searches using @searchable

GraphQL mutations mapping while using @searchable GraphQL transform

DynamoDB Streams capture modifications in your Amazon DynamoDB table used by @model and store them as events in a log for up to 24 hours. A custom AWS Lambda function will access this log and process the data in near-real time mapping each event (insert, modify or remove) to a corresponding Amazon Elasticsearch operation.

////////////////////////////////////////////////////////////////////
// Step 1: run GraphQL mutation (simplified)
$input = {
"id": "BikePoints_1",
"name": "River Street , Clerkenwell",
"location": {
"lat": 51.529163,
"lon": -0.109971
},
}
mutation createBikePoint($input: CreateBikePointInput!) {
createBikePoint(input:$input) {
id name location { lat lon }
}
}
////////////////////////////////////////////////////////////////////
// Step 2: run AWS AppSync Resolver DynamoDB mapping (simplified)
{
"version": "2017-02-28",
"operation": "PutItem",
"id": "BikePoints_1",
"attributeValues": {
"__typename": "BikePoint",
"name": "River Street , Clerkenwell",
"location": {
"lat": 51.529163,
"lon": -0.109971
},
}
}
////////////////////////////////////////////////////////////////////// Step 3: process DynamoDB Stream Event Record (simplified){
"eventID": "...",
"eventName": "INSERT",
"eventSource": "aws:dynamodb",
"awsRegion": "us-west-1",
"dynamodb": {
"Keys": {
"id": { "S": "BikePoints_1" },
},
"NewImage": {
"name": { "S": "River Street , Clerkenwell" },
...
},
},
"eventSourceARN": "arn:aws:...table/BikePoint/stream/..."
}
////////////////////////////////////////////////////////////////////
// Step 4: run Elasticsearch document operation (simplified)
PUT /bikepoint/_mapping/doc
{
"properties": {
"id": "BikePoints_1"
"name": "River Street , Clerkenwell",
"location": {
"lat": 51.529163,
"lon": -0.109971
}
}
}
type BikePoint @model @searchable {   
location: Location
}
type Location {
lat: Float!
lon: Float!
}

Important: enabling searches in your application will incur in costs as shown in the table at the end of this article. As part of the Free Tier you are able to use searches for at least 30 days and up to 1GB data transfer with no cost.

amplify push

Important: DO NOT update records with location information just yet. Wait until completing Part 2 of this series.

query SearchBikePoints(...) 
{
searchBikePoints(...) {
items {
id name location { lat lon }
}
nextToken
total
}
}

You can test your default search query using amplify console api from the command line and selecting GraphQL.

Conclusion

Ready to code?

Free tier for a new AWS Account. Check out latest pricing.

Thanks for reading!

My Name is Gerard Sans. I am a Developer Advocate at AWS Mobile working with AWS Amplify and AWS AppSync teams.

GraphQL is an open-source data query and manipulation language for APIs.

Mapbox is an American provider of custom online maps.

Angular is an open source project from Google.

Santander Cycles is a public bicycle hire scheme in London.

Transport for London (TfL) is a local government body responsible for the transport system in Greater London, England.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store