Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: documentation for interface, union, enum and scalar #34

Merged
merged 1 commit into from
Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,138 @@ All resolver methods share the same signature:
method($source, $args, $context, $info)
```

#### Interface Types

When working with interfaces, you need Resolvers for your interfaces as well. Given this schema:

```graphql schema
interface Person {
firstName: String!
lastName: String!
}

type Customer implements Person {
firstName: String!
lastName: String!
email: String!
}

type Supplier implements Person {
firstName: String!
lastName: String!
phone: String
}
```

You need to configure a `Person` Resolver as well as Resolvers for the concrete implementations.

While the concrete Type Resolvers do not need any special attention, the `Person` Resolver implements the
`__resolveType` function returning the type names:

```php
<?php

namespace Your\Package\GraphQL\Resolver\Type;

use t3n\GraphQL\ResolverInterface;

class PersonResolver implements ResolverInterface
{
public function __resolveType($source): ?string
{
if ($source instanceof Customer) {
return 'Customer';
} elseif ($source instanceof Supplier) {
return 'Supplier';
}
return null;
}
}
```

#### Union Types

Unions are resolved the same way as interfaces. For the example above, the corresponding Schema looks like this:

```graphql schema
union Person = Customer | Supplier
```

It is resolved again with a `PersonResolver` implementing the `__resolveType` function.

#### Enum Types

Enums are resolved automatically. The Resolver method simply returns a string matching the Enum value. For the Schema:
```graphql schema
enum Status {
ACTIVE
INACTIVE
}

type Customer {
status: Status!
}
```

The `CustomerResolver` method returns the value as string:
```php
public function status(Customer $customer): string
{
return $customer->isActive() ? 'ACTIVE' : 'INACTIVE';
}
```

#### Scalar Types

Types at the leaves of a Json tree are defined by Scalars. Own Scalars are implemented by a Resolver
implementing the functions `serialize()`, `parseLiteral()` and `parseValue()`.

This example shows the implementation of a `DateTime` Scalar Type. For the given Schema definition:

```graphql schema
scalar DateTime
```

The `DateTimeResolver` looks the following when working with Unix timestamps:

```php
<?php

namespace Your\Package\GraphQL\Resolver\Type;

use DateTime;
use GraphQL\Language\AST\IntValueNode;
use GraphQL\Language\AST\Node;
use t3n\GraphQL\ResolverInterface;

class DateTimeResolver implements ResolverInterface
{
public function serialize(DateTime $value): ?int
{
return $value->getTimestamp();
}

public function parseLiteral(Node $ast): ?DateTime
{
if ($ast instanceof IntValueNode) {
$dateTime = new DateTime();
$dateTime->setTimestamp((int)$ast->value);
return $dateTime;
}
return null;
}

public function parseValue(int $value): DateTime
{
$dateTime = new DateTime();
$dateTime->setTimestamp($value);
return $dateTime;
}
}
```

You have to make the `DateTimeResolver` available again through one of the configuration options in the Settings.yaml.

### Context

The third argument in your Resolver method signature is the Context.
Expand Down