{"id":35190043,"url":"https://github.com/edgarsn/laravel-graphql-test-utils","last_synced_at":"2026-04-09T21:45:48.847Z","repository":{"id":65215203,"uuid":"588076655","full_name":"edgarsn/laravel-graphql-test-utils","owner":"edgarsn","description":"This package helps you to test your GraphQL queries \u0026 mutations.","archived":false,"fork":false,"pushed_at":"2025-02-27T09:29:31.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"2.x","last_synced_at":"2025-10-21T20:34:43.143Z","etag":null,"topics":["graphql","laravel","rebing","testing","tests","utilities"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edgarsn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-01-12T09:15:47.000Z","updated_at":"2024-01-04T18:49:32.000Z","dependencies_parsed_at":"2024-03-14T08:28:56.181Z","dependency_job_id":"d093cdfc-54cf-48ee-9564-0a5c4ceb6ec9","html_url":"https://github.com/edgarsn/laravel-graphql-test-utils","commit_stats":{"total_commits":9,"total_committers":1,"mean_commits":9.0,"dds":0.0,"last_synced_commit":"c65f9d835f0dcb7364baeace9ec14092d1f17e47"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/edgarsn/laravel-graphql-test-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgarsn%2Flaravel-graphql-test-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgarsn%2Flaravel-graphql-test-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgarsn%2Flaravel-graphql-test-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgarsn%2Flaravel-graphql-test-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edgarsn","download_url":"https://codeload.github.com/edgarsn/laravel-graphql-test-utils/tar.gz/refs/heads/2.x","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edgarsn%2Flaravel-graphql-test-utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28111173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-29T02:00:07.021Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","laravel","rebing","testing","tests","utilities"],"created_at":"2025-12-29T05:37:19.244Z","updated_at":"2025-12-29T05:37:21.147Z","avatar_url":"https://github.com/edgarsn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel GraphQL testing utility\n\nThis package helps you to test your GraphQL queries \u0026 mutations in your TestCases.\nIt works with any laravel GraphQL server.\n\n## Requirements\n- Laravel 11.0, 12.0\n- PHP 8.2+\n\nFor Laravel 8, 9, 10 and PHP 8.0+ see 1.x branch.\n\n## Installation\nRequire the package via Composer:\n\n```bash\ncomposer require newman/laravel-graphql-test-utils\n```\n\n# :book: Documentation \u0026 Usage\n\nTo start using it, import our trait in your TestCase\n\n```php\n\u003c?php\n\nnamespace Tests;\n\nuse \\Newman\\LaravelGraphQLTestUtils\\Traits\\WithGraphQL;\n\nclass SomeTest extends TestCase {\n    use WithGraphQL;\n\n    public function test_it_returns_cars(): void\n    {\n        $response = $this\n            -\u003egraphql()\n            -\u003esetQuery('query ($search: String!) {\n                cars(search: $search) {\n                    brand\n                }\n            }')\n            -\u003esetVariables([\n                'search' =\u003e 'BMW',\n            ])\n            -\u003ecall();\n            \n        $this-\u003eassertEquals([\n            'cars' =\u003e [\n                ['brand' =\u003e 'BMW'],\n            ],\n        ], $response-\u003ejson('data'));\n    }\n}\n```\n\nNow you can explore our available builder methods.\n\n### Default assertions\n\nBy default, **we don't assert anything for you** after response is retrieved, but we expose function to register your handler to customize this behaviour.\n\n```php\n\u003c?php\n\nnamespace Tests;\n\nuse Newman\\LaravelGraphQLTestUtils\\GraphQLTesting;\nuse Newman\\LaravelGraphQLTestUtils\\TestResponse;\n\nclass TestCase extends \\Illuminate\\Foundation\\Testing\\TestCase\n{\n    protected function setUp(): void\n    {\n        parent::setUp();\n    \n        GraphQlTesting::defaultAssertions(function (TestResponse $response): void {\n            $response-\u003eassertOk()\n                -\u003eassertNoGraphQLErrors();\n        });\n    }\n}\n```\n\n## Builder methods\n\n### `setQuery`\n\nSet your GraphQL query.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003esetQuery('query {\n        cars {\n            brand\n        }\n    }')\n    -\u003ecall();\n```\n\n### `setVariables`\n\nSet multiple GraphQL variables as key-value pair.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003esetVariables(['name' =\u003e 'John', 'email' =\u003e 'my@email.com'])\n    -\u003ecall();\n\n// Variables will result as: ['name' =\u003e 'John', 'email' =\u003e 'my@email.com']\n```\n\n**Note:** Calling `setVariables`, will replace all previously set variables. You may want to use `mergeVariables` in that case instead.\n\n### `setVariable`\n\nSet variables one-by-one.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003esetVariable('name', 'Oliver')\n    -\u003esetVariable('surname', 'Smith')\n    -\u003ecall();\n\n// Variables will result as: ['name' =\u003e 'Oliver', 'surname' =\u003e 'Smith']\n```\n\nYou can also mix it with `setVariables` and `mergeVariables`.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003esetVariables(['name' =\u003e 'James', 'email' =\u003e 'my@email.com'])\n    -\u003esetVariable('name', 'Oliver')\n    -\u003esetVariable('surname', 'Smith')\n    -\u003emergeVariables(['surname' =\u003e 'Williams', 'birthday' =\u003e '1990-01-01'])\n    -\u003ecall();\n\n// Variables will result as: ['name' =\u003e 'Oliver', 'email' =\u003e 'my@email.com', 'surname' =\u003e 'Williams', 'birthday' =\u003e '1990-01-01']\n```\n\n**Note:** Calling `setVariable` with the same key will override previous value.\n\n### `mergeVariables`\n\nInstead of resetting all variables like `setVariables` does, this method merges previously set variables with a pair of new variables.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003esetVariables(['name' =\u003e 'John', 'email' =\u003e 'my@email.com'])\n    -\u003emergeVariables(['name' =\u003e 'James'])\n    -\u003ecall();\n\n// Variables will result as: ['name' =\u003e 'James', 'email' =\u003e 'my@email.com']\n```\n\n### `schema`\n\nSpecifies which GraphQL schema (name) to use.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003eschema('users')\n    -\u003ecall();\n```\n\n**Note:** Depending on the driver used, it may automatically resolve HTTP method to use based on schema. [More](#drivers).\n\n### `httpMethod`\n\nForces specific HTTP method to use. By default, it will resolve from schema (if driver provides that), otherwise `POST` will be used.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ehttpMethod('GET')\n    -\u003ecall();\n```\n\n### `driver`\n\nOptionally you can switch this query to other driver than default. Probably you won't need this.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003edriver('myCustom')\n    -\u003ecall();\n```\n\n### `withToken`\n\nAdds an Bearer Authorization token to request. \n\n```php\n$this\n    -\u003egraphql()\n    -\u003ewithToken('U88Itq0x3yHrhAgCa8mOWuUMKScGAX3zs0xHGnJnvHJoTOmpVTaDX2SVxwxQIsL8')\n    -\u003ecall();\n```\n\n### `withoutToken`\n\nRemove the authorization token from the request.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ewithoutToken()\n    -\u003ecall();\n```\n\n### `withHeader`\n\nAdd single header to request.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ewithHeader('Authorization', 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==')\n    -\u003ecall();\n```\n\n### `withHeader`\n\nAdd multiple headers to request.\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ewithHeaders([\n        'X-User-Language' =\u003e 'en',\n        'X-Client' =\u003e 'GraphQL-Test',\n    ])\n    -\u003ecall();\n```\n\n### `modifyRequest`\n\nSince in base we use Laravel\\`s `MakesHttpRequests` Trait, you can access those functions as well.\n\n```php\nuse \\Newman\\LaravelGraphQLTestUtils\\GraphQLRequest;\n\n$this\n    -\u003egraphql()\n    -\u003emodifyRequest(function (GraphQLRequest $request) {\n        $request-\u003eflushHeaders();\n        \n        // all MakesHttpRequest public methods can be called.\n        \n        // https://github.com/laravel/framework/blob/9.x/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php\n    })\n    -\u003ecall();\n```\n\n### `withDefaultAssertions` and `withoutDefaultAssertions`\n\nYou may want to disable or re-enable default assertions individually.\n\nDisable default assertions for this query:\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ewithoutDefaultAssertions()\n    -\u003ecall();\n```\n\n```php\nfunction getBaseBuilder() {\n    return $this-\u003egraphql()-\u003ewithoutDefaultAssertions();\n}\n\n// I want them to be enabled here.\ngetBaseBuilder()\n    -\u003ewithDefaultAssertions()\n    -\u003ecall();\n```\n\n### `call`\n\nMakes the call to GraphQL and returns our `TestResponse` class. Function has 2 arguments\n\n```php\n$this\n    -\u003egraphql()\n    -\u003ecall('query ($search: String!) { cars (search: $search) { brand } }', ['search' =\u003e 'BMW']);\n```\n\n**Note:** Variables passed to this function will merge with existing ones.\n\n## Response\n\nWe extend default Laravel\\`s `Illuminate\\Testing\\TestResponse` with a few GraphQL related functions/assertions.\n\n### `assertNoGraphQLErrors`\n\nAsserts no GraphQL errors were returned.\n\n### `assertGraphQLUnauthorized`\n\nAsserts there is an Unauthorized error in corresponding GraphQL error format.\n\n### `getQuery`\n\nAccess used query in the request.\n\n### `getVariables`\n\nAccess used variables in the request.\n\n### `getGraphQLErrors`\n\nReturns `array` of GraphQL errors or `null` when not present.\n\n### `hasGraphQLErrors`\n\nDetermines if there are any GraphQL errors.\n\n### `getGraphQLValidationMessages`\n\nGet list of Laravel validation messages or empty array when none.\n\n```php\n[\n    'name' =\u003e ['This field is required.', 'It must be a string'],\n    // ...\n]\n```\n\n### `getValidationFieldMessages`\n\nGet Laravel validation messages on specific field or empty array when none or field is not present.\n\n```php\n$messages = $response-\u003egetValidationFieldMessages('name'); // ['This field is required.', 'It must be a string']\n```\n\n### `getValidationFieldFirstMessage`\n\nGet first Laravel validation message on specific field or null when none or field is not present.\n\n```php\n$message = $response-\u003egetValidationFieldFirstMessage('name'); // 'This field is required.'\n```\n\n:information_source: Remember you can access all `Illuminate\\Testing\\TestResponse` functions as well.\n\n:question: Missing frequent response helper? Open up a new issue.\n\n## Custom Response class\n\nThis example shows you how to create your custom TestResponse class with your custom functions.\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Support;\n\nuse Newman\\LaravelGraphQLTestUtils\\TestResponse;\n\nclass CustomTestResponse extends TestResponse \n{\n\n    public function assertDataAlwaysContainsAge(): static \n    {\n        if (!$this-\u003ejson('data.age')) {\n            Assert::fail('Failed to assert that response contains age key.');\n        }\n    }\n}\n```\n\nThen in your ServiceProvider (e.g. `AppServiceProvider`) or TestCase:\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Support\\CustomTestResponse;\nuse Newman\\LaravelGraphQLTestUtils\\GraphQLTesting;\n\nclass AppServiceProvider extends \\Illuminate\\Support\\ServiceProvider\n{\n\n    public function boot()\n    {\n        GraphQLTesting::useCustomResponseHandler(CustomTestResponse::class);\n    }\n}\n```\n\n## Custom Builder class\n\nThis example shows you how to create your custom GraphQLBuilder class with your custom functions.\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Support;\n\nuse App\\Models\\User;\nuse Newman\\LaravelGraphQLTestUtils\\GraphQLBuilder;\n\nclass CustomGraphQLBuilder extends GraphQLBuilder \n{\n\n    public function withUser(User $user): static \n    {\n        $this-\u003ewithToken($user-\u003eapi_token)\n    \n        return $this;\n    }\n}\n```\n\nThen in your ServiceProvider (e.g. `AppServiceProvider`):\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Support\\CustomGraphQLBuilder;\nuse Newman\\LaravelGraphQLTestUtils\\Contracts\\GraphQLBuilderContract;\n\nclass AppServiceProvider extends \\Illuminate\\Support\\ServiceProvider\n{\n\n    public function register()\n    {\n        $this-\u003eapp-\u003ebind(GraphQLBuilderContract::class, CustomGraphQLBuilder::class);\n    }\n}\n```\n\n## \u003ca name=\"drivers\"\u003e\u003c/a\u003e Drivers\n\nDrivers help you to construct request by pulling information from GraphQL server config, so you can discard some builder calls.\nYou can write your custom driver if none of ours fits your needs.\n\n### RebingDriver (Default)\n\nhttps://github.com/rebing/graphql-laravel\n\nIt reads URL prefix from config `graphql.route.prefix` and takes first HTTP method from config by schema name.\n\nThis is the default driver.\n\n### NullDriver\n\nIt uses `/graphql` URL prefix and `POST` HTTP Method.\n\nTo use, call `GraphQLTesting::useNullDriver()` in your `AppServiceProvider.php` `boot()` method.\n\n### Custom driver\n\nExample on how to implement custom driver with name `myCustom`.\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Support\\GraphQLTestingDrivers;\n\nuse Illuminate\\Contracts\\Config\\Repository as ConfigContract;\nuse Illuminate\\Contracts\\Container\\Container;\nuse Newman\\LaravelGraphQLTestUtils\\Contracts\\DriverContract;\n\nclass MyCustomDriver implements DriverContract\n{\n    /**\n     * @var Container\n     */\n    protected $app;\n\n    public function __construct(Container $app)\n    {\n        $this-\u003eapp = $app;\n    }\n\n    public function getUrlPrefix(): ?string\n    {\n        // ... return your url prefix to GraphQL endpoint. in this case it would be /my-graphql\n        // returning null will fallback to default URL prefix.\n        return 'my-graphql';\n    }\n\n    public function getHttpMethodForSchema(string $schemaName): ?string\n    {\n        // ... detect HTTP method from schema name and return it.\n        // below is an example. You may read it from config or resolve any other way.\n        // returning null will fallback to default HTTP method.\n        return $schemaName == 'default' ? 'GET' : 'POST';\n    }\n}\n```\n\nThen in your ServiceProvider (e.g. `AppServiceProvider`):\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Support\\GraphQLTestingDrivers\\MyCustomDriver;\nuse Newman\\LaravelGraphQLTestUtils\\GraphQLTesting;\n\nclass AppServiceProvider extends \\Illuminate\\Support\\ServiceProvider\n{\n    public function boot()\n    {\n        // to activate this driver\n        \n        GraphQlTesting::useDriver('myCustom');\n    }\n    \n    public function register()\n    {\n        // ... your other bindings\n        $this-\u003eapp-\u003ebind('laravel-graphql-utils-driver:myCustom', MyCustomDriver::class);\n    }\n}\n```\n\n## :handshake: Contributing\n\nWe'll appreciate your collaboration to this package. \n\nWhen making pull requests, make sure:\n * All tests are passing: `composer test`\n * Test coverage is not reduced: `composer test-coverage`\n * There are no PHPStan errors: `composer phpstan`\n * Coding standard is followed: `composer lint` or `composer fix-style` to automatically fix it. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgarsn%2Flaravel-graphql-test-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedgarsn%2Flaravel-graphql-test-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedgarsn%2Flaravel-graphql-test-utils/lists"}