Giter VIP home page Giter VIP logo

supabase-graphql-example's Introduction

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

supabase-graphql-example's People

Contributors

ardatan avatar dthyresson avatar kiwicopple avatar n1ru4l avatar renovate-bot avatar renovate[bot] avatar thorwebdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

supabase-graphql-example's Issues

Recommended local development / migration workflow

Hi, this isn't really an issue and more of a question. I've never used Supabase before, but only heard good things, and since GraphQL is now supported (also gql-tag-operations-preset = ❤️), I've decided to finally give it a try for a side project I'm building. I've been reading through the local development guide and this repository and was wondering a few things about the recommended local development / migration workflow.

  • Seems like dbmate is used here, while the above guide uses the Supabase CLI. Which is recommended?
  • The public_profiles migration generated by dbmate will not result in a correct Profile table when using it in Supabase's SQL editor (it will result in columns updatedat and avatarurl). Will using dbmate (or any other tool for creating migrations) possibly result in the same when using supabase db push?

I hope posting this here is ok, but if it's not, feel free to close it and I'll ask elsewhere!

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/checkout v3
npm
app/package.json
  • @graphql-yoga/render-graphiql 2.13.12
  • @heroicons/react 1.0.6
  • @supabase/supabase-js 1.35.7
  • @supabase/ui 0.36.5
  • autoprefixer 10.4.12
  • graphql 16.6.0
  • javascript-time-ago 2.5.7
  • next 12.3.1
  • postcss 8.4.17
  • react 17.0.2
  • react-dom 17.0.2
  • tailwindcss 3.1.8
  • urql 2.2.3
  • @types/javascript-time-ago 2.0.3
  • @types/node 17.0.45
  • @types/react 17.0.50
  • eslint 8.24.0
  • eslint-config-next 12.3.1
  • typescript 4.6.2
package.json
  • @graphql-codegen/cli 2.13.1
  • @graphql-codegen/gql-tag-operations-preset 1.6.0
  • gradient-string 2.0.2
  • graphql 16.6.0
  • husky 7.0.4
  • lint-staged 12.5.0
  • prettier 2.8.8
  • progress 2.0.3

  • Check this box to trigger a request for Renovate to run again on this repository

How about the data?

Describe the problem

I followed the readme to clone this git to my local and connect to supabase.com project. I can view the site and login on localhost, but can't post new url. No response after submit. I try to run the sql in data folder but got some errors. And there is no full steps to build those data.

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.