Skip to content

Commit ed7155e

Browse files
committed
Implement a Union return type example
A simple example of a union return type, containing both BlogType as AuthorType.
1 parent b13c5b3 commit ed7155e

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog;
6+
7+
use Exception;
8+
9+
final class BlogNotFoundException extends Exception {}

src/Domain/Blog/BlogRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
interface BlogRepository
1313
{
14+
/**
15+
* @throws BlogNotFoundException
16+
*/
1417
public function getById(string $blogId): Blog;
1518

1619
public function getBlogs(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Resolver\Query;
6+
7+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Author\AuthorRepository;
8+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\BlogNotFoundException;
9+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\BlogRepository;
10+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Type\AuthorType;
11+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Type\BlogType;
12+
use Jerowork\GraphqlAttributeSchema\Attribute\Query;
13+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
14+
15+
#[Autoconfigure(public: true)]
16+
final readonly class SearchQuery
17+
{
18+
public function __construct(
19+
private BlogRepository $blogRepository,
20+
private AuthorRepository $authorRepository,
21+
) {}
22+
23+
#[Query(description: 'Search for an author or blog')]
24+
public function search(string $id): AuthorType|BlogType
25+
{
26+
try {
27+
$blog = $this->blogRepository->getById($id);
28+
29+
return new BlogType($blog);
30+
} catch (BlogNotFoundException) {
31+
$author = $this->authorRepository->getById($id);
32+
33+
return new AuthorType($author);
34+
}
35+
}
36+
}

src/Infrastructure/Persistence/Blog/SQLite/SQLiteBlogRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Persistence\Blog\SQLite;
66

77
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\Blog;
8+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\BlogNotFoundException;
89
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\BlogRepository;
910
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\Blogs;
1011
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\BlogStatus;
@@ -55,6 +56,10 @@ public function getById(string $blogId): Blog
5556
/** @var BlogRowPayload $row */
5657
$row = $result->fetchArray(SQLITE3_ASSOC);
5758

59+
if (!$row) {
60+
throw new BlogNotFoundException('Blog not found');
61+
}
62+
5863
return SQLiteBlogFactory::createFromRow($row);
5964
}
6065

0 commit comments

Comments
 (0)