14
5

More than 3 years have passed since last update.

Laravel+GraphQL Lighthouse チュートリアル 後半

Last updated at Posted at 2020-05-08

チュートリアル

前回の続きでLaravel+GraphQL Lighthouseチュートリアルを進めます。

Agenda

  1. フィールドにページネーション(ページャー)を追加
  2. Eloquentモデルの作成と更新
  3. サーバーに送信される値のバリデーション

所要時間: 10分程度

1. フィールドにページネーション(ページャー)を追加

https://lighthouse-php.com/master/api-reference/directives.html#paginate
こちらを参考に進めます。

graphql/schema.graphql
type Query {
    # posts: [Post!]! @all
    posts: [Post!]! @paginate
}

スキーマ定義は自動的に次のように変換されます。
(これは書かなくてokです。)

graphql/schema.graphql
type Query {
  posts(first: Int!, page: Int): PostPaginator
}

"A paginated list of Post items."
type PostPaginator {
  "A list of Post items."
  data: [Post!]!

  "Pagination information about the list of items."
  paginatorInfo: PaginatorInfo!
}

リクエストデータ
{
  posts(first: 5) {
    data {
      id
      title
    }
    paginatorInfo {
      currentPage
      lastPage
    }
  }
}

スクリーンショット 2020-05-07 23.11.03.png

簡単にページネーションを実装できました。
Laravelのコード何も書いてない...すごい(すごい)

2. Eloquentモデルの作成と更新

https://lighthouse-php.com/master/eloquent/getting-started.html#create
こちらを参考に進めます。

Create

データベースに User を作成する処理を実装します。

graphql/schema.graphql
type Mutation {
  createUser(
    name: String!
    email: String!
    password: String!
  ): User! @create
}

createミューテーションは、フィールドに渡された引数を使用して、新しいモデルインスタンスを作成します。

リクエストデータ
mutation {
  createUser(
    name: "foo"
    email: "foo@example.com"
    password: "secret"
  ) {
    id
    name
  }
}

スクリーンショット 2020-05-08 0.02.10.png

Update

データベースに登録されている User を更新する処理を実装します。

type Mutation {
  updateUser(
    id: ID!
    name: String
  ): User @update
}
mutation {
  updateUser(id: "4", name: "bar") {
    id
    name
  }
}

スクリーンショット 2020-05-08 21.17.51.png

3. サーバーに送信される値のバリデーション

https://lighthouse-php.com/master/security/validation.html#single-arguments
こちらを参考に進めます。

Laravel標準のバリデーションルールを利用する簡単な方法は、@rulesディレクティブを使用することです。

graphql/schema.graphql
type Mutation {
  createUser(email: String @rules(apply: ["email"])): User
}

バリデーションエラーの場合、Lighthouseは実行を中止し、レスポンスの一部としてバリデーションメッセージを返します。

リクエストデータ
mutation {
  createUser(
    name: "hoge"
    email: "hogehoge@example.com"
    password: "secret"
  ) {
    id
    name
  }
}
レスポンスデータ
{
  "errors": [
    {
      "message": "Validation failed for the field [createUser].",
      "extensions": {
        "validation": {
          "email": [
            "The email must be a valid email address."
          ]
        },
        "category": "validation"
      },
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createUser"
      ],
      "trace": [
        {
          "file": "/Users/ucan/work/lighthouse-tutorial/vendor/nuwave/lighthouse/src/Execution/ErrorBuffer.php",
          "line": 64,
          "call": "Nuwave\\Lighthouse\\Execution\\ErrorBuffer::Nuwave\\Lighthouse\\Execution\\{closure}('Validation failed for the field [createUser].', instance of Nuwave\\Lighthouse\\Execution\\ErrorBuffer)"
        },
        // トレースログは省略
      ]
    }
  ]
}

スクリーンショット 2020-05-08 21.26.18.png

さいごに

以上でLaravel+GraphQL Lighthouseチュートリアルは完了です。
もっと詳しい使い方は公式ドキュメントをご参照ください。
https://lighthouse-php.com/master/getting-started/installation.html

14
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
5