From 2f30437dd7f588496946e548c14cddc0265cc60a Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Tue, 12 Dec 2023 16:28:14 -0700 Subject: [PATCH] Simplified addAssociations --- src/Type/Entity.php | 85 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/Type/Entity.php b/src/Type/Entity.php index 7d8a2d23..313e570c 100644 --- a/src/Type/Entity.php +++ b/src/Type/Entity.php @@ -178,50 +178,49 @@ protected function addAssociations(array &$fields): void } $associationMetadata = $classMetadata->getAssociationMapping($associationName); - - switch ($associationMetadata['type']) { - case ClassMetadataInfo::ONE_TO_ONE: - case ClassMetadataInfo::MANY_TO_ONE: - case ClassMetadataInfo::TO_ONE: - $targetEntity = $associationMetadata['targetEntity']; - $fields[$associationName] = function () use ($targetEntity) { - $entity = $this->typeManager->build(self::class, $targetEntity); - - return [ - 'type' => $entity->getGraphQLType(), - 'description' => $entity->getDescription(), - ]; - }; - break; - case ClassMetadataInfo::ONE_TO_MANY: - case ClassMetadataInfo::MANY_TO_MANY: - case ClassMetadataInfo::TO_MANY: - $targetEntity = $associationMetadata['targetEntity']; - $fields[$associationName] = function () use ($targetEntity, $associationName) { - $entity = $this->typeManager->build(self::class, $targetEntity); - $shortName = $this->getTypeName() . '_' . $associationName; - - return [ - 'type' => $this->typeManager->build( - Connection::class, - $shortName . '_Connection', - $entity->getGraphQLType(), - ), - 'args' => [ - 'filter' => $this->filterFactory->get( - $entity, - $this, - $associationName, - $this->metadata['fields'][$associationName], - ), - 'pagination' => $this->typeManager->get('pagination'), - ], - 'description' => $this->metadata['fields'][$associationName]['description'], - 'resolve' => $this->collectionFactory->get($entity), - ]; - }; - break; + if ( + in_array($associationMetadata['type'], [ + ClassMetadataInfo::ONE_TO_ONE, + ClassMetadataInfo::MANY_TO_ONE, + ClassMetadataInfo::TO_ONE, + ]) + ) { + $targetEntity = $associationMetadata['targetEntity']; + $fields[$associationName] = function () use ($targetEntity) { + $entity = $this->typeManager->build(self::class, $targetEntity); + + return [ + 'type' => $entity->getGraphQLType(), + 'description' => $entity->getDescription(), + ]; + }; } + + // Collections + $targetEntity = $associationMetadata['targetEntity']; + $fields[$associationName] = function () use ($targetEntity, $associationName) { + $entity = $this->typeManager->build(self::class, $targetEntity); + $shortName = $this->getTypeName() . '_' . $associationName; + + return [ + 'type' => $this->typeManager->build( + Connection::class, + $shortName . '_Connection', + $entity->getGraphQLType(), + ), + 'args' => [ + 'filter' => $this->filterFactory->get( + $entity, + $this, + $associationName, + $this->metadata['fields'][$associationName], + ), + 'pagination' => $this->typeManager->get('pagination'), + ], + 'description' => $this->metadata['fields'][$associationName]['description'], + 'resolve' => $this->collectionFactory->get($entity), + ]; + }; } } }