Using GraphQL to join data across Google Places API and Postgres to build location based apps
TL;DR
Use Hasura Remote Joins to join geography data from PostGIS extension in your Postgres database with Google Places API in GraphQL. Boilerplate -> Google Places
Hasura Remote Joins with Google Places API and PostGIS
This post is a part of our Remote Joins. Remote Joins in Hasura allows you to join data across tables and remote data sources. Data Federation using Hasura Remote Joins is now available in our latest v1.3 release.
Try it out with Hasura Cloud and let us know what you think!
In this example, we will look at how data from Google Places API can be joined with existing geography data in Postgres with PostGIS extension using Hasura.
PostGIS
To store and compute on geo-location data, we are going to use PostGIS which is a spatial database extender for Postgres. It adds support for geographical objects on Postgres and also comes with utility functions such as distance, area, union, and intersection.
Enabling PostGIS
Before we begin, we need to enable the PostGIS extension on Postgres.
Head to Data tab in the API Console and click on SQL in nav bar on the left. And execute the following SQL commands:
-- User location data
CREATE TABLE user_location (
user_id INTEGER PRIMARY KEY,
location GEOGRAPHY(Point)
);
Set the API key as GOOGLE_MAPS_API_KEY environment variable.
This custom resolver is used to accept a location argument of type "geography" of PostGIS and returns restaurants around a radius with name, rating and address details.
Refer to API documentation for integration with other information like Photos, Reviews etc.
Get the GraphQL API Endpoint from Glitch and add it as remote schema.
Now let's add the Remote Relationship called restaurants
Now the GraphQL query to fetch this data in a single API call would look like the following:
query {
user_location {
user_id
restaurants {
name
lat
long
address
rating
}
}
}
Notice that, the nested query restaurants come from Google Places API and it will apply the filter of user_location.location = restaurants.location, there by only giving restaurants data near by the user.