To build your application you probably need images, video's or maybe even a presentation too.
The GraphQL Media Service handles all those media assets and centralizes them so you can find your content
just the way you like it: fast and efficiently., (*1)
- [X] Single endpoint to upload your files trough a API.
- [X] GraphQL Object for files to get details and download url.
- [X] Public and Private files using signed urls
- [X] Direct relations between files and entities
Installation
Install using composer:, (*2)
composer require graphql-media-service
How its works?
Usage
The following steps assume you have a configured GraphQLAPI using
graphql-bundle., (*3)
Add the following config in your config.yml
, (*4)
#config.yml
media_service:
class: AppBundle\Entity\File
default_storage: public_files
storage:
public_files:
local:
dir_name: "%kernel.root_dir%/../public/uploads"
base_url: 'http://example.com/uploads'
For performance reasons public files are served directly thought the http server,
then the base_url
must be a valid public accessible folder where the files are located., (*5)
Create a new entity File
, (*6)
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ynlo\GraphQLBundle\Annotation as GraphQL;
use Ynlo\GraphQLMediaService\Model\AbstractFile;
/**
* @ORM\Entity()
* @ORM\Table()
*
* @GraphQL\ObjectType()
*/
class File extends AbstractFile
{
}
At this point you must have a mutation called uploadFile
in your graphql schema,
see graphql-multipart-request-spec
for details of using multipart form data to upload files., (*7)
Assign uploaded files to existent object
Upload files to the server is only the first step,
you must able to link that files to existent objects.
For example link a uploaded photo to the user profile., (*8)
Create a field to store the relation on a existent entity:, (*9)
/**
* @ORM\Entity()
* @ORM\Table()
*
* @GraphQL\ObjectType()
*/
class Profile implements NodeInterface
{
//....
/**
* @var File
*
* @ORM\OneToOne(targetEntity="AppBundle\Entity\File", orphanRemoval=true)
*
* @GraphQL\Expose()
*
* @MediaService\AttachFile()
*/
protected $photo;
Note the annotation @MediaService\AttachFile()
is required on properties linked to
Files in order to resolve some parameters like the url
in runtime., (*10)
@TODO ..., (*11)