Skip to content

Commit

Permalink
Add @namespaced directive for namespacing by separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
sniper7kills authored Nov 28, 2023
1 parent 63ce62e commit b086f46
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -2279,6 +2279,51 @@ extend type Query @namespace(field: "App\\Blog") {

A [@namespace](#namespace) directive defined on a field directive wins in case of a conflict.

## @namespaced

```graphql
"""
A no-op nested field resolver that allows nesting of queries and mutations.
"""
directive @namespaced on FIELD_DEFINITION
```

The following example shows how one can namespace queries and mutations.

```graphql
type Query {
posts: PostQueries! @namespaced
}

type PostQueries {
single(
id: ID @eq
): Post! @find

list(
title: String @where(operator: "like")
): [Post!]! @paginate(defaultCount: 10)
}

type Mutation {
posts: PostMutations! @namespaced
}

type PostMutations {
create(
input: PostCreateInput @spread
): Post! @create

update(
input: PostUpdateInput @spread
): Post! @update

delete(
id: ID! @whereKey
): Post @delete
}
```

## @neq

```graphql
Expand Down
25 changes: 25 additions & 0 deletions src/Schema/Directives/NamespacedDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace App\GraphQL\Directives;

use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\FieldResolver;

final class NamespacedDirective extends BaseDirective implements FieldResolver
{
public static function definition(): string
{
return /** @lang GraphQL */ <<<'GRAPHQL'
"""
A no-op nested field resolver that allows nesting of queries and mutations.
"""
directive @namespaced on FIELD_DEFINITION
GRAPHQL;
}

public function resolveField(FieldValue $fieldValue): callable
{
return static fn (mixed $root): mixed => true;
}
}

0 comments on commit b086f46

Please sign in to comment.