Skip to content

Commit

Permalink
Merge pull request #16 from Tpt/up-mutation
Browse files Browse the repository at this point in the history
Adds description and deprecationReason in mutationWithClientMutationId
  • Loading branch information
ivome committed Mar 4, 2018
2 parents 694ad2f + 4f6bc75 commit 7558612
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Mutation/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Mutation {
*
* type MutationConfig = {
* name: string,
* description?: string,
* deprecationReason?: string,
* inputFields: InputObjectConfigFieldMap,
* outputFields: GraphQLFieldConfigMap,
* mutateAndGetPayload: mutationFn,
Expand Down Expand Up @@ -73,7 +75,7 @@ public static function mutationWithClientMutationId(array $config)
'fields' => $augmentedInputFields
]);

return [
$definition = [
'type' => $outputType,
'args' => [
'input' => [
Expand All @@ -86,6 +88,13 @@ public static function mutationWithClientMutationId(array $config)
return $payload;
}
];
if (array_key_exists('description', $config)){
$definition['description'] = $config['description'];
}
if (array_key_exists('deprecationReason', $config)){
$definition['deprecationReason'] = $config['deprecationReason'];
}
return $definition;
}

/**
Expand Down
150 changes: 150 additions & 0 deletions tests/Mutation/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class MutationTest extends \PHPUnit_Framework_TestCase
*/
protected $simpleMutation;

/**
* @var ObjectType
*/
protected $simpleMutationWithDescription;

/**
* @var ObjectType
*/
protected $simpleMutationWithDeprecationReason;

/**
* @var ObjectType
*/
Expand Down Expand Up @@ -56,6 +66,34 @@ public function setup()
}
]);

$this->simpleMutationWithDescription = Mutation::mutationWithClientMutationId([
'name' => 'SimpleMutationWithDescription',
'description' => 'Simple Mutation Description',
'inputFields' => [],
'outputFields' => [
'result' => [
'type' => Type::int()
]
],
'mutateAndGetPayload' => function () {
return ['result' => 1];
}
]);

$this->simpleMutationWithDeprecationReason = Mutation::mutationWithClientMutationId([
'name' => 'SimpleMutationWithDeprecationReason',
'inputFields' => [],
'outputFields' => [
'result' => [
'type' => Type::int()
]
],
'mutateAndGetPayload' => function () {
return ['result' => 1];
},
'deprecationReason' => 'Just because'
]);

$this->simpleMutationWithThunkFields = Mutation::mutationWithClientMutationId([
'name' => 'SimpleMutationWithThunkFields',
'inputFields' => function() {
Expand Down Expand Up @@ -105,6 +143,8 @@ public function setup()
'name' => 'Mutation',
'fields' => [
'simpleMutation' => $this->simpleMutation,
'simpleMutationWithDescription' => $this->simpleMutationWithDescription,
'simpleMutationWithDeprecationReason' => $this->simpleMutationWithDeprecationReason,
'simpleMutationWithThunkFields' => $this->simpleMutationWithThunkFields,
'edgeMutation' => $this->edgeMutation
]
Expand Down Expand Up @@ -332,6 +372,26 @@ public function testContainsCorrectField()
'kind' => 'OBJECT',
]
],
[
'name' => 'simpleMutationWithDescription',
'args' => [
[
'name' => 'input',
'type' => [
'name' => null,
'kind' => 'NON_NULL',
'ofType' => [
'name' => 'SimpleMutationWithDescriptionInput',
'kind' => 'INPUT_OBJECT'
]
],
]
],
'type' => [
'name' => 'SimpleMutationWithDescriptionPayload',
'kind' => 'OBJECT',
]
],
[
'name' => 'simpleMutationWithThunkFields',
'args' => [
Expand Down Expand Up @@ -404,6 +464,96 @@ public function testContainsCorrectField()
$this->assertValidQuery($query, $expected);
}

public function testContainsCorrectDescriptions() {
$query = '{
__schema {
mutationType {
fields {
name
description
}
}
}
}';

$expected = [
'__schema' => [
'mutationType' => [
'fields' => [
[
'name' => 'simpleMutation',
'description' => null
],
[
'name' => 'simpleMutationWithDescription',
'description' => 'Simple Mutation Description'
],
[
'name' => 'simpleMutationWithThunkFields',
'description' => null
],
[
'name' => 'edgeMutation',
'description' => null
]
]
]
]
];

$this->assertValidQuery($query, $expected);
}

public function testContainsCorrectDeprecationReasons() {
$query = '{
__schema {
mutationType {
fields(includeDeprecated: true) {
name
isDeprecated
deprecationReason
}
}
}
}';

$expected = [
'__schema' => [
'mutationType' => [
'fields' => [
[
'name' => 'simpleMutation',
'isDeprecated' => false,
'deprecationReason' => null
],
[
'name' => 'simpleMutationWithDescription',
'isDeprecated' => false,
'deprecationReason' => null
],
[
'name' => 'simpleMutationWithDeprecationReason',
'isDeprecated' => true,
'deprecationReason' => 'Just because',
],
[
'name' => 'simpleMutationWithThunkFields',
'isDeprecated' => false,
'deprecationReason' => null
],
[
'name' => 'edgeMutation',
'isDeprecated' => false,
'deprecationReason' => null
]
]
]
]
];

$this->assertValidQuery($query, $expected);
}

/**
* Helper function to test a query and the expected response.
*/
Expand Down

0 comments on commit 7558612

Please sign in to comment.