adeey/graphql-php
Composer 安装命令:
composer require adeey/graphql-php
包简介
GraphQL php library
README 文档
README
This library can build a ready to use query/mutation string from php array
Installation
composer require adeey/graphql-php
Classes
<?php require 'vendor/autoload.php'; use MaxGraphQL\Types\Query; // For Query use MaxGraphQL\Types\Mutation; // For Mutation
Steps to use
Steps to use:
-
Create new class object
$mutation = new Mutation('name');with mutation/query name -
Add what you want to select
$mutation->addSelect(['test', 'name']);2.1 Or you can pass a one name of field
$mutation->addSelect('name'); $mutation->addSelect('test'); $mutation->getSelect(); // ['name', 'test']
-
Add arguments(filters) to your query
$mutation->addArguments(['test' => 123]); -
Get the builded query
$mutation->getPreparedQuery(); -
Use the string in your request
OR
You can build string by calling static method:
// $arguments is optional Mutation::getPreparedQueryFrom('nameOfYourMutation', $selected, $arguments); Query::getPreparedQueryFrom('nameOfYourQuery', $selected, $arguments);
both of these methods will return string
Methods
Methods that can be called from object
- To return current selected fields
$object->getSelect() - To return current arguments
$object->getArguments()
Where is arguments/select?
query { HEREYOURNAME( argument: "ITS MY ARGUMENT" ) { HERE SELECT } }
php code of example:
$query = new Query('HEREYOURNAME'); $arguments = [ 'argument' => 'ITS MY ARGUMENT' ]; $select = [ 'HERE SELECT' ]; $query->addSelect($select); $query->addArguments($arguments); $query->getPreparedQuery(); // and here ours query
How to build a query like this? Its pretty easy
query { users( format: ALL, filter: { activeUsers: true, userIds: [1,2] } ) { id name code password channels { id titles { id } } ... on UserAdmin { userAdminLevel } } }
PHP code:
use MaxGraphQL\FieldTypes\Enum; $whatIWantToSelect = [ 'id', 'name', 'code', 'password', 'channels' => [ 'id', 'titles' => [ 'id' ] ], '... on UserAdmin' => [ 'userAdminLevel' ] ]; $filteringArguments = [ 'format' => new Enum('ALL'), // if you want write enum values you need to use Enum class 'filter' => [ 'activeUsers' => true, 'userIds' => [1,2] ] ]; $query = new Query('users'); $query->addSelect($whatIWantToSelect); $query->addArguments($filteringArguments); echo $query->getPreparedQuery(); // returns query string
The result of PHP code is string of query that equals my GraphQL query and its generated from PHP arrays:
query{users(format:ALL,filter:{activeUsers:true,userIds:[1,2]}){id,name,code,password,channels{id,titles{id}},... on UserAdmin{userAdminLevel}}}
How to build a mutation like this?
mutation { updateUser( id: "321", data: { name: "Test", age: 32, admin: false } ) { id name code password channels { id titles { id } } ... on UserAdmin { userAdminLevel } } }
PHP code:
$whatIWantToSelect = [ 'id', 'name', 'code', 'password', 'channels' => [ 'id', 'titles' => [ 'id' ] ], '... on UserAdmin' => [ 'userAdminLevel' ] ]; $mutationArguments = [ 'id' => '321', // look that id is in string format 'data' => [ 'name' => 'Test', 'age' => 32, // and the age is int 'admin' => false ] ]; $mutation = new Mutation('updateUser'); // updateUser - name of mutation $mutation->addSelect($whatIWantToSelect); $mutation->addArguments($mutationArguments); echo $mutation->getPreparedQuery(); // returns mutation string
Result mutation in string
mutation{updateUser(id:"321",data:{name:"Test",age:32,admin:false}){id,name,code,password,channels{id,titles{id}},... on UserAdmin{userAdminLevel}}}
Additional cases:
Sometimes we want to add extra filter to query like this:
query { users { all(pageSize: 25) { name ... } } }
We need to write like that:
$whatWeNeedToSelect = [ 'all(pageSize: 25)' => [ 'name', ... ] ];
adeey/graphql-php 适用场景与选型建议
adeey/graphql-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.19k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2022 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「array」 「query」 「field」 「graphql」 「graphql-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adeey/graphql-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adeey/graphql-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adeey/graphql-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Query filtering in your frontend
Anax Database Active Record module for model classes.
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 24.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-11