Skip to content

Implement the Relay specification for mutations #1597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions features/graphql/mutation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ Feature: GraphQL mutation support
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.__type.fields[0].name" should contain "delete"
And the JSON node "data.__type.fields[0].description" should contain "Deletes "
And the JSON node "data.__type.fields[0].type.name" should contain "DeleteMutation"
And the JSON node "data.__type.fields[0].description" should match '/^Deletes a [A-z]+\.$/'
And the JSON node "data.__type.fields[0].type.name" should match "/^delete[A-z]+Payload$/"
And the JSON node "data.__type.fields[0].type.kind" should be equal to "OBJECT"
And the JSON node "data.__type.fields[0].args[0].name" should be equal to "input"
And the JSON node "data.__type.fields[0].args[0].type.name" should contain "InputDeleteMutation"
And the JSON node "data.__type.fields[0].args[0].type.name" should match "/^delete[A-z]+Input$/"
And the JSON node "data.__type.fields[0].args[0].type.kind" should be equal to "INPUT_OBJECT"

Scenario: Create an item
When I send the following GraphQL request:
"""
mutation {
createFoo(input: {name: "A new one", bar: "new"}) {
id,
name,
createFoo(input: {name: "A new one", bar: "new", clientMutationId: "myId"}) {
id
name
bar
clientMutationId
}
}
"""
Expand All @@ -51,21 +52,24 @@ Feature: GraphQL mutation support
And the JSON node "data.createFoo.id" should be equal to "/foos/1"
And the JSON node "data.createFoo.name" should be equal to "A new one"
And the JSON node "data.createFoo.bar" should be equal to "new"
And the JSON node "data.createFoo.clientMutationId" should be equal to "myId"

