Skip to content

Commit

Permalink
refactor(graphql)!: Added parent to the Source.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Jan 26, 2024
1 parent 59a6624 commit 30f3e95
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 266 deletions.
2 changes: 2 additions & 0 deletions packages/graphql/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ This section is actual only if you are extending the package. Please review and

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Directives\PropertyDirective`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Sources\*`

* [ ] `LastDragon_ru\LaraASP\GraphQL\Builder\Types\InputObject`

* [ ] `LastDragon_ru\LaraASP\GraphQL\SortBy\Builders\*` => `LastDragon_ru\LaraASP\GraphQL\SortBy\Sorters\*`
Expand Down
271 changes: 121 additions & 150 deletions packages/graphql/src/Builder/BuilderInfoDetectorTest.php

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions packages/graphql/src/Builder/Sources/InputFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

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

public function __construct(
AstManipulator $manipulator,
private InputObjectTypeDefinitionNode|InputObjectType $object,
InputSource $parent,
private InputValueDefinitionNode|InputObjectField $field,
) {
parent::__construct($manipulator, $field instanceof InputObjectField ? $field->getType() : $field->type);
parent::__construct(
$manipulator,
$field instanceof InputObjectField ? $field->getType() : $field->type,
$parent,
);
}

// <editor-fold desc="Getters / Setters">
// =========================================================================
public function getObject(): InputObjectTypeDefinitionNode|InputObjectType {
return $this->object;
return $this->getParent()->getType();
}

