stagem/php-graphql-scalars
Composer 安装命令:
composer require stagem/php-graphql-scalars
包简介
PHP library of custom GraphQL Scalars for creating precise type-safe GraphQL schemas
README 文档
README
Custom GraphQL Scalars for creating precise type-safe GraphQL schemas.
Requirements
- PHP 7.2+
- PHP Webonyx GraphQL
Installation
-
using composer:
composer require stagem/php-graphql-scalars -
using other PSR-4 compliant autoloader: clone this project to where your included libraries are and point your autoloader to look for the "\Stagem\GraphQL" namespace in the "src" directory of this project
Available types
- DateType
- DateTimeType
- IterableType
- JsonType
- EmailType
- SimpleObjectType
DateType
A date string, such as 2012-12-31, compliant with the full-date format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
This scalar is a description of the date, as used for birthdays for example. It cannot represent an instant on the time-line.
DateTimeType
The DateTime scalar type represents time data, represented as an ISO-8601 encoded UTC date string.
Result Coercion
PHP DateTime or DateTimeImmutable instances are coerced to a DateTimeInterface::ATOM (RFC 3339) compliant date string.
Input Coercion
When expected as an input type, most of valid ISO-8601 compliant date strings are accepted.
All next formats are valid and will be successfully parsed:
- "2010-12-07T23:00:00.000Z"
- "2010-12-07T23:00:00"
- "2010-12-07T23:00:00Z"
- "2010-12-07T23:00:00+01:00"
IterableType & JsonType
Any valid JSON format.
SimpleObjectType
Any valid PHP object which can be converted to array. Additionally support asArray and toArray methods for conversion.
Usage
Simply create DateTimeType and use it in your GraphQl Schema.
Remember any type must be unique.
<?php use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\ObjectType; use Stagem\GraphQL\Type\DateTimeType; $dateTimeType = new DateTimeType(); $queryType = new ObjectType([ 'name' => 'Query', 'fields' => [ 'echoDate' => [ 'type' => Type::string(), 'args' => [ 'date' => ['type' => $dateTimeType], ], 'resolve' => function ($root, $args) { return $args['date']; } ], ], ]);
Advanced usage It is good practice use a PSR-11 container for getting types. In the following example, we use zendframework/zend-servicemanager, because it offers useful concepts such as: invokables, aliases, factories and abstract factories. But any other PSR-11 container implementation could be used instead.
The keys should be the whatever you use to refer to the type in your model. Typically that would be either the FQCN of a PHP class "native" type such as DateTime, or the FQCN of a PHP class implementing the GraphQL type, or directly the GraphQL type name:
<?php use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\ObjectType; use Stagem\GraphQL\Type\DateTimeType; use Stagem\GraphQL\Type\JsonType; $customTypes = new \Zend\ServiceManager\ServiceManager([ 'invokables' => [ DateTime::class => DateTimeType::class, 'Json' => JsonType::class, ], ]); $queryType = new ObjectType([ 'name' => 'Query', 'fields' => [ 'echoDate' => [ 'type' => Type::string(), 'args' => [ 'date' => ['type' => $customTypes->get(DateTime::class)], ], 'resolve' => function ($root, $args) { return $args['date']; } ], ], ]);
That way it is not necessary to annotate every single getter returning one of the configured type. It will be mapped automatically.
Contributing
Please feel free to report bugs or request features using the Issues tab. If you'd like to contribute, feel free to fork our repository and send a pull request with your modifications. Let's make it better together!
History
When we started use GraphGL it was strange that nobody create library with useful custom GraphQL types.
Googling around we found many questions with proposals (for example one, two) to include such types in GraphQL but in all cases it was rejected.
Other libraries create own internal types just copying it from each other.
After that, we have decided to correct this injustice and created this library that every one can use it without copy-past.
Our inspiration was taken from okgrow/graphql-scalars but it is not port of that library.
stagem/php-graphql-scalars 适用场景与选型建议
stagem/php-graphql-scalars 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.86k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「scalars」 「graphql」 「custom types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 stagem/php-graphql-scalars 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stagem/php-graphql-scalars 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 stagem/php-graphql-scalars 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
GraphQL authentication for your headless Craft CMS applications.
Authorize introspection documentarion for rebing/graphql-laravel
Sylius backend integration for Vue Storefront 2
Multiauth and custom grants for laravel passport
GraphQL client and query builder.
Custom SilverStripe tweaks to keep things tidy.
统计信息
- 总下载量: 23.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04