Code Monkey home page Code Monkey logo

laravel-medialibrary-graphql's Introduction

Laravel Medialibrary GraphQL

This package contains GraphQL queries and mutations to manage any type of media files and make them attacheable to any given model.

About

We use Lighthouse for GraphQL.

The management of the mediafiles is based on the spatie/laravel-medialibrary package.

By default this package uses Model from config auth.providers.users.model for assign files.
But you can change this after publish package config and change 'laravel-medialibrary-graphql.models.default' value.

Installation

Require the package through composer.

composer require marqant-lab/laravel-medialibrary-graphql

You will have to run the migrations to setup the media table.

php artisan migrate

Publish the configuration.

php artisan vendor:publish --provider="Marqant\LaravelMediaLibraryGraphQL\Providers\LaravelMediaLibraryGraphQLServiceProvider" --tag=config

In this config you can specify a model to assign files to ('models.default') and many other settings. The model should implements Spatie\MediaLibrary\HasMedia interface and use Spatie\MediaLibrary\InteractsWithMedia trait.

For example User model:

<?php
...
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
...
class User extends ... implements HasMedia
{
    use InteractsWithMedia;
    
    // ...

Also you can add as many models as you need.
Example of config:

/**
     * Model(s) to attach media files to
     *
     * by default User model
     *
     * you can specify any model to attach media files
     */
    'models' => [
        'default' => config('auth.providers.users.model'),
        'one_more_model' => \Some\Namespace\Model::class,
    ],

But in this case you need to send 'model' param for getMedia() query and uploadFile() and deleteAllMedia() mutations.
If 'model' param is empty (null) then 'models.default' model will be taken to attach files.

If you need Spatie\MediaLibrary config:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

If you plan to use the web route 'media/download/' to download files, add the MEDIA_API_KEY variable to your .env file to secure your application's downloads with an api key.

You need to set this key as 'apiKey' at headers.

GET http://your.awesome.site/media/download/4bb0e054-e98f-4906-b3f5-0277fd63a194/  
Content-Type: application/json  
apiKey: {your_secure_api_key}  

This package uses it's own @guardMedia directive for secure.
By default 'guard' in config is 'sanctum' but you can change it after publish package's config file.
You can setup our marqant-lab/auth-graphql package for auth and follow all instructions of the package.

After this add import to your schema.graphql

#import ../vendor/marqant-lab/lighthouse-json/graphql/*.graphql
#import ../vendor/marqant-lab/laravel-medialibrary-graphql/graphql/*.graphql

Queries

Query Requires input Returns
getMedia id: Int! (ID of the model need to delete all files from), [Media]
model: String (model key from config, if null 'default'
model will be taken)
downloadMedia uuid: String! String!

Mutations

Mutation Requires input Returns
uploadFile id: Int! (ID of the model need to attach file to), [Media]
file: Upload!,
model: String (model key from config,
if null 'default' model will be taken),
name: String, properties: Json
deleteMedia uuid: String! String
deleteAllMedia id: Int! (ID of the model need to delete all files from), String
model: String (model key from config,
if null 'default' model will be taken),

uploadFile mutation example:

mutation UploadFile($id: Int!, $file: Upload!, $model: String, $name: String, $properties: Json) {
  uploadFile(id: $id, file: $file, model: $model, name: $name, properties: $properties) {
    id
    name
    fileName
    path
    url
    downloadUrl
    properties
    type
    uuid
    createdAt
    updatedAt
  }
}
{
    "id": 1,
    "model": null,
    "name": "PDF file",
    "properties": {
        "title": "test title",
        "description": "test description"
    }
}

plus 'file' type Upload (models)

response example:

{
  "data": {
    "uploadFile": [
      {
        "name": "PDF file",
        "fileName": "001.pdf",
        "path": "",
        "url": "",
        "downloadUrl": "http://your.awesome.site/media/download/4bb0e054-e98f-4906-b3f5-0277fd63a194/",
        "properties": "{\"title\":\"test title\",\"description\":\"test description\"}",
        "type": "pdf",
        "uuid": "4bb0e054-e98f-4906-b3f5-0277fd63a194"
      },
      ...

Tests

If you want to execute package tests add this to the phpunit.xml

<testsuite name="LaravelMedialibraryGraphQL">
    <directory suffix="Test.php">./vendor/marqant-lab/laravel-medialibrary-graphql/tests</directory>
</testsuite>

And after you can check it by executing:

php artisan test --group=GraphQLMediaLibrary
# or
phpunit --group=GraphQLMediaLibrary

laravel-medialibrary-graphql's People

Contributors

dmitrysidorenkoshim avatar tjventurini avatar tommy70404 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.