Skip to content

Commit

Permalink
fix entity_id, add graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyraul committed Sep 19, 2024
1 parent 5fec224 commit be0ecb5
Show file tree
Hide file tree
Showing 62 changed files with 362 additions and 141 deletions.
3 changes: 2 additions & 1 deletion .jeeves.phpunit_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ Mygento:
api: true
cacheable: true
readonly: true
graphql: true
cache_tag: samp_poster
columns:
id:
entity_id:
type: int
pk: true
unsigned: true
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ look in ```.jeeves.phpunit_v1.yaml``` or ```.jeeves.phpunit_v0.yaml```
| tablename | String | N | %vendor%_%module%_%entity%
| comment | String | N
| api | Boolean | N | false
| graphql | Boolean | N | false
| cacheable | Boolean | N | false
| cache_tag | String | N
| per_store | Boolean | N | false
Expand Down
2 changes: 1 addition & 1 deletion src/Jeeves/Generators/Crud/Interfaces/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function genModelInterface(
$set->addComment('@return $this');
}

if ($this->snakeCaseToCamelCase($name) == 'id') {
if (in_array($this->snakeCaseToCamelCase($name), ['id', 'entityId'])) {
$param->setNullable(false);
$param->setType(null);

Expand Down
8 changes: 6 additions & 2 deletions src/Jeeves/Generators/Crud/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ public function genModel(
$getter->setReturnNullable($generated ? true : !$notNullable);

$setter->setReturnType('self');
$setParam->setType($this->convertType($value['type']));
$setParam->setNullable(!$notNullable);
if ($name === 'entity_id') {
$setter->addComment('@param ' . $this->convertType($value['type']) . ' $' . $this->snakeCaseToCamelCase($name));
} else {
$setParam->setType($this->convertType($value['type']));
$setParam->setNullable(!$notNullable);
}

if ($this->snakeCaseToCamelCase($name) == 'id') {
$setParam->setNullable(false);
Expand Down
2 changes: 1 addition & 1 deletion src/Jeeves/Generators/Shipping/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getTitle(): array
'sortOrder' => '20',
]),
self::V => [
'label' => 'Method Title',
'label' => 'Carrier Title',
'validate' => 'required-entry',
],
];
Expand Down
10 changes: 10 additions & 0 deletions src/Jeeves/Model/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ private function generate(Crud\Entity $entity): Crud\Result
$this->generateAdminUI($entity);
}

if ($entity->hasGraphQL()) {
$this->generateGraphQL($entity);
}

$acl = [new Acl($entity->getEntityAcl(), $entity->getEntityAclTitle())];
$dbschema = $this->generateDbSchema($entity);
$events = $this->generateEvents($entity);
Expand Down Expand Up @@ -240,6 +244,12 @@ private function generateModels(Crud\Entity $entity)
$generator->generateModels($entity);
}

private function generateGraphQL(Crud\Entity $entity)
{
$generator = new Crud\GraphQL($this->io);
$generator->generateSchema($entity);
}

private function generateRepository(Crud\Entity $entity)
{
$generator = new Crud\Repository($this->io);
Expand Down
2 changes: 1 addition & 1 deletion src/Jeeves/Model/Crud/Controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mygento\Jeeves\Model\Crud;

use Mygento\Jeeves\Generators\Crud\Controllers as Generators;
use Mygento\Jeeves\Generators\Crud\Controllers as Generators;
use Mygento\Jeeves\IO\IOInterface;
use Mygento\Jeeves\Model\Generator;

Expand Down
7 changes: 7 additions & 0 deletions src/Jeeves/Model/Crud/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Entity extends Generator
private Module $module;
private $api;
private $gui;
private bool $graphql = false;
private bool $readonly = false;
private bool $withStore = false;
private $tablename;
Expand Down Expand Up @@ -105,6 +106,7 @@ public function setConfig(array $config)
$this->readonly = $config['readonly'] ?? false;
$this->withStore = $config['per_store'] ?? false;
$this->comment = $config['comment'] ?? null;
$this->graphql = $config['graphql'] ?? false;

$this->tablename = $config['tablename'] ??
$this->getConverter()->camelCaseToSnakeCase($this->module->getVendor())
Expand Down Expand Up @@ -171,6 +173,11 @@ public function hasApi(): bool
return $this->api;
}

public function hasGraphQL(): bool
{
return $this->graphql;
}

