Skip to content

Commit fc6723f

Browse files
authored
Blog01 stanning (#1285)
1 parent a691c37 commit fc6723f

File tree

7 files changed

+111
-295
lines changed

7 files changed

+111
-295
lines changed

examples/01-blog/Blog/Type/CommentType.php

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use GraphQL\Examples\Blog\Types;
1010
use GraphQL\Type\Definition\ListOfType;
1111
use GraphQL\Type\Definition\ObjectType;
12-
use GraphQL\Type\Definition\ResolveInfo;
1312

1413
class CommentType extends ObjectType
1514
{
@@ -19,8 +18,18 @@ public function __construct()
1918
'name' => 'Comment',
2019
'fields' => static fn (): array => [
2120
'id' => Types::id(),
22-
'author' => Types::user(),
23-
'parent' => Types::comment(),
21+
'author' => [
22+
'type' => Types::user(),
23+
'resolve' => static fn (Comment $comment): ?User => $comment->isAnonymous
24+
? null
25+
: DataSource::findUser($comment->authorId),
26+
],
27+
'parent' => [
28+
'type' => Types::comment(),
29+
'resolve' => static fn (Comment $comment): ?Comment => $comment->parentId !== null
30+
? DataSource::findComment($comment->parentId)
31+
: null,
32+
],
2433
'isAnonymous' => Types::boolean(),
2534
'replies' => [
2635
'type' => new ListOfType(Types::comment()),
@@ -31,56 +40,17 @@ public function __construct()
3140
'defaultValue' => 5,
3241
],
3342
],
43+
'resolve' => fn (Comment $comment, array $args): array => DataSource::findReplies($comment->id, $args['limit'], $args['after'] ?? null),
44+
],
45+
'totalReplyCount' => [
46+
'type' => Types::int(),
47+
'resolve' => static fn (Comment $comment): int => DataSource::countReplies($comment->id),
3448
],
35-
'totalReplyCount' => Types::int(),
3649

37-
'body' => HtmlField::build('body'),
50+
'body' => HtmlField::build([
51+
'resolve' => static fn (Comment $comment): string => $comment->body,
52+
]),
3853
],
39-
'resolveField' => function (Comment $comment, array $args, $context, ResolveInfo $info) {
40-
$fieldName = $info->fieldName;
41-
42-
$method = 'resolve' . \ucfirst($fieldName);
43-
if (\method_exists($this, $method)) {
44-
return $this->{$method}($comment, $args, $context, $info);
45-
}
46-
47-
return $comment->{$fieldName};
48-
},
4954
]);
5055
}
51-
52-
public function resolveAuthor(Comment $comment): ?User
53-
{
54-
if ($comment->isAnonymous) {
55-
return null;
56-
}
57-
58-
return DataSource::findUser($comment->authorId);
59-
}
60-
61-
public function resolveParent(Comment $comment): ?Comment
62-
{
63-
if ($comment->parentId !== null) {
64-
return DataSource::findComment($comment->parentId);
65-
}
66-
67-
return null;
68-
}
69-
70-
/**
71-
* @param array{limit: int, after?: int} $args
72-
*
73-
* @return array<int, Comment>
74-
*/
75-
public function resolveReplies(Comment $comment, array $args): array
76-
{
77-
$args += ['after' => null];
78-
79-
return DataSource::findReplies($comment->id, $args['limit'], $args['after']);
80-
}
81-
82-
public function resolveTotalReplyCount(Comment $comment): int
83-
{
84-
return DataSource::countReplies($comment->id);
85-
}
8656
}

