mll-lab / graphql-php-scalars
一组用于https://github.com/webonyx/graphql-php的定制标量类型
Requires
- php: ^8
- ext-json: *
- egulias/email-validator: ^2.1.17 || ^3 || ^4
- spatie/regex: ^1.4 || ^2 || ^3
- thecodingmachine/safe: ^1.3 || ^2
- webonyx/graphql-php: ^15
Requires (Dev)
- ergebnis/composer-normalize: ^2.16
- mll-lab/php-cs-fixer-config: ^5
- phpstan/extension-installer: ^1
- phpstan/phpstan: ^1
- phpstan/phpstan-deprecation-rules: ^1
- phpstan/phpstan-phpunit: ^1
- phpstan/phpstan-strict-rules: ^1
- phpunit/phpunit: ^9 || ^10
- symfony/var-dumper: ^5.4 || ^6
- thecodingmachine/phpstan-safe-rule: ^1.1
README
一组用于https://github.com/webonyx/graphql-php的定制标量类型
安装
composer require mll-lab/graphql-php-scalars
使用
您可以使用提供的标量类型,就像在模式定义中使用其他类型一样。查看SchemaUsageTest以获取示例。
BigInt
表示大整数的任意长数字序列。
Date
日期字符串,格式为Y-m-d
,例如2011-05-23
。
以下转换适用于所有日期标量
- 输出值可以是有效的日期字符串或
\DateTimeInterface
实例。 - 输入值必须是有效的日期字符串,并将转换为
\DateTimeImmutable
实例。
DateTime
日期时间字符串,格式为Y-m-d H:i:s
,例如2018-05-23 13:43:32
。
DateTimeTz
日期时间字符串,格式为Y-m-d\TH:i:s.uP
,例如2020-04-20T16:20:04+04:00
,2020-04-20T16:20:04Z
。
符合RFC 5321的电子邮件。
IntRange
允许定义数值标量,其中值必须在定义的最小值和最大值之间。
use MLL\GraphQLScalars\IntRange; final class UpToADozen extends IntRange { protected static function min(): int { return 1; } protected static function max(): int { return 12; } }
JSON
以JavaScript对象表示法编码的任意数据。请参阅https://www.json.org。
这期望一个JSON格式的字符串,而不是任意JSON值或GraphQL文字。
type Query { foo(bar: JSON!): JSON! } # Wrong, the given value is a GraphQL literal object { foo(bar: { baz: 2 }) } # Correct, the given value is a JSON string representing an object { foo(bar: "{ \"bar\": 2 }") }
// Wrong, the variable value is a JSON object { "query": "query ($bar: JSON!) { foo(bar: $bar) }", "variables": { "bar": { "baz": 2 } } } // Correct, the variable value is a JSON string representing an object { "query": "query ($bar: JSON!) { foo(bar: $bar) }", "variables": { "bar": "{ \"bar\": 2 }" } }
JSON响应将包含嵌套的JSON字符串。
{ "data": { "foo": "{ \"bar\": 2 }" } }
Mixed
松散类型,允许任何值。在传递大的Int
或Float
文字时要小心,因为它们可能在服务器端无法正确解析。如果您处理的是非常大的数字,请使用String
文字以确保安全。
Null
总是null
。严格验证值是否非空,不进行强制转换。
Regex
Regex
类允许您定义一个自定义标量,该标量验证给定的值是否与正则表达式匹配。
定义自定义标量最快的方法是make
工厂方法。只需提供一个名称和一个正则表达式,您将收到一个可立即使用的自定义正则表达式标量。
use MLL\GraphQLScalars\Regex; $hexValue = Regex::make( 'HexValue', 'A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue) are hexadecimal integers between `00` and `FF` specifying the intensity of the color.', '/^#?([a-f0-9]{6}|[a-f0-9]{3})$/' );
您还可以将正则表达式标量定义为类。
use MLL\GraphQLScalars\Regex; // The name is implicitly set through the class name here class HexValue extends Regex { /** The description that is used for schema introspection. */ public ?string $description = /** @lang Markdown */<<<'MARKDOWN' A hexadecimal color is specified with: `#RRGGBB`, where `RR` (red), `GG` (green) and `BB` (blue) are hexadecimal integers between `00` and `FF` specifying the intensity of the color. MARKDOWN; public static function regex(): string { return '/^#?([a-f0-9]{6}|[a-f0-9]{3})$/'; } }
StringScalar
StringScalar
封装了创建基于字符串的标量类型所需的所有样板代码。它执行基本的检查和强制转换,您可以将注意力集中在您用例的特定逻辑上。
您必须指定一个检查给定字符串是否有效的函数。使用工厂方法make
在运行时生成实例。
use MLL\GraphQLScalars\StringScalar; $coolName = StringScalar::make( 'CoolName', 'A name that is most definitely cool.', static fn (string $name): bool => in_array($name, [ 'Vladar', 'Benedikt', 'Christopher', ]), );
或者,您可以简单地扩展该类,查看Email标量的实现以了解如何进行。