Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Lara Asp Graphql Testing Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Package

    composer require --dev lastdragon-ru/lara-asp-graphql-testing
    
  2. 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"
    }
    
  3. 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');
        }
    }
    
  4. Key Files to Reference


Implementation Patterns

Workflow: Query/Schema Validation

  1. 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);
    
  2. Schema-Level Assertions

    $this->assertGraphQLSchema()
         ->hasType('User')
         ->hasType('Post')
         ->hasField('User', 'posts')
         ->hasField('Post', 'title');
    
  3. 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');
    

Integration with Laravel Features

  • 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');
    

Common Patterns

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)

Gotchas and Tips

Pitfalls

  1. Assertion Order Matters

    • Chained assertions (e.g., hasNoErrors()->hasField()) fail silently if the first assertion fails.
    • Fix: Use should() for explicit expectations:
      $this->assertGraphQLResponse($response)
           ->should()
           ->haveNoErrors()
           ->haveField('user');
      
  2. Schema Caching

    • The package caches schema introspection. Clear it after schema changes:
      $this->artisan('graphql:clear-cache');
      
  3. Mocking Dependencies

    • If your GraphQL resolver depends on external services, mock them in the test class:
      protected function setUp(): void
      {
          $this->mock(UserRepository::class, function ($mock) {
              $mock->shouldReceive('find')->andReturn(new User());
          });
      }
      
  4. Performance with Large Schemas

    • Introspection queries can be slow. Limit tests to critical types/fields:
      $this->assertGraphQLSchema()
           ->hasType('User')
           ->hasField('User', 'id') // Skip full schema dump
           ->ignoreMissingTypes(['DeprecatedType']);
      

Debugging Tips

  • 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 } } }'
    

Extension Points

  1. 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;
    }
    
  2. Override Helpers Publish the package config:

    php artisan vendor:publish --provider="LastDragon\LaraAspGraphQLTesting\ServiceProvider"
    

    Then customize config/lara-asp-graphql-testing.php.

  3. Plugin System Register custom plugins in GraphQLTestCase:

    protected function getGraphQLTestPlugins(): array
    {
        return [
            new \App\GraphQL\Testing\CustomPlugin(),
        ];
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime