Using GraphQL to join data across Google Places API and Postgres to build location based apps
Tags: graphql, postgis, webdev, remotejoins
Published: 2019-12-23 07:19:12

---
title: Using GraphQL to join data across Google Places API and Postgres to build location based apps
published: true
date: 2019-07-12 15:34:50 UTC
tags: graphql, postgis, webdev, remote joins
canonical_url: https://blog.hasura.io/graphql-join-data-google-places-api-postgres-postgis-location/
---

## 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](https://github.com/praveenweb/google-places-remote-schema)

![](https://blog.hasura.io/content/images/2019/07/google-places-remote-schema-banner.png)<figcaption>Hasura Remote Joins with Google Places API and PostGIS </figcaption>

This post is a part of our [Remote Joins](https://blog.hasura.io/remote-joins-a-graphql-api-to-join-database-and-other-data-sources/) (available in preview) series. Remote Joins in Hasura allows you to join data across tables and remote data sources. You can sign up [here](https://hasura.io/remote-joins) if you'd like to be notified when we launch. Head to [the PR](https://github.com/hasura/graphql-engine/pull/2395) 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](https://discord.gg/hasura) or comment on [github](https://github.com/hasura/graphql-engine) 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:

{% gist https://gist.github.com/dsandip/a153dbd3907a174c9949c77a2f31e290.js %}

```
-- 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
[![](https://raw.githubusercontent.com/hasura/graphql-engine/master/community/boilerplates/auth-webhooks/nodejs-express/assets/deploy-glitch.png)](http://glitch.com/edit/#!/import/github/praveenweb/google-places-remote-schema)

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](https://developers.google.com/places/web-service/get-api-key) 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.

![](https://blog.hasura.io/content/images/2019/07/remote-schema-google-places.png)

Now let's add the Remote Relationship called `restaurants`

![](https://blog.hasura.io/content/images/2019/07/remote-joins.png)

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.