Skip to content

Commit

Permalink
refactor(graphql)!: Type registration (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru authored May 2, 2024
2 parents 7d7bf1d + 104eff5 commit 73d3a0e
Show file tree
Hide file tree
Showing 26 changed files with 216 additions and 357 deletions.
2 changes: 1 addition & 1 deletion packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Converts scalar into GraphQL Type. Similar to Lighthouse's `@scalar` directive,
>
> ```graphql
> scalar JsonString
> @scalar(
> @type(
> class: "LastDragon_ru\\LaraASP\\GraphQL\\Scalars\\JsonStringType"
> )
> ```
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Please also see [changelog](https://github.com/LastDragon-ru/lara-asp/releases)
>
> Maybe a good idea to add test (at least) with `LastDragon_ru\LaraASP\GraphQL\Testing\GraphQLAssertions::assertGraphQLSchemaEquals()` assertion before the upgrade 🤗
# Upgrade from v6

* [ ] The `\LastDragon_ru\LaraASP\GraphQL\Scalars\JsonStringType` is not implement `\LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition` anymore. To add the scalar into the Schema, you can use `@type`/`@scalar` directive, or create a custom implementation of `TypeDefinition` contract to use with `Builder`/`Manipulator`.

* [ ] `\LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition::getTypeDefinition()` return type changed.

# Upgrade from v5

## General
Expand Down
9 changes: 3 additions & 6 deletions packages/graphql/src/Builder/Contracts/TypeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use UnitEnum;

interface TypeDefinition {
/**
Expand All @@ -18,15 +18,12 @@ public function getTypeName(TypeSource $source, Context $context): string;
/**
* Returns the type definition for given Source if possible. The name must be equal to `$name`.
*
* @see TypeReference
*
* @return (TypeDefinitionNode&Node)|(Type&NamedType)|null Returning {@see Type} is deprecated, please use
* {@see TypeReference} instead.
* @return (TypeDefinitionNode&Node)|class-string<(Type&NamedType)|UnitEnum>|null
*/
public function getTypeDefinition(
Manipulator $manipulator,
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null;
): TypeDefinitionNode|string|null;
}
19 changes: 5 additions & 14 deletions packages/graphql/src/Builder/Manipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextOperators;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Operator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeProvider;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Exceptions\FakeTypeDefinitionIsNotFake;
Expand All @@ -35,7 +34,6 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\Source;
use LastDragon_ru\LaraASP\GraphQL\Package;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use Nuwave\Lighthouse\Schema\DirectiveLocator;
Expand All @@ -46,7 +44,7 @@
use function array_unshift;
use function count;
use function implode;
use function trigger_deprecation;
use function is_string;

class Manipulator extends AstManipulator implements TypeProvider {
// <editor-fold desc="TypeProvider">
Expand All @@ -67,21 +65,14 @@ public function getType(string $definition, TypeSource $source, Context $context
// Create new
$node = $instance->getTypeDefinition($this, $source, $context, $name);

if ($node instanceof Type && !($node instanceof TypeReference)) {
trigger_deprecation(
Package::Name,
'6.3.0',
'Returning `%s` from `%s` is deprecated, please use `%s` instead.',
Type::class,
TypeDefinition::class.'::getTypeDefinition()',
TypeReference::class,
);
}

if (!$node) {
throw new TypeDefinitionImpossibleToCreateType($definition, $source, $context);
}

if (is_string($node)) {
$node = new TypeReference($name, $node);
}

if ($name !== $this->getName($node)) {
throw new TypeDefinitionInvalidTypeName($definition, $name, $this->getName($node), $context);
}
Expand Down
5 changes: 1 addition & 4 deletions packages/graphql/src/Builder/Types/InputObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ abstract protected function getDescription(
Context $context,
): string;

/**
* @inheritDoc
*/
#[Override]
public function getTypeDefinition(
Manipulator $manipulator,
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
): TypeDefinitionNode|string|null {
// Source?
$source = $manipulator->getTypeSource($source->getTypeDefinition());

Expand Down
27 changes: 1 addition & 26 deletions packages/graphql/src/Scalars/JsonStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\StringValueNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Definition\StringType;
use GraphQL\Type\Definition\Type;
use GraphQL\Utils\Utils;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use Override;

use function is_string;
use function json_validate;
use function sprintf;

class JsonStringType extends StringType implements TypeDefinition {
class JsonStringType extends StringType {
public string $name = 'JsonString';
public ?string $description = 'Represents JSON string.';

Expand Down Expand Up @@ -83,22 +76,4 @@ protected function validate(mixed $value, string $error): string {
return $value;
}
// </editor-fold>

// <editor-fold desc="TypeDefinition">
// =========================================================================
#[Override]
public function getTypeName(TypeSource $source, Context $context): string {
return $this->name();
}

#[Override]
public function getTypeDefinition(
Manipulator $manipulator,
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
return new TypeReference($name, self::class);
}
// </editor-fold>
}
3 changes: 1 addition & 2 deletions packages/graphql/src/SearchBy/Directives/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Closure;
use Exception;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\EnumType;
Expand Down Expand Up @@ -1622,7 +1621,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode&Node {
): TypeDefinitionNode|string|null {
return Parser::inputObjectTypeDefinition(
<<<GRAPHQL
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ pagination).
"""
scalar StreamOffset
@type(
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Types\\Offset"
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Scalars\\Offset"
)

type A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
): TypeDefinitionNode|string|null {
// Object?
if (!$source->isObject()) {
return null;
Expand Down
6 changes: 1 addition & 5 deletions packages/graphql/src/SearchBy/Types/Enumeration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextOperators;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
Expand Down Expand Up @@ -32,16 +31,13 @@ public function getTypeName(TypeSource $source, Context $context): string {
return "{$directiveName}{$builderName}Enum{$typeName}{$nullable}";
}

/**
* @inheritDoc
*/
#[Override]
public function getTypeDefinition(
Manipulator $manipulator,
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
): TypeDefinitionNode|string|null {
// Enum?
if (!$source->isEnum()) {
return null;
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/src/SearchBy/Types/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace LastDragon_ru\LaraASP\GraphQL\SearchBy\Types;

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Enums\Flag as FlagEnum;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Directives\Directive;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use Override;

class Flag implements TypeDefinition {
Expand All @@ -29,7 +27,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
return new TypeReference($name, FlagEnum::class);
): TypeDefinitionNode|string|null {
return FlagEnum::class;
}
}
3 changes: 1 addition & 2 deletions packages/graphql/src/SearchBy/Types/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
Expand All @@ -31,7 +30,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
): TypeDefinitionNode|string|null {
return Parser::inputObjectTypeDefinition(
<<<GRAPHQL
input {$name} {
Expand Down
6 changes: 1 addition & 5 deletions packages/graphql/src/SearchBy/Types/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextBuilderInfo;
use LastDragon_ru\LaraASP\GraphQL\Builder\Context\HandlerContextOperators;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
Expand Down Expand Up @@ -32,16 +31,13 @@ public function getTypeName(TypeSource $source, Context $context): string {
return "{$directiveName}{$builderName}Scalar{$typeName}{$nullable}";
}

/**
* @inheritDoc
*/
#[Override]
public function getTypeDefinition(
Manipulator $manipulator,
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
): TypeDefinitionNode|string|null {
// Scalar?
if (!$source->isScalar()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ pagination).
"""
scalar StreamOffset
@type(
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Types\\Offset"
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Scalars\\Offset"
)

type A {
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql/src/SortBy/Types/Direction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Types;

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Enums\Direction as DirectionEnum;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use Override;

class Direction implements TypeDefinition {
Expand All @@ -29,7 +27,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
return new TypeReference($name, DirectionEnum::class);
): TypeDefinitionNode|string|null {
return DirectionEnum::class;
}
}
6 changes: 2 additions & 4 deletions packages/graphql/src/SortBy/Types/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace LastDragon_ru\LaraASP\GraphQL\SortBy\Types;

use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\Context;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeDefinition;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Enums\Flag as FlagEnum;
use LastDragon_ru\LaraASP\GraphQL\Builder\Manipulator;
use LastDragon_ru\LaraASP\GraphQL\SortBy\Directives\Directive;
use LastDragon_ru\LaraASP\GraphQL\Utils\TypeReference;
use Override;

class Flag implements TypeDefinition {
Expand All @@ -29,7 +27,7 @@ public function getTypeDefinition(
TypeSource $source,
Context $context,
string $name,
): TypeDefinitionNode|Type|null {
return new TypeReference($name, FlagEnum::class);
): TypeDefinitionNode|string|null {
return FlagEnum::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pagination).
"""
scalar StreamOffset
@type(
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Types\\Offset"
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Scalars\\Offset"
)

type Query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pagination).
"""
scalar StreamOffset
@type(
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Types\\Offset"
class: "LastDragon_ru\\LaraASP\\GraphQL\\Stream\\Scalars\\Offset"
)

type Query
Expand Down
Loading

0 comments on commit 73d3a0e

Please sign in to comment.