Skip to content

Commit a525e5b

Browse files
authored
Simplify blog example TypeRegistry
1 parent b1d5956 commit a525e5b

File tree

22 files changed

+130
-271
lines changed

22 files changed

+130
-271
lines changed

examples/00-hello-world/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ php -S localhost:8080 graphql.php
1212
### Try query
1313

1414
```
15-
curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080
15+
curl --data '{"query": "query { echo(message: \"Hello World\") }" }' --header "Content-Type: application/json" http://localhost:8080
1616
```
1717

1818
### Try mutation
1919

2020
```
21-
curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080
21+
curl --data '{"query": "mutation { sum(x: 2, y: 2) }" }' --header "Content-Type: application/json" http://localhost:8080
2222
```

examples/00-hello-world/graphql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
// php -S localhost:8080 graphql.php
55

66
// Try query
7-
// curl -d '{"query": "query { echo(message: \"Hello World\") }" }' -H "Content-Type: application/json" http://localhost:8080
7+
// curl --data '{"query": "query { echo(message: \"Hello World\") }" }' --header "Content-Type: application/json" http://localhost:8080
88

99
// Try mutation
10-
// curl -d '{"query": "mutation { sum(x: 2, y: 2) }" }' -H "Content-Type: application/json" http://localhost:8080
10+
// curl --data '{"query": "mutation { sum(x: 2, y: 2) }" }' --header "Content-Type: application/json" http://localhost:8080
1111

1212
require_once __DIR__ . '/../../vendor/autoload.php';
1313

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use GraphQL\Examples\Blog\Data\DataSource;
77
use GraphQL\Examples\Blog\Data\User;
88
use GraphQL\Examples\Blog\Type\Field\HtmlField;
9-
use GraphQL\Examples\Blog\Types;
9+
use GraphQL\Examples\Blog\TypeRegistry;
1010
use GraphQL\Type\Definition\ListOfType;
1111
use GraphQL\Type\Definition\ObjectType;
12+
use GraphQL\Type\Definition\Type;
1213

