Browse Source

Update operation finished.

pull/222/head
Sebastian Stehle 8 years ago
parent
commit
343f76aa61
  1. 20
      src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AppMutationsGraphType.cs
  2. 44
      src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataChangedResultGraphType.cs
  3. 266
      tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs

20
src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AppMutationsGraphType.cs

@ -32,11 +32,13 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
var contentType = model.GetContentType(schema.Id); var contentType = model.GetContentType(schema.Id);
var contentDataType = model.GetContentDataType(schema.Id); var contentDataType = model.GetContentDataType(schema.Id);
var resultType = new ContentDataChangedResultGraphType(schemaType, schemaName, contentDataType);
var inputType = new ContentDataGraphInputType(model, schema); var inputType = new ContentDataGraphInputType(model, schema);
AddContentCreate(schemaId, schemaType, schemaName, inputType, contentDataType, contentType); AddContentCreate(schemaId, schemaType, schemaName, inputType, contentDataType, contentType);
AddContentUpdate(schemaId, schemaType, schemaName, inputType, contentDataType); AddContentUpdate(schemaId, schemaType, schemaName, inputType, resultType);
AddContentPatch(schemaId, schemaType, schemaName, inputType, contentDataType); AddContentPatch(schemaId, schemaType, schemaName, inputType, resultType);
AddContentPublish(schemaId, schemaType, schemaName); AddContentPublish(schemaId, schemaType, schemaName);
AddContentUnpublish(schemaId, schemaType, schemaName); AddContentUnpublish(schemaId, schemaType, schemaName);
AddContentArchive(schemaId, schemaType, schemaName); AddContentArchive(schemaId, schemaType, schemaName);
@ -93,7 +95,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
}); });
} }
private void AddContentUpdate(NamedId<Guid> schemaId, string schemaType, string schemaName, ContentDataGraphInputType inputType, IComplexGraphType contentDataType) private void AddContentUpdate(NamedId<Guid> schemaId, string schemaType, string schemaName, ContentDataGraphInputType inputType, IComplexGraphType resultType)
{ {
AddField(new FieldType AddField(new FieldType
{ {
@ -120,7 +122,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
DefaultValue = EtagVersion.Any DefaultValue = EtagVersion.Any
} }
}, },
ResolvedType = new NonNullGraphType(contentDataType), ResolvedType = new NonNullGraphType(resultType),
Resolver = ResolveAsync(async (c, publish) => Resolver = ResolveAsync(async (c, publish) =>
{ {
var contentId = c.GetArgument<Guid>("id"); var contentId = c.GetArgument<Guid>("id");
@ -131,13 +133,13 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
var result = commandContext.Result<ContentDataChangedResult>(); var result = commandContext.Result<ContentDataChangedResult>();
return result.Data; return result;
}), }),
Description = $"Update an {schemaName} content by id." Description = $"Update an {schemaName} content by id."
}); });
} }
private void AddContentPatch(NamedId<Guid> schemaId, string schemaType, string schemaName, ContentDataGraphInputType inputType, IComplexGraphType contentDataType) private void AddContentPatch(NamedId<Guid> schemaId, string schemaType, string schemaName, ContentDataGraphInputType inputType, IComplexGraphType resultType)
{ {
AddField(new FieldType AddField(new FieldType
{ {
@ -164,7 +166,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
DefaultValue = EtagVersion.Any DefaultValue = EtagVersion.Any
} }
}, },
ResolvedType = new NonNullGraphType(contentDataType), ResolvedType = new NonNullGraphType(resultType),
Resolver = ResolveAsync(async (c, publish) => Resolver = ResolveAsync(async (c, publish) =>
{ {
var contentId = c.GetArgument<Guid>("id"); var contentId = c.GetArgument<Guid>("id");
@ -175,7 +177,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
var result = commandContext.Result<ContentDataChangedResult>(); var result = commandContext.Result<ContentDataChangedResult>();
return result.Data; return result;
}), }),
Description = $"Patch a {schemaName} content." Description = $"Patch a {schemaName} content."
}); });
@ -303,7 +305,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
return action(c, command => return action(c, command =>
{ {
command.ExpectedVersion = c.GetArgument<int>("expectedVersion"); command.ExpectedVersion = c.GetArgument("expectedVersion", EtagVersion.Any);
return e.CommandBus.PublishAsync(command); return e.CommandBus.PublishAsync(command);
}); });

44
src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataChangedResultGraphType.cs

@ -0,0 +1,44 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using GraphQL.Resolvers;
using GraphQL.Types;
namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types
{
public sealed class ContentDataChangedResultGraphType : ObjectGraphType<ContentDataChangedResult>
{
public ContentDataChangedResultGraphType(string schemaType, string schemaName, IComplexGraphType contentDataType)
{
Name = $"{schemaName}DataChangedResultDto";
AddField(new FieldType
{
Name = "version",
ResolvedType = new IntGraphType(),
Resolver = Resolve(x => x.Version),
Description = $"The new version of the {schemaName} content."
});
AddField(new FieldType
{
Name = "data",
ResolvedType = new NonNullGraphType(contentDataType),
Resolver = Resolve(x => x.Data),
Description = $"The new data of the {schemaName} content."
});
Description = $"The result of the {schemaName} mutation";
}
private static IFieldResolver Resolve(Func<ContentDataChangedResult, object> action)
{
return new FuncFieldResolver<ContentDataChangedResult, object>(c => action(c.Source));
}
}
}

266
tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs

@ -29,53 +29,150 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
} }
[Fact] [Fact]
public async Task Should_return_single_content_when_patching_content() public async Task Should_return_single_content_when_updating_content()
{ {
var contentId = Guid.NewGuid(); var contentId = Guid.NewGuid();
var content = CreateContent(contentId, Guid.Empty, Guid.Empty); var content = CreateContent(contentId, Guid.Empty, Guid.Empty);
var query = $@" var query = $@"
mutation OP($data: MySchemaInputDto!) {{ mutation OP($data: MySchemaInputDto!) {{
patchMySchemaContent(id: ""{contentId}"", data: $data) {{ updateMySchemaContent(id: ""{contentId}"", data: $data, expectedVersion: 10) {{
myString {{ version
de data {{
}} myString {{
myNumber {{ de
iv }}
}} myNumber {{
myBoolean {{ iv
iv }}
}} myBoolean {{
myDatetime {{ iv
iv }}
}} myDatetime {{
myJson {{ iv
iv }}
}} myJson {{
myGeolocation {{ iv
iv }}
}} myGeolocation {{
myTags {{ iv
iv }}
myTags {{
iv
}}
}} }}
}} }}
}}"; }}";
commandContext.Complete(new ContentDataChangedResult(content.Data, 1)); commandContext.Complete(new ContentDataChangedResult(content.Data, 13));
var camelContent = new NamedContentData(); var inputContent = GetInputContent(content);
foreach (var kvp in content.Data) var variables =
new JObject(
new JProperty("data", inputContent));
var result = await sut.QueryAsync(app, user, new GraphQLQuery { Query = query, Variables = variables });
var expected = new
{ {
if (kvp.Key != "my-json") data = new
{ {
camelContent[kvp.Key.ToCamelCase()] = kvp.Value; updateMySchemaContent = new
{
version = 13,
data = new
{
myString = new
{
de = "value"
},
myNumber = new
{
iv = 1
},
myBoolean = new
{
iv = true
},
myDatetime = new
{
iv = content.LastModified.ToDateTimeUtc()
},
myJson = new
{
iv = new
{
value = 1
}
},
myGeolocation = new
{
iv = new
{
latitude = 10,
longitude = 20
}
},
myTags = new
{
iv = new[]
{
"tag1",
"tag2"
}
}
}
}
} }
} };
AssertResult(expected, result);
}
[Fact]
public async Task Should_return_single_content_when_patching_content()
{
var contentId = Guid.NewGuid();
var content = CreateContent(contentId, Guid.Empty, Guid.Empty);
var query = $@"
mutation OP($data: MySchemaInputDto!) {{
patchMySchemaContent(id: ""{contentId}"", data: $data, expectedVersion: 10) {{
version
data {{
myString {{
de
}}
myNumber {{
iv
}}
myBoolean {{
iv
}}
myDatetime {{
iv
}}
myJson {{
iv
}}
myGeolocation {{
iv
}}
myTags {{
iv
}}
}}
}}
}}";
commandContext.Complete(new ContentDataChangedResult(content.Data, 13));
var inputContent = GetInputContent(content);
var variables = var variables =
new JObject( new JObject(
new JProperty("data", JObject.FromObject(camelContent))); new JProperty("data", inputContent));
var result = await sut.QueryAsync(app, user, new GraphQLQuery { Query = query, Variables = variables }); var result = await sut.QueryAsync(app, user, new GraphQLQuery { Query = query, Variables = variables });
@ -85,43 +182,46 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
{ {
patchMySchemaContent = new patchMySchemaContent = new
{ {
myString = new version = 13,
{ data = new {
de = "value" myString = new
},
myNumber = new
{
iv = 1
},
myBoolean = new
{
iv = true
},
myDatetime = new
{
iv = content.LastModified.ToDateTimeUtc()
},
myJson = new
{
iv = new
{ {
value = 1 de = "value"
} },
}, myNumber = new
myGeolocation = new
{
iv = new
{ {
latitude = 10, iv = 1
longitude = 20 },
} myBoolean = new
}, {
myTags = new iv = true
{ },
iv = new[] myDatetime = new
{
iv = content.LastModified.ToDateTimeUtc()
},
myJson = new
{
iv = new
{
value = 1
}
},
myGeolocation = new
{
iv = new
{
latitude = 10,
longitude = 20
}
},
myTags = new
{ {
"tag1", iv = new[]
"tag2" {
"tag1",
"tag2"
}
} }
} }
} }
@ -138,7 +238,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
var query = $@" var query = $@"
mutation {{ mutation {{
publishMySchemaContent(id: ""{contentId}"") {{ publishMySchemaContent(id: ""{contentId}"", expectedVersion: 10) {{
version version
}} }}
}}"; }}";
@ -164,7 +264,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
A<ChangeContentStatus>.That.Matches(x => A<ChangeContentStatus>.That.Matches(x =>
x.SchemaId.Equals(schema.NamedId()) && x.SchemaId.Equals(schema.NamedId()) &&
x.ContentId == contentId && x.ContentId == contentId &&
x.Status == Status.Published))) x.Status == Status.Published &&
x.ExpectedVersion == 10)))
.MustHaveHappened(); .MustHaveHappened();
} }
@ -175,7 +276,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
var query = $@" var query = $@"
mutation {{ mutation {{
unpublishMySchemaContent(id: ""{contentId}"") {{ unpublishMySchemaContent(id: ""{contentId}"", expectedVersion: 10) {{
version version
}} }}
}}"; }}";
@ -201,7 +302,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
A<ChangeContentStatus>.That.Matches(x => A<ChangeContentStatus>.That.Matches(x =>
x.SchemaId.Equals(schema.NamedId()) && x.SchemaId.Equals(schema.NamedId()) &&
x.ContentId == contentId && x.ContentId == contentId &&
x.Status == Status.Draft))) x.Status == Status.Draft &&
x.ExpectedVersion == 10)))
.MustHaveHappened(); .MustHaveHappened();
} }
@ -212,7 +314,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
var query = $@" var query = $@"
mutation {{ mutation {{
archiveMySchemaContent(id: ""{contentId}"") {{ archiveMySchemaContent(id: ""{contentId}"", expectedVersion: 10) {{
version version
}} }}
}}"; }}";
@ -238,7 +340,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
A<ChangeContentStatus>.That.Matches(x => A<ChangeContentStatus>.That.Matches(x =>
x.SchemaId.Equals(schema.NamedId()) && x.SchemaId.Equals(schema.NamedId()) &&
x.ContentId == contentId && x.ContentId == contentId &&
x.Status == Status.Archived))) x.Status == Status.Archived &&
x.ExpectedVersion == 10)))
.MustHaveHappened(); .MustHaveHappened();
} }
@ -249,7 +352,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
var query = $@" var query = $@"
mutation {{ mutation {{
restoreMySchemaContent(id: ""{contentId}"") {{ restoreMySchemaContent(id: ""{contentId}"", expectedVersion: 10) {{
version version
}} }}
}}"; }}";
@ -275,7 +378,8 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
A<ChangeContentStatus>.That.Matches(x => A<ChangeContentStatus>.That.Matches(x =>
x.SchemaId.Equals(schema.NamedId()) && x.SchemaId.Equals(schema.NamedId()) &&
x.ContentId == contentId && x.ContentId == contentId &&
x.Status == Status.Draft))) x.Status == Status.Draft &&
x.ExpectedVersion == 10)))
.MustHaveHappened(); .MustHaveHappened();
} }
@ -286,7 +390,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
var query = $@" var query = $@"
mutation {{ mutation {{
deleteMySchemaContent(id: ""{contentId}"") {{ deleteMySchemaContent(id: ""{contentId}"", expectedVersion: 10) {{
version version
}} }}
}}"; }}";
@ -311,8 +415,24 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL
A.CallTo(() => commandBus.PublishAsync( A.CallTo(() => commandBus.PublishAsync(
A<DeleteContent>.That.Matches(x => A<DeleteContent>.That.Matches(x =>
x.SchemaId.Equals(schema.NamedId()) && x.SchemaId.Equals(schema.NamedId()) &&
x.ContentId == contentId))) x.ContentId == contentId &&
x.ExpectedVersion == 10)))
.MustHaveHappened(); .MustHaveHappened();
} }
private static JObject GetInputContent(IContentEntity content)
{
var camelContent = new NamedContentData();
foreach (var kvp in content.Data)
{
if (kvp.Key != "my-json")
{
camelContent[kvp.Key.ToCamelCase()] = kvp.Value;
}
}
return JObject.FromObject(camelContent);
}
} }
} }

Loading…
Cancel
Save