-
-
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.
- Loading branch information
Showing
8 changed files
with
394 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Nuwave\Relay\Support\Definition; | ||
|
||
use Validator; | ||
use GraphQL\Type\Definition\Type; | ||
use GraphQL\Type\Definition\ObjectType; | ||
use GraphQL\Type\Definition\ResolveInfo; | ||
use GraphQL\Type\Definition\InputObjectType; | ||
use Nuwave\Lighthouse\Support\Exceptions\ValidationError; | ||
|
||
abstract class RelayMutation extends GraphQLMutation | ||
{ | ||
/** | ||
* Type being mutated is RelayType. | ||
* | ||
* @var boolean | ||
*/ | ||
protected $mutatesRelayType = true; | ||
|
||
/** | ||
* Mutation id sent from client. | ||
* | ||
* @var integer|null | ||
*/ | ||
protected $clientMutationId = null; | ||
|
||
/** | ||
* Generate Relay compliant output type. | ||
* | ||
* @return InputObjectType | ||
*/ | ||
public function type() | ||
{ | ||
return new ObjectType([ | ||
'name' => ucfirst($this->name()) . 'Payload', | ||
'fields' => array_merge($this->outputFields(), [ | ||
'clientMutationId' => [ | ||
'type' => Type::nonNull(Type::string()), | ||
'resolve' => function () { | ||
return $this->clientMutationId; | ||
} | ||
] | ||
]) | ||
]); | ||
} | ||
|
||
/** | ||
* Generate Relay compliant arguments. | ||
* | ||
* @return array | ||
*/ | ||
public function args() | ||
{ | ||
$inputType = new InputObjectType([ | ||
'name' => ucfirst($this->name()) . 'Input', | ||
'fields' => array_merge($this->inputFields(), [ | ||
'clientMutationId' => [ | ||
'type' => Type::nonNull(Type::string()) | ||
] | ||
]) | ||
]); | ||
return [ | ||
'input' => [ | ||
'type' => Type::nonNull($inputType) | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Resolve mutation. | ||
* | ||
* @param mixed $_ | ||
* @param array $args | ||
* @param ResolveInfo $info | ||
* @return array | ||
*/ | ||
public function resolve($_, $args, ResolveInfo $info) | ||
{ | ||
if ($this->mutatesRelayType && isset($args['input']['id'])) { | ||
$args['input']['relay_id'] = $args['input']['id']; | ||
$args['input']['id'] = $this->decodeRelayId($args['input']['id']); | ||
} | ||
|
||
$this->clientMutationId = $args['input']['clientMutationId']; | ||
|
||
return $this->mutateAndGetPayload($args['input'], $info); | ||
} | ||
|
||
/** | ||
* Get input for validation. | ||
* | ||
* @param array $arguments | ||
* @return array | ||
*/ | ||
protected function getInput(array $arguments) | ||
{ | ||
return array_get($arguments, '1.input', []); | ||
} | ||
|
||
/** | ||
* Perform mutation. | ||
* | ||
* @param array $input | ||
* @param ResolveInfo $info | ||
* @return array | ||
*/ | ||
abstract protected function mutateAndGetPayload(array $input, ResolveInfo $info); | ||
|
||
/** | ||
* List of available input fields. | ||
* | ||
* @return array | ||
*/ | ||
abstract protected function inputFields(); | ||
|
||
/** | ||
* List of output fields. | ||
* | ||
* @return array | ||
*/ | ||
abstract protected function outputFields(); | ||
|
||
/** | ||
* Get name of mutation. | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function name(); | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Nuwave\Lighthouse\Support\Interfaces; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
|
||
interface RelayMutation | ||
{ | ||
/** | ||
* List of output fields. | ||
* | ||
* @return array | ||
*/ | ||
public function outputFields(); | ||
|
||
/** | ||
* Perform mutation. | ||
* | ||
* @param array $input | ||
* @param \GraphQL\Type\Definition\ResolveInfo $info | ||
* @return array | ||
*/ | ||
public function mutateAndGetPayload(array $input, ResolveInfo $info); | ||
} |
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 |
---|---|---|
|
@@ -2,13 +2,17 @@ | |
|
||
namespace Nuwave\Lighthouse\Tests\Queries; | ||
|
||
use Nuwave\Lighthouse\Support\Traits\GlobalIdTrait; | ||
use Nuwave\Lighthouse\Tests\TestCase; | ||
use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\UserType; | ||
use Nuwave\Lighthouse\Tests\Support\GraphQL\Types\TaskType; | ||
use Nuwave\Lighthouse\Tests\Support\GraphQL\Mutations\UpdateEmailMutation; | ||
use Nuwave\Lighthouse\Tests\Support\GraphQL\Mutations\UpdateEmailRelayMutation; | ||
|
||
class MutationTest extends TestCase | ||
{ | ||
use GlobalIdTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
|
@@ -33,4 +37,46 @@ public function itCanResolveMutation() | |
|
||
$this->assertEquals(['data' => $expected], $this->executeQuery($query)); | ||
} | ||
|
||
/** | ||
* @test | ||
* @group failing | ||
*/ | ||
public function itCanResolveRelayMutation() | ||
{ | ||
$id = $this->encodeGlobalId(UserType::class, 'foo'); | ||
|
||
$query = 'mutation UpdateUserEmailRelay($input: UpdateUserPasswordRelayInput!) { | ||
updateEmail(input: $input) { | ||
user { | ||
} | ||
clientMutationId | ||
} | ||
}'; | ||
|
||
$expected = [ | ||
'updateEmail' => [ | ||
'user' => [ | ||
'email' => '[email protected]' | ||
], | ||
'clientMutationId' => 'abcde', | ||
] | ||
]; | ||
|
||
$variables = [ | ||
'input' => [ | ||
'id' => $id, | ||
'email' => '[email protected]', | ||
'clientMutationId' => 'abcde' | ||
] | ||
]; | ||
|
||
$graphql = app('graphql'); | ||
$graphql->schema()->type('user', UserType::class); | ||
$graphql->schema()->type('task', TaskType::class); | ||
$graphql->schema()->mutation('updateEmail', UpdateEmailRelayMutation::class); | ||
|
||
$this->assertEquals(['data' => $expected], $this->executeQuery($query, $variables)); | ||
} | ||
} |
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
Oops, something went wrong.