public function getField(): InputValueDefinitionNode|InputObjectField {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/Builder/Sources/InputSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use GraphQL\Type\Definition\InputObjectType;

/**
* @extends Source<InputObjectTypeDefinitionNode|InputObjectType>
* @extends Source<InputObjectTypeDefinitionNode|InputObjectType, null>
*/
class InputSource extends Source {
public function getField(InputValueDefinitionNode|InputObjectField $field): InputFieldSource {
return new InputFieldSource($this->getManipulator(), $this->getType(), $field);
return new InputFieldSource($this->getManipulator(), $this, $field);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,35 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

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

public function __construct(
AstManipulator $manipulator,
private InterfaceTypeDefinitionNode|InterfaceType $object,
private FieldDefinitionNode|FieldDefinition $field,
InterfaceFieldSource $parent,
private InputValueDefinitionNode|Argument $argument,
) {
parent::__construct($manipulator, $argument instanceof Argument ? $argument->getType() : $argument->type);
parent::__construct(
$manipulator,
$argument instanceof Argument ? $argument->getType() : $argument->type,
$parent,
);
}

// <editor-fold desc="Getters / Setters">
// =========================================================================
public function getObject(): InterfaceTypeDefinitionNode|InterfaceType {
return $this->object;
return $this->getParent()->getObject();
}

public function getField(): FieldDefinition|FieldDefinitionNode {
return $this->field;
return $this->getParent()->getField();
}

public function getArgument(): InputValueDefinitionNode|Argument {
return $this->argument;
}
// </editor-fold>

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getParent(): InterfaceFieldSource {
return new InterfaceFieldSource($this->getManipulator(), $this->getObject(), $this->getField());
}
// </editor-fold>
}
23 changes: 9 additions & 14 deletions packages/graphql/src/Builder/Sources/InterfaceFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

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

public function __construct(
AstManipulator $manipulator,
private InterfaceTypeDefinitionNode|InterfaceType $object,
InterfaceSource $parent,
private FieldDefinitionNode|FieldDefinition $field,
) {
parent::__construct($manipulator, $field instanceof FieldDefinition ? $field->getType() : $field->type);
parent::__construct(
$manipulator,
$field instanceof FieldDefinition ? $field->getType() : $field->type,
$parent,
);
}

// <editor-fold desc="Getters / Setters">
// =========================================================================
public function getObject(): InterfaceTypeDefinitionNode|InterfaceType {
return $this->object;
return $this->getParent()->getType();
}

public function getField(): FieldDefinition|FieldDefinitionNode {
Expand All @@ -42,17 +46,8 @@ public function getField(): FieldDefinition|FieldDefinitionNode {

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getParent(): InterfaceSource {
return new InterfaceSource($this->getManipulator(), $this->getObject());
}

public function getArgument(InputValueDefinitionNode|Argument $argument): InterfaceFieldArgumentSource {
return new InterfaceFieldArgumentSource(
$this->getManipulator(),
$this->getObject(),
$this->getField(),
$argument,
);
return new InterfaceFieldArgumentSource($this->getManipulator(), $this, $argument);
}
// </editor-fold>
}
4 changes: 2 additions & 2 deletions packages/graphql/src/Builder/Sources/InterfaceSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use GraphQL\Type\Definition\InterfaceType;

/**
* @extends Source<InterfaceTypeDefinitionNode|InterfaceType>
* @extends Source<InterfaceTypeDefinitionNode|InterfaceType, null>
*/
class InterfaceSource extends Source {
public function getField(FieldDefinitionNode|FieldDefinition $field): InterfaceFieldSource {
return new InterfaceFieldSource($this->getManipulator(), $this->getType(), $field);
return new InterfaceFieldSource($this->getManipulator(), $this, $field);
}
}
22 changes: 9 additions & 13 deletions packages/graphql/src/Builder/Sources/ObjectFieldArgumentSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,35 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

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

public function __construct(
AstManipulator $manipulator,
private ObjectTypeDefinitionNode|ObjectType $object,
private FieldDefinitionNode|FieldDefinition $field,
ObjectFieldSource $parent,
private InputValueDefinitionNode|Argument $argument,
) {
parent::__construct($manipulator, $argument instanceof Argument ? $argument->getType() : $argument->type);
parent::__construct(
$manipulator,
$argument instanceof Argument ? $argument->getType() : $argument->type,
$parent,
);
}

// <editor-fold desc="Getters / Setters">
// =========================================================================
public function getObject(): ObjectTypeDefinitionNode|ObjectType {
return $this->object;
return $this->getParent()->getObject();
}

public function getField(): FieldDefinition|FieldDefinitionNode {
return $this->field;
return $this->getParent()->getField();
}

public function getArgument(): InputValueDefinitionNode|Argument {
return $this->argument;
}
// </editor-fold>

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getParent(): ObjectFieldSource {
return new ObjectFieldSource($this->getManipulator(), $this->getObject(), $this->getField());
}
// </editor-fold>
}
18 changes: 9 additions & 9 deletions packages/graphql/src/Builder/Sources/ObjectFieldSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

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

public function __construct(
AstManipulator $manipulator,
private ObjectTypeDefinitionNode|ObjectType $object,
ObjectSource $parent,
private FieldDefinitionNode|FieldDefinition $field,
) {
parent::__construct($manipulator, $field instanceof FieldDefinition ? $field->getType() : $field->type);
parent::__construct(
$manipulator,
$field instanceof FieldDefinition ? $field->getType() : $field->type,
$parent,
);
}

// <editor-fold desc="Getters / Setters">
// =========================================================================
public function getObject(): ObjectTypeDefinitionNode|ObjectType {
return $this->object;
return $this->getParent()->getType();
}

public function getField(): FieldDefinition|FieldDefinitionNode {
Expand All @@ -42,12 +46,8 @@ public function getField(): FieldDefinition|FieldDefinitionNode {

// <editor-fold desc="Helpers">
// =================================================================================================================
public function getParent(): ObjectSource {
return new ObjectSource($this->getManipulator(), $this->getObject());
}

public function getArgument(InputValueDefinitionNode|Argument $argument): ObjectFieldArgumentSource {
return new ObjectFieldArgumentSource($this->getManipulator(), $this->getObject(), $this->getField(), $argument);
return new ObjectFieldArgumentSource($this->getManipulator(), $this, $argument);
}
// </editor-fold>
}
4 changes: 2 additions & 2 deletions packages/graphql/src/Builder/Sources/ObjectSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use GraphQL\Type\Definition\ObjectType;

/**
* @extends Source<ObjectTypeDefinitionNode|ObjectType>
* @extends Source<ObjectTypeDefinitionNode|ObjectType, null>
*/
class ObjectSource extends Source {
public function getField(FieldDefinitionNode|FieldDefinition $field): ObjectFieldSource {
return new ObjectFieldSource($this->getManipulator(), $this->getType(), $field);
return new ObjectFieldSource($this->getManipulator(), $this, $field);
}
}
12 changes: 11 additions & 1 deletion packages/graphql/src/Builder/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@

/**
* @template TType of (TypeDefinitionNode&Node)|NamedTypeNode|ListTypeNode|NonNullTypeNode|Type
* @template TParent of TypeSource|null
*/
class Source implements TypeSource {
/**
* @param TType $type
* @param TType $type
* @param TParent $parent
*/
public function __construct(
private AstManipulator $manipulator,
private TypeDefinitionNode|Node|Type $type,
private TypeSource|null $parent = null,
) {
// empty
}
Expand All @@ -31,6 +34,13 @@ public function __construct(
protected function getManipulator(): AstManipulator {
return $this->manipulator;
}

/**
* @return TParent
*/
public function getParent(): ?TypeSource {
return $this->parent;
}
// </editor-fold>

// <editor-fold desc="API">
Expand Down
28 changes: 22 additions & 6 deletions packages/graphql/src/Builder/Traits/WithSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,45 @@
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceFieldSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectSource;
use LastDragon_ru\LaraASP\GraphQL\Utils\AstManipulator;

trait WithSource {
/**
* @return ($type is InterfaceTypeDefinitionNode ? InterfaceSource : ObjectSource)
*/
protected function getTypeSource(
AstManipulator $manipulator,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode $type,
): InterfaceSource|ObjectSource {
return $type instanceof InterfaceTypeDefinitionNode
? new InterfaceSource($manipulator, $type)
: new ObjectSource($manipulator, $type);
}

/**
* @return ($type is InterfaceTypeDefinitionNode ? InterfaceFieldSource : ObjectFieldSource)
*/
protected function getFieldSource(
AstManipulator $manipulator,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode $type,
FieldDefinitionNode $field,
): ObjectFieldSource|InterfaceFieldSource {
return $type instanceof InterfaceTypeDefinitionNode
? new InterfaceFieldSource($manipulator, $type, $field)
: new ObjectFieldSource($manipulator, $type, $field);
return $this->getTypeSource($manipulator, $type)->getField($field);
}

/**
* @return ($type is InterfaceTypeDefinitionNode ? InterfaceFieldArgumentSource : ObjectFieldArgumentSource)
*/
protected function getFieldArgumentSource(
AstManipulator $manipulator,
ObjectTypeDefinitionNode|InterfaceTypeDefinitionNode $type,
FieldDefinitionNode $field,
InputValueDefinitionNode $argument,
): ObjectFieldArgumentSource|InterfaceFieldArgumentSource {
return $type instanceof InterfaceTypeDefinitionNode
? new InterfaceFieldArgumentSource($manipulator, $type, $field, $argument)
: new ObjectFieldArgumentSource($manipulator, $type, $field, $argument);
return $this->getFieldSource($manipulator, $type, $field)->getArgument($argument);
}
}
3 changes: 2 additions & 1 deletion packages/graphql/src/Stream/Directives/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\InterfaceFieldSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldArgumentSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectFieldSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Sources\ObjectSource;
use LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithManipulator;
use LastDragon_ru\LaraASP\GraphQL\Builder\Traits\WithSource;
use LastDragon_ru\LaraASP\GraphQL\Package;
Expand Down Expand Up @@ -436,7 +437,7 @@ public function resolveField(FieldValue $fieldValue): callable {
return function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $info): StreamValue {
// Offset
$manipulator = $this->getAstManipulator(new DocumentAST());
$source = new ObjectFieldSource($manipulator, $info->parentType, $info->fieldDefinition);
$source = (new ObjectSource($manipulator, $info->parentType))->getField($info->fieldDefinition);
$offset = $this->getFieldValue(StreamOffsetDirective::class, $manipulator, $source, $info, $args);

// Builder
Expand Down
Loading

0 comments on commit 30f3e95

Please sign in to comment.