From dd89947fa546f8ab6aa18ecd1bc9d33634ce27af Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 7 Jun 2018 16:52:27 +0200 Subject: [PATCH] Temp. --- .../Contents/GraphQL/GraphQLModel.cs | 2 +- .../Contents/GraphQL/Types/AllTypes.cs | 30 +++++++----- .../Types/ContentDataGraphInputType.cs | 13 +---- .../GraphQL/Types/InputFieldVisitor.cs | 18 +++---- ...dObjectGraphType.cs => NestedGraphType.cs} | 4 +- .../GraphQL/Types/NestedInputGraphType.cs | 47 +++++++++++++++++++ .../GraphQL/Types/QueryGraphTypeVisitor.cs | 6 +-- .../{JTokenConverter.cs => JsonConverter.cs} | 10 ++-- .../GraphQL/Types/Utils/JsonGraphType.cs | 42 +++++++++++++++++ .../Utils/{JTokenValue.cs => JsonValue.cs} | 8 ++-- .../GraphQL/Types/Utils/NoopGraphType.cs | 6 +++ 11 files changed, 138 insertions(+), 48 deletions(-) rename src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/{NestedObjectGraphType.cs => NestedGraphType.cs} (92%) create mode 100644 src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedInputGraphType.cs rename src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/{JTokenConverter.cs => JsonConverter.cs} (72%) create mode 100644 src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonGraphType.cs rename src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/{JTokenValue.cs => JsonValue.cs} (73%) diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs index 7fd817f78..d449c0364 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/GraphQLModel.cs @@ -54,7 +54,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL schemasById = schemas.ToDictionary(x => x.Id); graphQLSchema = BuildSchema(this); - graphQLSchema.RegisterValueConverter(JTokenConverter.Instance); + graphQLSchema.RegisterValueConverter(JsonConverter.Instance); InitializeContentTypes(); } diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AllTypes.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AllTypes.cs index 3d0def8e0..70b3c14e2 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AllTypes.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/AllTypes.cs @@ -22,13 +22,21 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types public static readonly IGraphType Date = new DateGraphType(); + public static readonly IGraphType Json = new JsonGraphType(); + + public static readonly IGraphType Tags = new ListGraphType>(); + public static readonly IGraphType Float = new FloatGraphType(); + public static readonly IGraphType Status = new EnumerationGraphType(); + public static readonly IGraphType String = new StringGraphType(); public static readonly IGraphType Boolean = new BooleanGraphType(); - public static readonly IGraphType StatusType = new EnumerationGraphType(); + public static readonly IGraphType References = new ListGraphType>(); + + public static readonly IGraphType GeolocationInput = new GeolocationInputGraphType(); public static readonly IGraphType NonNullInt = new NonNullGraphType(Int); @@ -42,28 +50,24 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types public static readonly IGraphType NonNullBoolean = new NonNullGraphType(Boolean); - public static readonly IGraphType NonNullStatusType = new NonNullGraphType(StatusType); + public static readonly IGraphType NonNullStatusType = new NonNullGraphType(Status); public static readonly IGraphType NoopArray = new NoopGraphType("Array"); - public static readonly IGraphType NoopDate = new NoopGraphType("Date"); - - public static readonly IGraphType NoopJson = new NoopGraphType("Json"); + public static readonly IGraphType NoopDate = new NoopGraphType(Date); - public static readonly IGraphType NoopTags = new NoopGraphType("Tags"); + public static readonly IGraphType NoopJson = new NoopGraphType(Json); - public static readonly IGraphType NoopFloat = new NoopGraphType("Float"); + public static readonly IGraphType NoopTags = new NoopGraphType(Tags); - public static readonly IGraphType NoopString = new NoopGraphType("String"); + public static readonly IGraphType NoopFloat = new NoopGraphType(Float); - public static readonly IGraphType NoopBoolean = new NoopGraphType("Boolean"); + public static readonly IGraphType NoopString = new NoopGraphType(String); - public static readonly IGraphType NoopGeolocation = new NoopGraphType("Geolocation"); + public static readonly IGraphType NoopBoolean = new NoopGraphType(Boolean); - public static readonly IGraphType NoopReferences = new NoopGraphType("References"); + public static readonly IGraphType NoopGeolocation = new NoopGraphType(GeolocationInput); public static readonly IGraphType CommandVersion = new CommandVersionGraphType(); - - public static readonly IGraphType GeolocationInput = new GeolocationInputGraphType(); } } diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataGraphInputType.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataGraphInputType.cs index be0f0ef74..0186366a9 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataGraphInputType.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ContentDataGraphInputType.cs @@ -5,12 +5,8 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== -using System.Collections.Generic; using System.Linq; -using GraphQL.Resolvers; using GraphQL.Types; -using Newtonsoft.Json.Linq; -using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Entities.Schemas; using Squidex.Infrastructure; @@ -50,23 +46,18 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types fieldGraphType.AddField(new FieldType { Name = partitionItem.Key, - ResolvedType = inputType, Resolver = null, + ResolvedType = inputType, Description = field.RawProperties.Hints }); } fieldGraphType.Description = $"The input structure of the {fieldName} of a {schemaName} content type."; - var fieldResolver = new FuncFieldResolver>(c => - { - return c.Source.GetOrDefault(field.Name); - }); - AddField(new FieldType { Name = field.Name.ToCamelCase(), - Resolver = fieldResolver, + Resolver = null, ResolvedType = fieldGraphType, Description = $"The {fieldName} field." }); diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/InputFieldVisitor.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/InputFieldVisitor.cs index a47356642..e000e13e5 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/InputFieldVisitor.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/InputFieldVisitor.cs @@ -25,47 +25,47 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types public IGraphType Visit(IField field) { - return AllTypes.NoopReferences; + return AllTypes.References; } public IGraphType Visit(IField field) { - return AllTypes.NoopBoolean; + return AllTypes.Boolean; } public IGraphType Visit(IField field) { - return AllTypes.NoopDate; + return AllTypes.Date; } public IGraphType Visit(IField field) { - return AllTypes.NoopGeolocation; + return AllTypes.GeolocationInput; } public IGraphType Visit(IField field) { - return AllTypes.NoopJson; + return AllTypes.Json; } public IGraphType Visit(IField field) { - return AllTypes.NoopFloat; + return AllTypes.Float; } public IGraphType Visit(IField field) { - return AllTypes.NoopReferences; + return AllTypes.References; } public IGraphType Visit(IField field) { - return AllTypes.NoopString; + return AllTypes.String; } public IGraphType Visit(IField field) { - return AllTypes.NoopTags; + return AllTypes.Tags; } } } diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedObjectGraphType.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedGraphType.cs similarity index 92% rename from src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedObjectGraphType.cs rename to src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedGraphType.cs index dc9428fa5..dfbb998f7 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedObjectGraphType.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedGraphType.cs @@ -15,9 +15,9 @@ using Squidex.Infrastructure; namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types { - public sealed class NestedObjectGraphType : ObjectGraphType + public sealed class NestedGraphType : ObjectGraphType { - public NestedObjectGraphType(IGraphModel model, ISchemaEntity schema, IArrayField field) + public NestedGraphType(IGraphModel model, ISchemaEntity schema, IArrayField field) { var schemaType = schema.TypeName(); var schemaName = schema.DisplayName(); diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedInputGraphType.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedInputGraphType.cs new file mode 100644 index 000000000..44434370d --- /dev/null +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/NestedInputGraphType.cs @@ -0,0 +1,47 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System.Linq; +using GraphQL.Types; +using Squidex.Domain.Apps.Core.Schemas; +using Squidex.Domain.Apps.Entities.Schemas; +using Squidex.Infrastructure; + +namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types +{ + public sealed class NestedInputGraphType : InputObjectGraphType + { + public NestedInputGraphType(IGraphModel model, ISchemaEntity schema, IArrayField field) + { + var schemaType = schema.TypeName(); + var schemaName = schema.DisplayName(); + + var fieldType = field.TypeName(); + var fieldName = field.DisplayName(); + + Name = $"{schemaType}{fieldName}ChildDto"; + + foreach (var nestedField in field.Fields.Where(x => !x.IsHidden)) + { + var fieldInfo = model.GetGraphType(schema, nestedField); + + if (fieldInfo.ResolveType != null) + { + AddField(new FieldType + { + Name = nestedField.Name.ToCamelCase(), + Resolver = null, + ResolvedType = fieldInfo.ResolveType, + Description = $"The {fieldName}/{nestedField.DisplayName()} nested field." + }); + } + } + + Description = $"The structure of a {schemaName}.{fieldName} nested schema."; + } + } +} diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/QueryGraphTypeVisitor.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/QueryGraphTypeVisitor.cs index 3cb56c0a3..f330b5844 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/QueryGraphTypeVisitor.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/QueryGraphTypeVisitor.cs @@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types public (IGraphType ResolveType, ValueResolver Resolver) Visit(IField field) { - return ResolveAssets(assetListType); + return ResolveAssets(); } public (IGraphType ResolveType, ValueResolver Resolver) Visit(IField field) @@ -88,12 +88,12 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types private (IGraphType ResolveType, ValueResolver Resolver) ResolveNested(IArrayField field) { - var schemaFieldType = new ListGraphType(new NonNullGraphType(new NestedObjectGraphType(model, schema, field))); + var schemaFieldType = new ListGraphType(new NonNullGraphType(new NestedGraphType(model, schema, field))); return (schemaFieldType, NoopResolver); } - private (IGraphType ResolveType, ValueResolver Resolver) ResolveAssets(IGraphType assetListType) + private (IGraphType ResolveType, ValueResolver Resolver) ResolveAssets() { var resolver = new ValueResolver((value, c) => { diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenConverter.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonConverter.cs similarity index 72% rename from src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenConverter.cs rename to src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonConverter.cs index 8987e0d97..62bc939fb 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenConverter.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonConverter.cs @@ -11,22 +11,22 @@ using Newtonsoft.Json.Linq; namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types.Utils { - public sealed class JTokenConverter : IAstFromValueConverter + public sealed class JsonConverter : IAstFromValueConverter { - public static readonly JTokenConverter Instance = new JTokenConverter(); + public static readonly JsonConverter Instance = new JsonConverter(); - private JTokenConverter() + private JsonConverter() { } public IValue Convert(object value, IGraphType type) { - return new JTokenValue(value as JToken); + return new JsonValue(value as JObject); } public bool Matches(object value, IGraphType type) { - return value is JToken; + return value is JObject; } } } diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonGraphType.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonGraphType.cs new file mode 100644 index 000000000..3b99c8412 --- /dev/null +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonGraphType.cs @@ -0,0 +1,42 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using GraphQL.Language.AST; +using GraphQL.Types; + +namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types.Utils +{ + public sealed class JsonGraphType : ScalarGraphType + { + public JsonGraphType() + { + Name = "Json"; + + Description = "Unstructured Json object"; + } + + public override object Serialize(object value) + { + return value; + } + + public override object ParseValue(object value) + { + return value; + } + + public override object ParseLiteral(IValue value) + { + if (value is JsonValue jsonGraphType) + { + return jsonGraphType.Value; + } + + return value; + } + } +} diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenValue.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonValue.cs similarity index 73% rename from src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenValue.cs rename to src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonValue.cs index 2e230f324..4977e33d2 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JTokenValue.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/JsonValue.cs @@ -10,16 +10,16 @@ using Newtonsoft.Json.Linq; namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types.Utils { - public sealed class JTokenValue : ValueNode + public sealed class JsonValue : ValueNode { - public JTokenValue(JToken value) + public JsonValue(JObject value) { Value = value; } - protected override bool Equals(ValueNode node) + protected override bool Equals(ValueNode node) { - return node.Value.Equals(Value); + return false; } } } diff --git a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/NoopGraphType.cs b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/NoopGraphType.cs index 53863ca70..8b8c3ceea 100644 --- a/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/NoopGraphType.cs +++ b/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/Utils/NoopGraphType.cs @@ -17,6 +17,12 @@ namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types.Utils Name = name; } + public NoopGraphType(IGraphType type) + : this(type.Name) + { + Description = type.Description; + } + public override object Serialize(object value) { return value;