使用Laravel可以创建GraphQL服务器的Lighthouse说明(介绍)

这次我要创建一个GraphQL服务器,并且使用Lighthouse。以下是一个备忘录和简单的解释。这是一个入门篇。

灯塔

公式网站为:https://lighthouse-php.com/
这是一个用于在Laravel中使用GraphQL的库。

引进、引入、引导

    インストール
composer require nuwave/lighthouse
    デフォルトスキーマの公開
php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=schema

生成的架构文件

"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")

"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

"A datetime and timezone string in ISO 8601 format `Y-m-dTH:i:sO`, e.g. `2020-04-20T13:53:12+02:00`."
scalar DateTimeTz @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTimeTz")

type Query {
    users: [User!]! @paginate(defaultCount: 10)
    user(id: ID @eq): User @find
}

type User {
    id: ID!
    name: String!
    email: String!
    created_at: DateTime!
    updated_at: DateTime!
}
    configファイルの作成
php artisan vendor:publish --provider="Nuwave\Lighthouse\LighthouseServiceProvider" --tag=config

使用上述命令将在 config/lighthouse.php 中创建文件。
可以更改端点以及其他各种设置。

    DevTool GraphQL-PlayGround

方便的工具,可以用来确认GraphQL模式并执行查询。
顺便提一下,像Postman这样的客户端工具也支持GraphQL。

composer require --dev mll-lab/laravel-graphql-playground
スクリーンショット 2020-06-11 15.35.01.png

我看了公式,内容非常简单。引入部分已经结束,下次我打算总结一下lighthouse的指令。

bannerAds