Skip to content

Instantly share code, notes, and snippets.

@bz0
Last active December 6, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bz0/6d539596d0f1d6651e3c7085cffee960 to your computer and use it in GitHub Desktop.
Save bz0/6d539596d0f1d6651e3c7085cffee960 to your computer and use it in GitHub Desktop.

LaravelでのGraphQL開発

前提

下記ライブラリを利用
https://github.com/nuwave/lighthouse

playgroundで動作確認
https://localhost/graphql-playground

公式マニュアル:
https://lighthouse-php.com

ページネーションのカスタム

query {
  TQuery(first:10, page:1) {
    data{
      title
      genre
      writer
    }
    paginatorInfo{
      total
    }
  }
}

src/graphql/schema.graphql

type Query {
    TQuery: [Test!]! @paginate(builder: "App\\GraphQL\\Queries\\TestQuery")
}

type Test {
    title: String
    writer: String
    genre: Int
}
namespace App\GraphQL\Queries;

use Illuminate\Support\Facades\DB;

class TestQuery
{
    public function __invoke($_, array $args)
    {
        return DB::table('test1')
            ->join('test2',[['test1.id', '=', 'test2.test1_id']])
            ->orderBy('test2.created_at', 'asc');
    }
}

カラム単位にメソッドからデータ取得

https://stackoverflow.com/questions/67113127/how-to-properly-serve-custom-queries-with-method-directive-in-lighthouse-php

app/GraphQL/Queries/xxx.php

namespace App\GraphQL\Queries;

class xxx
{
    public function __invoke($_, array $args)
    {
        return $this;
    }

    public function test()
    {
        return 1111;
    }
}

graphql/schema.graphql

type Query {
    xxx: xxx
}

type xxx {
    test: Int @method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment