Skip to content

Commit

Permalink
Merge pull request #34 from swisscomeventandmedia/task/doc-resolver-t…
Browse files Browse the repository at this point in the history
…ypes

TASK: documentation for interface, union, enum and scalar
  • Loading branch information
johannessteu authored Dec 30, 2021
2 parents 170c4c0 + 2fee9cc commit cee6ec9
Showing 1 changed file with 132 additions and 0 deletions.
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

0 comments on commit cee6ec9

Please sign in to comment.