secundo/laravel-graphql-query-builder
最新稳定版本:v1.0.0
Composer 安装命令:
composer require secundo/laravel-graphql-query-builder
包简介
A powerful and elegant GraphQL query builder for PHP, designed specifically for Laravel applications
README 文档
README
A powerful and elegant GraphQL query builder for PHP, designed specifically for Laravel applications. Build GraphQL queries and mutations with a fluent, type-safe interface.
Features
- Fluent API: Build GraphQL queries using an intuitive, chainable interface
- Type Safety: Full PHP 8+ type hints for better IDE support and fewer runtime errors
- Laravel Integration: Built with Laravel conventions and auto-discovery
- Variable Support: Automatic variable handling with type safety
- Fragment Support: Create reusable query fragments
- Inline Fragments: Type-based conditional field selection
Requirements
- PHP 8.3 or higher
- Laravel 11.0 or higher
Installation
You can install the package via composer:
composer require secundo/laravel-graphql-query-builder
The service provider will be automatically registered using Laravel's package discovery.
Quick Start
Basic Query
use Secundo\GraphQL\Builder; $builder = new Builder(); $query = $builder->query() ->field('products', [], ['id', 'title', 'handle']) ->toGraphQL(); // Output: // query { // products { // id // title // handle // } // }
Query with Arguments
$query = $builder->query() ->field('product', ['id' => 'gid://shopify/Product/123'], ['id', 'title', 'handle']) ->toGraphQL(); // Output: // query { // product(id: "gid://shopify/Product/123") { // id // title // handle // } // }
Nested Fields
$query = $builder->query() ->field('products', ['first' => 10], function ($field) { $field->field('edges', [], function ($field) { $field->field('node', [], ['id', 'title']); $field->field('cursor'); }); $field->field('pageInfo', [], ['hasNextPage', 'endCursor']); }) ->toGraphQL();
Using Variables
$query = $builder->query('GetProduct') ->variable('id', 'ID!', 'gid://shopify/Product/123') ->field('product', ['id' => '$id'], ['id', 'title', 'description']) ->toGraphQL(); // Get variable values for the request $variables = $builder->getVariableValues(); // Output: // query GetProduct($id: ID!) { // product(id: $id) { // id // title // description // } // } // Variables: {"id": "gid://shopify/Product/123"}
Mutations
$mutation = $builder->mutation('UpdateProduct') ->variable('id', 'ID!') ->variable('input', 'ProductInput!') ->field('productUpdate', ['id' => '$id', 'product' => '$input'], function ($field) { $field->field('product', [], ['id', 'title']); $field->field('userErrors', [], ['field', 'message']); }) ->toGraphQL();
Fragments
$query = $builder->query() ->fragment('ProductFields', 'Product', ['id', 'title', 'handle', 'createdAt']) ->field('products', [], function ($field) { $field->field('edges', [], function ($field) { $field->field('node', [], ['...ProductFields']); }); }) ->toGraphQL();
Inline Fragments
$query = $builder->query() ->field('node', ['id' => '$id'], function ($field) { $field->field('id'); $field->inlineFragment('Product', function ($fragment) { $fragment->field('title'); $fragment->field('handle'); }); $field->inlineFragment('Collection', function ($fragment) { $fragment->field('title'); $fragment->field('description'); }); }) ->toGraphQL(); // Output: // query { // node(id: $id) { // id // ... on Product { // title // handle // } // ... on Collection { // title // description // } // } // }
Advanced Usage
Multiple Variables with Default Values
$query = $builder->query('SearchProducts') ->variables([ 'first' => ['type' => 'Int', 'value' => 10], 'query' => ['type' => 'String', 'value' => 'title:shirt'], 'sortKey' => ['type' => 'ProductSortKeys', 'value' => 'CREATED_AT'] ]) ->field('products', [ 'first' => '$first', 'query' => '$query', 'sortKey' => '$sortKey' ], ['id', 'title']);
Field Aliases
use Secundo\GraphQL\Types\Field; $field = new Field('product'); $field->alias('myProduct') ->arguments(['id' => 'gid://shopify/Product/123']) ->fields(['id', 'title']);
Directives
$field = new Field('expensiveField'); $field->directive('include', ['if' => '$includeExpensive']) ->fields(['data']); $field = new Field('debugInfo'); $field->directive('skip', ['if' => '$production']) ->fields(['logs']);
Laravel Integration
Using the Facade
use Secundo\GraphQL\Facades\GraphQL; $query = GraphQL::query() ->field('shop', [], ['name', 'email']) ->toGraphQL();
Dependency Injection
use Secundo\GraphQL\GraphQL; class ProductController extends Controller { public function __construct( private GraphQL $graphql ) {} public function index() { $query = $this->graphql->query() ->field('products', ['first' => 10], ['id', 'title']) ->toGraphQL(); // Execute query... } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
If you discover a security vulnerability, please send an email to einar@secundo.com. All security vulnerabilities will be promptly addressed.
Credits
License
The MIT License (MIT). Please see License File for more information.
secundo/laravel-graphql-query-builder 适用场景与选型建议
secundo/laravel-graphql-query-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.23k 次下载、GitHub Stars 达 2, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「client」 「shopify」 「laravel」 「query-builder」 「graphql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 secundo/laravel-graphql-query-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 secundo/laravel-graphql-query-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 secundo/laravel-graphql-query-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Shopify package for Laravel to aide in app development
A basic Shopify API wrapper with REST and GraphQL support.
A set of APIs to retrieve data from Shopify
统计信息
- 总下载量: 3.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知