Skip to content

Repository files navigation

GraphQL Files Bundle

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") {
idurlfileNamemimeTypeextensionsizeresized(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") {
idurlfileNamemimeTypeextensionsize
}
}

Also bundle provides ImageField to use in your API like this:

{
me {
idfirstNamelastNameimage { // imagefieldfrombundleurlresized(width: 100, height: 100, mode: INSET) {
url
}
}
}
}

or you can add arguments directly to the image field for your convenience.

{
me {
idfirstNamelastNamesmall: image(width: 100, height: 100, mode: INSET) { // resizeddirectlyurl
} medium: image(width: 500, height: 300, mode: OUTBOUND) { // differentmodeurl
} fullSize: image {
url
}
}
}

How to use

1. Installation:

composer require youshido/graphql-files-bundle

2. Configuration:

2.1 Enable bundle in your AppKernel.php:

$bundles[] = newYoushido\GraphQLFilesBundle\GraphQLFilesBundle()

2.2. Add new routing in routing.yml:

graphql_file.image_resizer:
resource: "@GraphQLFilesBundle/Resources/config/routing.yml"

2.3. Configurate bundle in config.yml

This is full configuration and by default are not needed:

graph_ql_files:
image_driver: gd #imagine driver, can be gd, imagick or gmagickstorage: local #or s3platform: orm #or odmlocal: #config for local storageweb_root: "%kernel.root_dir%/../web"path_prefix: "uploads"s3: #config for s3 storage client: ~ #s3 client servicebucket: ~directory: ''models:
image_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\ImageValidationModelfile_validation_model: Youshido\GraphQLFilesBundle\Model\Validation\FileValidationModelorm:
image: Youshido\GraphQLFilesBundle\Entity\Imagefile: Youshido\GraphQLFilesBundle\Entity\Fileodm:
image: Youshido\GraphQLFilesBundle\Document\Imagefile: Youshido\GraphQLFilesBundle\Document\File

3. Set-up GraphQL schema:

3.1 Add UploadImageMutation to your MutationType:

<?phpuseYoushido\GraphQLFilesBundle\GraphQL\Field\UploadBase64ImageField;
useYoushido\GraphQLFilesBundle\GraphQL\Field\UploadImageField;
useYoushido\GraphQLFilesBundle\GraphQL\Field\UploadFileField;
class MutationType extends AbstractObjectType
{
publicfunctionbuild($config)
{
$config->addFields([
// imagesnewUploadBase64ImageField(),
newUploadImageField(),
// filesnewUploadFileField(),
// other mutations
]);
}
}

3.2 Add image field to your type:

useYoushido\GraphQLFilesBundle\GraphQL\Field\ImageField;
class YourType extends AbstractObjectType
{
publicfunctionbuild($config)
{
$config->addFields([
// your type fieldsnewImageField()
]);
}
}

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages