-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify exposing root type
Query
in nested results (#2384)
- Loading branch information
Showing
8 changed files
with
139 additions
and
1 deletion.
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,38 @@ | ||
# Best Practices | ||
|
||
When starting out with developing a GraphQL API, it is a good idea to look at existing best practices. | ||
We recommend you use [GraphQL Rules](https://graphql-rules.com) and the following tips as a starting point | ||
to develop a set of guidelines that works for you. | ||
|
||
## In the mutation response, return a field of type Query | ||
|
||
If you decide to [return the `Query` object in every mutation payload](https://graphql-rules.com/rules/mutation-payload-query), | ||
Lighthouse makes it very easy. | ||
|
||
```graphql | ||
type Mutation { | ||
likePost(id: ID!): LikePostResult! | ||
} | ||
|
||
type LikePostResult { | ||
record: Post! | ||
query: Query! | ||
} | ||
``` | ||
|
||
Lighthouse automatically resolves the `Query` type, your mutation resolver only has to focus on its specific work. | ||
|
||
```php | ||
namespace App\GraphQL\Mutations; | ||
|
||
final class LikePost | ||
{ | ||
/** @param array{id: string} $args */ | ||
public function __invoke(mixed $root, array $args): array | ||
{ | ||
// do the main work | ||
|
||
return ['record' => $post]; | ||
} | ||
} | ||
``` |
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,38 @@ | ||
# Best Practices | ||
|
||
When starting out with developing a GraphQL API, it is a good idea to look at existing best practices. | ||
We recommend you use [GraphQL Rules](https://graphql-rules.com) and the following tips as a starting point | ||
to develop a set of guidelines that works for you. | ||
|
||
## In the mutation response, return a field of type Query | ||
|
||
If you decide to [return the `Query` object in every mutation payload](https://graphql-rules.com/rules/mutation-payload-query), | ||
Lighthouse makes it very easy. | ||
|
||
```graphql | ||
type Mutation { | ||
likePost(id: ID!): LikePostResult! | ||
} | ||
|
||
type LikePostResult { | ||
record: Post! | ||
query: Query! | ||
} | ||
``` | ||
|
||
Lighthouse automatically resolves the `Query` type, your mutation resolver only has to focus on its specific work. | ||
|
||
```php | ||
namespace App\GraphQL\Mutations; | ||
|
||
final class LikePost | ||
{ | ||
/** @param array{id: string} $args */ | ||
public function __invoke(mixed $root, array $args): array | ||
{ | ||
// do the main work | ||
|
||
return ['record' => $post]; | ||
} | ||
} | ||
``` |
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