compwright/graphql-php-scalars

此包已被弃用且不再维护。作者建议使用compwright/graphql-php-jetpack包。

用于与https://github.com/webonyx/graphql-php一起使用的自定义标量类型的集合

v1.1.0 2024-01-30 22:43 UTC

This package is auto-updated.

Last update: 2024-01-31 22:48:55 UTC


README

用于与https://github.com/webonyx/graphql-php一起使用的自定义标量类型的集合

Validate GitHub license Packagist Packagist

这是mll-labs/graphql-php-scalars的分支,支持PHP 7.4并消除了几个不必要的第三方依赖。因此,电子邮件验证将稍微严格一些(我们依赖于内置的PHP filter_var())。

安装

composer require compwright/graphql-php-scalars

使用

您可以使用提供的Scalars,就像在模式定义中使用任何其他类型一样。请查看SchemaUsageTest以获取示例。

如果使用SDL,您可以使用包含的ScalarDirectiveDecorator来加载所需的自定义标量类并将其附加到自定义标量类型。

您的模式中需要以下内容

# used to specify the desired class to execute for a custom scalar
directive @scalar(class: String!) on SCALAR

# add the directive to each custom scalar
scalar Email @scalar(class: "Compwright\\GraphqlScalars\\Email")

然后,在加载模式时,将ScalarDirectiveDecorator的实例作为$ typeConfigDecorator参数传递给BuildSchema::build()

BuildSchema::build($ast, new ScalarDirectiveDecorator());

如果需要多个装饰器来实现其他各种功能,它们可以使用类似https://github.com/thephpleague/pipeline的东西串联起来。

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:002020-04-20T16:20:04Z

Email

符合RFC 5321的电子邮件。

JSON

使用JavaScript对象表示法编码的任意数据。请参阅 https://www.json.org

这期望的是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 }")
}

JSON响应将包含嵌套的JSON字符串。

{
  "data": {
    "foo": "{ \"bar\": 2 }"
  }
}

Mixed

宽松的类型,允许任何值。在传递大的 IntFloat 字面量时请小心,因为它们在服务器端可能无法正确解析。如果处理的是非常大的数字,请使用 String 字面量以确保安全。

Null

始终为 null。严格验证值是否非空,不进行强制转换。

Regex

Regex 类允许你定义一个自定义标量,用于验证给定的值是否匹配正则表达式。

定义自定义标量最快的方法是使用 make 工厂方法。只需提供一个名称和一个正则表达式,你将获得一个可用于的现成自定义正则表达式标量。

use Compwright\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 Compwright\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 = <<<'DESCRIPTION'
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.
DESCRIPTION;

    public static function regex(): string
    {
        return '/^#?([a-f0-9]{6}|[a-f0-9]{3})$/';
    }
}

StringScalar

StringScalar 封装了创建基于字符串的标量类型所涉及的所有样板代码。它执行基本的检查和强制转换,你可以专注于特定于你的用例的最小逻辑。

你需要指定一个检查给定字符串是否有效的函数。使用工厂方法 make 生成实例。

use Compwright\GraphqlScalars\StringScalar;

$coolName = StringScalar::make(
    'CoolName',
    'A name that is most definitely cool.',
    static function (string $name): bool {
        return in_array($name, [
           'Vladar',
           'Benedikt',
           'Christopher',
        ]);
    }
);

或者,你可以简单地扩展该类,查看 Email 标量的实现以了解如何进行。