jojorb / mapbox-datasets-remote-schema

Mapbox Datasets transformed into GraphQL to be added as a remote schema in Hasura

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mapbox datasets remote schema

Meet Hasura and Mapbox

We store user Point coord on our Postgres (gis enabled PostGis) and query custom aggregate poi stored inside our Mapbox service as a Tileset.

We first need to create a Mapbox datasets. In a second time the Mapbox Tilequery API allows you to retrieve data about specific features from a vector tileset, based on a given latitude and longitude.

How to create a simple pipeline to create a Mapbox Tileset from push on Github: mapbox-tileset-action

Hasura Postgis with remote schema

docker-compose up

Insert Sample Data

Go to SQL tab to add some very basic user id and coordinates into a new user_location table. Change users coordinates according to your mapbox datasets, we will query data informations in a 3000 meters radius in resolver.js.

-- User location data
CREATE TABLE user_location (
  user_id INTEGER PRIMARY KEY,
  location GEOGRAPHY(Point)
);

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": [2.5365543365478516, 48.77236950468487]
      }
    },
    {
      "user_id": 2,
      "location": {
        "type": "Point",
        "coordinates": [2.5059127807617183, 48.7550429007606]
      }
    }
  ]
}

Deploy with Glitch

  1. Click the following button to edit on glitch

    glitch-deploy-button

  2. Rename .env-sample into .env and add the following environment variables.

MAPBOX_API_KEY='YOUR-MAPBOX-TOKEN'
TILESET_ID='YOUR-TILE-ID'
  1. Open the app on glitch or local Apollo server to test qickly

Query

query($location: geography!) {
  places(location: $location) {
    name
    address
    lat
    lng
    distance
    rating
  }
}

Test some coordinates in the variable to query mapbox datasets

{
  "location": {
    "type": "Point",
    "coordinates": [2.5365543365478516, 48.77236950468487]
  }
}

in Hasura console add remote schema

In Remote Shema just add a new remot shema and enter your glitch app url in GraphQL server URL

in Hasura console add the Remote Relationship called

Now in DATA > Relationships > Remote Relationships click on add remote relationships and select your dataset and tables to work with

query user info with the remote schema

query {
  user_location {
    user_id
     shops {
      name
      address
      lat
      lng
      rating
      distance
    }
  }
}

About

Mapbox Datasets transformed into GraphQL to be added as a remote schema in Hasura


Languages

Language:JavaScript 100.0%