examples/01-blog/Blog/Type/Field/HtmlField.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
class HtmlField
99
{
1010
/**
11-
* @return array<string, mixed>
11+
* @param array{
12+
* resolve: callable
13+
* } $config
14+
*
15+
* @return array<mixed>
1216
*/
13-
public static function build(string $objectKey): array
17+
public static function build(array $config): array
1418
{
19+
$resolver = $config['resolve'];
20+
1521
// Demonstrates how to organize re-usable fields
1622
// Usual example: when the same field with same args shows up in different types
1723
// (for example when it is a part of some interface)
@@ -24,8 +30,8 @@ public static function build(string $objectKey): array
2430
],
2531
'maxLength' => Types::int(),
2632
],
27-
'resolve' => static function ($object, $args) use ($objectKey) {
28-
$html = $object->{$objectKey};
33+
'resolve' => static function ($rootValue, array $args) use ($resolver): ?string {
34+
$html = $resolver($rootValue, $args);
2935
$text = \strip_tags($html);
3036

3137
if (isset($args['maxLength'])) {

examples/01-blog/Blog/Type/QueryType.php

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
namespace GraphQL\Examples\Blog\Type;
44

5-
use GraphQL\Examples\Blog\AppContext;
65
use GraphQL\Examples\Blog\Data\DataSource;
76
use GraphQL\Examples\Blog\Data\Story;
87
use GraphQL\Examples\Blog\Data\User;
98
use GraphQL\Examples\Blog\Types;
109
use GraphQL\Type\Definition\ListOfType;
1110
use GraphQL\Type\Definition\NonNull;
1211
use GraphQL\Type\Definition\ObjectType;
13-
use GraphQL\Type\Definition\ResolveInfo;
1412
use GraphQL\Type\Definition\Type;
1513

1614
class QueryType extends ObjectType
@@ -26,6 +24,7 @@ public function __construct()
2624
'args' => [
2725
'id' => new NonNull(Types::id()),
2826
],
27+
'resolve' => static fn ($rootValue, array $args): ?User => DataSource::findUser((int) $args['id']),
2928
],
3029
'viewer' => [
3130
'type' => Types::user(),
@@ -45,73 +44,34 @@ public function __construct()
4544
'defaultValue' => 10,
4645
],
4746
],
47+
'resolve' => static fn ($rootValue, $args): array => DataSource::findStories(
48+
$args['limit'],
49+
isset($args['after'])
50+
? (int) $args['after']
51+
: null
52+
),
4853
],
4954
'lastStoryPosted' => [
5055
'type' => Types::story(),
5156
'description' => 'Returns last story posted for this blog',
57+
'resolve' => static fn (): ?Story => DataSource::findLatestStory(),
5258
],
5359
'deprecatedField' => [
5460
'type' => Types::string(),
5561
'deprecationReason' => 'This field is deprecated!',
62+
'resolve' => static fn (): string => 'You can request deprecated field, but it is not displayed in auto-generated documentation by default.',
5663
],
5764
'fieldWithException' => [
5865
'type' => Types::string(),
5966
'resolve' => static function (): void {
6067
throw new \Exception('Exception message thrown in field resolver');
6168
},
6269
],
63-
'hello' => Type::string(),
70+
'hello' => [
71+
'type' => Type::string(),
72+
'resolve' => static fn (): string => 'Your graphql-php endpoint is ready! Use a GraphQL client to explore the schema.',
73+
],
6474
],
65-
'resolveField' => fn ($rootValue, array $args, $context, ResolveInfo $info) => $this->{$info->fieldName}($rootValue, $args, $context, $info),
6675
]);
6776
}
68-
69-
/**
70-
* @param null $rootValue
71-
* @param array{id: string} $args
72-
*/
73-
public function user($rootValue, array $args): ?User
74-
{
75-
return DataSource::findUser((int) $args['id']);
76-
}
77-
78-
/**
79-
* @param null $rootValue
80-
* @param array<never> $args
81-
*/
82-
public function viewer($rootValue, array $args, AppContext $context): User
83-
{
84-
return $context->viewer;
85-
}
86-
87-
/**
88-
* @param null $rootValue
89-
* @param array{limit: int, after?: string} $args
90-
*
91-
* @return array<int, Story>
92-
*/
93-
public function stories($rootValue, array $args): array
94-
{
95-
return DataSource::findStories(
96-
$args['limit'],
97-
isset($args['after'])
98-
? (int) $args['after']
99-
: null
100-
);
101-
}
102-
103-
public function lastStoryPosted(): ?Story
104-
{
105-
return DataSource::findLatestStory();
106-
}
107-
108-
public function hello(): string
109-
{
110-
return 'Your graphql-php endpoint is ready! Use a GraphQL client to explore the schema.';
111-
}
112-
113-
public function deprecatedField(): string
114-
{
115-
return 'You can request deprecated field, but it is not displayed in auto-generated documentation by default.';
116-
}
11777
}

0 commit comments

Comments
 (0)