Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LaravelでGraphQLを試してみた

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for 2bo 2bo
January 18, 2020

 LaravelでGraphQLを試してみた

Avatar for 2bo

2bo

January 18, 2020
Tweet

More Decks by 2bo

Other Decks in Technology

Transcript

  1. 8 Users • id • name Posts • id •

    user_id • content 1 * Users and Posts
  2. namespace App; class User extends Model { public function posts():

    HasMany { return $this->hasMany(Post::class); } } namespace App; class Post extends Model { public function user(): BelongsTo { return $this->belongsTo(User::class); } } app/User.php app/Post.php 10
  3. 複数のPostを持つ User::posts(): HasManyと対応付け type User { id: ID! name: String!

    posts: [Post!]! @hasMany } type Post { id: ID! content: String! user: User! @belongsTo } type Query { userById(id: Int! @eq): User @find } タイプ定義 Modelと自動で対応付け フィールド名と型( !はNOT NULL, [ ]は配列) 1人のユーザに所属する Post::user(): BelongsToと対応付け 特別なタイプ データ取得クエリを定義 graphql/schema.graphql idに一致した1ユーザ分の情報を返す 12
  4. References ◎ GraphQL ◎ Lighthouse ◎ GraphQL入門 - 使いたくなるGraphQL ◎

    「GraphQL」徹底入門 ─ RESTとの比較、API・フロント双方の実装から 学ぶ ◎ LaravelにLighthouseを導入してGraphQLサーバーを作る ◎ [Laravel] Fakerで日本語を扱う ◎ Laravel Lighthouse GraphQL - Sorting on server side 19
  5. 1. Laravelのプロジェクトを作る 1.1. $ laravel new lighthouse-sample 2. マイグレートする(デフォルトでUsersテーブルが作成される) 2.1.

    $ php artisan migrate 3. lighthouseとlaravel-graphql-playgroundを追加する 3.1. $ composer require nuwave/lighthouse 3.2. $ composer require mll-lab/laravel-graphql-playground 4. graphql/schema.graphqlを生成する 4.1. $ php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=schema 5. http://[hostname]/graphql-playgroundをブラウザで開く おまけ~Lighthouseを試す際の導入手順~ 20