Skip to content

Commit

Permalink
refactor(graphql)!: Ability to override type of *FieldSource/`*Argu…
Browse files Browse the repository at this point in the history
…mentSource`.
  • Loading branch information
LastDragon-ru committed Jan 27, 2024
1 parent 30f3e95 commit 760a71d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 46 deletions.
8 changes: 3 additions & 5 deletions packages/graphql/src/Builder/Contracts/TypeSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace LastDragon_ru\LaraASP\GraphQL\Builder\Contracts;

use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Type;
use Stringable;

interface TypeSource extends Stringable {
/**
* @return (TypeDefinitionNode&Node)|NamedTypeNode|ListTypeNode|NonNullTypeNode|Type
* @return (TypeDefinitionNode&Node)|(TypeNode&Node)|Type
*/
public function getType(): TypeDefinitionNode|NamedTypeNode|ListTypeNode|NonNullTypeNode|Type;
public function getType(): TypeDefinitionNode|TypeNode|Type;

public function getTypeName(): string;

Expand Down
13 changes: 8 additions & 5 deletions packages/graphql/src/Builder/Sources/InputFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use GraphQL\Language\AST\ArgumentNode;
use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
Expand All @@ -17,19 +16,23 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

/**
* @extends Source<NamedTypeNode|ListTypeNode|NonNullTypeNode|Type, InputSource>
* @extends Source<(TypeNode&Node)|Type, InputSource>
*/
class InputFieldSource extends Source {
use Field;

/**
* @param (TypeNode&Node)|Type|null $type
*/
public function __construct(
AstManipulator $manipulator,
InputSource $parent,
private InputValueDefinitionNode|InputObjectField $field,
TypeNode|Type|null $type = null,
) {
parent::__construct(
$manipulator,
$field instanceof InputObjectField ? $field->getType() : $field->type,
$type ?? ($field instanceof InputObjectField ? $field->getType() : $field->type),
$parent,
);
}
Expand Down
13 changes: 11 additions & 2 deletions packages/graphql/src/Builder/Sources/InputSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@

use GraphQL\Language\AST\InputObjectTypeDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\InputObjectField;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\Type;

/**
* @extends Source<InputObjectTypeDefinitionNode|InputObjectType, null>
*/
class InputSource extends Source {
public function getField(InputValueDefinitionNode|InputObjectField $field): InputFieldSource {
return new InputFieldSource($this->getManipulator(), $this, $field);
/**
* @param (TypeNode&Node)|Type|null $type
*/
public function getField(
InputValueDefinitionNode|InputObjectField $field,
TypeNode|Type $type = null,
): InputFieldSource {
return new InputFieldSource($this->getManipulator(), $this, $field, $type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InterfaceType;
Expand All @@ -16,19 +15,23 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

/**
* @extends Source<NamedTypeNode|ListTypeNode|NonNullTypeNode|Type, InterfaceFieldSource>
* @extends Source<(TypeNode&Node)|Type, InterfaceFieldSource>
*/
class InterfaceFieldArgumentSource extends Source {
use FieldArgument;

/**
* @param (TypeNode&Node)|Type|null $type
*/
public function __construct(
AstManipulator $manipulator,
InterfaceFieldSource $parent,
private InputValueDefinitionNode|Argument $argument,
TypeNode|Type|null $type = null,
) {
parent::__construct(
$manipulator,
$argument instanceof Argument ? $argument->getType() : $argument->type,
$type ?? ($argument instanceof Argument ? $argument->getType() : $argument->type),
$parent,
);
}
Expand Down
23 changes: 16 additions & 7 deletions packages/graphql/src/Builder/Sources/InterfaceFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InterfaceType;
Expand All @@ -16,19 +15,23 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

/**
* @extends Source<NamedTypeNode|ListTypeNode|NonNullTypeNode|Type, InterfaceSource>
* @extends Source<(TypeNode&Node)|Type, InterfaceSource>
*/
class InterfaceFieldSource extends Source {
use Field;

/**
* @param (TypeNode&Node)|Type|null $type
*/
public function __construct(
AstManipulator $manipulator,
InterfaceSource $parent,
private FieldDefinitionNode|FieldDefinition $field,
TypeNode|Type|null $type,
) {
parent::__construct(
$manipulator,
$field instanceof FieldDefinition ? $field->getType() : $field->type,
$type ?? ($field instanceof FieldDefinition ? $field->getType() : $field->type),
$parent,
);
}
Expand All @@ -46,8 +49,14 @@ public function getField(): FieldDefinition|FieldDefinitionNode {

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getArgument(InputValueDefinitionNode|Argument $argument): InterfaceFieldArgumentSource {
return new InterfaceFieldArgumentSource($this->getManipulator(), $this, $argument);
/**
* @param (TypeNode&Node)|Type|null $type
*/
public function getArgument(
InputValueDefinitionNode|Argument $argument,
TypeNode|Type $type = null,
): InterfaceFieldArgumentSource {
return new InterfaceFieldArgumentSource($this->getManipulator(), $this, $argument, $type);
}
// </editor-fold>
}
13 changes: 11 additions & 2 deletions packages/graphql/src/Builder/Sources/InterfaceSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@

use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InterfaceTypeDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\Type;

/**
* @extends Source<InterfaceTypeDefinitionNode|InterfaceType, null>
*/
class InterfaceSource extends Source {
public function getField(FieldDefinitionNode|FieldDefinition $field): InterfaceFieldSource {
return new InterfaceFieldSource($this->getManipulator(), $this, $field);
/**
* @param (TypeNode&Node)|Type|null $type
*/
public function getField(
FieldDefinitionNode|FieldDefinition $field,
TypeNode|Type $type = null,
): InterfaceFieldSource {
return new InterfaceFieldSource($this->getManipulator(), $this, $field, $type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ObjectType;
Expand All @@ -16,19 +15,23 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

/**
* @extends Source<NamedTypeNode|ListTypeNode|NonNullTypeNode|Type, ObjectFieldSource>
* @extends Source<(TypeNode&Node)|Type, ObjectFieldSource>
*/
class ObjectFieldArgumentSource extends Source {
use FieldArgument;

/**
* @param (TypeNode&Node)|Type|null $type
*/
public function __construct(
AstManipulator $manipulator,
ObjectFieldSource $parent,
private InputValueDefinitionNode|Argument $argument,
TypeNode|Type|null $type = null,
) {
parent::__construct(
$manipulator,
$argument instanceof Argument ? $argument->getType() : $argument->type,
$type ?? ($argument instanceof Argument ? $argument->getType() : $argument->type),
$parent,
);
}
Expand Down
23 changes: 16 additions & 7 deletions packages/graphql/src/Builder/Sources/ObjectFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\InputValueDefinitionNode;
use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Argument;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ObjectType;
Expand All @@ -16,19 +15,23 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

/**
* @extends Source<NamedTypeNode|ListTypeNode|NonNullTypeNode|Type, ObjectSource>
* @extends Source<(TypeNode&Node)|Type, ObjectSource>
*/
class ObjectFieldSource extends Source {
use Field;

/**
* @param (TypeNode&Node)|Type|null $type
*/
public function __construct(
AstManipulator $manipulator,
ObjectSource $parent,
private FieldDefinitionNode|FieldDefinition $field,
TypeNode|Type|null $type = null,
) {
parent::__construct(
$manipulator,
$field instanceof FieldDefinition ? $field->getType() : $field->type,
$type ?? ($field instanceof FieldDefinition ? $field->getType() : $field->type),
$parent,
);
}
Expand All @@ -46,8 +49,14 @@ public function getField(): FieldDefinition|FieldDefinitionNode {

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getArgument(InputValueDefinitionNode|Argument $argument): ObjectFieldArgumentSource {
return new ObjectFieldArgumentSource($this->getManipulator(), $this, $argument);
/**
* @param (TypeNode&Node)|Type|null $type
*/
public function getArgument(
InputValueDefinitionNode|Argument $argument,
TypeNode|Type $type = null,
): ObjectFieldArgumentSource {
return new ObjectFieldArgumentSource($this->getManipulator(), $this, $argument, $type);
}
// </editor-fold>
}
13 changes: 11 additions & 2 deletions packages/graphql/src/Builder/Sources/ObjectSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
namespace LastDragon_ru\LaraASP\GraphQL\Builder\Sources;

use GraphQL\Language\AST\FieldDefinitionNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;

/**
* @extends Source<ObjectTypeDefinitionNode|ObjectType, null>
*/
class ObjectSource extends Source {
public function getField(FieldDefinitionNode|FieldDefinition $field): ObjectFieldSource {
return new ObjectFieldSource($this->getManipulator(), $this, $field);
/**
* @param (TypeNode&Node)|Type|null $type
*/
public function getField(
FieldDefinitionNode|FieldDefinition $field,
TypeNode|Type $type = null,
): ObjectFieldSource {
return new ObjectFieldSource($this->getManipulator(), $this, $field, $type);
}
}
10 changes: 4 additions & 6 deletions packages/graphql/src/Builder/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace LastDragon_ru\LaraASP\GraphQL\Builder\Sources;

use GraphQL\Language\AST\ListTypeNode;
use GraphQL\Language\AST\NamedTypeNode;
use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NonNullTypeNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Type\Definition\Type;
use LastDragon_ru\LaraASP\GraphQL\Builder\Contracts\TypeSource;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;
use Override;

/**
* @template TType of (TypeDefinitionNode&Node)|NamedTypeNode|ListTypeNode|NonNullTypeNode|Type
* @template TType of (TypeDefinitionNode&Node)|(TypeNode&Node)|Type
* @template TParent of TypeSource|null
*/
class Source implements TypeSource {
Expand All @@ -23,7 +21,7 @@ class Source implements TypeSource {
*/
public function __construct(
private AstManipulator $manipulator,
private TypeDefinitionNode|Node|Type $type,
private TypeDefinitionNode|TypeNode|Type $type,
private TypeSource|null $parent = null,
) {
// empty
Expand All @@ -49,7 +47,7 @@ public function getParent(): ?TypeSource {
* @return TType
*/
#[Override]
public function getType(): TypeDefinitionNode|NamedTypeNode|ListTypeNode|NonNullTypeNode|Type {
public function getType(): TypeDefinitionNode|TypeNode|Type {
return $this->type;
}

Expand Down

0 comments on commit 760a71d

Please sign in to comment.