• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language PLpgSQL
  • Created over 2 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A HackerNews-like clone built with Supabase and pg_graphql

Supabase GraphQL Example

A basic HackerNews-like clone where posts can be submitted with url links and then up and down voted.

graphql-hn

Showcase

Backend

  • CRUD (Query + Mutation Operations)
  • Cursor Based Pagination
  • Authorization / Postgres Row Level Security
  • Supabase - Create a backend in less than 2 minutes. Start your project with a Postgres Database, Authentication, instant APIs, Realtime subscriptions and Storage.
  • pg_graphql - A native PostgreSQL extension adding GraphQL support. The extension keeps schema generation, query parsing, and resolvers all neatly contained on your database server requiring no external services.
  • Postgres Triggers and Postgres Functions - When votes are in, use triggers to invoke a Postgres function that calculates a post score to rank the feed
  • Postgres Enumerated Types - Enums help defined the direction of a vote: UP or DOWN.

Frontend

  • Next.js - React Framework
  • TypeScript - TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.
  • graphql-code-generator - Generate code from your GraphQL schema and operations with a simple CLI
  • gql-tag-operations-preset - This code gen preset generates typings for your inline gql function usages, without having to manually specify import statements for the documents
  • urql - A highly customizable and versatile GraphQL client
  • Gravatar - Default avatar profile images from Gravatar

Functionality

  • Registration
  • Get a ranked feed of posts
  • Create Post
  • Delete Post
  • Create Comment
  • Delete Comment
  • Upvote/Downvote Post
  • View Profile (Account)
  • View Profile (Public)
  • Pagination (Posts, Comments)

QuickStart

Setup env vars

Install dependencies, GraphQL codegen, run app

yarn
yarn codegen
yarn workspace app dev

Deploy to Vercel

Provide the following settings to deploy a production build to Vercel:

  • BUILD COMMAND: yarn codegen && yarn workspace app build
  • OUTPUT DIRECTORY: ./app/.next
  • INSTALL COMMAND: yarn
  • DEVELOPMENT COMMAND: yarn codegen && yarn workspace app dev --port $PORT

Development

  1. Fetch latest GraphQL Schema
yarn codegen:fetch
  1. Generate Types and Watch for Changes
yarn codegen:watch
  1. Run server
yarn workspace app dev

Synchronize the GraphQL schema

Note: You need to call select graphql.rebuild_schema() manually to synchronize the GraphQL schema with the SQL schema after altering the SQL schema.

Manage Schema with dbmate

  1. brew install dbmate
  2. Setup .env with DATABASE_URL
  3. Dump Schema
cd data
dbmate dump

Note: If pgdump fails due to row locks, a workaround is to grant the postgres role superuser permissions with ALTER USER postgres WITH SUPERUSER. After dumping the schema, you should reset the permissions using ALTER USER postgres WITH NOSUPERUSER. You can run these statements in the Superbase Dashboard SQL Editors.

Schema (Public)

  • Profile belongs to auth.users

  • Post

  • Comment belongs to Post and Profile

  • Vote belongs to Post (can have a direction of UP/DOWN)

  • direction enum is "UP" or "DOWN"

Constraints

  • Post url is unique
  • Vote is unique per Profile, Post (ie, you cannot vote more than once -- up or down)

See: ./data/db/schema.sql

Note: The schema includes the entire Supabase schema with auth, storage, functions, etc.

Seed Data

A data file for all Supabase Blog posts from the RSS feed can be found in ./data/seed/blog_posts.csv and can be loaded. Another file for comments is available as well.

Note: Assumes a known profileId currently.

GraphQL Schema

See: ./graphql/schema/schema.graphql

Example Query

See: ./graphql/queries/

Use: https://mvrfvzcivgabojxddwtk.supabase.co/graphql/v1

Note: Needs headers


Content-Type: application/json
apiKey: <supabase_anon_key>

GraphiQL

GraphiQL is an in-browser IDE for writing, validating, and testing GraphQL queries.

Visit http://localhost:3000/api/graphiql for the Yoga GraphiQL Playground where you can experiment with queries and mutations.

Note: Needs headers


Content-Type: application/json
apiKey: <supabase_anon_key>

Note: In order for the RLS policies authenticate you, you have to pass an authorization header (see example):

authorization: Bearer <access_token>

Ranked Feed

query {
  rankedFeed: postCollection(orderBy: [{ voteRank: AscNullsFirst }]) {
    edges {
      post: node {
        id
        title
        url
        upVoteTotal
        downVoteTotal
        voteTotal
        voteDelta
        score
        voteRank
        comments: commentCollection {
          edges {
            node {
              id
              message
              profile {
                id
                username
                avatarUrl
              }
            }
          }
          commentCount: totalCount
        }
      }
    }
  }
}

