Skip to content

Commit

Permalink
improve types and naming
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Nov 29, 2023
1 parent 8213656 commit bb45379
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
11 changes: 6 additions & 5 deletions docs/schema-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ final class Types
/** @var array<string, Type&NamedType> */
private static array $types = [];

public static function byTypeName(string $typeName): \Closure
/** @return Type&NamedType */
public static function load(string $typeName): Type
{
if (isset(self::$types[$typeName])) {
return self::$types[$typeName];
Expand Down Expand Up @@ -199,7 +200,7 @@ final class Types
}

/** @return \Closure(): Type&NamedType */
private static function get(string $className): \Closure
private static function lazyByClassName(string $className): \Closure
{
return static fn () => self::byClassName($className);
}
Expand All @@ -209,8 +210,8 @@ final class Types
public static function id(): ScalarType { return Type::id(); }
public static function int(): ScalarType { return Type::int(); }
public static function string(): ScalarType { return Type::string(); }
public static function author(): callable { return self::get(AuthorType::class); }
public static function story(): callable { return self::get(StoryType::class); }
public static function author(): callable { return self::lazyByClassName(AuthorType::class); }
public static function story(): callable { return self::lazyByClassName(StoryType::class); }
...
}

Expand All @@ -231,7 +232,7 @@ $schema = new Schema([
],
],
]),
'typeLoader' => Types::byTypeName(...),
'typeLoader' => Types::load(...),
]);
```

Expand Down
44 changes: 22 additions & 22 deletions examples/01-blog/Blog/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class Types
*
* @return Type&NamedType
*/
public static function byTypeName(string $typeName): Type
public static function load(string $typeName): Type
{
if (isset(self::$types[$typeName])) {
return self::$types[$typeName];
Expand All @@ -59,16 +59,6 @@ public static function byTypeName(string $typeName): Type
return self::$types[$typeName] = $type;
}

/**
* @param class-string<Type&NamedType> $classname
*
* @return \Closure(): Type
*/
private static function get(string $classname): \Closure
{
return static fn () => self::byClassName($classname);
}

/** @param class-string<Type&NamedType> $className */
private static function byClassName(string $className): Type
{
Expand All @@ -83,6 +73,16 @@ private static function byClassName(string $className): Type
return self::$types[$typeName] ??= new $className();
}

/**
* @param class-string<Type&NamedType> $classname
*
* @return \Closure(): Type&NamedType
*/
private static function lazyByClassName(string $classname): \Closure

Check failure on line 81 in examples/01-blog/Blog/Types.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (7.4)

PHPDoc tag @return contains unresolvable type.

Check failure on line 81 in examples/01-blog/Blog/Types.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8)

PHPDoc tag @return contains unresolvable type.

Check failure on line 81 in examples/01-blog/Blog/Types.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.1)

PHPDoc tag @return contains unresolvable type.

Check failure on line 81 in examples/01-blog/Blog/Types.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

PHPDoc tag @return contains unresolvable type.
{
return static fn () => self::byClassName($classname);
}

/** @throws InvariantViolation */
public static function boolean(): ScalarType
{
Expand Down Expand Up @@ -115,56 +115,56 @@ public static function string(): ScalarType

public static function user(): callable
{
return self::get(UserType::class);
return self::lazyByClassName(UserType::class);
}

public static function story(): callable
{
return self::get(StoryType::class);
return self::lazyByClassName(StoryType::class);
}

public static function comment(): callable
{
return self::get(CommentType::class);
return self::lazyByClassName(CommentType::class);
}

public static function image(): callable
{
return self::get(ImageType::class);
return self::lazyByClassName(ImageType::class);
}

public static function node(): callable
{
return self::get(NodeType::class);
return self::lazyByClassName(NodeType::class);
}

public static function mention(): callable
{
return self::get(SearchResultType::class);
return self::lazyByClassName(SearchResultType::class);
}

public static function imageSize(): callable
{
return self::get(ImageSizeType::class);
return self::lazyByClassName(ImageSizeType::class);
}

public static function contentFormat(): callable
{
return self::get(ContentFormatType::class);
return self::lazyByClassName(ContentFormatType::class);
}

public static function storyAffordances(): callable
{
return self::get(StoryAffordancesType::class);
return self::lazyByClassName(StoryAffordancesType::class);
}

public static function email(): callable
{
return self::get(EmailType::class);
return self::lazyByClassName(EmailType::class);
}

public static function url(): callable
{
return self::get(UrlType::class);
return self::lazyByClassName(UrlType::class);
}
}
2 changes: 1 addition & 1 deletion examples/01-blog/graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
$schema = new Schema(
(new SchemaConfig())
->setQuery(new QueryType())
->setTypeLoader([Types::class, 'byTypeName'])
->setTypeLoader([Types::class, 'load'])
);

// Prepare context that will be available in all field resolvers (as 3rd argument):
Expand Down

0 comments on commit bb45379

Please sign in to comment.