Installation
composer require arthem/graphql-mapper
Ensure webonyx/graphql-php is also installed (dependency).
Define a Schema Mapping File
Create a YAML file (e.g., graphql/schema.yml) to map your Eloquent models to GraphQL types. Example:
types:
User:
model: App\Models\User
fields:
id:
type: ID!
name:
type: String
email:
type: String
Load the Schema in Laravel
Register the schema loader in a service provider (e.g., GraphQLServiceProvider):
use Arthem\GraphQLMapper\Loader\YamlLoader;
use Arthem\GraphQLMapper\Schema\Builder;
public function boot()
{
$loader = new YamlLoader(__DIR__.'/../config/graphql/schema.yml');
$schema = Builder::build($loader->load());
$this->app->singleton('graphql.schema', function () use ($schema) {
return $schema;
});
}
First Use Case: Query a Model
Use the schema in a GraphQL endpoint (e.g., via graphql-php's GraphQLServer):
$query = '
query {
users {
id
name
}
}
';
$result = $this->app->make('graphql.schema')->executeQuery($query);
Model-Driven Schema Definition
Post model with relationships:
types:
Post:
model: App\Models\Post
fields:
id: { type: ID! }
title: { type: String! }
author:
type: User
resolve:
handler: doctrine
field: user
Resolver Handlers
doctrine, eloquent) or custom resolvers for complex logic:
fields:
fullName:
type: String
resolve:
handler: custom
class: App\GraphQL\Resolvers\FullNameResolver
Query/Mutation Generation
// Fetch a user by ID
$query = $schema->createQuery('users', ['id' => 1]);
$result = $schema->executeQuery($query);
Integration with Laravel Routes
routes/web.php:
Route::post('/graphql', function () {
return $this->app->make('graphql.schema')->executeQuery(
request()->input('query'),
request()->input('variables', [])
);
});
SchemaValidator to catch misconfigurations early.Abandoned Package
graphql-php/graphql with custom mappers.YAML Configuration Quirks
yamllint).resolve.handler matches a registered resolver (e.g., doctrine requires Doctrine ORM).User -> Post -> User) without custom resolvers.Performance Issues
doctrine may trigger N+1 queries. Use eager loading or custom resolvers.$schema = Builder::build($config, ['cache' => true]);
Laravel-Specific Gotchas
eloquent handler or write custom resolvers.App\Models\User).graphql-php to return detailed errors:
$schema->setErrorFormatter(function ($e) {
return [
'message' => $e->getMessage(),
'trace' => $e->getTrace(),
];
});
class CustomResolver implements ResolverInterface {
public function resolve($root, array $args, Schema $schema) {
\Log::debug('Resolver called with args:', $args);
// ...
}
}
SchemaValidator to check for syntax errors:
$validator = new SchemaValidator();
$errors = $validator->validate($config);
Custom Resolvers
Arthem\GraphQLMapper\Resolver\ResolverInterface for bespoke logic:
class CustomResolver implements ResolverInterface {
public function resolve($root, array $args, Schema $schema) {
return $root->customAttribute;
}
}
fields:
customField:
type: String
resolve:
handler: custom
class: App\GraphQL\Resolvers\CustomResolver
Dynamic Field Resolution
Arthem\GraphQLMapper\Field\DynamicField for runtime field resolution (e.g., conditional fields).Middleware Integration
GraphQLServer wrapper.Testing
$schema = $this->app->make('graphql.schema');
$result = $schema->executeQuery($query, [], [], ['mock' => true]);
How can I help you explore Laravel packages today?