Row Level Security Matrix (RLS)

You can query all policies via: select * from pg_policies.

See: Row Level Security Matrix (RLS)

Read More

Troubleshooting

  1. dbmate can create schema_migrations tables in schemas. To make sure they are not included in your GraphQL Schema:
revoke select on table public.schema_migrations from anon, authenticated;
  1. To enable inflection
comment on schema public is e'@graphql({"inflect_names": true})';
  1. Try the heartbeat to see if pg_graphql can access requests
select graphql_public.graphql(
	null,
	$$ { heartbeat }$$
)

Returns:

{ "data": { "heartbeat": "2022-07-28T17:07:07.90513" } }
  1. Is the public_graphql schema not exposed properly?

Getting an 406 status or error message like:

{
    "message": "The schema must be one of the following: public, storage"
}

Then be sure to expose the graphql_public in Settings > Project settings > API.

The schema to expose in your API. Tables, views and stored procedures in this schema will get API endpoints.

image

More Repositories

1

postgres_lsp

A Language Server for Postgres
Rust
3,188
star
2

postgres-new

In-browser Postgres sandbox with AI assistance
TypeScript
2,212
star
3

nextjs-openai-doc-search

Template for building your own custom ChatGPT style doc search powered by Next.js, OpenAI, and Supabase.
TypeScript
1,249
star
4

copycat

Generate deterministic fake values: The same input will always generate the same fake-output.
TypeScript
755
star
5

supabase-csharp

A C# Client library for Supabase
C#
491
star
6

supabase-on-aws

Self-hosted Supabase on AWS
TypeScript
382
star
7

supabase-kt

A Kotlin Multiplatform Client for Supabase.
Kotlin
364
star
8

sql-examples

Curated list of SQL to help you find useful script easily πŸš€
Vue
358
star
9

chatgpt-your-files

Production-ready MVP for securely chatting with your documents using pgvector
TypeScript
330
star
10

postgrest-rs

Rust client for PostgREST
Rust
328
star
11

supabase-custom-claims

How to implement custom claims with Supabase
JavaScript
254
star
12

snapshot

Capture a snapshot (or subset) of your Postgres database whilst transforming the data.
TypeScript
211
star
13

supabase-kubernetes

Helm 3 charts to deploy a Supabase on Kubernetes
Smarty
206
star
14

svelte-kanban

PLpgSQL
203
star
15

godot-engine.supabase

A lightweight addon which integrates Supabase APIs for Godot Engine out of the box.
GDScript
172
star
16

nuxt-supabase

A supa simple wrapper around Supabase.js to enable usage within Nuxt.
TypeScript
167
star
17

postgrest-go

Isomorphic Go client for PostgREST. (Now Updating)
Go
125
star
18

supabase-ui-svelte

Supabase authentication UI for Svelte
Svelte
120
star
19

postgrest-csharp

A C# Client library for Postgrest
C#
117
star
20

firebase-to-supabase

Firebase to Supabase Migration Guide
JavaScript
100
star
21

vue-supabase

A supa simple wrapper around Supabase.js to enable usage within Vue.
TypeScript
87
star
22

expo-stripe-payments-with-supabase-functions

Bring the Func(πŸ•Ί)
TypeScript
86
star
23

realtime-csharp

A C# client library for supabase/realtime.
C#
71
star
24

svelte-supabase

JavaScript
71
star
25

partner-gallery-example

Supabase Partner Gallery Example
TypeScript
67
star
26

supabase-sveltekit-example

Svelte
65
star
27

supabase-flutter-quickstart

Flutter implementation of the Quickstart Supabase User Management app.
C++
61
star
28

deno-fresh-openai-doc-search

Template for building your own custom ChatGPT style doc search powered by Fresh, Deno, OpenAI, and Supabase.
TypeScript
60
star
29

supabase-traefik

Python
57
star
30

flutter-stripe-payments-with-supabase-functions

Dart
55
star
31

supabase-terraform

HCL
53
star
32

postgrest-kt

Postgrest Kotlin Client
Kotlin
52
star
33

supabase-vscode-extension

Supabase Extension for VS Code and GitHub Copilot.
TypeScript
50
star
34

flutter-auth-ui

Supabase Auth UI library for Flutter
Dart
49
star
35

supabase-rb

An isomorphic Ruby client for Supabase.
Ruby
46
star
36

