Skip to content

Commit

Permalink
Update DefiningSchema.md
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax authored Mar 22, 2024
1 parent f4b0112 commit 0fb94b4
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions docs/DefiningSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Lets jump stright into examples for each kind.
> \Graphpinator\Typesystem\Type
```graphql
# My Starship type
type Starship {
id: ID!
name: String!
length(unit: LengthUnit! = METER): Float
}
```

```php
<?php declare(strict_types = 1);

Expand Down Expand Up @@ -137,14 +137,14 @@ The contract of the interface must be satisfied, the variance rules apply on bot
> \Graphpinator\Typesystem\InterfaceType
```graphql
# My Character interface
interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}
```

```php
<?php declare(strict_types = 1);

Expand Down Expand Up @@ -219,9 +219,9 @@ In this case the fields from parent interface are automatically included and the
> \Graphpinator\Typesystem\UnionType
```graphql
# My SearchResult union
union SearchResult = Human | Droid | Starship
```

```php
<?php declare(strict_types = 1);

Expand Down Expand Up @@ -267,6 +267,44 @@ In similar fashion as for the `Interface`, the `createResolvedValue` function mu

### Scalar

> \Graphpinator\Typesystem\ScalarType
```graphql
# EmailAddress type - string which contains valid email address.
scalar EmailAddress @specifiedBy(url: "https://datatracker.ietf.org/doc/html/rfc5322#section-3.4.1")
```
```php
<?php declare(strict_types = 1);

namespace App\Type;

use Graphpinator\Typesystem\Attribute\Description;
use Graphpinator\Typesystem\ScalarType;

#[Desctiption('EmailAddress type - string which contains valid email address.')]
final class EmailAddressType extends ScalarType
{
protected const NAME = 'EmailAddress';

public function __construct()
{
parent::__construct();

$this->setSpecifiedBy('https://datatracker.ietf.org/doc/html/rfc5322#section-3.4.1');
}

public function validateNonNullValue(mixed $rawValue) : bool
{
return \is_string($rawValue)
&& (bool) \filter_var($rawValue, \FILTER_VALIDATE_EMAIL);
}
}
```

The `validateNonNullValue` works the same way as in `Type` - when the function returns `false` an `InvalidValue` is thrown. This can be used to restrict the value of this scalar to a valid email address.

This example is taken from the [extra-types package](https://github.com/graphpql/graphpinator-extra-types) which includes some useful types out of the scope of the official spceficition.

### Enum

### Input
Expand Down

0 comments on commit 0fb94b4

Please sign in to comment.