LoginSignup
0

More than 3 years have passed since last update.

Lighthouse-PHPを使用したLaravel Graphqlサーバー

Last updated at Posted at 2020-02-18

みなさんこんにちは、最近行方不明になって申し訳ありませんが、仕事で忙しくて、ブログを更新する時間を見つけることができませんでした

Hello everybody, sorry I´ve been missing lately, but I´ve been busy with work, and couldn´t find the time to update the blog

始めましょう

Dockerコンテナを使用してlaravelをインストールする

Install laravel using docker container

Create Laravel Project using Docker

初期移行を実行する

Run Initial Migrations

sudo docker-compose exec app-server php artisan migrate
Lighthouse PHPをインストールする

Install lighthouse-php

lighthouse-phpをインストールする

docker run --rm -v $(pwd):/app composer require nuwave/lighthouse

graphqlのデフォルトスキーマを公開する

Publish default schema for graphql

sudo docker-compose exec app-server php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=schema

app-serverで、docker-composeファイルで使用する名前を忘れずに変更してください

in app-server, remember to change for the name you use on your docker-compose file

Graphql Playgroundをインストールする

次の手順はオプションですが、推奨されますが、graphql-playgroundをインストールします

Next step is optional, but recommended, install the graphql-playground

docker run --rm -v $(pwd):/app composer require mll-lab/laravel-graphql-playground

遊び場のURLはhttp://localhost/graphql-playground

url for the playground is http://localhost/graphql-playground

構成

コンテナをもう一度実行し、 docker-compose up --build

graphqlのデフォルト設定をエクスポートします

run the containers once more, docker-compose up --build

export the graphql default config

docker-compose exec app-server php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=config
突然変異とクエリ

クエリを実行する前に、データを挿入する必要があります。最初に突然変異を作成します

Before doing a query, we need to insert data, lets first create a mutation

sudo docker-compose exec app-server php artisan lighthouse:mutation createUser

これにより、 app \ graphql \ mutationsフォルダー内にCreateUser.phpが作成されます。

簡単なリゾルバを追加しましょう

This will create a CreateUser.phpinside the app\graphql\mutations folder

Lets add a simple resolver


public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $args["password"] = Hash::make($args["password"]);

        return User::create($args);
    }

schema.graphqlファイル内にルート変更を追加します

Add the route mutation inside the schema.graphql file

type Mutation {
    createUser(name: String!, email: String!, password: String!): User
}

これで、不眠症を使用しているので、簡単な突然変異を作成できます。これが私の突然変異の作成方法です

または、http:// localhost / graphql-playground のプレイグラウンドを使用できます

With this, you can create a simple mutation, since I´m using insomnia, this is how I create my mutation

or you can use the playground at http://localhost/graphql-playground

Insomnia POST : http://localhost/graphql
Playground : http://localhost/graphql-playground

mutation{
  createUser(name:"My Name",email:"my@email.com",password:"mypassword"){
    id,
    name,
  }
}

200を取得すると、応答がすでにjsonであることがわかります。クエリリクエストを作成してユーザーを確認できます。

Once you get the 200, you can see the response is already a json, we can check the users by going making a query request

POST: http://localhost/graphql
Playground : http://localhost/graphql-playground

query{
  users{
    data{
      id,
      name,
      email
    }
  }
}

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
0