Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 38b5ebed authored by Nicolas's avatar Nicolas
Browse files

Ajout du GraphQL

parent 3b9605a2
No related branches found
No related tags found
No related merge requests found
# Changelog # Changelog
## [1.7.12] - 2020-04-19
### Added
- Mise en place du GraphQL
- Ajout des dépendances pour le GraphQL
## [1.6.12] - 2020-04-19 ## [1.6.12] - 2020-04-19
### Added ### Added
- Mise en place de la classe PdfRepository pour supprimer le load (deprecated) - Mise en place de la classe PdfRepository pour supprimer le load (deprecated)
......
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\DataProvider;
use \NicolasBejean\MediaManager\Api\ImageRepositoryInterface;
use \NicolasBejean\MediaManager\Api\Data\ImageInterface;
use \Magento\Widget\Model\Template\FilterEmulate;
use \Magento\Framework\Exception\LocalizedException;
/**
* Class Image
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/DataProvider
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Image
{
/**
* @var ImageRepositoryInterface
*/
private $imageRepository;
/**
* @var FilterEmulate
*/
private $widgetFilter;
/**
* @param ImageRepositoryInterface $imageRepository
* @param FilterEmulate $widgetFilter
*/
public function __construct(
ImageRepositoryInterface $imageRepository,
FilterEmulate $widgetFilter
) {
$this->imageRepository = $imageRepository;
$this->widgetFilter = $widgetFilter;
}
/**
* Get Image data
*
* @param int $imageId
* @return string|array
*/
public function getData(int $imageId): array
{
try {
$image = $this->imageRepository->getById($imageId);
} catch (LocalizedException $e) {
return __('The image with the \'%1\' ID doesn\'t exist.', $imageId);
}
return [
ImageInterface::IMAGE_ID => $image->getId(),
ImageInterface::PATH => $image->getPath(),
ImageInterface::ALT => $image->getAlt()
];
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\DataProvider;
use \NicolasBejean\MediaManager\Api\PdfRepositoryInterface;
use \NicolasBejean\MediaManager\Api\Data\PdfInterface;
use \Magento\Widget\Model\Template\FilterEmulate;
use \Magento\Framework\Exception\LocalizedException;
/**
* Class Pdf
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/DataProvider
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Pdf
{
/**
* @var PdfRepositoryInterface
*/
private $pdfRepository;
/**
* @var FilterEmulate
*/
private $widgetFilter;
/**
* @param PdfRepositoryInterface $pdfRepository
* @param FilterEmulate $widgetFilter
*/
public function __construct(
PdfRepositoryInterface $pdfRepository,
FilterEmulate $widgetFilter
) {
$this->pdfRepository = $pdfRepository;
$this->widgetFilter = $widgetFilter;
}
/**
* Get Pdf data
*
* @param int $pdfId
* @return string|array
*/
public function getData(int $pdfId): array
{
try {
$pdf = $this->pdfRepository->getById($pdfId);
} catch (LocalizedException $e) {
return __('The PDF with the \'%1\' ID doesn\'t exist.', $pdfId);
}
return [
PdfInterface::PDF_ID => $pdf->getId(),
PdfInterface::PATH => $pdf->getPath(),
PdfInterface::IMAGE_PATH => $pdf->getImagePath(),
PdfInterface::ALT => $pdf->getAlt()
];
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\DataProvider;
use \NicolasBejean\MediaManager\Api\VideoRepositoryInterface;
use \NicolasBejean\MediaManager\Api\Data\VideoInterface;
use \Magento\Widget\Model\Template\FilterEmulate;
use \Magento\Framework\Exception\LocalizedException;
/**
* Class Video
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/DataProvider
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Video
{
/**
* @var VideoRepositoryInterface
*/
private $videoRepository;
/**
* @var FilterEmulate
*/
private $widgetFilter;
/**
* @param VideoRepositoryInterface $videoRepository
* @param FilterEmulate $widgetFilter
*/
public function __construct(
VideoRepositoryInterface $videoRepository,
FilterEmulate $widgetFilter
) {
$this->videoRepository = $videoRepository;
$this->widgetFilter = $widgetFilter;
}
/**
* Get Video data
*
* @param int $videoId
* @return string|array
*/
public function getData(int $videoId): array
{
try {
$video = $this->videoRepository->getById($videoId);
} catch (LocalizedException $e) {
return __('The video with the \'%1\' ID doesn\'t exist.', $videoId);
}
return [
VideoInterface::VIDEO_ID => $video->getId(),
VideoInterface::PATH => $video->getPath(),
VideoInterface::IMAGE_PATH => $video->getImagePath(),
VideoInterface::ALT => $video->getAlt()
];
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\Image;
use \NicolasBejean\MediaManager\Api\Data\ImageInterface;
use \NicolasBejean\MediaManager\Model\Image as ImageModel;
use \Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
/**
* Class Identity
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/Image
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Identity implements IdentityInterface
{
/** @var string */
private $cacheTag = ImageModel::CACHE_TAG;
/**
* Get image identities from resolved data
*
* @param array $resolvedData
* @return string[]
*/
public function getIdentities(array $resolvedData): array
{
$ids = [];
$items = $resolvedData['items'] ?? [];
foreach ($items as $item) {
if (is_array($item) && !empty($item[ImageInterface::IMAGE_ID])) {
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[ImageInterface::IMAGE_ID]);
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[ImageInterface::PATH]);
}
}
if (!empty($ids)) {
array_unshift($ids, $this->cacheTag);
}
return $ids;
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver;
use \NicolasBejean\MediaManager\Model\Resolver\DataProvider\Image as ImageDataProvider;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\GraphQl\Config\Element\Field;
use \Magento\Framework\GraphQl\Exception\GraphQlInputException;
use \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use \Magento\Framework\GraphQl\Query\ResolverInterface;
use \Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Class Images
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Images implements ResolverInterface
{
/**
* @var ImageDataProvider
*/
private $imageDataProvider;
/**
* @param ImageDataProvider $imageDataProvider
*/
public function __construct(
ImageDataProvider $imageDataProvider
) {
$this->imageDataProvider = $imageDataProvider;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$imageIds = $this->getImageIds($args);
$imagesData = $this->getImagesData($imageIds);
return [
'items' => $imagesData,
];
}
/**
* Get image id
*
* @param array $args
* @return string[]
* @throws GraphQlInputException
*/
private function getImageIds(array $args): array
{
if (!isset($args['id']) || !is_array($args['id']) || count($args['id']) === 0) {
throw new GraphQlInputException(__('"id" of Media Manager Images should be specified'));
}
return $args['id'];
}
/**
* Get images data
*
* @param array $imageIds
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getImagesData(array $imageIds): array
{
$imagesData = [];
foreach ($imageIds as $imageId) {
try {
$imagesData[$imageId] = $this->imageDataProvider->getData($imageId);
} catch (NoSuchEntityException $e) {
$imagesData[$imageId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
}
return $imagesData;
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\Pdf;
use \NicolasBejean\MediaManager\Api\Data\PdfInterface;
use \NicolasBejean\MediaManager\Model\Pdf as PdfModel;
use \Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
/**
* Class Identity
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/Pdf
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Identity implements IdentityInterface
{
/** @var string */
private $cacheTag = PdfModel::CACHE_TAG;
/**
* Get image identities from resolved data
*
* @param array $resolvedData
* @return string[]
*/
public function getIdentities(array $resolvedData): array
{
$ids = [];
$items = $resolvedData['items'] ?? [];
foreach ($items as $item) {
if (is_array($item) && !empty($item[PdfInterface::PDF_ID])) {
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[PdfInterface::PDF_ID]);
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[PdfInterface::PATH]);
}
}
if (!empty($ids)) {
array_unshift($ids, $this->cacheTag);
}
return $ids;
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver;
use \NicolasBejean\MediaManager\Model\Resolver\DataProvider\Pdf as PdfDataProvider;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\GraphQl\Config\Element\Field;
use \Magento\Framework\GraphQl\Exception\GraphQlInputException;
use \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use \Magento\Framework\GraphQl\Query\ResolverInterface;
use \Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Class Pdfs
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Pdfs implements ResolverInterface
{
/**
* @var PdfDataProvider
*/
private $pdfDataProvider;
/**
* @param PdfDataProvider $pdfDataProvider
*/
public function __construct(
PdfDataProvider $pdfDataProvider
) {
$this->pdfDataProvider = $pdfDataProvider;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$pdfIds = $this->getPdfIds($args);
$pdfsData = $this->getPdfsData($pdfIds);
return [
'items' => $pdfsData,
];
}
/**
* Get pdf id
*
* @param array $args
* @return string[]
* @throws GraphQlInputException
*/
private function getPdfIds(array $args): array
{
if (!isset($args['id']) || !is_array($args['id']) || count($args['id']) === 0) {
throw new GraphQlInputException(__('"id" of Media Manager Pdfs should be specified'));
}
return $args['id'];
}
/**
* Get pdfs data
*
* @param array $pdfIds
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getPdfsData(array $pdfIds): array
{
$pdfsData = [];
foreach ($pdfIds as $pdfId) {
try {
$pdfsData[$pdfId] = $this->pdfDataProvider->getData($pdfId);
} catch (NoSuchEntityException $e) {
$pdfsData[$pdfId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
}
return $pdfsData;
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver\Video;
use \NicolasBejean\MediaManager\Api\Data\VideoInterface;
use \NicolasBejean\MediaManager\Model\Video as VideoModel;
use \Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
/**
* Class Identity
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver/Video
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Identity implements IdentityInterface
{
/** @var string */
private $cacheTag = VideoModel::CACHE_TAG;
/**
* Get image identities from resolved data
*
* @param array $resolvedData
* @return string[]
*/
public function getIdentities(array $resolvedData): array
{
$ids = [];
$items = $resolvedData['items'] ?? [];
foreach ($items as $item) {
if (is_array($item) && !empty($item[VideoInterface::VIDEO_ID])) {
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[VideoInterface::VIDEO_ID]);
$ids[] = sprintf('%s_%s', $this->cacheTag, $item[VideoInterface::PATH]);
}
}
if (!empty($ids)) {
array_unshift($ids, $this->cacheTag);
}
return $ids;
}
}
<?php
declare(strict_types=1);
namespace NicolasBejean\MediaManager\Model\Resolver;
use \NicolasBejean\MediaManager\Model\Resolver\DataProvider\Video as VideoDataProvider;
use \Magento\Framework\Exception\NoSuchEntityException;
use \Magento\Framework\GraphQl\Config\Element\Field;
use \Magento\Framework\GraphQl\Exception\GraphQlInputException;
use \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use \Magento\Framework\GraphQl\Query\ResolverInterface;
use \Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Class Videos
*
* @category PHP
* @package NicolasBejean\MediaManager\Model/Resolver
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/mediamanager/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class Videos implements ResolverInterface
{
/**
* @var VideoDataProvider
*/
private $videoDataProvider;
/**
* @param VideoDataProvider $videoDataProvider
*/
public function __construct(
VideoDataProvider $videoDataProvider
) {
$this->videoDataProvider = $videoDataProvider;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$videoIds = $this->getVideoIds($args);
$videosData = $this->getVideosData($videoIds);
return [
'items' => $videosData,
];
}
/**
* Get video id
*
* @param array $args
* @return string[]
* @throws GraphQlInputException
*/
private function getVideoIds(array $args): array
{
if (!isset($args['id']) || !is_array($args['id']) || count($args['id']) === 0) {
throw new GraphQlInputException(__('"id" of Media Manager Videos should be specified'));
}
return $args['id'];
}
/**
* Get videos data
*
* @param array $videoIds
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getVideosData(array $videoIds): array
{
$videosData = [];
foreach ($videoIds as $videoId) {
try {
$videosData[$videoId] = $this->videoDataProvider->getData($videoId);
} catch (NoSuchEntityException $e) {
$videosData[$videoId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
}
}
return $videosData;
}
}
...@@ -2,12 +2,18 @@ ...@@ -2,12 +2,18 @@
"name": "nicolasbejean/mediamanager", "name": "nicolasbejean/mediamanager",
"description": "Gestionnaire de médias", "description": "Gestionnaire de médias",
"type": "magento2-module", "type": "magento2-module",
"version": "1.6.12", "version": "1.7.12",
"require": { "require": {
"php": "~7.1.3||~7.2.0||~7.3.0", "php": "~7.1.3||~7.2.0||~7.3.0",
"magento/framework": "102.0.*",
"magento/module-widget": "101.0.*", "magento/module-widget": "101.0.*",
"nicolasbejean/base": ">=1.3.2" "nicolasbejean/base": ">=1.3.2"
}, },
"suggest": {
"magento/module-graph-ql": "100.3.*",
"magento/module-graph-ql-cache": "100.3.*",
"magento/module-store-graph-ql": "100.3.*"
},
"license": [ "license": [
"OSL-3.0", "OSL-3.0",
"AFL-3.0" "AFL-3.0"
......
<?xml version="1.0"?> <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="NicolasBejean_MediaManager" setup_version="1.6.12"> <module name="NicolasBejean_MediaManager" setup_version="1.7.12">
<sequence> <sequence>
<module name="Magento_Widget" /> <module name="Magento_Widget" />
<module name="NicolasBejean_Base"/> <module name="NicolasBejean_Base"/>
......
type Query {
MediaManagerImage (
id: [Int] @doc(description: "ID of the Media Manager Image")
): MediaManagerImages @resolver(class: "NicolasBejean\\MediaManager\\Model\\Resolver\\Images") @doc(description: "The Media Manager Image query returns information about Image") @cache(cacheIdentity: "NicolasBejean\\MediaManager\\Model\\Resolver\\Image\\Identity")
MediaManagerPdf (
id: [Int] @doc(description: "ID of the Media Manager PDF")
): MediaManagerPdfs @resolver(class: "NicolasBejean\\MediaManager\\Model\\Resolver\\Pdfs") @doc(description: "The Media Manager PDF query returns information about PDF") @cache(cacheIdentity: "NicolasBejean\\MediaManager\\Model\\Resolver\\Pdf\\Identity")
MediaManagerVideo (
id: [Int] @doc(description: "ID of the Media Manager Video")
): MediaManagerVideos @resolver(class: "NicolasBejean\\MediaManager\\Model\\Resolver\\Videos") @doc(description: "The Media Manager Video query returns information about Video") @cache(cacheIdentity: "NicolasBejean\\MediaManager\\Model\\Resolver\\Video\\Identity")
}
type MediaManagerImages @doc(description: "Images information") {
items: [MediaManagerImage] @doc(description: "An array of Image")
}
type MediaManagerImage @doc(description: "Media Manager Image defines all Images information") {
image_path: String @doc(description: "Media Manager Image path")
alt: String @doc(description: "Media Manager Image alt")
}
type MediaManagerPdfs @doc(description: "Pdfs information") {
items: [MediaManagerPdf] @doc(description: "An array of PDF")
}
type MediaManagerPdf @doc(description: "Media Manager PDF defines all Pdfs information") {
pdf_path: String @doc(description: "Media Manager PDF Path")
image_path: String @doc(description: "Media Manager PDF Image path")
alt: String @doc(description: "Media Manager PDF Image alt")
}
type MediaManagerVideos @doc(description: "Videos information") {
items: [MediaManagerVideo] @doc(description: "An array of Video")
}
type MediaManagerVideo @doc(description: "Media Manager Video defines all Videos information") {
video_path: String @doc(description: "Media Manager Video path")
image_path: String @doc(description: "Media Manager Video Image path")
alt: String @doc(description: "Media Manager Video Image alt")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment