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

GraphQL はいいぞ! ~Laravel で学ぶ GraphQL 入門~

SAW
July 20, 2024

GraphQL はいいぞ! ~Laravel で学ぶ GraphQL 入門~

PHP"オレ"カンファレンス神戸 #1 の発表資料です。

SAW

July 20, 2024
Tweet

More Decks by SAW

Other Decks in Programming

Transcript

  1. 1)1ΦϨΧϯϑΝϨϯεਆށ $(whoami) w ࢯ໊Ճ౻फҰ࿠ ࡀ  w ϋϯυϧωʔϜ4"8 w 9

    چ5XJUUFS !B[VLJ@FBUFS w ؔ੢ͷ*5ΤϯδχΞίϛϡχςΟͷ೐΍͔͠୲౰ ࣗশ  w େࡕࡏॅɾѪ஌ग़਎ w ಘҙ෼໺8FCΞϓϦέʔγϣϯ։ൃ w -BSBWFM 7VF w झຯήʔϜ🎮ɾυϥΠϒ🏎ɾϰΝΠΦϦϯ🎻 
  2. 1)1ΦϨΧϯϑΝϨϯεਆށ $(history) w ֶྺम࢜ ιϑτ΢ΣΞ޻ֶ  w ೆࢁେֶ৘ใཧ޻ֶ෦ιϑτ΢ΣΞ޻ֶՊ೥݄ଔۀ w ೆࢁେֶཧ޻ֶݚڀՊιϑτ΢ΣΞ޻ֶઐ߈ത࢜લظ՝ఔ೥݄मྃ

    w ओͳ৬຿࣮੷ w Ξϯέʔτ࡞੒γεςϜͷϦϓϨΠε 7VFKT -BSBWFM  w ֎ࠃޠൃԻ࿅श8FCΞϓϦέʔγϣϯͷ։ൃ 7VFKT -BSBWFM  w ݸਓ։ൃ w ӳ୯ޠֶश༻8FCΞϓϦέʔγϣϯΛ։ൃத🍊 7VFKT -BSBWFM 
  3. 1)1ΦϨΧϯϑΝϨϯεਆށ (SBQI2-ͱ͸ w "1*ͷͨΊͷ໰͍߹Θͤݴޠ w (SBQI2-Ͱ͸ΫΤϦͰඞཁͳσʔλΛࢦఆ w ͞·͟·ͳϓϩάϥϛϯάݴޠͰͷαʔόʔ࣮૷ϥΠϒϥϦ͕ଘࡏ w αʔόʔ࣮૷ͷͨΊͷϥΠϒϥϦͷྫ

    w 1)1-JHIUIPVTF w 1ZUIPO(SBQIFOF 4USBXCFSSZ w ΫϥΠΞϯτ༻ͷϥΠϒϥϦͷྫ w +BWB4DSJQU"QPMMP$MJFOU w 4XJGU"QPMMPJ04 4XJGU(SBQI2- 
  4. 1)1ΦϨΧϯϑΝϨϯεਆށ  ΫϥΠΞϯτ͕ΫΤϦΛૹ৴͢Δ  αʔόʔ͸ཁٻ͞ΕΔϑΟʔϧυͷσʔλΛऔಘ͢ΔॲཧΛ࣮ߦ͢Δ  औಘͨ͠σʔλΛΫϥΠΞϯτʹฦ٫͢Δ (SBQI2-Λར༻ͨ͠"1*௨৴ͷྲྀΕ  ΫϥΠΞϯτ

    αʔόʔ query loginUser { user { me } } { "data": { "user": { "me": "SAW" } } }  ΫΤϦΛૹ৴  ཁٻ͞ΕΔϑΟʔϧυͷ σʔλΛऔಘ  σʔλΛ+40/Ͱฦ٫
  5. 1)1ΦϨΧϯϑΝϨϯεਆށ σʔλૢ࡞ͷ෼ྨ w (SBQI2-ʹ͓͚Δσʔλૢ࡞ͷ෼ྨ͸ҎԼͷͭ w 2VFSZσʔλͷಡΈࠐΈૢ࡞ w $36%ͷ3 3FBE Λ୲౰

    w .VUBUJPOσʔλͷॻ͖ࠐΈૢ࡞ w $36%ͷ$ $SFBUF 6 6QEBUF % %FMFUF Λ୲౰ w 4VCTDSJQUJPOαʔόʔͷߋ৽ΛϦΞϧλΠϜʹऔಘ w αʔόʔͷσʔλ͕มߋ͞ΕΔͱTVCTDSJCF͍ͯ͠ΔΫϥΠΞϯτʹ௨஌ 
  6. 1)1ΦϨΧϯϑΝϨϯεਆށ Ϧετܕʹ͍ͭͯ w (SBQI2-Ͱ͸ϦετΛׅ֯ހ [] Ͱදݱ w Ϧετͷத਎ͱϦετࣗ਎͕OVMMBCMF͔ΛͦΕͧΕࢦఆ  Ϧετͷܕએݴ

    ఆٛ [Int] ϦετOVMMBCMF த਎OVMMBCMFͳ*OU [Int!] ϦετOVMMBCMF த਎OPOOVMMBCMFͳ*OU [Int]! ϦετOPOOVMMBCMF த਎OVMMBCMFͳ*OU [Int!]! ϦετOPOOVMMBCMF த਎OPOOVMMBCMFͳ*OU
  7. 1)1ΦϨΧϯϑΝϨϯεਆށ -JHIUIPVTFΛ࢖࣮ͬͨ૷ͷཁૉ  εΩʔϚఆٛ  Ϧκϧό  &MPRVFOU.PEFM  (SBQI2-αʔόʔͷ಺෦

    TDIFNB SFTPMWFS .PEFM $MJFOU ϦκϧόΛ࣮ߦ ϞσϧΛࢀর ϞσϧΛฦ٫ ࣮ߦ݁ՌΛฦ٫ ϦΫΤετΛૹ৴ ϨεϙϯεΛฦ٫
  8. 1)1ΦϨΧϯϑΝϨϯεਆށ &MPRVFOU.PEFMͷ࣮૷ྫ w &MPRVFOUϞσϧͱςʔϒϧΛ࡞੒͢Δ.JHSBUJPOΫϥεΛ࡞੒ w &MPRVFOU.PEFMPost w ςʔϒϧposts  public

    function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id('id'); $table->string('title'); $table->string('content'); $table->timestamps(); }); } QPTUTςʔϒϧͷ.JHSBUJPOͷఆٛ final class Post extends Model { } 1PTUϞσϧͷఆٛ
  9. 1)1ΦϨΧϯϑΝϨϯεਆށ εΩʔϚఆٛͷ࣮૷ྫ w graphql/schema.graphqlΛฤूͯ͠εΩʔϚఆٛΛ௥Ճ w &MPRVFOU.PEFMͷߏ଄ΛΦϒδΣΫτͱͯ͠ܕఆٛ w "1*ͱͯ͠Մೳͳૢ࡞Λ2VFSZʹͦΕͧΕఆٛ w posts͢΂ͯͷPostϞσϧͷϦετΛฦ٫

    w postࢦఆͨ͠idʹҰக͢ΔPostϞσϧΛฦ٫  type Post { id: ID! title: String! content: String! } type Query { posts: [Post!]! post(id: Int!): Post } graphql/schema.graphql
  10. 1)1ΦϨΧϯϑΝϨϯεਆށ Ϧιϧόͷ࣮૷ྫ w εΩʔϚͰఆٛͨ͠ૢ࡞ʹରԠ͢ΔॲཧΛ࣮૷ w 2VFSZͷϦκϧόͷTDB ff PME͸ҎԼͷartisanίϚϯυ͔Βੜ੒Մೳ w php

    artisan lighthouse:query 操作名  use App\Models\Post as ModelsPost; // posts のリゾルバ final readonly class Posts { public function __invoke() { $posts = ModelsPost::all(); return $posts; } } App\GraphQL\Queries\Posts use App\Models\Post as ModelsPost; // post のリゾルバ final readonly class Post { public function __invoke(null $_, array $args) { $id = $args['id']; $post = ModelsPost::find($id); return $post; } } App\GraphQL\Queries\Post