Skip to content
GraphQL with Laravel Framework
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets
config
resources/views
src
tests Add Mutation Feb 1, 2019
.gitignore Init package Jan 31, 2019
README.md updated instruction with auto-discovery Mar 31, 2019
composer.json added auto-discovery Mar 31, 2019
composer.lock
phpunit.xml

README.md

Total Downloads Latest Stable Version

Laravel GraphQL

The objective of this project is to facilitate the integration of the webonyx/graphql-php library with the Laravel Framework.

How to install

Use composer to install this package

composer require supliu/laravel-graphql

From Laravel 5.5 onwards, it's possible to take advantage of auto-discovery of the service provider. For Laravel versions before 5.5, you must register the service provider in your config/app.php

'providers' => [
    /*
     * Package Service Providers...
     */
    \Supliu\LaravelGraphQL\ServiceProvider::class,
],

Execute a publish with artisan command:

php artisan vendor:publish --provider="Supliu\LaravelGraphQL\ServiceProvider"

How to use

You must create your Query and Mutation classes and register on config/graphql.php so that GraphQL can read.

'queries' => [
    'detailHero' => \App\GraphQL\Queries\DetailHero::class
],

'mutations' => [
    'updateHero' => \App\GraphQL\Mutations\UpdateHero::class
]

Query

Below is an example of a Query class that returns the data of a Star Wars hero:

<?php

namespace App\GraphQL\Queries;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Supliu\LaravelGraphQL\Query;

class DetailHero extends Query
{
    /**
     * @return array
     */
    protected function args(): array
    {
        return [
            'id' => Type::nonNull(Type::int())
        ];
    }
    
    /**
     * @return Type
     */
    protected function typeResult(): Type
    {
        return new ObjectType([
            'name' => 'HeroQueryResult',
            'fields' => [
                'name' => Type::string()
            ]
        ]);
    }

    /**
     * @return mixed
     */
    protected function resolve($root, $args, $context, $info)
    {
        return Hero::find($args['id']);
    }
}

Mutation

Below is an example of a Mutation class that returns if update worked:

<?php

namespace App\GraphQL\Mutations;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Supliu\LaravelGraphQL\Mutation;

class UpdateHero extends Mutation
{
    protected function typeResult(): Type
    {
        return new ObjectType([
            'name' => 'UpdateHeroResult',
            'fields' => [
                'error' => Type::boolean(),
                'message' => Type::string()
            ]
        ]);
    }

    /**
     * @return array
     */
    protected function args(): array
    {
        return [
            'id' => Type::nonNull(Type::int())
            'name' => Type::nonNull(Type::string())
        ];
    }

    /**
     * @return mixed
     */
    protected function resolve($root, $args, $context, $info)
    {
        Hero::find($args['id'])->update([
          'name' => $args['name']
        ]);
    
        return [
            'error' => false,
            'message' => 'Updated!'
        ];
    }
}

License

The Laravel GraphQL is open-sourced project licensed under the MIT license.

You can’t perform that action at this time.