RedWoodJS

# yarn rw g page <name> <route>
yarn rw g page home /
  • レイアウト: 共通の構造

prisma

  • /api/db/schema.prisma

  • migrateは

    • yarn rw prisma migrate dev
    • メッセージ入力
    • /scripts/seed.js も実行される
  • yarn rw prisma studio で確認

scaffold

  • yarn rw g scaffold post
    • CRUD API
    • routingの登録
<Route path="/posts/new" page={PostNewPostPage} name="newPost" />
<Route path="/posts/{id:Int}/edit" page={PostEditPostPage} name="editPost" />
<Route path="/posts/{id:Int}" page={PostPostPage} name="post" />
  • routingも設定されている

    • いい感じのフォームも出来ている
  • /posts<PostsCell /> を表示する

    • @ /src/components/Post/PostsCell/PostsCell.tsx
      • Cell という単位
        • Query (GraphQL query), Loading, Empty, Failure, Success のコンポーネントがある

Cells

yarn rw g cell Articles
  • /src/components/ArticlesCell/ が出来る
  • HomePage.tsx
import ArticlesCell from 'src/components/ArticlesCell'
 
<ArticlesCell />

dynamic routing

  • /article/{id} に飛びたい
yarn rw g page Article
  • ArticlesCell.tsx#Success でリンクの追加
import { Link, routes } from '@redwoodjs/router'
 
...
 
<Link to={
  routes.article({ id: article.id })
} />
  {item.title}
</Link>
  • Routes.tsx
<Route path="/article/{id}" page={ArticlePage} name="article" />
  • pages/ArticlePage.tsxroute.article() 行を削除

Articleのページを作る

yarn rw g cell Article
  • Article CellをArticle Pageから使用
import ArticleCell from 'src/components/ArticleCell'
 
const ArticlePage = ({ id }) => {
  <ArticleCell id={id} />
}
  • idInt だがURLは文字列なのでエラー

  • yarn rw devGraphQL Playground開く

    • なんか入力欄使えない

認証機能

  • dbAuth というものが用意されている
    • DB内に保存する
  • Routing /posts -> /admin/posts/
yarn rw setup auth dbAuth
  • schema.prismaUser モデル作成

  • yarn rw prisma migrate dev

  • すると GraphQL API(CRUD)に認証がつく

    • @requireAuth ディレクティブがつく
  • ページで認証をかけるにはRoutes.tsxで <Set><Private unauthenticated="home"> で囲む

  • 認証をスキップ

    • /api/src/graphql/posts.sdl.ts@requireAuth@skipAuth に変える

login, signupページ

yarn rw g dbAuth
  • /login にアクセスしてユーザー登録すると見れるように
  • auth系の操作は import { useAuth } from '@redwoodjs/auth' hooksがある

ここまでで The Tutorial のChapter 2まで相当

todo

Tailwind CSS

yarn rw setup tailwind

で入るっぽい

参考文献