public function getEntityAcl()
{
return $this->getFullname() . '::' . $this->getEntityLowercase();
Expand Down
86 changes: 86 additions & 0 deletions src/Jeeves/Model/Crud/GraphQL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Mygento\Jeeves\Model\Crud;

use Mygento\Jeeves\IO\IOInterface;
use Mygento\Jeeves\Model\Generator;

class GraphQL extends Generator
{
public function __construct(IOInterface $io)
{
$this->setIO($io);
}

public function generateSchema(Entity $entity)
{
$filePath = $entity->getPath() . '/etc/';
$columns = $this->getFields($entity);
$this->writeFile(
$filePath . 'schema.graphqls',
implode(PHP_EOL, [
'type ' . $entity->getEntityApiName() . ' @doc(description: "' . ($entity->getComment() ?: $entity->getPrintName()) . '") {',
implode(
PHP_EOL,
$columns
),
'}',
])
);
}

private function getFields(Entity $entity)
{
$result = array_map(
function (string $column, array $param) {
if (isset($param['pk']) && $param['pk'] === true) {
return null;
}

$nullable = $param['nullable'] ?? true;

switch ($param['type']) {
case 'boolean':
$nullable = false;
$type = 'Boolean';
break;
case 'blob':
case 'varbinary':
return null;
case 'int':
case 'smallint':
case 'bigint':
case 'tinyint':
$type = 'Int';
break;
case 'price':
$type = 'Money';
break;
case 'real':
case 'decimal':
case 'float':
case 'double':
$type = 'Float';
break;
case 'text':
case 'mediumtext':
case 'longtext':
case 'date':
case 'datetime':
case 'timestamp':
case 'varchar':
$type = 'String';
break;
default:
throw new \Exception('Error column type');
}

return self::TAB . $column . ': ' . $type . (!$nullable ? '!' : '') . ' @doc(description: "' . ($param['comment'] ?? ucfirst($column)) . '")';
},
array_keys($entity->getColumns()),
$entity->getColumns()
);

return array_filter($result);
}
}
4 changes: 2 additions & 2 deletions src/Jeeves/Model/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ private function generate(string $carrier, Module $mod): Shipping\Result
$result->updateCarrierConfigs([
$carrier => [
'active' => 0,
'title' => $carrier,
'code' => $carrier,
],
]);
$result->updateDefaultConfigs([
$carrier => [
'active' => 0,
'name' => $carrier,
'title' => $carrier,
'title' => $this->getConverter()->getEntityPrintName($carrier),
'debug' => '0',
'test' => '1',
'model' => $this->mod->getNamespace() . '\Model\Carrier',
Expand Down
4 changes: 2 additions & 2 deletions src/Jeeves/Model/Shipping/Configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ private function generateAdminConfig(array $entity, string $namespace): array
$generator = new System();

return [
'label' => $this->getConverter()->getEntityPrintName($entity['title']),
'label' => $this->getConverter()->getEntityPrintName($entity['code']),
$generator->getEnabled(),
$generator->getTitle(),
$generator->getSort(),
$generator->getTest(),
$generator->getDebug(),
$generator->getAuthGroup(),
$generator->getOptionsGroup(),
$generator->getPackageGroup($entity['title']),
$generator->getPackageGroup($entity['code']),
$generator->getTaxGroup($namespace),
$generator->getOrderStatusGroup(),
$generator->getMarkingGroup(),
Expand Down
1 change: 1 addition & 0 deletions test/Crud/CrudV1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function testCrudV1(string $path)
$this->checkXml('etc/events.xml');
$this->checkXml('etc/module.xml');
$this->checkFile('registration.php');
$this->checkFile('etc/schema.graphqls');
}

private function checkInterfaces()
Expand Down
25 changes: 19 additions & 6 deletions test/Expectations/Crud/81/v1/Api/Data/PosterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
interface PosterInterface extends IdentityInterface
{
public const CACHE_TAG = 'samp_poster';
public const ID = 'id';
public const ENTITY_ID = 'entity_id';
public const NAME = 'name';
public const SUBNAME = 'subname';
public const FAMILY = 'family';
public const ACTIVE = 'active';
public const PRODUCT_ID = 'product_id';

/**
* Get id
* Get entity id
* @return int|null
*/
public function getId(): ?int;
public function getEntityId(): ?int;

/**
* Set id
* @param int $id
* Set entity id
* @param int $entityId
* @return $this
*/
public function setId($id): self;
public function setEntityId($entityId): self;

/**
* Get name
Expand Down Expand Up @@ -89,4 +89,17 @@ public function getProductId(): ?int;
* @return $this
*/
public function setProductId(?int $productId): self;

/**
* Get ID
* @return int|null
*/
public function getId(): ?int;

/**
* Set ID
* @param int $id
* @return $this
*/
public function setId($id): self;
}
2 changes: 1 addition & 1 deletion test/Expectations/Crud/81/v1/Model/Card/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
string $requestFieldName,
array $meta = [],
array $data = [],
PoolInterface $pool = null,
?PoolInterface $pool = null,
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
string $requestFieldName,
array $meta = [],
array $data = [],
PoolInterface $pool = null,
?PoolInterface $pool = null,
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
string $requestFieldName,
array $meta = [],
array $data = [],
PoolInterface $pool = null,
?PoolInterface $pool = null,
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);

Expand Down
31 changes: 24 additions & 7 deletions test/Expectations/Crud/81/v1/Model/Poster.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ public function getIdentities(): array
}

/**
* Get id
* Get entity id
*/
public function getId(): ?int
public function getEntityId(): ?int
{
return $this->getData(self::ID);
return $this->getData(self::ENTITY_ID);
}

/**
* Set id
* @param int $id
* Set entity id
* @param int $entityId
*/
public function setId($id): self
public function setEntityId($entityId): self
{
return $this->setData(self::ID, $id);
return $this->setData(self::ENTITY_ID, $entityId);
}

/**
Expand Down Expand Up @@ -112,6 +112,23 @@ public function setProductId(?int $productId): self
return $this->setData(self::PRODUCT_ID, $productId);
}

/**
* Get ID
*/
public function getId(): ?int
{
return $this->getData(self::ENTITY_ID);
}

/**
* Set ID
* @param int $id
*/
public function setId($id): self
{
return $this->setData(self::ENTITY_ID, $id);
}

/**
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion test/Expectations/Crud/81/v1/Model/Poster/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
string $requestFieldName,
array $meta = [],
array $data = [],
PoolInterface $pool = null,
?PoolInterface $pool = null,
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);

Expand Down
2 changes: 1 addition & 1 deletion test/Expectations/Crud/81/v1/Model/ResourceModel/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
private readonly EntityManager $entityManager,
private readonly MetadataPool $metadataPool,
Context $context,
string $connectionName = null,
?string $connectionName = null,
) {
parent::__construct($context, $connectionName);
}
Expand Down
Loading

0 comments on commit be0ecb5

Please sign in to comment.