1314
class CommentType extends ObjectType
1415
{
@@ -17,33 +18,33 @@ public function __construct()
1718
parent::__construct([
1819
'name' => 'Comment',
1920
'fields' => static fn (): array => [
20-
'id' => Types::id(),
21+
'id' => Type::id(),
2122
'author' => [
22-
'type' => Types::user(),
23+
'type' => TypeRegistry::type(UserType::class),
2324
'resolve' => static fn (Comment $comment): ?User => $comment->isAnonymous
2425
? null
2526
: DataSource::findUser($comment->authorId),
2627
],
2728
'parent' => [
28-
'type' => Types::comment(),
29+
'type' => TypeRegistry::type(CommentType::class),
2930
'resolve' => static fn (Comment $comment): ?Comment => $comment->parentId !== null
3031
? DataSource::findComment($comment->parentId)
3132
: null,
3233
],
33-
'isAnonymous' => Types::boolean(),
34+
'isAnonymous' => Type::boolean(),
3435
'replies' => [
35-
'type' => new ListOfType(Types::comment()),
36+
'type' => new ListOfType(TypeRegistry::type(CommentType::class)),
3637
'args' => [
37-
'after' => Types::int(),
38+
'after' => Type::int(),
3839
'limit' => [
39-
'type' => Types::int(),
40+
'type' => Type::int(),
4041
'defaultValue' => 5,
4142
],
4243
],
4344
'resolve' => fn (Comment $comment, array $args): array => DataSource::findReplies($comment->id, $args['limit'], $args['after'] ?? null),
4445
],
4546
'totalReplyCount' => [
46-
'type' => Types::int(),
47+
'type' => Type::int(),
4748
'resolve' => static fn (Comment $comment): int => DataSource::countReplies($comment->id),
4849
],
4950

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use GraphQL\Error\InvariantViolation;
66
use GraphQL\Examples\Blog\Type\Enum\ContentFormatType;
7-
use GraphQL\Examples\Blog\Types;
7+
use GraphQL\Examples\Blog\TypeRegistry;
8+
use GraphQL\Type\Definition\Type;
89

910
class HtmlField
1011
{
@@ -25,13 +26,13 @@ public static function build(array $config): array
2526
// Usual example: when the same field with same args shows up in different types
2627
// (for example when it is a part of some interface)
2728
return [
28-
'type' => Types::string(),
29+
'type' => Type::string(),
2930
'args' => [
3031
'format' => [
31-
'type' => Types::contentFormat(),
32+
'type' => TypeRegistry::type(ContentFormatType::class),
3233
'defaultValue' => ContentFormatType::FORMAT_HTML,
3334
],
34-
'maxLength' => Types::int(),
35+
'maxLength' => Type::int(),
3536
],
3637
'resolve' => static function ($rootValue, array $args) use ($resolver): ?string {
3738
$html = $resolver($rootValue, $args);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use GraphQL\Examples\Blog\AppContext;
66
use GraphQL\Examples\Blog\Data\Image;
7-
use GraphQL\Examples\Blog\Types;
7+
use GraphQL\Examples\Blog\Type\Enum\ImageSizeType;
8+
use GraphQL\Examples\Blog\Type\Scalar\UrlType;
9+
use GraphQL\Examples\Blog\TypeRegistry;
810
use GraphQL\Type\Definition\NonNull;
911
use GraphQL\Type\Definition\ObjectType;
1012
use GraphQL\Type\Definition\ResolveInfo;
13+
use GraphQL\Type\Definition\Type;
1114

1215
class ImageType extends ObjectType
1316
{
@@ -16,24 +19,24 @@ public function __construct()
1619
parent::__construct([
1720
'name' => 'Image',
1821
'fields' => [
19-
'id' => Types::id(),
20-
'size' => Types::imageSize(),
21-
'width' => Types::int(),
22-
'height' => Types::int(),
22+
'id' => Type::id(),
23+
'size' => TypeRegistry::type(ImageSizeType::class),
24+
'width' => Type::int(),
25+
'height' => Type::int(),
2326
'url' => [
24-
'type' => Types::url(),
27+
'type' => TypeRegistry::type(UrlType::class),
2528
'resolve' => [$this, 'resolveUrl'],
2629
],
2730

2831
// Just for the sake of example
2932
'fieldWithError' => [
30-
'type' => Types::string(),
33+
'type' => Type::string(),
3134
'resolve' => static function (): void {
3235
throw new \Exception('Field with exception');
3336
},
3437
],
3538
'nonNullFieldWithError' => [
36-
'type' => new NonNull(Types::string()),
39+
'type' => new NonNull(Type::string()),
3740
'resolve' => static function (): void {
3841
throw new \Exception('Non-null field with exception');
3942
},

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use GraphQL\Examples\Blog\Data\Image;
66
use GraphQL\Examples\Blog\Data\Story;
77
use GraphQL\Examples\Blog\Data\User;
8-
use GraphQL\Examples\Blog\Types;
8+
use GraphQL\Examples\Blog\TypeRegistry;
99
use GraphQL\Type\Definition\InterfaceType;
1010
use GraphQL\Type\Definition\ObjectType;
11+
use GraphQL\Type\Definition\Type;
1112
use GraphQL\Utils\Utils;
1213

1314
class NodeType extends InterfaceType
@@ -17,7 +18,7 @@ public function __construct()
1718
parent::__construct([
1819
'name' => 'Node',
1920
'fields' => [
20-
'id' => Types::id(),
21+
'id' => Type::id(),
2122
],
2223
'resolveType' => [$this, 'resolveNodeType'],
2324
]);
@@ -33,15 +34,15 @@ public function __construct()
3334
public function resolveNodeType($object)
3435
{
3536
if ($object instanceof User) {
36-
return Types::user();
37+
return TypeRegistry::type(UserType::class);
3738
}
3839

3940
if ($object instanceof Image) {
40-
return Types::image();
41+
return TypeRegistry::type(ImageType::class);
4142
}
4243

4344
if ($object instanceof Story) {
44-
return Types::story();
45+
return TypeRegistry::type(StoryType::class);
4546
}
4647

4748
$notNode = Utils::printSafe($object);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use GraphQL\Examples\Blog\Data\DataSource;
66
use GraphQL\Examples\Blog\Data\Story;
77
use GraphQL\Examples\Blog\Data\User;
8-
use GraphQL\Examples\Blog\Types;
8+
use GraphQL\Examples\Blog\TypeRegistry;
99
use GraphQL\Type\Definition\ListOfType;
1010
use GraphQL\Type\Definition\NonNull;
1111
use GraphQL\Type\Definition\ObjectType;
@@ -19,27 +19,27 @@ public function __construct()
1919
'name' => 'Query',
2020
'fields' => [
2121
'user' => [
22-
'type' => Types::user(),
22+
'type' => TypeRegistry::type(UserType::class),
2323
'description' => 'Returns user by id (in range of 1-5)',
2424
'args' => [
25-
'id' => new NonNull(Types::id()),
25+
'id' => new NonNull(Type::id()),
2626
],
2727
'resolve' => static fn ($rootValue, array $args): ?User => DataSource::findUser((int) $args['id']),
2828
],
2929
'viewer' => [
30-
'type' => Types::user(),
30+
'type' => TypeRegistry::type(UserType::class),
3131
'description' => 'Represents currently logged-in user (for the sake of example - simply returns user with id == 1)',
3232
],
3333
'stories' => [
34-
'type' => new ListOfType(Types::story()),
34+
'type' => new ListOfType(TypeRegistry::type(StoryType::class)),
3535
'description' => 'Returns subset of stories posted for this blog',
3636
'args' => [
3737
'after' => [
38-
'type' => Types::id(),
38+
'type' => Type::id(),
3939
'description' => 'Fetch stories listed after the story with this ID',
4040
],
4141
'limit' => [
42-
'type' => Types::int(),
42+
'type' => Type::int(),
4343
'description' => 'Number of stories to be returned',
4444
'defaultValue' => 10,
4545
],
@@ -52,17 +52,17 @@ public function __construct()
5252
),
5353
],
5454
'lastStoryPosted' => [
55-
'type' => Types::story(),
55+
'type' => TypeRegistry::type(StoryType::class),
5656
'description' => 'Returns last story posted for this blog',
5757
'resolve' => static fn (): ?Story => DataSource::findLatestStory(),
5858
],
5959
'deprecatedField' => [
60-
'type' => Types::string(),
60+
'type' => Type::string(),
6161
'deprecationReason' => 'This field is deprecated!',
6262
'resolve' => static fn (): string => 'You can request deprecated field, but it is not displayed in auto-generated documentation by default.',
6363
],
6464
'fieldWithException' => [
65-
'type' => Types::string(),
65+
'type' => Type::string(),
6666
'resolve' => static function (): void {
6767
throw new \Exception('Exception message thrown in field resolver');
6868
},

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use GraphQL\Examples\Blog\Data\Story;
66
use GraphQL\Examples\Blog\Data\User;
7-
use GraphQL\Examples\Blog\Types;
7+
use GraphQL\Examples\Blog\TypeRegistry;
88
use GraphQL\Type\Definition\UnionType;
99

1010
class SearchResultType extends UnionType
@@ -14,16 +14,16 @@ public function __construct()
1414
parent::__construct([
1515
'name' => 'SearchResult',
1616
'types' => static fn (): array => [
17-
Types::story(),
18-
Types::user(),
17+
TypeRegistry::type(StoryType::class),
18+
TypeRegistry::type(UserType::class),
1919
],
2020
'resolveType' => static function (object $value): callable {
2121
if ($value instanceof Story) {
22-
return Types::story();
22+
return TypeRegistry::type(StoryType::class);
2323
}
2424

2525
if ($value instanceof User) {
26-
return Types::user();
26+
return TypeRegistry::type(UserType::class);
2727
}
2828

2929
$unknownType = \get_class($value);

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
use GraphQL\Examples\Blog\Data\User;
99
use GraphQL\Examples\Blog\Type\Enum\StoryAffordancesType;
1010
use GraphQL\Examples\Blog\Type\Field\HtmlField;
11-
use GraphQL\Examples\Blog\Types;
11+
use GraphQL\Examples\Blog\TypeRegistry;
1212
use GraphQL\Type\Definition\ListOfType;
1313
use GraphQL\Type\Definition\ObjectType;
14+
use GraphQL\Type\Definition\Type;
1415

1516
class StoryType extends ObjectType
1617
{
@@ -19,28 +20,28 @@ public function __construct()
1920
parent::__construct([
2021
'name' => 'Story',
2122
'fields' => static fn (): array => [
22-
'id' => Types::id(),
23+
'id' => Type::id(),
2324
'author' => [
24-
'type' => Types::user(),
25+
'type' => TypeRegistry::type(UserType::class),
2526
'resolve' => static fn (Story $story): ?User => DataSource::findUser($story->authorId),
2627
],
2728
'mentions' => [
28-
'type' => new ListOfType(Types::mention()),
29+
'type' => new ListOfType(TypeRegistry::type(SearchResultType::class)),
2930
'resolve' => static fn (Story $story): array => DataSource::findStoryMentions($story->id),
3031
],
3132
'totalCommentCount' => [
32-
'type' => Types::int(),
33+
'type' => Type::int(),
3334
'resolve' => static fn (Story $story): int => DataSource::countComments($story->id),
3435
],
3536
'comments' => [
36-
'type' => new ListOfType(Types::comment()),
37+
'type' => new ListOfType(TypeRegistry::type(CommentType::class)),
3738
'args' => [
3839
'after' => [
39-
'type' => Types::id(),
40+
'type' => Type::id(),
4041
'description' => 'Load all comments listed after given comment ID',
4142
],
4243
'limit' => [
43-
'type' => Types::int(),
44+
'type' => Type::int(),
4445
'defaultValue' => 5,
4546
],
4647
],
@@ -53,22 +54,22 @@ public function __construct()
5354
),
5455
],
5556
'likes' => [
56-
'type' => new ListOfType(Types::user()),
57+
'type' => new ListOfType(TypeRegistry::type(UserType::class)),
5758
'args' => [
5859
'limit' => [
59-
'type' => Types::int(),
60+
'type' => Type::int(),
6061
'description' => 'Limit the number of recent likes returned',
6162
'defaultValue' => 5,
6263
],
6364
],
6465
'resolve' => static fn (Story $story): array => DataSource::findLikes($story->id, 10),
6566
],
6667
'likedBy' => [
67-
'type' => new ListOfType(Types::user()),
68+
'type' => new ListOfType(TypeRegistry::type(UserType::class)),
6869
'resolve' => static fn (Story $story) => DataSource::findLikes($story->id, 10),
6970
],
7071
'affordances' => [
71-
'type' => new ListOfType(Types::storyAffordances()),
72+
'type' => new ListOfType(TypeRegistry::type(StoryAffordancesType::class)),
7273
'resolve' => function (Story $story, array $args, AppContext $context): array {
7374
/** @var array<int, string> $affordances */
7475
$affordances = [];
@@ -88,14 +89,14 @@ public function __construct()
8889
},
8990
],
9091
'hasViewerLiked' => [
91-
'type' => Types::boolean(),
92+
'type' => Type::boolean(),
9293
'resolve' => static fn (Story $story, array $args, AppContext $context): bool => DataSource::isLikedBy($story->id, $context->viewer->id),
9394
],
9495
'body' => HtmlField::build([
9596
'resolve' => static fn (Story $story): string => $story->body,
9697
]),
9798
],
98-
'interfaces' => [Types::node()],
99+
'interfaces' => [TypeRegistry::type(NodeType::class)],
99100
]);
100101
}
101102
}

0 commit comments

Comments
 (0)