graphile / graphile/graphile.github.io

Recipe: Creating a viewer/"logged in user" root field

Aperta
#67 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
SCSS
Stelle
27
Fork
126
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Here's another proposal for a very simple recipe. Again, feel free to do what you want with this.

## Creating a viewer/"logged in user" root field
A lot of API:s use the "viewer" convention, putting a field on the root query that references the currently logged in user. If you have your authentication set up properly and a have a way of getting the currently logged in user's user id, you can do this very easily with Postgraphile:

```sql
create function my_schema.viewer()
returns my_schema.users as $$
select *
from my_schema.users
where id = current_user_id(); /* current_user_id is a function that returns the logged in user's id, usually by extracting it from the auth token used */
$$ language sql stable;
```

This will put a field (viewer) on your root query that returns the logged in user. Very convenient! Check out the docs on security for handling authorization and adding a current_user_id function.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.