13
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel 5.7 + GraphQL(ScalarType編)

Last updated at Posted at 2018-10-15

LaravelとGraphQLでAPI開発こちらの記事の続きです。

シリーズ記事

GraphQLのスカラー型

Folkloreatelier/laravel-graphqlに標準で組み込まれているスカラー型はID、String、Int、Float、Booleanの5種類しかありません。
日付型(DateTime)をGraphQLで表示するために自分でカスタムスカラー型を定義します。

$ php artisan make:graphql:scalar DateTimeType

app/GraphQL/Scalars/DateTimeType.php

<?php

declare(strict_types = 1);

namespace App\GraphQL\Scalars;

use Folklore\GraphQL\Support\Contracts\TypeConvertible;
use GraphQL\Type\Definition\ScalarType;
use DateTime;

class DateTimeType extends ScalarType implements TypeConvertible
{
    /**
     * @var string
     */
    public $name = 'DateTimeType';

    /**
     * @var string
     */
    public $description = 'DateTime scalar type';

    /**
     * @param mixed $value
     * @return mixed
     */
    public function serialize($value) : DateTime
    {
        return $value;
    }

    /**
     * @param mixed $value
     * @return string
     */
    public function parseValue($value) : string
    {
        return $value;
    }

    /**
     * @param $ast
     * @return null|string
     */
    public function parseLiteral($ast) : ?string
    {
        return null;
    }

    /**
     * @return DateTimeType
     */
    public function toType() : DateTimeType
    {
        return new static();
    }
}

強い型付けを追加しただけで、コードの挙動はひな形のままです。

config/graphql.php

    'types' => [
        App\GraphQL\Type\UserType::class,
        // custom scalar type
        App\GraphQL\Scalars\DateTimeType::class,
    ],

作成したスカラー型クラスを登録します。

app/GraphQL/Type/UserType.php

    public function fields() : array
    {
        return [
            // ... 省略
            'created_at' => [
                'type' => GraphQL::type('DateTimeType'),
                'description' => 'The created_at of user',
            ],
            'updated_at' => [
                'type' => GraphQL::type('DateTimeType'),
                'description' => 'The updated_at of user',
            ],
        ];
    }

UserTypeクラスの fields 関数にDateTime型のカラムを追加します。

動作確認

{
  user {
    id
    name
    created_at
    updated_at
  }
}

queryを実行します。

{
  "data": {
    "user": {
      "id": "1",
      "name": "Maximus Bechtelar",
      "created_at": {
        "date": "2018-10-13 09:13:29.000000",
        "timezone_type": 3,
        "timezone": "UTC"
      },
      "updated_at": {
        "date": "2018-10-13 09:13:29.000000",
        "timezone_type": 3,
        "timezone": "UTC"
      }
    }
  }
}

とこんな感じでDateTimeの値を取得できます。

UnixTimeを返したい

app/GraphQL/Scalars/DateTimeType.php

    public function serialize($value) : int
    {
        return (int)$value->format('U');
    }

serialize関数で DateTime 型を返していたところを UnitTime に変換して int 型で返します。

{
  user {
    id
    name
    created_at
    updated_at
  }
}

同じqueryを発行します。

{
  "data": {
    "user": {
      "id": "1",
      "name": "Maximus Bechtelar",
      "created_at": 1539422009,
      "updated_at": 1539422009
    }
  }
}

UnixTime が返ってきます。
シンプルに書けて良き😊

13
3
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
13
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?