lastdragon-ru/lara-asp-graphql-testing
Testing helpers for GraphQL in Laravel apps using lara-asp. Provides utilities and assertions to build requests, execute queries/mutations, and validate responses in automated tests, making GraphQL endpoint testing faster and more reliable.
Install the Package
composer require --dev lastdragon-ru/lara-asp-graphql-testing
Ensure Compatibility
Verify your project uses lastdragon-ru/lara-asp-graphql (v1.x+ recommended).
Check composer.json for the base package:
"require": {
"lastdragon-ru/lara-asp-graphql": "^1.0"
}
First Test Case Extend your test class with the trait and assert a simple query:
use LastDragon\LaraAspGraphQLTesting\Assertions;
class GraphQLTest extends TestCase
{
use Assertions;
public function test_basic_query()
{
$response = $this->graphQL('{ user(id: 1) { id name } }');
$this->assertGraphQLResponse($response)
->hasNoErrors()
->hasData()
->hasField('user')
->hasField('user.id')
->hasField('user.name');
}
}
Key Files to Reference
Assertions.php (core methods)GraphQLTestCase.php (base test class)Test Query Execution
$response = $this->graphQL('
query GetUser($id: ID!) {
user(id: $id) {
id
posts { title }
}
}
', ['id' => 1]);
$this->assertGraphQLResponse($response)
->hasNoErrors()
->hasField('user.posts')
->hasFieldCount('user.posts', 2);
Schema-Level Assertions
$this->assertGraphQLSchema()
->hasType('User')
->hasType('Post')
->hasField('User', 'posts')
->hasField('Post', 'title');
Mutation Testing
$response = $this->graphQL('
mutation CreatePost($title: String!) {
createPost(title: $title) { id }
}
', ['title' => 'Test']);
$this->assertGraphQLResponse($response)
->hasNoErrors()
->hasField('createPost.id')
->isInt('createPost.id');
Authentication
$this->actingAs($user)
->graphQL('{ me { id email } }')
->assertGraphQLResponse()
->hasField('me.id');
Rate Limiting
$this->assertGraphQLRateLimit(5); // Check if 5 requests are allowed
Middleware Combine with Laravel’s HTTP tests:
$response = $this->withHeaders(['X-API-TOKEN' => 'secret'])
->graphQL('{ secretData }');
$this->assertGraphQLResponse($response)
->hasField('secretData');
| Pattern | Example |
|---|---|
| Field Value Checks | $this->assertGraphQLResponse()->isString('user.name')->equals('John') |
| Error Handling | $this->assertGraphQLResponse()->hasError('ValidationError') |
| Pagination | $this->assertGraphQLResponse()->hasField('posts')->hasField('pageInfo') |
| Subscriptions | (Use graphQLSubscription helper if supported) |
Assertion Order Matters
hasNoErrors()->hasField()) fail silently if the first assertion fails.should() for explicit expectations:
$this->assertGraphQLResponse($response)
->should()
->haveNoErrors()
->haveField('user');
Schema Caching
$this->artisan('graphql:clear-cache');
Mocking Dependencies
protected function setUp(): void
{
$this->mock(UserRepository::class, function ($mock) {
$mock->shouldReceive('find')->andReturn(new User());
});
}
Performance with Large Schemas
$this->assertGraphQLSchema()
->hasType('User')
->hasField('User', 'id') // Skip full schema dump
->ignoreMissingTypes(['DeprecatedType']);
Dump Raw Response
$this->assertGraphQLResponse($response)->dump(); // Logs full response
Enable Query Logging
Add to config/graphql.php:
'logging' => [
'enabled' => env('GRAPHQL_LOG_QUERIES', false),
'path' => storage_path('logs/graphql.log'),
],
Compare with graphql CLI
Test queries manually via Artisan:
php artisan graphql:query '{ __schema { types { name } } }'
Custom Assertions
Extend the Assertions trait:
use LastDragon\LaraAspGraphQLTesting\Assertions as BaseAssertions;
trait CustomAssertions
{
public function assertCustomRule($response, $rule)
{
// Implement logic (e.g., check for business rules)
}
}
class MyTest extends TestCase
{
use BaseAssertions, CustomAssertions;
}
Override Helpers Publish the package config:
php artisan vendor:publish --provider="LastDragon\LaraAspGraphQLTesting\ServiceProvider"
Then customize config/lara-asp-graphql-testing.php.
Plugin System
Register custom plugins in GraphQLTestCase:
protected function getGraphQLTestPlugins(): array
{
return [
new \App\GraphQL\Testing\CustomPlugin(),
];
}
How can I help you explore Laravel packages today?