Distributed GraphQL with Postgraphile and Supabase
Your apps will run faster if the APIs they call are physically located close to your end users. This tutorial will use Section to deploy the open-source Postgraphile container to multiple datacenters. We will configure it to use Supabase as the Postgres database backend.
The Postgraphile container we will use is available on DockerHub.
- You need an account on Supabase.
Create a Database on Supabase
Create a new Postgres instance for Postgraphile to connect to. For this tutorial we will be using Supabase as they provide free, managed Postgres instances. But any Postgres database will work.
- Visit https://app.supabase.com/ and click "New project".
- Select a name, password, and region for your database. Make sure to save the password, as you will need it later.
- Click "Create new project". Creating the project can take a while, so be patient.
- Once the project is created, navigate to the "Database" tab on the left.
You have just created an empty database on Supabase. Postgraphile will populate it with metadata upon first connection.
Get Your Connection String
Your connection string will be of the form
postgresql://postgres:[YOUR-PASSWORD]@[YOUR-SUPABASE-ENDPOINT]:5432/postgres. An example (with mock credentials) would look like,
- Go to Settings, and then Database.
- Scroll down to the "Connection string" section and copy the connection string from the "URI" tab. (Do not use the connection string in "Connection pooling", as Postgraphile is doing connection pooling of its own.)
- Insert the password you saved earlier into the string at
Create a Deployment for Postgraphile
Next, create the deployment for Postgraphile as
postgraphile-deployment.yaml. This will direct Section to run the Postgraphile open source container. Substitute
- image: graphile/postgraphile:latest
- name: DATABASE_URL
Apply this deployment resource to your Project with either the Kubernetes dashboard or
kubectl apply -f postgraphile-deployment.yaml.
Expose the Postgraphile Console on the Internet
We want to expose the Postgraphile console on the Internet. Create
ingress-upstream.yaml as defined below. Note that it directs port 80 traffic to port 5000 on the Postgraphile container.
- name: 80-80
Apply this service resource to your Project with either the Kubernetes dashboard or
kubectl apply -f ingress-upstream.yaml.
See the pods running on Section's network using
kubectl get pods -o wide.
-o wide switch shows where your GraphQL API is running according to the default AEE location optimization strategy. Your GraphQL API will be optimally deployed according to traffic.
Experiment with Postgraphile
Now, you can start using the GraphiQL interface of Postgraphile by visiting the
YOUR.DOMAIN.COM according to your DNS and HTTPS configuration.