johnlui/graphql-laravel

A Laravel GraphQL reference implementation forked from webonyx/graphql-php

v1.5.1 2023-06-11 15:17 UTC

This package is auto-updated.

Last update: 2024-04-11 17:14:37 UTC


README

99667011-b71cfc80-2aa6-11eb-95d2-b2e1bbfa73f4.png

GitHub version php-version License 68747470733a2f2f7472617669732d63692e6f72672f6a6f686e6c75692f4772617068514c2d4c61726176656c2e7376673f6272616e63683d6d6173746572


webonyx/graphql-php 的 Laravel 适配,魔改原库,将其彻底融入 Laravel 生命周期。

功能

  1. 接入 Laravel Middleware
  2. 依赖 Laravel Exception 处理异常流程
  3. 完美接入 Eloquent
  4. 提炼出 GraphQL 路由
  5. 提供 Types 文件夹,并提供强大的 Type 基类
  6. 提供 QueryFields 文件夹,提供类似于 Controller 归类的功能
  7. 暴露用户请求字段给 QueryField,便于节约资源、并发 RPC 等后续操作
  8. 提供 JSON API 语境下的调试函数 cc()

用法

安装 Composer 包

composer require johnlui/graphql-laravel

部署开发目录及配置文件到 app 下

php artisan vendor:publish

快速入门

  1. app\GraphApp 为工作文件夹
  2. Routes.php 为路由文件
  3. QueryFields 目录存放响应代码
  4. Types 目录存放自定义类型
  5. config/graphql.php 为配置文件

demo

query:

query ($id: Int) {
  foo(id: $id) {
    id
    foo
    bar
  }
}

variables:

此处为 JSON 格式描述,开发时应使用 key-value 形式描述 variables,不应该使用 JSON 字符串。

{
  "id": 200
}

启动 server

php artisan serve

url

http://127.0.0.1:8000/graphql

修改默认路径请编辑config/graphql.php中的apiUri

curl raw 代码描述

curl --location --request POST 'http://127.0.0.1:8000/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query($id: Int) {\n    foo(id: $id) {\n        id\n        foo\n        bar\n    }\n}","variables":{"id":200}}'

返回值

{
  "status": 0,
  "message": "OK",
  "values": {
    "foo": {
      "id": 200,
      "foo": "foo",
      "bar": "bar"
    },
    "queries": []
  }
}

License

The johnlui/GraphQL-Laravel is open-sourced software licensed under the MIT license