Skip to content

Commit

Permalink
v0.2.2 (#5)
Browse files Browse the repository at this point in the history
* Add UnionType
* Add  Having types and fields
  • Loading branch information
MartinPetricko authored Jan 26, 2024
1 parent bb15b57 commit 0df1d1a
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 12 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## [Unreleased]

## [0.2.2] - 2024-01-26
### Added
- Having types and fields
- GroupType instead of anonymous declaration in GroupField
- UnionType

### Changed
- Depracated GroupField::FIELD_COLUMN constant, use GroupType::FIELD_COLUMN instead

## [0.2.1] - 2023-07-03
### Added
- Null and NotNull where comparators
Expand All @@ -25,7 +34,8 @@
- Webonyx driver
- Webonyx schema transformer

[Unreleased]: https://github.com/efabrica-team/graphql/compare/0.2.1...main
[Unreleased]: https://github.com/efabrica-team/graphql/compare/0.2.2...main
[0.2.2]: https://github.com/efabrica-team/graphql/compare/0.2.1...0.2.2
[0.2.1]: https://github.com/efabrica-team/graphql/compare/0.2.0...0.2.1
[0.2.0]: https://github.com/efabrica-team/graphql/compare/0.1.0...0.2.0
[0.1.0]: https://github.com/efabrica-team/graphql/compare/0.0.0...0.1.0
4 changes: 4 additions & 0 deletions src/Schema/Custom/Arguments/ConditionsArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Efabrica\GraphQL\Schema\Custom\Arguments;

use Efabrica\GraphQL\Schema\Custom\Fields\GroupField;
use Efabrica\GraphQL\Schema\Custom\Fields\HavingAndField;
use Efabrica\GraphQL\Schema\Custom\Fields\HavingOrField;
use Efabrica\GraphQL\Schema\Custom\Fields\WhereAndField;
use Efabrica\GraphQL\Schema\Custom\Fields\WhereOrField;
use Efabrica\GraphQL\Schema\Definition\Arguments\FieldArgument;
Expand All @@ -20,6 +22,8 @@ public function __construct()
->setFields([
new WhereAndField(),
new WhereOrField(),
new HavingAndField(),
new HavingOrField(),
new GroupField(),
])
);
Expand Down
14 changes: 7 additions & 7 deletions src/Schema/Custom/Fields/GroupField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

namespace Efabrica\GraphQL\Schema\Custom\Fields;

use Efabrica\GraphQL\Schema\Custom\Types\GroupType;
use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;
use Efabrica\GraphQL\Schema\Definition\Types\InputObjectType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\StringType;

class GroupField extends InputObjectField
{
public const NAME = 'group';

public const FIELD_COLUMN = 'column';
/**
* TODO: remove in 0.3.0
* @deprecated Use GroupType::FIELD_COLUMN instead
*/
public const FIELD_COLUMN = GroupType::FIELD_COLUMN;

public function __construct()
{
parent::__construct(
self::NAME,
(new InputObjectType('group_type'))
->setFields([
new InputObjectField(self::FIELD_COLUMN, new StringType()),
])
new GroupType()
);
$this->setMulti()
->setNullable();
Expand Down
22 changes: 22 additions & 0 deletions src/Schema/Custom/Fields/HavingAndField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Efabrica\GraphQL\Schema\Custom\Fields;

use Efabrica\GraphQL\Schema\Custom\Types\HavingType;
use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;

class HavingAndField extends InputObjectField
{
public const NAME = 'having';

public function __construct()
{
parent::__construct(
self::NAME,
new HavingType()
);

$this->setMulti()
->setNullable();
}
}
22 changes: 22 additions & 0 deletions src/Schema/Custom/Fields/HavingOrField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Efabrica\GraphQL\Schema\Custom\Fields;

use Efabrica\GraphQL\Schema\Custom\Types\HavingType;
use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;

class HavingOrField extends InputObjectField
{
public const NAME = 'having_or';

public function __construct()
{
parent::__construct(
self::NAME,
new HavingType()
);

$this->setMulti()
->setNullable();
}
}
3 changes: 1 addition & 2 deletions src/Schema/Custom/Fields/WhereAndField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public function __construct()
new WhereType()
);

$this->setName(self::NAME)
->setMulti()
$this->setMulti()
->setNullable();
}
}
3 changes: 1 addition & 2 deletions src/Schema/Custom/Fields/WhereOrField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public function __construct()
new WhereType()
);

$this->setName(self::NAME)
->setMulti()
$this->setMulti()
->setNullable();
}
}
23 changes: 23 additions & 0 deletions src/Schema/Custom/Types/GroupType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Efabrica\GraphQL\Schema\Custom\Types;

use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;
use Efabrica\GraphQL\Schema\Definition\Types\InputObjectType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\StringType;

