File tree Expand file tree Collapse file tree 4 files changed +53
-0
lines changed
Api/Http/GraphQL/Resolver/Query Expand file tree Collapse file tree 4 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 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 {}
Original file line number Diff line number Diff line change 1111 */
1212interface BlogRepository
1313{
14+ /**
15+ * @throws BlogNotFoundException
16+ */
1417 public function getById (string $ blogId ): Blog ;
1518
1619 public function getBlogs (
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55namespace Jerowork \ExampleApplicationGraphqlAttributeSchema \Infrastructure \Persistence \Blog \SQLite ;
66
77use Jerowork \ExampleApplicationGraphqlAttributeSchema \Domain \Blog \Blog ;
8+ use Jerowork \ExampleApplicationGraphqlAttributeSchema \Domain \Blog \BlogNotFoundException ;
89use Jerowork \ExampleApplicationGraphqlAttributeSchema \Domain \Blog \BlogRepository ;
910use Jerowork \ExampleApplicationGraphqlAttributeSchema \Domain \Blog \Blogs ;
1011use 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
You can’t perform that action at this time.
0 commit comments