Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object Collaborator and mutation unitTypeCreate #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/entities/Settings/CollaboratorEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Vertuoza\Entities\Settings;

class CollaboratorEntity
{
public string $id;
public string $name;
public string $firstName;
}
3 changes: 2 additions & 1 deletion src/graphql/GqlMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ static function sandbox()
static function schema(\GraphQL\Executor\Promise\PromiseAdapter $graphQLPromiseAdapter, PromiseAdapterInterface $dataLoaderPromiseAdapter)
{
$query = new Query();
// $mutation = new Mutation();
$mutation = new Mutation();

$schema = new Schema((
new SchemaConfig())
->setQuery($query)
->setMutation($mutation)
->setTypeLoader([Types::class, 'byTypename'])
);

Expand Down
28 changes: 28 additions & 0 deletions src/graphql/Resolvers/Mutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Vertuoza\Api\Graphql\Resolvers;

use GraphQL\Type\Definition\ObjectType;
use Vertuoza\Api\Graphql\Resolvers\Settings\UnitTypes\UnitTypeMutation;
use Vertuoza\Api\Graphql\Types;

final class Mutation extends ObjectType
{
public function __construct()
{
$config = [
'fields' => function () {
return [
'hello' => [
'type' => Types::string(),
'resolve' => function ($root, $args) {
return 'world';
}
],
...UnitTypeMutation::get(),
];
}
];
parent::__construct($config);
}
}
4 changes: 3 additions & 1 deletion src/graphql/Resolvers/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GraphQL\Type\Definition\ObjectType;
use Vertuoza\Api\Graphql\Resolvers\Settings\UnitTypes\UnitTypeQuery;
use Vertuoza\Api\Graphql\Resolvers\Settings\Collaborators\CollaboratorQuery;
use Vertuoza\Api\Graphql\Types;

final class Query extends ObjectType
Expand All @@ -19,7 +20,8 @@ public function __construct()
return 'world';
}
],
...UnitTypeQuery::get()
...UnitTypeQuery::get(),
...CollaboratorQuery::get(),
];
}
];
Expand Down
32 changes: 32 additions & 0 deletions src/graphql/Resolvers/Settings/Collaborators/Collaborator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Vertuoza\Api\Graphql\Resolvers\Settings\Collaborators;

use GraphQL\Type\Definition\NonNull;
use GraphQL\Type\Definition\ObjectType;
use Vertuoza\Api\Graphql\Types;

class Collaborator extends ObjectType
{
public function __construct()
{
parent::__construct([
'name' => 'Collaborator',
'description' => 'Collaborator"',
'fields' => static fn (): array => [
'id' => [
'description' => "Unique identifier of the collaborator",
'type' => New NonNull(Types::id())
],
'name' => [
'description' => "Name of the collaborator",
'type' => New NonNull(Types::string())
],
'firstName' => [
'description' => "First name of the collaborator",
'type' => New NonNull(Types::string())
],
],
]);
}
}
35 changes: 35 additions & 0 deletions src/graphql/Resolvers/Settings/Collaborators/CollaboratorQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Vertuoza\Api\Graphql\Resolvers\Settings\Collaborators;

use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NonNull;
use Vertuoza\Api\Context\VertuozaContext;
use Vertuoza\Api\Graphql\Context\RequestContext;
use Vertuoza\Api\Graphql\Types;

class CollaboratorQuery
{
static function get()
{
return [
'collaboratorsById' => [
'type' => new NonNull(Types::get(Collaborator::class)),
'args' => [
'id' => new NonNull(Types::ID()),
],
'resolve' => static fn ($rootValue, $args, RequestContext $context)
=> $context->useCases->collaborator
->collaboratorsById
->handle($args['id'], $context)
],
'collaborators' => [
'type' => new NonNull(new ListOfType(new NonNull(Types::get(Collaborator::class)))),
'resolve' => static fn ($rootValue, $args, RequestContext $context)
=> $context->useCases->collaborator
->collaboratorsFindMany
->handle($context)
],
];
}
}
28 changes: 28 additions & 0 deletions src/graphql/Resolvers/Settings/UnitTypes/UnitTypeInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Vertuoza\Api\Graphql\Resolvers\Settings\UnitTypes;

use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\NonNull;
use Vertuoza\Api\Graphql\Types;
use Vertuoza\Repositories\Settings\UnitTypes\UnitTypeMutationData;

class UnitTypeInput extends InputObjectType
{
public function __construct()
{
parent::__construct([
'name' => 'UnitTypeCreateInput',
'description' => 'UnitTypeCreateInput"',
'fields' => static fn (): array => [
'name' => [
'description' => "Name of the unit type",
'type' => New NonNull(Types::string())
],
],
'parseValue' => fn($values) => new UnitTypeMutationData(
$values['name']
)
]);
}
}
28 changes: 28 additions & 0 deletions src/graphql/Resolvers/Settings/UnitTypes/UnitTypeMutation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Vertuoza\Api\Graphql\Resolvers\Settings\UnitTypes;

use GraphQL\Type\Definition\NonNull;
use Vertuoza\Api\Graphql\Context\RequestContext;
use Vertuoza\Api\Graphql\Types;



