Skip to content

Commit

Permalink
Update documentation with updated interface handling
Browse files Browse the repository at this point in the history
Including:
- [BC] Use `#[InterfaceType]` to define interfaces
- Allow defining interfaces on classes (optionally abstract)
- Allow cascading implemented and extended interfaces
  • Loading branch information
jerowork committed Feb 13, 2025
1 parent efd8e4a commit ecb3fdc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This library is still work in progress:
- ~~Inject autowiring services~~
- ~~Connection, edge, nodes (see https://relay.dev/graphql/connections.htm)~~
- ~~GraphQL interfaces, inheritance~~
- ~~Interfaces extending interfaces (see https://graphql.org/learn/schema/#interface-types)~~
- Subscriptions
- Interfaces extending interfaces (see https://graphql.org/learn/schema/#interface-types)
- Union types (see https://graphql.org/learn/schema/#union-types)
- Directives (see https://graphql.org/learn/schema/#directives)
58 changes: 56 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ final readonly class YourType

#### Inheritance and Interfaces

GraphQL supports inheritance using interfaces. To configure an interface, simply add `#[Type]` to a PHP interface:
GraphQL supports inheritance using interfaces. To configure an interface, simply add `#[InterfaceType]` to a PHP interface or (abstract) class:

```php
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
use Jerowork\GraphqlAttributeSchema\Attribute\Type;

#[Type]
// Define an InterfaceType on interface
#[InterfaceType]
interface UserType
{
// With PHP 8.4, you can define fields using property hooks
Expand All @@ -122,6 +123,19 @@ interface UserType
#[Field]
public function getName(): ?string;
}

// Or define an InterfaceType on (abstract) class
#[InterfaceType]
abstract readonly class AbstractUserType
{
public function __construct(
#[Field]
public string $id,
) {}

#[Field]
abstract public function getName(): ?string;
}
```

Each implementation inherits all fields from the interface, in addition to its own fields:
Expand All @@ -147,6 +161,46 @@ final readonly class AgentType implements UserType
}
```

Other than a single `#[Type]` implementing one interface `#[InterfaceType]` as in the example above,
*GraphQL Attribute Schema* also supports multiple interfaces:

```php
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
use Jerowork\GraphqlAttributeSchema\Attribute\InterfaceType;

#[InterfaceType]
interface FooType
{
#[Field]
public function getFoo() : string;
}

#[InterfaceType]
interface BarType extends FooType
{
#[Field]
public function getBar() : string;
}

#[InterfaceType]
abstract readonly class AbstractBazType implements FooType
{
public function __construct(
#[Field]
public string $id,
) {}

#[Field]
abstract public function getBaz() : string;
}

#[Type]
final readonly class QuxType extends AbstractBazType
{
// Implement all required logic from interfaces/extends
}
```

#### Automatic schema creation

*GraphQL Attribute Schema* automatically reads the `__construct` signature and detects input arguments,
Expand Down

0 comments on commit ecb3fdc

Please sign in to comment.