realtime-swift

A Swift client for Supabase Realtime server.
Swift
45
star
37

supabase-by-example

TypeScript
44
star
38

flutter-chat

Simple chat application built with Flutter and Supabase.
Dart
44
star
39

postgrest-swift

Swift client for PostgREST
Swift
43
star
40

gotrue-csharp

C# implementation of Supabase's GoTrue
C#
39
star
41

postgrest-rb

Isomorphic Ruby client for PostgREST.
Ruby
32
star
42

gotrue-swift

A Swift client library for GoTrue.
Swift
32
star
43

storage-go

Storage util client for Supabase in Go
Go
31
star
44

vec2pg

Migrate vector workloads to Postgres
Python
29
star
45

chatgpt-plugin-template-deno

Template for building ChatGPT Plugins in TypeScript that run on Supabase's custom Deno Edge Runtime.
TypeScript
27
star
46

storage-swift

Swift client library to interact with Supabase Storage
Swift
25
star
47

base64url-js

Pure TypeScript implementation of Base64-URL encoding for JavaScript strings.
TypeScript
22
star
48

gotrue-kt

Kotlin Client for GoTrue API
Kotlin
21
star
49

storage-csharp

A C# implementation of Supabase's Object Storage API
C#
19
star
50

sql-to-rest

SQL to PostgREST translator
TypeScript
19
star
51

postgrest-ex

Elixir Client library for PostgREST
Elixir
18
star
52

gotrue-ex

An Elixir client for the GoTrue authentication service
Elixir
16
star
53

heroku-to-supabase

Heroku to Supabase Migration Guide
16
star
54

sveltekit-subscription-payments

Clone, deploy, and fully customize a SaaS subscription application with SvelteKit.
TypeScript
13
star
55

nuxt3-quickstarter

CSS
13
star
56

gotrue-go

Typed Golang cilent for the Supabase fork of GoTrue
Go
12
star
57

supabase-php

PHP
11
star
58

pg_headerkit

A set of functions for adding special features to your application that uses PostgREST API calls to your PostgreSQL database.
PLpgSQL
11
star
59

functions-csharp

C# client for interacting with Supabase Functions
C#
11
star
60

auth-go

Go
10
star
61

gotrue-java

A Java client library for the GoTrue API.
Java
10
star
62

onesignal

Next.js app showcasing how you can implement push notification using OneSignal and Supabase
TypeScript
10
star
63

postgres-wasm-fdw

A WebAssembly foreign data wrapper example project
Rust
9
star
64

functions-swift

Swift Client library to interact with Supabase Functions.
Swift
8
star
65

supabase-adminpack

A Trusted Language Extension containing a variety of useful databse admin queries.
8
star
66

functions-go

Golang client library to interact with Supabase Functions.
Go
7
star
67

unboring.sg

A website and browser extensions to discover things to eat, do, and learn.
TypeScript
7
star
68

supabase-community-bot

TypeScript
5
star
69

ai-writer

An AI writing assistant powered by OpenAI, Supabase, and Next.js
TypeScript
5
star
70

storage-java

A Java client library for the Supabase Storage API
Java
4
star
71

supabase-management-js

Convenience wrapper for the Supabase Management API: https://supabase.com/docs/reference/api/introduction
TypeScript
4
star
72

supabase-go

Go
4
star
73

deno-supabase-js

Using supabase-js in Deno.
4
star
74

launchweek.dev

Tracking launch weeks across the industry, for product discovery and inspiration.
JavaScript
3
star
75

.github

Supabase Community
3
star
76

core-swift

Shared interfaces and helpers for Swift libraries
Swift
2
star
77

supa-jam

TypeScript
2
star
78

nftree-garden

Mint an NFT, plant a Tree!
JavaScript
1
star
79

realtime-go

1
star
80

functions-rs

Rust
1
star
81

squad

1
star
82

gotrue-php

PHP
1
star
83

storage-php

PHP
1
star
84

postgrest-php

PHP
1
star
85

deno-storage-js

Repo to overwrite https://deno.land/x/storagee listing.
1
star
86

supabase-fastify-api

TypeScript
1
star
87

gotrue-fsharp

F# client for interacting with Supabase GoTrue
F#
1
star
88

realtime-php

1
star
89

core-py

Shared interfaces and helpers for the supabase-py libs
Python
1
star
90

hacktoberfest-hackathon-template

Template for the Supabase Hacktoberfest Hackathon that follows https://hacktoberfest.digitalocean.com/resources/maintainers
1
star
91

postgrest-java

1
star