Symfony bundle for easy implementation images to your GraphQL API ( bundle with GraphQL implementation and its documentation is here ). Bundle provides UploadImageMutation
:
mutation {
uploadImage(field: "image") {
id
url
resized(width: 100, height: 100, mode: INSET) {
url
}
}
}
Mutation assumes that request content-type is multipart/form-data
and include image data in field that is passed as argument field
.
Also bundle provides ImageField
to use in your API like this:
{
me {
id
firstName
lastName
image { // image field from bundle
url
resized(width: 100, height: 100, mode: INSET) {
url
}
}
}
}
or you can add arguments directly to the image field for your convenience.
{
me {
id
firstName
lastName
small: image(width: 100, height: 100, mode: INSET) { // resized directly
url
}
medium: image(width: 500, height: 300, mode: OUTBOUND) { // different mode
url
}
fullSize: image {
url
}
}
}
composer require youshido/api-images
$bundles[] = new Youshido\ImagesBundle\ImagesBundle()
images:
resource: "@ImagesBundle/Controller/"
type: annotation
images:
web_root: "%kernel.root_dir%/../web" #your app web root
path_prefix: "uploads/images" #folder in web root where images will be stored
platform: orm #orm or odm
driver: gd #imagine driver, can be gd, imagick or gmagick
Add image property and implement ImageableInterface
to your entity:
<?php
use Youshido\ImagesBundle\Entity\Interfaces\ImageableInterface;
/**
* @ORM\Entity
*/
class YourEntity implements ImageableInterface
{
// other your properties
/**
* @var Image
*
* @ORM\ManyToOne(targetEntity="Youshido\ImagesBundle\Entity\Image")
*/
private $image;
/**
* @return Image
*/
public function getImage()
{
return $this->image;
}
/**
* @param Image $image
*
* @return User
*/
public function setImage(Image $image = null)
{
$this->image = $image;
return $this;
}
Use ImageableTrait
and implement ImageableInterface
to your entity:
<?php
use Youshido\ImagesBundle\Document\Interfaces\ImageableInterface;
use Youshido\ImagesBundle\Document\Traits\ImageableTrait;
/**
* @MongoDB\Document()
*/
class Category implements ImageableInterface
{
use ImageableTrait;
//other properties
<?php
use Youshido\ImagesBundle\GraphQL\Field\Mutation\UploadImageField;
class MutationType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
new UploadImageField(),
// other mutations
]);
}
}
use Youshido\ImagesBundle\GraphQL\Field\Query\ImageField;
class CategoryType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
// category fields
new ImageField()
]);
}
}
//for odm
$image = $dm->getRepository(Image::class)->find($imageId);
$yourEntity->setImage($image ? new EmbeddedImage($image) : null);
//for orm
$image = $em->getRepository(Image::class)->find($imageId);
$yourEntity->setImage($image);