PHPackages                             compwright/graphql-php-scalars - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. compwright/graphql-php-scalars

Abandoned → [compwright/graphql-php-jetpack](/?search=compwright%2Fgraphql-php-jetpack)ArchivedLibrary[API Development](/categories/api)

compwright/graphql-php-scalars
==============================

A collection of custom scalar types for usage with https://github.com/webonyx/graphql-php

v1.1.0(2y ago)027MITPHPPHP ^7.4 || ^8

Since Jan 29Pushed 2y agoCompare

[ Source](https://github.com/compwright/graphql-php-scalars)[ Packagist](https://packagist.org/packages/compwright/graphql-php-scalars)[ GitHub Sponsors](https://github.com/compwright)[ RSS](/packages/compwright-graphql-php-scalars/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (4)Used By (0)

graphql-php-scalars
===================

[](#graphql-php-scalars)

A collection of custom scalar types for usage with

[![Validate](https://github.com/compwright/graphql-php-scalars/actions/workflows/validate.yml/badge.svg)](https://github.com/compwright/graphql-php-scalars/actions/workflows/validate.yml)[![GitHub license](https://camo.githubusercontent.com/4499316c80f9a5fb933952d31027aa8b0896a52a8e6c7df9aad8d174eead2721/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636f6d707772696768742f6772617068716c2d7068702d7363616c6172732e737667)](https://github.com/compwright/graphql-php-scalars/blob/master/LICENSE)[![Packagist](https://camo.githubusercontent.com/6dab0f81f1ea4ed4aba34196d2ac65cb5070bdd90766a21698c4e1d6506642a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6d707772696768742f6772617068716c2d7068702d7363616c6172732e737667)](https://packagist.org/packages/compwright/graphql-php-scalars)[![Packagist](https://camo.githubusercontent.com/5c44c05e0bceaa158b11e660fe643bd81223b106483133f27f0a45bd356dc203/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6d707772696768742f6772617068716c2d7068702d7363616c6172732e737667)](https://packagist.org/packages/compwright/graphql-php-scalars)

> This is a fork of mll-labs/graphql-php-scalars that supports PHP 7.4 and eliminates several unnecessary third party dependencies. As a result, email validation will be slightly stricter (we rely on the built-in PHP filter\_var()).

Installation
------------

[](#installation)

```
composer require compwright/graphql-php-scalars

```

Usage
-----

[](#usage)

You can use the provided Scalars just like any other type in your schema definition. Check [SchemaUsageTest](tests/SchemaUsageTest.php) for an example.

If using the SDL, you can use the included ScalarDirectiveDecorator to load your desired custom scalar class and attach it to a custom scalar type.

You'll need this in your schema:

```
# 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")
```

Then when loading the schema, pass an instance of ScalarDirectiveDecorator as the $typeConfigDecorator argument to BuildSchema::build():

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

If multiple decorators are needed to implement various other things, they could be pipelined together using something like [https://github.com/thephpleague/pipeline](league%5Cpipeline).

### [BigInt](src/BigInt.php)

[](#bigint)

An arbitrarily long sequence of digits that represents a big integer.

### [Date](src/Date.php)

[](#date)

A date string with format `Y-m-d`, e.g. `2011-05-23`.

The following conversion applies to all date scalars:

- Outgoing values can either be valid date strings or `\DateTimeInterface` instances.
- Incoming values must always be valid date strings and will be converted to `\DateTimeImmutable` instances.

### [DateTime](src/DateTime.php)

[](#datetime)

A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`.

### [DateTimeTz](src/DateTimeTz.php)

[](#datetimetz)

A datetime string with format `Y-m-d\TH:i:s.uP`, e.g. `2020-04-20T16:20:04+04:00`, `2020-04-20T16:20:04Z`.

### [Email](src/Email.php)

[](#email)

A [RFC 5321](https://tools.ietf.org/html/rfc5321) compliant email.

### [JSON](src/JSON.php)

[](#json)

Arbitrary data encoded in JavaScript Object Notation. See .

This expects a string in JSON format, not a GraphQL literal.

```
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 responses will contain nested JSON strings.

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

### [Mixed](src/MixedScalar.php)

[](#mixed)

Loose type that allows any value. Be careful when passing in large `Int` or `Float` literals, as they may not be parsed correctly on the server side. Use `String` literals if you are dealing with really large numbers to be on the safe side.

### [Null](src/NullScalar.php)

[](#null)

Always `null`. Strictly validates value is non-null, no coercion.

### [Regex](src/Regex.php)

[](#regex)

The `Regex` class allows you to define a custom scalar that validates that the given value matches a regular expression.

The quickest way to define a custom scalar is the `make` factory method. Just provide a name and a regular expression, you will receive a ready-to-use custom regex scalar.

```
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})$/'
);
```

You may also define your regex scalar as a class.

```
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 =
