graphile / graphile/graphile.github.io

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

Đang mở
#67 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
SCSS
Star
27
Fork
126
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Xem xét đề xuất này cùng với tài liệu bảo mật hiện có mà đề xuất tham chiếu và cách tổ chức recipe hiện tại của trang web. Thêm một recipe giải thích trường root của viewer, bao gồm mẫu SQL được minh họa và các giả định về xác thực của mẫu đó; được xem là hoàn tất khi recipe được đăng tại vị trí tài liệu phù hợp và liên kết đến hướng dẫn bảo mật liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
graphql, sql
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.