Coder Social home page Coder Social logo

hasura-graphql-on-gcloud's Introduction

hasura-graphql-on-gcloud

Hasura GraphQL Engine on Google Cloud

Pre-requisites

  1. Google cloud account with billing enabled
  2. gcloud CLI

1. Create a Google Cloud Project

2. Create a Google Cloud SQL Postgres instance

Create a PostgreSQL instance:

gcloud sql instances create hge-pg --database-version=POSTGRES_9_6 \
       --cpu=1 --memory=3840MiB --region=asia-south1

Set a password for postgres user:

gcloud sql users set-password postgres no-host --instance=hge-pg \
       --password=[PASSWORD]

Make a note of the [PASSWORD].

3. Create a Kubernetes cluster

gcloud container clusters create hge-k8s \
      --zone asia-south1-a \
      --num-nodes 1

4. Configure and deploy GraphQL Engine

Create a service account and download the json file by following this guide.

Or if you didn't read that guide, at least ensure you:

  1. Enable Cloud SQL Admin API for your project
  2. Create a new Services Account
  3. Select Cloud SQL Admin as the role
  4. Click Create Key to download the json file

Create a k8s secret with this service account:

kubectl create secret generic cloudsql-instance-credentials \
    --from-file=credentials.json=[PROXY_KEY_FILE_PATH]

Replace [PROXY_KEY_FILE_PATH] with the filename of the download json.

Create another secret with the database user and password (Use the [PASSWORD] noted earlier):

kubectl create secret generic cloudsql-db-credentials \
    --from-literal=username=postgres --from-literal=password=[PASSWORD]

Get the INSTANCE_CONNECTION_NAME:

gcloud sql instances describe hge-pg
# it'll be something like this:
# connectionName: myproject1:us-central1:myinstance1

Edit deployment.yaml and replace INSTANCE_CONNECTION_NAME with the value.

Create the deployment:

kubectl create -f deployment.yaml

Ensure the pod is running:

kubectl get pods

Check logs if there are errors.

Expose GraphQL Engine on a Google LoadBalancer:

kubectl expose deploy/hasura-graphql-engine \
        --port 80 --target-port 8080 \
        --type LoadBalancer

Get the external IP:

kubectl get service

Open the console by navigating the the external IP in a browser.

Note down this IP as HGE_IP.

5. Create table

Create the table using the console:

Table name: profile

Columns:

id: Integer auto-increment
name: Text
address: Text
lat: Numeric, Nullable
lng: Numeric, Nullable

6. Create a Google Cloud Function

We are going to use Google Maps API in our cloud function.

Enable Google Maps API and get an API key (we'll call it GMAPS_API_KEY) following this guide

Check the Places box to get access to Geocoding API.

We'll follow this guide and create a Cloud Function with NodeJS 8.

gcloud components update &&
gcloud components install beta

Goto the cloudfunction directory:

cd cloudfunction

Edit .env.yaml and add values for the following as shown:

# .env.yaml
GMAPS_API_KEY: '[GMAPS_API_KEY]'
HASURA_GRAPHQL_ENGINE_URL: 'http://[HGE_IP]/v1alpha1/graphql'
gcloud beta functions deploy trigger \
       --runtime nodejs8 \
       --trigger-http \
       --region asia-south1 \
       --env-vars-file .env.yaml

Get the trigger URL:

httpsTrigger:
  url: https://asia-south1-hasura-test.cloudfunctions.net/trigger

Goto HGE_IP on browser, Events -> Add Trigger and create a new trigger:

Trigger name: profile_change
Schema/Table: public/profile
Operations: Insert
Webhook URL: [Trigger URL]

Once the trigger is created, goto Data -> profile -> Insert row and add a new profile with name and address, save. Goto Browse rows tabs to see lat and lng updated, by the cloud function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.