diff --git a/docs/schema-definition.md b/docs/schema-definition.md index 99179a06e..fd6d6eb9c 100644 --- a/docs/schema-definition.md +++ b/docs/schema-definition.md @@ -164,7 +164,8 @@ final class Types /** @var array */ 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]; @@ -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); } @@ -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); } ... } @@ -231,7 +232,7 @@ $schema = new Schema([ ], ], ]), - 'typeLoader' => Types::byTypeName(...), + 'typeLoader' => Types::load(...), ]); ``` diff --git a/examples/01-blog/Blog/Types.php b/examples/01-blog/Blog/Types.php index f7078d733..626120d4a 100644 --- a/examples/01-blog/Blog/Types.php +++ b/examples/01-blog/Blog/Types.php @@ -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]; @@ -59,16 +59,6 @@ public static function byTypeName(string $typeName): Type return self::$types[$typeName] = $type; } - /** - * @param class-string $classname - * - * @return \Closure(): Type - */ - private static function get(string $classname): \Closure - { - return static fn () => self::byClassName($classname); - } - /** @param class-string $className */ private static function byClassName(string $className): Type { @@ -83,6 +73,16 @@ private static function byClassName(string $className): Type return self::$types[$typeName] ??= new $className(); } + /** + * @param class-string $classname + * + * @return \Closure(): Type&NamedType + */ + private static function lazyByClassName(string $classname): \Closure + { + return static fn () => self::byClassName($classname); + } + /** @throws InvariantViolation */ public static function boolean(): ScalarType { @@ -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); } } diff --git a/examples/01-blog/graphql.php b/examples/01-blog/graphql.php index a3fcaf315..09204a659 100644 --- a/examples/01-blog/graphql.php +++ b/examples/01-blog/graphql.php @@ -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):