Symfony bundle for easy implementation images and files to your GraphQL API (bundle with GraphQL implementation and its documentation is here). Bundle provides UploadImageMutation
:
mutation {
uploadImage(field: "file") {
id
url
fileName
mimeType
extension
size
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
.
Upload file mutation:
mutation {
uploadFile(field: "file") {
id
url
fileName
mimeType
extension
size
}
}
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/graphql-files-bundle
$bundles[] = new Youshido\GraphQLFilesBundle\GraphQLFilesBundle()
graphql_file.image_resizer:
resource: "@GraphQLFilesBundle/Resources/config/routing.yml"
This is full configuration and by default are not needed:
graph_ql_files:
image_driver: gd #imagine driver, can be gd, imagick or gmagick
storage: local #or s3
platform: orm #or odm
local: #config for local storage
web_root: "%kernel.root_dir%/../web"
path_prefix: "uploads"
s3: #config for s3 storage
client: ~ #s3 client service
bucket: ~
directory: ''
models:
image_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\ImageValidationModel
file_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\FileValidationModel
orm:
image: Youshido\GraphQLFilesBundle\Entity\Image
file: Youshido\GraphQLFilesBundle\Entity\File
odm:
image: Youshido\GraphQLFilesBundle\Document\Image
file: Youshido\GraphQLFilesBundle\Document\File
<?php
use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadBase64ImageField;
use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadImageField;
use Youshido\GraphQLFilesBundle\GraphQL\Field\UploadFileField;
class MutationType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
// images
new UploadBase64ImageField(),
new UploadImageField(),
// files
new UploadFileField(),
// other mutations
]);
}
}
use Youshido\GraphQLFilesBundle\GraphQL\Field\ImageField;
class YourType extends AbstractObjectType
{
public function build($config)
{
$config->addFields([
// your type fields
new ImageField()
]);
}
}