Skip to content

Commit

Permalink
Merge pull request #160 from chihiro-adachi/type-to-schema
Browse files Browse the repository at this point in the history
プロパティの型定義からスキーマを生成する
  • Loading branch information
kiy0taka authored Sep 21, 2023
2 parents b3730ac + 4f5402c commit caa5949
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
42 changes: 42 additions & 0 deletions GraphQL/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ private function createObjectType($className)
return $acc;
}, $fields);

// Entityのプロパティの型定義からスキーマを生成する
$props = $classMetadata->getReflectionClass()->getProperties();
$fields = array_reduce($props, function ($acc, \ReflectionProperty $prop) use ($className) {

if (!$this->entityAccessPolicy->canReadProperty($className, $prop->getName())) {
return $acc;
}

if (!$prop->hasType()) {
return $acc;
}

// Attributeがある場合はDoctrineのAttributeが指定されているものとみなす
if (count($prop->getAttributes()) > 0) {
return $acc;
}

$type = $this->convertPhpTypeToType($prop->getType());
if ($type) {
$acc[$prop->getName()] = $type;
}

return $acc;
}, $fields);

return $fields;
},
'entityClass' => $className,
Expand Down Expand Up @@ -135,4 +160,21 @@ private function isToManyAssociation($mapping)
{
return $mapping['type'] & ClassMetadata::TO_MANY;
}

private function convertPhpTypeToType(\ReflectionNamedType $refType)
{
$type = match ($refType->getName()) {
'int' => Type::int(),
'string' => Type::string(),
'bool' => Type::boolean(),
'float' => Type::float(),
default => null,
};

if ($type) {
return $refType->allowsNull() ? $type : Type::nonNull($type);
}

return null;
}
}
4 changes: 2 additions & 2 deletions Resource/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ services:
Eccube\Entity\Plugin: ['id', 'name', 'code', 'enabled', 'version', 'source', 'initialized', 'create_date', 'update_date']
Eccube\Entity\Product: ['id', 'name', 'note', 'description_list', 'description_detail', 'search_word', 'free_area', 'create_date', 'update_date', 'ProductCategories', 'ProductClasses', 'ProductImage', 'ProductTag', 'CustomerFavoriteProducts', 'Creator', 'Status']
Eccube\Entity\ProductCategory: ['product_id', 'category_id', 'Product', 'Category']
Eccube\Entity\ProductClass: ['id', 'code', 'stock', 'stock_unlimited', 'sale_limit', 'price01', 'price02', 'delivery_fee', 'visible', 'create_date', 'update_date', 'currency_code', 'point_rate', 'ProductStock', 'TaxRule', 'Product', 'SaleType', 'ClassCategory1', 'ClassCategory2', 'DeliveryDuration', 'Creator']
Eccube\Entity\ProductClass: ['id', 'code', 'stock', 'stock_unlimited', 'sale_limit', 'price01', 'price02', 'delivery_fee', 'visible', 'create_date', 'update_date', 'currency_code', 'point_rate', 'ProductStock', 'TaxRule', 'Product', 'SaleType', 'ClassCategory1', 'ClassCategory2', 'DeliveryDuration', 'Creator', 'price01_inc_tax', 'price02_inc_tax']
Eccube\Entity\ProductImage: ['id', 'file_name', 'sort_no', 'create_date', 'Product', 'Creator']
Eccube\Entity\ProductStock: ['id', 'stock', 'create_date', 'update_date', 'ProductClass', 'Creator']
Eccube\Entity\ProductTag: ['id', 'create_date', 'Product', 'Tag', 'Creator']
Expand Down Expand Up @@ -183,7 +183,7 @@ services:
Eccube\Entity\PaymentOption: ['delivery_id', 'payment_id', 'Delivery', 'Payment']
Eccube\Entity\Product: ['id', 'name', 'note', 'description_list', 'description_detail', 'search_word', 'free_area', 'ProductCategories', 'ProductClasses', 'ProductImage', 'ProductTag', 'CustomerFavoriteProducts', 'Status']
Eccube\Entity\ProductCategory: ['product_id', 'category_id', 'Product', 'Category']
Eccube\Entity\ProductClass: ['id', 'code', 'stock', 'stock_unlimited', 'sale_limit', 'price01', 'price02', 'delivery_fee', 'currency_code', 'point_rate', 'ProductStock', 'TaxRule', 'Product', 'SaleType', 'ClassCategory1', 'ClassCategory2', 'DeliveryDuration']
Eccube\Entity\ProductClass: ['id', 'code', 'stock', 'stock_unlimited', 'sale_limit', 'price01', 'price02', 'delivery_fee', 'currency_code', 'point_rate', 'ProductStock', 'TaxRule', 'Product', 'SaleType', 'ClassCategory1', 'ClassCategory2', 'DeliveryDuration', 'price01_inc_tax', 'price02_inc_tax']
Eccube\Entity\ProductImage: ['id', 'file_name', 'sort_no', 'Product']
Eccube\Entity\ProductStock: ['id', 'stock', 'ProductClass']
Eccube\Entity\ProductTag: ['id', 'Product', 'Tag']
Expand Down
2 changes: 1 addition & 1 deletion Tests/Form/Type/Front/EntryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function getFormData()
],
'phone_number' => $faker->phoneNumber,
'email' => $faker->safeEmail(),
'plain_password' => $faker->password($this->eccubeConfig['eccube_password_min_len'], $this->eccubeConfig['eccube_password_max_len']).'1A',
'plain_password' => $faker->password($this->eccubeConfig['eccube_password_min_len'], $this->eccubeConfig['eccube_password_max_len'] - 2).'1A',
'birth' => $faker->dateTimeBetween('-100 years', '-1 days')->format('Y-m-d\TH:i:sP'),
'sex' => $faker->numberBetween(1, 3),
'job' => $faker->numberBetween(1, 18),
Expand Down

0 comments on commit caa5949

Please sign in to comment.