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 

Remote Joins (available in preview) in Hasura allows you to join data across tables and remote data sources.

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)
);

Insert Sample Data

GraphQL Mutation

mutation insertUserLocation($user_location_objects: [user_location_insert_input!]!) {
    insert_user_location(objects: $user_location_objects) {
        returning{
            location
            user_id
        }
    }
}

Variable would be the following:

{
 "user_location_objects": [
   {
     "user_id": 1,
     "location": {
       "type": "Point",
       "coordinates": [12.9406589, 77.6185572]
     }
   },
   {
     "user_id": 2,
     "location": {
       "type": "Point",
       "coordinates": [12.939553, 77.6183303]
     }
   }
 ]
}

Now we want to fetch the top restaurants nearby the coordinates of the user location. Google Places API is available as a REST API to get this data.

Adding Google Places API as Remote Schema

To be able to query Google Places data via Hasura, it needs to be added as a Remote Schema using the Hasura Console.

Deploy Custom Resolver

Add the following environment variables in the .env file on glitch.

GOOGLE_MAPS_API_KEY=xxx 
PORT=3000
  • Get the Google Maps API Key by visting the Google Maps Platform page.
  • 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.


Hasura is an open-source engine that gives you realtime GraphQL APIs on new or existing Postgres databases, with built-in support for stitching custom GraphQL APIs and triggering webhooks on database changes.