-
-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@namespaced
directive for namespacing by seperation of concerns (…
…#2478) Co-authored-by: Will G <[email protected]>
- Loading branch information
1 parent
26eea8b
commit 90950d1
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Nuwave\Lighthouse\Schema\Directives; | ||
|
||
use Nuwave\Lighthouse\Schema\Values\FieldValue; | ||
use Nuwave\Lighthouse\Support\Contracts\FieldResolver; | ||
|
||
class NamespacedDirective extends BaseDirective implements FieldResolver | ||
{ | ||
public static function definition(): string | ||
{ | ||
return /** @lang GraphQL */ <<<'GRAPHQL' | ||
""" | ||
Provides a no-op field resolver that allows nesting of queries and mutations. | ||
Useful to implement [namespacing by separation of concerns](https://www.apollographql.com/docs/technotes/TN0012-namespacing-by-separation-of-concern). | ||
""" | ||
directive @namespaced on FIELD_DEFINITION | ||
GRAPHQL; | ||
} | ||
|
||
public function resolveField(FieldValue $fieldValue): callable | ||
{ | ||
return static fn (): bool => true; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
tests/Integration/Schema/Directives/NamespacedDirectiveTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Tests\Integration\Schema\Directives; | ||
|
||
use Tests\DBTestCase; | ||
|
||
final class NamespacedDirectiveTest extends DBTestCase | ||
{ | ||
public function testCRUDModelDirectives(): void | ||
{ | ||
$this->schema .= /** @lang GraphQL */ ' | ||
type Query { | ||
user: UserQueries! @namespaced | ||
} | ||
type UserQueries { | ||
find(id: ID! @eq): User @find | ||
list: [User!]! @all | ||
} | ||
type Mutation { | ||
user: UserMutations! @namespaced | ||
} | ||
type UserMutations { | ||
create(name: String!): User @create | ||
update(id: ID!, name: String): User @update | ||
delete(id: ID! @whereKey): User @update | ||
} | ||
type User { | ||
id: ID! | ||
name: String! | ||
} | ||
'; | ||
|
||
$name = 'foo'; | ||
$createUserResponse = $this->graphQL(/** @lang GraphQL */ ' | ||
mutation ($name: String!) { | ||
user { | ||
create(name: $name) { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
', [ | ||
'name' => $name, | ||
]); | ||
$createUserResponse->assertJson([ | ||
'data' => [ | ||
'user' => [ | ||
'create' => [ | ||
'name' => $name, | ||
], | ||
], | ||
], | ||
]); | ||
$userID = $createUserResponse->json('data.user.create.id'); | ||
|
||
$this->graphQL(/** @lang GraphQL */ ' | ||
query ($id: ID!) { | ||
user { | ||
find(id: $id) { | ||
id | ||
} | ||
list { | ||
id | ||
} | ||
} | ||
} | ||
', [ | ||
'id' => $userID, | ||
])->assertExactJson([ | ||
'data' => [ | ||
'user' => [ | ||
'find' => [ | ||
'id' => $userID, | ||
], | ||
'list' => [ | ||
[ | ||
'id' => $userID, | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
|
||
$newName = 'bar'; | ||
$this->graphQL(/** @lang GraphQL */ ' | ||
mutation ($id: ID!, $name: String) { | ||
user { | ||
update(id: $id, name: $name) { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
', [ | ||
'id' => $userID, | ||
'name' => $newName, | ||
])->assertExactJson([ | ||
'data' => [ | ||
'user' => [ | ||
'update' => [ | ||
'id' => $userID, | ||
'name' => $newName, | ||
], | ||
], | ||
], | ||
]); | ||
|
||
$this->graphQL(/** @lang GraphQL */ ' | ||
mutation ($id: ID!) { | ||
user { | ||
delete(id: $id) { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
', [ | ||
'id' => $userID, | ||
])->assertExactJson([ | ||
'data' => [ | ||
'user' => [ | ||
'delete' => [ | ||
'id' => $userID, | ||
'name' => $newName, | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
} |