Skip to content

Commit

Permalink
プロパティの型定義からスキーマを生成する
Browse files Browse the repository at this point in the history
  • Loading branch information
chihiro-adachi committed Sep 20, 2023
1 parent b3730ac commit a616797
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions GraphQL/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ 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;
}

$acc[$prop->getName()] = [
'type' => $this->convertPhpTypeToType($prop->getType()),
];

return $acc;
}, $fields);

return $fields;
},
'entityClass' => $className,
Expand Down Expand Up @@ -135,4 +159,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

0 comments on commit a616797

Please sign in to comment.