@dropSchema
Scenario: Delete an item through a mutation
When I send the following GraphQL request:
"""
mutation {
deleteFoo(input: {id: "/foos/1"}) {
deleteFoo(input: {id: "/foos/1", clientMutationId: "anotherId"}) {
id
clientMutationId
}
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.deleteFoo.id" should be equal to "/foos/1"
And the JSON node "data.deleteFoo.clientMutationId" should be equal to "anotherId"

@createSchema
@dropSchema
Expand All @@ -74,15 +78,17 @@ Feature: GraphQL mutation support
When I send the following GraphQL request:
"""
mutation {
deleteCompositeRelation(input: {id: "/composite_relations/compositeItem=1;compositeLabel=1"}) {
deleteCompositeRelation(input: {id: "/composite_relations/compositeItem=1;compositeLabel=1", clientMutationId: "myId"}) {
id
clientMutationId
}
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.deleteCompositeRelation.id" should be equal to "/composite_relations/compositeItem=1;compositeLabel=1"
And the JSON node "data.deleteCompositeRelation.clientMutationId" should be equal to "myId"

@createSchema
@dropSchema
Expand All @@ -91,10 +97,11 @@ Feature: GraphQL mutation support
When I send the following GraphQL request:
"""
mutation {
updateFoo(input: {id: "/foos/1", bar: "Modified description."}) {
id,
name,
updateFoo(input: {id: "/foos/1", bar: "Modified description.", clientMutationId: "myId"}) {
id
name
bar
clientMutationId
}
}
"""
Expand All @@ -104,6 +111,7 @@ Feature: GraphQL mutation support
And the JSON node "data.updateFoo.id" should be equal to "/foos/1"
And the JSON node "data.updateFoo.name" should be equal to "Hawsepipe"
And the JSON node "data.updateFoo.bar" should be equal to "Modified description."
And the JSON node "data.updateFoo.clientMutationId" should be equal to "myId"

@createSchema
@dropSchema
Expand All @@ -112,9 +120,10 @@ Feature: GraphQL mutation support
When I send the following GraphQL request:
"""
mutation {
updateCompositeRelation(input: {id: "/composite_relations/compositeItem=1;compositeLabel=2", value: "Modified value."}) {
updateCompositeRelation(input: {id: "/composite_relations/compositeItem=1;compositeLabel=2", value: "Modified value.", clientMutationId: "myId"}) {
id
value
clientMutationId
}
}
"""
Expand All @@ -123,3 +132,4 @@ Feature: GraphQL mutation support
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.updateCompositeRelation.id" should be equal to "/composite_relations/compositeItem=1;compositeLabel=2"
And the JSON node "data.updateCompositeRelation.value" should be equal to "Modified value."
And the JSON node "data.updateCompositeRelation.clientMutationId" should be equal to "myId"
15 changes: 11 additions & 4 deletions src/Graphql/Resolver/Factory/ItemMutationResolverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function __construct(IriConverterInterface $iriConverter, DataPersisterIn
public function __invoke(string $resourceClass = null, string $rootClass = null, string $operationName = null): callable
{
return function ($root, $args, $context, ResolveInfo $info) use ($resourceClass, $operationName) {
$data = ['clientMutationId' => $args['input']['clientMutationId'] ?? null];
$item = null;

if (isset($args['input']['id'])) {
try {
$item = $this->iriConverter->getItemFromIri($args['input']['id']);
Expand All @@ -75,13 +77,18 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
$item,
ItemNormalizer::FORMAT,
$resourceMetadata->getGraphqlAttribute($operationName, 'normalization_context', [], true)
);
) + $data;

case 'delete':
$this->dataPersister->remove($item);

return $args['input'];
if ($item) {
$this->dataPersister->remove($item);
$data['id'] = $args['input']['id'];
} else {
$data['id'] = null;
}
}

return $data;
};
}
}
28 changes: 19 additions & 9 deletions src/Graphql/Type/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,18 @@ private function convertType(Type $type, bool $input = false, string $mutationNa
*/
private function getResourceObjectType(string $resourceClass, ResourceMetadata $resourceMetadata, bool $input = false, string $mutationName = null): GraphQLType
{
$shortName = $resourceMetadata->getShortName();
if ($input) {
$shortName .= 'Input';
if (isset($this->graphqlTypes[$resourceClass][$mutationName][$input])) {
return $this->graphqlTypes[$resourceClass][$mutationName][$input];
}

$shortName = $resourceMetadata->getShortName();
if (null !== $mutationName) {
$shortName .= ucfirst($mutationName).'Mutation';
$shortName = $mutationName.ucfirst($shortName);
}

if (isset($this->graphqlTypes[$resourceClass][$mutationName][$input])) {
return $this->graphqlTypes[$resourceClass][$mutationName][$input];
if ($input) {
$shortName .= 'Input';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if $mutationName is null but $input is true? You will concatenate a string on a non-existing variable. But could this situation happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$shortName always exists.

} elseif (null !== $mutationName) {
$shortName .= 'Payload';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May this be shorter using sprintf?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it will be more clear as $shortName is built from conditions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, sprinf doesn't help.


$configuration = [
Expand All @@ -325,10 +327,14 @@ private function getResourceObjectType(string $resourceClass, ResourceMetadata $
private function getResourceObjectTypeFields(string $resource, bool $input = false, string $mutationName = null): array
{
$fields = [];
$idField = ['type' => GraphQLType::id()];
$idField = ['type' => GraphQLType::nonNull(GraphQLType::id())];
$clientMutationId = GraphQLType::nonNull(GraphQLType::string());

if ('delete' === $mutationName) {
return ['id' => $idField];
return [
'id' => $idField,
'clientMutationId' => $clientMutationId,
];
}

if (!$input || 'create' !== $mutationName) {
Expand All @@ -350,6 +356,10 @@ private function getResourceObjectTypeFields(string $resource, bool $input = fal
}
}

if (null !== $mutationName) {
$fields['clientMutationId'] = $clientMutationId;
}

return $fields;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testCreateItemMutationResolverNoItem()
$resolverFactory = $this->createItemMutationResolverFactory(null, $dataPersisterProphecy);
$resolver = $resolverFactory(Dummy::class, Dummy::class, 'delete');

$resolver(null, ['input' => ['id' => '/dummies/3']], null, new ResolveInfo([]));
$resolver(null, ['input' => ['id' => '/dummies/3', 'clientMutationId' => '1936']], null, new ResolveInfo([]));
}

public function testCreateItemDeleteMutationResolver()
Expand All @@ -56,7 +56,7 @@ public function testCreateItemDeleteMutationResolver()
$resolverFactory = $this->createItemMutationResolverFactory($dummy, $dataPersisterProphecy);
$resolver = $resolverFactory(Dummy::class, null, 'delete');

$this->assertEquals(['id' => '/dummies/3'], $resolver(null, ['input' => ['id' => '/dummies/3']], null, new ResolveInfo([])));
$this->assertEquals(['id' => '/dummies/3', 'clientMutationId' => '1936'], $resolver(null, ['input' => ['id' => '/dummies/3', 'clientMutationId' => '1936']], null, new ResolveInfo([])));
}

private function createItemMutationResolverFactory($item, ObjectProphecy $dataPersisterProphecy): ResolverFactoryInterface
Expand Down