Skip to content

Commit 6cd6658

Browse files
committed
Implement an interface type: Content: Blog
1 parent 01bb04f commit 6cd6658

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Blog\BlogRepository;
8+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Type\BlogType;
9+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Type\ContentType;
10+
use Jerowork\GraphqlAttributeSchema\Attribute\Query;
11+
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
12+
13+
#[Autoconfigure(public: true)]
14+
final readonly class ContentQuery
15+
{
16+
public function __construct(
17+
private BlogRepository $blogRepository,
18+
) {}
19+
20+
#[Query]
21+
public function content(string $id): ContentType
22+
{
23+
// Other types can be added here, e.g. a page type
24+
$blog = $this->blogRepository->getById($id);
25+
26+
return new BlogType($blog);
27+
}
28+
}

src/Infrastructure/Api/Http/GraphQL/Type/BlogType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use DateTimeImmutable;
88
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Domain\Blog\Blog;
9-
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Loader\AuthorTypeLoader;
109
use Jerowork\GraphqlAttributeSchema\Attribute\Cursor;
1110
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
1211
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ListType;
@@ -16,7 +15,7 @@
1615

1716
#[Exclude]
1817
#[Type]
19-
final readonly class BlogType
18+
final readonly class BlogType implements ContentType
2019
{
2120
public function __construct(
2221
public Blog $blog,
@@ -41,7 +40,6 @@ public function getTitle(): string
4140
return $this->blog->title;
4241
}
4342

44-
#[Field(type: AuthorType::class, deferredTypeLoader: AuthorTypeLoader::class)]
4543
public function getAuthor(): string
4644
{
4745
return $this->blog->authorId;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Type;
6+
7+
use Jerowork\ExampleApplicationGraphqlAttributeSchema\Infrastructure\Api\Http\GraphQL\Loader\AuthorTypeLoader;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Field;
9+
use Jerowork\GraphqlAttributeSchema\Attribute\InterfaceType;
10+
11+
#[InterfaceType]
12+
interface ContentType
13+
{
14+
#[Field(type: AuthorType::class, deferredTypeLoader: AuthorTypeLoader::class)]
15+
public function getAuthor(): string;
16+
}

0 commit comments

Comments
 (0)