Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 

GraphQL Testing Helper for Laravel

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Elegant GraphQL testing utilities for Laravel. Works with any GraphQL library. Especially with Lighthouse.

Installation

You can install the package via composer:

composer require --dev marvinrabe/laravel-graphql-test

And then add the trait to your TestCase class:

<?php

namespace Tests;

abstract class TestCase extends BaseTestCase
{
    use MarvinRabe\LaravelGraphQLTest\TestGraphQL;

    // ...
}

When your GraphQL endpoint is not /graphql you have to specify it manually:

public $graphQLEndpoint = 'graphql';

Usage

Queries

You can write queries like this:

$this->query('account', ['id' => 123], ['id']);

Note that this function returns an \Illuminate\Foundation\Testing\TestResponse. Therefore you might use any Laravel testing methods. For example:

$this->query('account', ['id' => 123], ['id'])
  ->assertSuccessful()
  ->assertJsonFragment([
    'id' => 123
  ]);

With nested resources:

$this->query('account', ['id' => 123], ['transactions' => ['id']]);

Without a third argument it will be assumed that the second one is the selection set:

$this->query('accounts', ['id']);

When you only pass the object name, you get the GraphQLClient instead of the Laravel TestResponse:

$this->query('accounts')->getGql();

Mutations

Same as queries:

$this->mutation('accounts')->getGql();
$this->mutation('accounts', ['id']);
$this->mutation('accounts', ['id' => 123]); 

Argument Order

For simplicity you can find the correct argument order in the following table:

Method Arguments Returns
query (object) GraphQLClient
query (object, selectionSet) TestResponse
query (object, arguments, selectionSet) TestResponse
mutation (object) GraphQLClient
mutation (object, selectionSet) TestResponse
mutation (object, arguments, selectionSet) TestResponse

Enums

Because PHP has no built in Enum support. You have to use the provided enum helper:

$this->query('accounts', ['status' => $this->enum('closed')], ['id']);

Or create a EnumType manually:

$this->query('accounts', ['status' => new \MarvinRabe\LaravelGraphQLTest\Scalars\EnumType('closed')], ['id']);

Limitations

The QueryBuilder provided by this library is not safe for use in production code. It is designed for ease of use and does not comply to the GraphQL specifications fully. Use it only for testing purposes! You have been warned.

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

About

πŸ›  Elegant GraphQL testing utilities for Laravel.

Topics

Resources

License

Packages

No packages published

Languages