@@ -52,18 +52,19 @@ abstract class TestCase extends BaseTestCase
5252The most natural way of testing your GraphQL API is to run actual GraphQL queries.
5353
5454The ` graphQL ` test helper runs a query on your GraphQL endpoint and returns a ` TestResponse ` .
55+ For GraphQL literals, prefer ` /** @lang GraphQL */ ` plus nowdoc (` <<<'GRAPHQL' ` ).
5556
5657``` php
5758public function testQueriesPosts(): void
5859{
59- $response = $this->graphQL(/** @lang GraphQL */ '
60+ $response = $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
6061 {
6162 posts {
6263 id
6364 title
6465 }
6566 }
66- ' );
67+ GRAPHQL );
6768}
6869```
6970
@@ -72,13 +73,13 @@ If you want to use variables within your query, pass an associative array as the
7273``` php
7374public function testCreatePost(): void
7475{
75- $response = $this->graphQL(/** @lang GraphQL */ '
76+ $response = $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
7677 mutation ($title: String!) {
7778 createPost(title: $title) {
7879 id
7980 }
8081 }
81- ' , [
82+ GRAPHQL , [
8283 'title' => 'Automatic testing proven to reduce stress levels in developers'
8384 ]);
8485}
@@ -89,15 +90,15 @@ You can run a subscription query the same way.
8990``` php
9091public function testPostsSubscription(): void
9192{
92- $response = $this->graphQL(/** @lang GraphQL */ '
93+ $response = $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
9394 {
9495 subscription {
9596 onPostCreated {
9697 title
9798 }
9899 }
99100 }
100- ' );
101+ GRAPHQL );
101102}
102103```
103104
@@ -116,14 +117,14 @@ public function testQueriesPosts(): void
116117{
117118 $post = factory(Post::class)->create();
118119
119- $this->graphQL(/** @lang GraphQL */ '
120+ $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
120121 {
121122 posts {
122123 id
123124 title
124125 }
125126 }
126- ' )->assertJson([
127+ GRAPHQL )->assertJson([
127128 'data' => [
128129 'posts' => [
129130 [
@@ -141,28 +142,33 @@ You can also extract data from the response and use it within any assertion.
141142``` php
142143public function testOrdersUsersByName(): void
143144{
144- factory(User::class)->create(['name' => 'Oliver'] );
145- factory(User::class)->create([' name' => 'Chris']) ;
146- factory(User::class)->create(['name' => 'Benedikt'] );
145+ $oliver = factory(User::class)->make( );
146+ $oliver-> name = 'Oliver' ;
147+ $oliver->save( );
147148
148- $response = $this->graphQL(/** @lang GraphQL */ '
149+ $chris = factory(User::class)->make();
150+ $chris->name = 'Chris';
151+ $chris->save();
152+
153+ $benedikt = factory(User::class)->make();
154+ $benedikt->name = 'Benedikt';
155+ $benedikt->save();
156+
157+ $response = $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
149158 {
150159 users(orderBy: "name") {
151160 name
152161 }
153162 }
154- ' );
163+ GRAPHQL );
155164
156165 $names = $response->json("data.*.name");
157166
158- $this->assertSame(
159- [
160- 'Benedikt',
161- 'Chris',
162- 'Oliver',
163- ],
164- $names
165- );
167+ $this->assertSame([
168+ 'Benedikt',
169+ 'Chris',
170+ 'Oliver',
171+ ], $names);
166172}
167173```
168174
@@ -237,11 +243,11 @@ For example, you might want to ensure that validation works properly:
237243
238244``` php
239245$this
240- ->graphQL(/** @lang GraphQL */ '
246+ ->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
241247 mutation {
242248 createUser(email: "invalid email")
243249 }
244- ' )
250+ GRAPHQL )
245251 ->assertGraphQLValidationKeys(['email']);
246252```
247253
@@ -256,11 +262,11 @@ such as `assertGraphQLErrorMessage()`:
256262
257263``` php
258264$this
259- ->graphQL(/** @lang GraphQL */ '
265+ ->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
260266 mutation {
261267 shouldTriggerSomeError
262268 }
263- ' )
269+ GRAPHQL )
264270 ->assertGraphQLErrorMessage($expectedMessage);
265271```
266272
@@ -271,11 +277,11 @@ You must disable Lighthouse's error handling with `rethrowGraphQLErrors()` to en
271277$this->rethrowGraphQLErrors();
272278
273279$this->expectException(SomethingWentWrongException::class);
274- $this->graphQL(/** @lang GraphQL */ '
280+ $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
275281{
276282 oops
277283}
278- ' );
284+ GRAPHQL );
279285```
280286
281287## Simulating File Uploads
@@ -286,11 +292,11 @@ Since multipart form requests are tricky to construct, you can use the `multipar
286292
287293``` php
288294$operations = [
289- 'query' => /** @lang GraphQL */ '
295+ 'query' => /** @lang GraphQL */ <<<'GRAPHQL '
290296 mutation ($file: Upload!) {
291297 upload(file: $file)
292298 }
293- ' ,
299+ GRAPHQL ,
294300 'variables' => [
295301 'file' => null,
296302 ],
@@ -343,12 +349,12 @@ When sending requests with field containing `@defer`, use the `streamGraphQL()`
343349It automatically captures the full streamed response and provides you the returned chunks.
344350
345351``` php
346- $chunks = $this->streamGraphQL(/** @lang GraphQL */ '
352+ $chunks = $this->streamGraphQL(/** @lang GraphQL */ <<<'GRAPHQL '
347353{
348354 now
349355 later @defer
350356}
351- ' );
357+ GRAPHQL );
352358
353359$this->assertSame(
354360 [
@@ -399,11 +405,11 @@ Assertions work differently as a result:
399405``` php
400406public function testHelloWorld(): void
401407{
402- $this->graphQL(/** @lang GraphQL */ '
408+ $this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL '
403409 {
404410 hello
405411 }
406- ' )->seeJson([
412+ GRAPHQL )->seeJson([
407413 'data' => [
408414 'hello' => 'world',
409415 ],
0 commit comments