Skip to content

Commit 06f9136

Browse files
authored
Supabase specific docs (#323)
* outline for supabase doc * towards supabase doc * federation for supabase landing doc * rm outdated jpgs * rm quickstart jpg * add local mode
1 parent 7f55ec2 commit 06f9136

8 files changed

+188
-0
lines changed

docs/assets/supabase_api_key.png

513 KB
Loading

docs/assets/supabase_graphiql.png

548 KB
Loading
502 KB
Loading
546 KB
Loading

docs/assets/supabase_project_ref.png

462 KB
Loading

docs/assets/supabase_sql_editor.png

338 KB
Loading

docs/supabase.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
The Supabase GraphQL API is automatically reflected from your database's schema using [pg_graphql](https://github.com/supabase/pg_graphql). It supports:
2+
3+
- Basic CRUD operations (Create/Read/Update/Delete)
4+
- Support for Tables, Views, Materialized Views, and Foreign Tables
5+
- Arbitrarily deep relationships among tables/views
6+
- User defined computed fields
7+
- Postgres' security model - including Row Level Security, Roles, and Grants
8+
9+
All requests resolve in a single round-trip leading to fast response times and high throughput.
10+
11+
If you haven't created a Supabase project, do that [here](https://database.new) so you can follow along with the guide.
12+
13+
## API
14+
15+
If you're new to GraphQL or Supabase, we strongly recommend starting with Supabase GraphQL by following the [Supabase Studio guide](#supabase-studio).
16+
17+
Fore more experienced users, or when you're ready to productionize your application, access the API using [supabase-js](#supabase-js), [GraphiQL](#connecting-graphiql), or any HTTP client, for example [cURL](#curl).
18+
19+
### Supabase Studio
20+
21+
The easiest way to make a GraphQL request with Supabase is to use [Supabase Studio's builtin GraphiQL IDE](https://app.supabase.com/project/_/api/graphiql).
22+
You can access GraphiQL [here](https://app.supabase.com/project/_/api/graphiql) by selecting the relevant project. Alternatively, navigate there within Studio at `API Docs > GraphQL > GraphiQL`.
23+
24+
![graphiql](./assets/supabase_graphiql.png)
25+
26+
Type queries in the central query editor and use the green icon to submit requests to the server. Results are shown in the output display to the right of the editor.
27+
28+
To explore the API visually, select the docs icon shown below and navigate through each type to see how they connect to the Graph.
29+
30+
![graphiql](./assets/supabase_graphiql_explore.png)
31+
32+
pg_graphql mirrors the structure of the project's SQL schema in the GraphQL API. If your project is new and empty, the GraphQL API will be empty as well, with the exception of basic introspection types. For a more interesting result, go to the SQL or table editor and create a table.
33+
34+
![graphiql](./assets/supabase_sql_editor.png)
35+
36+
Head back to GraphiQL to see the new table reflected in your GraphQL API's Query and Mutation types.
37+
38+
![graphiql](./assets/supabase_graphiql_query_table.png)
39+
40+
If you'd like your type and field names to match the GraphQL convention of `PascalCase` for types and `camelCase` for fields, check out the [pg_graphql inflection guide](/pg_graphql/configuration/#inflection).
41+
42+
### HTTP Request
43+
44+
To access the GraphQL API over HTTP, first collect your [project reference](#project-reference-project_ref) and [API Key](#api-key-api_key).
45+
46+
### cURL
47+
48+
To hit the Supabase GraphQL API using cURL, submit a `POST` request to your GraphQL API's URL shown below, substituting in your [PROJECT_REF](#project-reference-project_ref) and passing the project's [API_KEY](#api-key-api_key) as the `apiKey` header:
49+
50+
51+
```sh
52+
curl -X POST https://<PROJECT_REF>.supabase.co/graphql/v1 \
53+
-H 'apiKey: <API_KEY>' \
54+
-H 'Content-Type: application/json' \
55+
--data-raw '{"query": "{ accountCollection(first: 1) { edges { node { id } } } }", "variables": {}}'
56+
```
57+
58+
In that example, the GraphQL `query` is
59+
```graphql
60+
{
61+
accountCollection(first: 1) {
62+
edges {
63+
node {
64+
id
65+
}
66+
}
67+
}
68+
}
69+
```
70+
71+
and there are no `variables`
72+
```js
73+
{}
74+
```
75+
76+
### supabase-js
77+
78+
The JS ecosystem supports multiple prominent GraphQL frameworks. [supabase-js](https://supabase.com/docs/reference/javascript/introduction) is unopinionated about your GraphQL tooling and can integrate with all of them.
79+
80+
For an example integration, check out the [Relay guide](/pg_graphql/usage_with_relay), complete with Supabase Auth support.
81+
82+
### GraphiQL
83+
84+
If you'd prefer to connect to Supabase GraphQL using an external IDE like GraphiQL, save the HTML snippet below as `supabase_graphiql.html` and open it in your browser. Be sure to substitute in your [PROJECT_REF](#project-reference-project_ref) and [API_KEY](#api-key-api_key) beneath the `EDIT BELOW` comment:
85+
86+
87+
```html
88+
<html>
89+
<head>
90+
<title>GraphiQL</title>
91+
<link href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.4.7/graphiql.css" rel="stylesheet" />
92+
</head>
93+
<body style="margin: 0;">
94+
<div id="graphiql" style="height: 100vh;"></div>
95+
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
96+
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
97+
<script
98+
crossorigin
99+
src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/2.4.7/graphiql.js"
100+
></script>
101+
<script>
102+
103+
////////////////
104+
// EDIT BELOW //
105+
////////////////
106+
107+
const fetcher = GraphiQL.createFetcher({
108+
url: 'https://<PROJECT_REF>.supabase.co/graphql/v1',
109+
headers: {
110+
"apiKey": "<API_KEY>",
111+
}
112+
});
113+
ReactDOM.render(
114+
React.createElement(GraphiQL, { fetcher: fetcher }),
115+
document.getElementById('graphiql'),
116+
);
117+
</script>
118+
</body>
119+
</html>
120+
```
121+
122+
## Version Management
123+
124+
To maximize stability, you are in control of when to upgrade your GraphQL API.
125+
To see which version of pg_graphql you have, and the highest upgrade version available, execute:
126+
127+
```sql
128+
select * from pg_available_extensions where name = 'pg_graphql'
129+
```
130+
131+
Which returns a table, for example:
132+
133+
| name | default_version | installed_version | comment |
134+
|------------|-----------------|-------------------|-----------------|
135+
| pg_graphql | 1.2.0 | 1.1.0 | GraphQL support |
136+
137+
The `default_version` is the highest version available on your database. The `installed_version` is the version currently enabled in your database.
138+
If the two differ, as in the example, you can upgrade your installed version by running:
139+
140+
```sql
141+
drop extension pg_graphql; -- drop version 1.1.0
142+
create extension pg_graphql; -- install default version 1.2.0
143+
```
144+
145+
To upgrade your GraphQL API with 0 downtime.
146+
147+
When making a decision to upgrade, you can review features of the upgraded version in the [changelog](/pg_graphql/changelog/).
148+
149+
Always test a new version of pg_graphql extensively on a development or staging instance before updating your production instance. pg_graphql follows SemVer, which makes API backwards compatibility relatively safe for minor and patch updates. Even so, it's critical to verify that changes do not negatively impact the specifics of your project's API in other ways, e.g. requests/sec or CPU load.
150+
151+
152+
## Local Development
153+
154+
When starting a local project through the [Supabase CLI](https://supabase.com/docs/guides/cli), the output of `supabase start` provides the information needed to call the GraphQL API directly. You can also use the Supabase Studio url to access [the builtin GraphiQL IDE](https://app.supabase.com/project/_/api/graphiql).
155+
156+
```sh
157+
> supabase start
158+
...
159+
160+
Started supabase local development setup.
161+
162+
GraphQL URL: http://localhost:54321/graphql/v1 <-- GraphQL endpoint
163+
DB URL: ...
164+
Studio URL: http://localhost:54323 <-- Supabase Studio
165+
Inbucket URL: ...
166+
JWT secret: ...
167+
anon key: eyJhbGciOiJIUzI1...<truncated> <-- API_KEY
168+
service_role key: ...
169+
```
170+
171+
172+
## Term Reference
173+
174+
### Project Reference (PROJECT_REF)
175+
176+
Your Supabase project reference or PROJECT_REF is a 20 digit unique identifier for your project, for example `bvykdyhlwawojivopztl`.
177+
The project reference is used throughout your supabase application including the project's API URL. You can find the project reference in by logging
178+
in to Supabase Studio and navigating to `Settings > General > Project Settings > Reference ID`
179+
180+
![project_ref](./assets/supabase_project_ref.png)
181+
182+
183+
### API Key (API_KEY)
184+
185+
Your Supabase API Key is a public value that must be sent with every API request. The key is visible in Supabase Studio at `Settings > API > Project API keys`
186+
187+
![project_ref](./assets/supabase_api_key.png)

mkdocs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nav:
1515
- Security: 'security.md'
1616
- Configuration: 'configuration.md'
1717
- Guides:
18+
- Supabase: 'supabase.md'
1819
- Usage with Relay: 'usage_with_relay.md'
1920
- Example Schema: 'example_schema.md'
2021
- Installation: 'installation.md'

0 commit comments

Comments
 (0)