DEV Community

Hasura for Hasura

Posted on • Originally published at blog.hasura.io on

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 (available in preview) series. Remote Joins in Hasura allows you to join data across tables and remote data sources. You can sign up here if you'd like to be notified when we launch. Head to the PR to check out more details, read the preview docs, and to try out a test Hasura image that you can spin up. Jump on our discord or comment on github 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)
);

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.

Top comments (0)