class UnitTypeMutation
{
static function get()
{
return [
'unitTypeCreate' => [
'type' => (Types::get(UnitType::class)),
'args' => [
'input' => new NonNull(Types::get(UnitTypeInput::class)),
],
'resolve' => static fn ($rootValue, $args, RequestContext $context)
=> $context->useCases->unitType
->unitTypeCreate
->handle($args['input'], $context)
],
];
}
}
3 changes: 3 additions & 0 deletions src/repositories/RepositoriesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use Overblog\PromiseAdapter\PromiseAdapterInterface;
use Vertuoza\Repositories\Database\QueryBuilder;
use Vertuoza\Repositories\Settings\Collaborators\CollaboratorRepository;
use Vertuoza\Repositories\Settings\UnitTypes\UnitTypeRepository;

class RepositoriesFactory
{
public UnitTypeRepository $unitType;
public CollaboratorRepository $collaborator;

public function __construct(QueryBuilder $database, PromiseAdapterInterface $dataLoaderPromiseAdapter)
{
$this->unitType = new UnitTypeRepository($database, $dataLoaderPromiseAdapter);
$this->collaborator = new CollaboratorRepository($database, $dataLoaderPromiseAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Vertuoza\Repositories\Settings\Collaborators;

class CollaboratorMutationData
{
public string $name;
public string $firstName;
}
141 changes: 141 additions & 0 deletions src/repositories/Settings/Collaborators/CollaboratorRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Vertuoza\Repositories\Settings\Collaborators;

use Overblog\DataLoader\DataLoader;
use Overblog\PromiseAdapter\PromiseAdapterInterface;
use React\Promise\Promise;
use Vertuoza\Repositories\Database\QueryBuilder;
use Vertuoza\Repositories\Settings\Collaborators\Models\CollaboratorMapper;
use Vertuoza\Repositories\Settings\Collaborators\Models\CollaboratorModel;
use Vertuoza\Repositories\Settings\Collaborators\CollaboratorMutationData;

use function React\Async\async;

class CollaboratorRepository
{
protected array $getbyIdsDL;
private QueryBuilder $db;
protected PromiseAdapterInterface $dataLoaderPromiseAdapter;

public function __construct(
private QueryBuilder $database,
PromiseAdapterInterface $dataLoaderPromiseAdapter
) {
$this->db = $database;
$this->dataLoaderPromiseAdapter = $dataLoaderPromiseAdapter;
$this->getbyIdsDL = [];
}

private function fetchByIds(string $tenantId, array $ids)
{
return async(function () use ($tenantId, $ids) {
$query = $this->getQueryBuilder()
->where(function ($query) use ($tenantId) {
$query->where([CollaboratorModel::getTenantColumnName() => $tenantId])
->orWhere(CollaboratorModel::getTenantColumnName(), null);
});
$query->whereNull('deleted_at');
$query->whereIn(CollaboratorModel::getPkColumnName(), $ids);

$entities = $query->get()->mapWithKeys(function ($row) {
$entity = CollaboratorMapper::modelToEntity(CollaboratorModel::fromStdclass($row));
return [$entity->id => $entity];
});

// Map the IDs to the corresponding entities, preserving the order of IDs.
return collect($ids)
->map(fn ($id) => $entities->get($id))
->toArray();
})();
}

protected function getDataloader(string $tenantId): DataLoader
{
if (!isset($this->getbyIdsDL[$tenantId])) {

$dl = new DataLoader(function (array $ids) use ($tenantId) {
return $this->fetchByIds($tenantId, $ids);
}, $this->dataLoaderPromiseAdapter);
$this->getbyIdsDL[$tenantId] = $dl;
}

return $this->getbyIdsDL[$tenantId];
}


protected function getQueryBuilder()
{
return $this->db->getConnection()->table(CollaboratorModel::getTableName());
}

public function getByIds(array $ids, string $tenantId): Promise
{
return $this->getDataloader($tenantId)->loadMany($ids);
}

public function getById(string $id, string $tenantId): Promise
{
return $this->getDataloader($tenantId)->load($id);
}

public function countCollaboratorWithLabel(string $name, string $tenantId, string|int|null $excludeId = null)
{
return async(
fn () => $this->getQueryBuilder()
->where('label', $name)
->whereNull('deleted_at')
->where(function ($query) use ($excludeId) {
if (isset($excludeId))
$query->where('id', '!=', $excludeId);
})
->where(function ($query) use ($tenantId) {
$query->where(CollaboratorModel::getTenantColumnName(), '=', $tenantId)
->orWhereNull(CollaboratorModel::getTenantColumnName());
})
)();
}

public function findMany(string $tenantId)
{
return async(
fn () => $this->getQueryBuilder()
->whereNull('deleted_at')
->where(function ($query) use ($tenantId) {
$query->where(CollaboratorModel::getTenantColumnName(), '=', $tenantId)
->orWhereNull(CollaboratorModel::getTenantColumnName());
})
->get()
->map(function ($row) {
return CollaboratorMapper::modelToEntity(CollaboratorModel::fromStdclass($row));
})
)();
}

public function create(CollaboratorMutationData $data, string $tenantId): int|string
{
$newId = $this->getQueryBuilder()->insertGetId(
CollaboratorMapper::serializeCreate($data, $tenantId)
);
return $newId;
}

public function update(string $id, CollaboratorMutationData $data)
{
$this->getQueryBuilder()
->where(CollaboratorModel::getPkColumnName(), $id)
->update(CollaboratorMapper::serializeUpdate($data));

$this->clearCache($id);
}

private function clearCache(string $id)
{
foreach ($this->getbyIdsDL as $dl) {
if ($dl->key_exists($id)) {
$dl->clear($id);
return;
}
}
}
}
Loading