class GroupType extends InputObjectType
{
public const FIELD_COLUMN = 'column';

public function __construct()
{
parent::__construct(
'group_type',
);

$this->setFields(fn() => [
new InputObjectField(self::FIELD_COLUMN, new StringType()),
]);
}
}
35 changes: 35 additions & 0 deletions src/Schema/Custom/Types/HavingType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Efabrica\GraphQL\Schema\Custom\Types;

use Efabrica\GraphQL\Schema\Custom\Fields\HavingAndField;
use Efabrica\GraphQL\Schema\Custom\Fields\HavingOrField;
use Efabrica\GraphQL\Schema\Definition\Fields\InputObjectField;
use Efabrica\GraphQL\Schema\Definition\Types\InputObjectType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\StringType;

class HavingType extends InputObjectType
{
public const FIELD_COLUMN = 'column';
public const FIELD_VALUE = 'value';
public const FIELD_COMPARATOR = 'comparator';

public function __construct()
{
parent::__construct(
'having_type',
);

$this->setFields(fn() => [
new HavingAndField(),
new HavingOrField(),
(new InputObjectField(self::FIELD_COLUMN, new StringType()))
->setNullable(),
(new InputObjectField(self::FIELD_VALUE, new StringType()))
->setMulti()
->setNullable(),
(new InputObjectField(self::FIELD_COMPARATOR, new WhereComparatorEnum()))
->setDefaultValue(WhereComparatorEnum::EQUAL),
]);
}
}
65 changes: 65 additions & 0 deletions src/Schema/Definition/Types/UnionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Efabrica\GraphQL\Schema\Definition\Types;

class UnionType extends Type
{
/**
* @var ObjectType[]
*/
protected array $objectTypes = [];

/** @var callable */
protected $objectTypesCallback;

/** @var callable */
protected $resolveType;

public function getObjectTypes(): array
{
if ($this->objectTypesCallback === null) {
return $this->objectTypes;
}

$objectTypes = [];
foreach (call_user_func($this->objectTypesCallback) as $type) {
$objectTypes[$type->getName()] = $type;
}

return array_merge($objectTypes, $this->objectTypes);
}

public function addObjectType(ObjectType $objectType): self
{
$this->objectTypes[$objectType->getName()] = $objectType;
return $this;
}

/**
* @param ObjectType[]|callable $objectTypes
*/
public function setObjectTypes($objectTypes): self
{
if (is_callable($objectTypes)) {
$this->objectTypesCallback = $objectTypes;
} else {
$this->objectTypes = [];
foreach ($objectTypes as $objectType) {
$this->objectTypes[$objectType->getName()] = $objectType;
}
}

return $this;
}

public function getResolveType(): callable
{
return $this->resolveType;
}

public function setResolveType(callable $resolveType): self
{
$this->resolveType = $resolveType;
return $this;
}
}
21 changes: 21 additions & 0 deletions src/Schema/Transformers/WebonyxSchemaTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\ScalarType;
use Efabrica\GraphQL\Schema\Definition\Types\Scalar\StringType;
use Efabrica\GraphQL\Schema\Definition\Types\Type;
use Efabrica\GraphQL\Schema\Definition\Types\UnionType;
use GraphQL\Type\Definition\EnumType as WebonyxEnumType;
use GraphQL\Type\Definition\InputObjectType as WebonyxInputObjectType;
use GraphQL\Type\Definition\ObjectType as WebonyxObjectType;
use GraphQL\Type\Definition\ResolveInfo as WebonyxResolveInfo;
use GraphQL\Type\Definition\Type as WebonyxType;
use GraphQL\Type\Definition\UnionType as WebonyxUnionType;
use GraphQL\Type\Schema as WebonyxSchema;
use GraphQL\Type\SchemaConfig as WebonyxSchemaConfig;
use Throwable;
Expand Down Expand Up @@ -89,6 +91,25 @@ private function transformType(Type $type): WebonyxType
'description' => $type->getDescription(),
'values' => $values,
]);
} elseif ($type instanceof UnionType) {
$transformedType = new WebonyxUnionType([
'name' => $type->getName(),
'description' => $type->getDescription(),
'types' => function () use ($type) {
$objectTypes = [];
foreach ($type->getObjectTypes() as $objectType) {
$objectTypes[] = $this->transformType($objectType);
}
return $objectTypes;
},
'resolveType' => function($value) use ($type): WebonyxObjectType {
$objectType = $this->transformType(call_user_func($type->getResolveType(), $value));
if (!$objectType instanceof WebonyxObjectType) {
throw new SchemaTransformerException('Union resolveType must be instance of "' . WebonyxObjectType::class . '".');
}
return $objectType;
},
]);
} elseif ($type instanceof InputObjectType) {
$transformedType = new WebonyxInputObjectType([
'name' => $type->getName(),
Expand Down

0 comments on commit 0df1d1a

Please sign in to comment.