1. Go to this page and download the library: Download glesys/butler-graphql library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
glesys / butler-graphql example snippets
namespace App\Http\Graphql\Queries;
class PendingSignups
{
public function __invoke($root, $args, $context)
{
return Signups::where('status', 'pending')->get();
}
}
namespace App\Http\Controllers;
use Butler\Graphql\Concerns\HandlesGraphqlRequests;
class GraphqlController extends Controller
{
use HandlesGraphqlRequests;
}
public function __invoke()
{
return function ($root, $args, $context, $info) {
//
};
}
namespace App\Http\Graphql\Types;
class Signup
{
public function verificationToken($source, $args, $context, $info)
{
return $source->token;
}
}
namespace App\Http\Graphql\Types;
class Post
{
public function attachment($source, $args, $context, $info)
{
return [
'__typename' => 'Photo',
'height' => 200,
'width' => 300,
];
}
}
namespace App\Http\Graphql\Types;
class Post
{
public function attachment($source, $args, $context, $info)
{
return [
'height' => 200,
'width' => 300,
];
}
public function resolveTypeForAttachment($source, $context, $info)
{
if (isset($source['height'], $source['width'])) {
return 'Photo';
}
if (isset($source['length'])) {
return 'Video';
}
}
}
namespace App\Http\Graphql\Queries;
use App\Attachment;
class Attachments
{
public function __invoke($source, $args, $context, $info)
{
return Attachment::all();
}
public function resolveType($source, $context, $info)
{
return $source->type; // `Photo` or `Video`
}
}
namespace App\Http\Graphql\Types;
use App\Models\Article;
class Article
{
public function comments(Article $source, $args, $context, $info)
{
return $context['loader'](function ($articleIds) {
return Comment::whereIn('article_id', $articleIds)
->cursor()
->groupBy('article_id');
}, [])->load($source->id);
}
}
namespace App\Http\Graphql\Types;
use App\Models\Article;
use Closure;
class Article
{
public function comments(Article $source, $args, $context, $info)
{
return $context['loader'](Closure::fromCallable([$this, 'loadComments']), [])
->load($source->id);
}
public function topVotedComment(Article $source, $args, $context, $info)
{
$comments = yield $context['loader'](Closure::fromCallable([$this, 'loadComments']))
->load($source->id);
return collect($comments)->sortByDesc('votes')->first();
}
private function loadComments($articleIds)
{
return Comment::whereIn('article_id', $articleIds)
->cursor()
->groupBy('article_id');
}
}
namespace App\Http\Controllers;
use Butler\Graphql\Concerns\HandlesGraphqlRequests;
use GraphQL\Type\Schema;
use GraphQL\Language\AST\DocumentNode;
class GraphqlController extends Controller
{
use HandlesGraphqlRequests;
/**
* @param $schema A parsed version of your GraphQL schema
* @param $query A parsed version of the consumer's query
* @param $operationName The passed operation name (for when the query contains multiple operations)
* @param $variables Any variables passed alongside the query
*/
public function beforeExecutionHook(Schema $schema, DocumentNode $query, string $operationName = null, $variables = null): void
{
// TODO: Implement custom authorization logic here
}
}
bash
php artisan vendor:publish
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.