cfpinto/graphql
Laravel package to add a GraphQL API to your app, offering schema setup, query/mutation handling, and integration with Laravel’s routing and services so you can expose application data through GraphQL with minimal boilerplate.
Installation
composer require cfpinto/graphql
Add the service provider to config/app.php:
'providers' => [
// ...
CFPinto\GraphQL\GraphQLServiceProvider::class,
],
Basic Query Builder
Import the GraphQL facade:
use CFPinto\GraphQL\Facades\GraphQL;
Construct a simple query:
$query = GraphQL::query('users')
->select(['id', 'name', 'email'])
->first();
First Use Case: Fetching a Single Resource
$user = GraphQL::query('users')
->where('id', 1)
->first();
CRUD Operations
$created = GraphQL::mutation('createUser')
->setVariables(['name' => 'John', 'email' => 'john@example.com'])
->execute();
$users = GraphQL::query('users')
->paginate(10)
->get();
$updated = GraphQL::mutation('updateUser')
->where('id', 1)
->setVariables(['name' => 'Updated Name'])
->execute();
$deleted = GraphQL::mutation('deleteUser')
->where('id', 1)
->execute();
Nested Relationships
$posts = GraphQL::query('posts')
->with(['author' => ['name', 'email']])
->get();
Conditional Queries
if ($activeOnly) {
$query = GraphQL::query('users')->where('active', true);
}
Eloquent Integration
Use the fromModel() helper to map queries to Eloquent models:
$query = GraphQL::query()->fromModel(User::class)->select(['id', 'name']);
API Resource Transformation Convert results to API resources:
$users = GraphQL::query('users')->get()->transform(new UserResource());
Middleware for Authentication Wrap queries in middleware for authorization:
$query = GraphQL::query('adminDashboard')->middleware('can:view-admin');
Schema Mismatch
GraphQL::debug(true) to log raw queries and responses.Variable Injection Risks
$variables = ['id' => (int) request('id')];
GraphQL::mutation('updateUser')->setVariables($variables)->execute();
Pagination Quirks
paginate() method assumes the GraphQL endpoint returns a pageInfo object. Customize with:
GraphQL::query('users')->paginate(10, 'customCursorField');
Enable Debug Mode
GraphQL::debug(true); // Logs queries and responses to Laravel logs
Inspect Raw Queries
Use GraphQL::getQuery() to see the constructed query string before execution:
$queryString = GraphQL::query('users')->select(['id'])->getQuery();
Custom Query Builders Extend the base builder for domain-specific logic:
class UserQueryBuilder extends \CFPinto\GraphQL\QueryBuilder
{
public function activeOnly()
{
return $this->where('active', true);
}
}
Register it in the service provider:
GraphQL::extend('users', UserQueryBuilder::class);
Response Transformers Override default response handling:
GraphQL::transformer(function ($response) {
return collect($response)->map(fn ($item) => [
'id' => $item['id'],
'full_name' => $item['first_name'] . ' ' . $item['last_name']
]);
});
Error Handling Customize error responses globally:
GraphQL::onError(function (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
});
Default Headers
Set default headers (e.g., for authentication) in config/graphql.php:
'headers' => [
'Authorization' => 'Bearer ' . auth()->token(),
],
Endpoint Overrides Override the default endpoint per query:
GraphQL::query('users')->endpoint('https://custom-api.com/graphql');
How can I help you explore Laravel packages today?