From a6167972fe63901dc3ad9d17c10e9ca40bfcfb03 Mon Sep 17 00:00:00 2001 From: Chihiro Adachi <8196725+chihiro-adachi@users.noreply.github.com> Date: Wed, 20 Sep 2023 16:12:47 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=83=91=E3=83=86=E3=82=A3?= =?UTF-8?q?=E3=81=AE=E5=9E=8B=E5=AE=9A=E7=BE=A9=E3=81=8B=E3=82=89=E3=82=B9?= =?UTF-8?q?=E3=82=AD=E3=83=BC=E3=83=9E=E3=82=92=E7=94=9F=E6=88=90=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GraphQL/Types.php | 41 +++++++++++++++++++++++++++++++++++ Resource/config/services.yaml | 4 ++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/GraphQL/Types.php b/GraphQL/Types.php index 4dd1a4a..7506508 100644 --- a/GraphQL/Types.php +++ b/GraphQL/Types.php @@ -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, @@ -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; + } } diff --git a/Resource/config/services.yaml b/Resource/config/services.yaml index c710ea6..ff2a103 100644 --- a/Resource/config/services.yaml +++ b/Resource/config/services.yaml @@ -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'] @@ -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']