From 29734a3954d6abadb4f5eebfd67e2e02fd507765 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sat, 8 Jul 2017 21:42:36 +0200 Subject: [PATCH] Some restructuring --- src/Squidex.Domain.Apps.Core/Schemas/Field.cs | 2 - .../Squidex.Domain.Apps.Core.csproj | 1 - .../GraphQl/ContentDataGraphType.cs | 37 ++++++++ .../GraphQl/ContentFieldGraphType.cs | 36 +++++++ ...SchemaGraphType.cs => ContentGraphType.cs} | 45 --------- .../GraphQl/GraphQLContext.cs | 94 +++++++++++++++++++ .../GraphQl/IGraphQLContext.cs} | 17 ++-- .../GraphQl/IGraphQLResolver.cs | 15 --- 8 files changed, 177 insertions(+), 70 deletions(-) create mode 100644 src/Squidex.Domain.Apps.Read/GraphQl/ContentDataGraphType.cs create mode 100644 src/Squidex.Domain.Apps.Read/GraphQl/ContentFieldGraphType.cs rename src/Squidex.Domain.Apps.Read/GraphQl/{SchemaGraphType.cs => ContentGraphType.cs} (59%) create mode 100644 src/Squidex.Domain.Apps.Read/GraphQl/GraphQLContext.cs rename src/{Squidex.Domain.Apps.Core/Schemas/IGraphQLResolver.cs => Squidex.Domain.Apps.Read/GraphQl/IGraphQLContext.cs} (50%) delete mode 100644 src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLResolver.cs diff --git a/src/Squidex.Domain.Apps.Core/Schemas/Field.cs b/src/Squidex.Domain.Apps.Core/Schemas/Field.cs index eba635edf..b70304613 100644 --- a/src/Squidex.Domain.Apps.Core/Schemas/Field.cs +++ b/src/Squidex.Domain.Apps.Core/Schemas/Field.cs @@ -79,8 +79,6 @@ namespace Squidex.Domain.Apps.Core.Schemas public abstract object ConvertValue(JToken value); - public abstract IGraph - public Field Hide() { return Clone(clone => clone.isHidden = true); diff --git a/src/Squidex.Domain.Apps.Core/Squidex.Domain.Apps.Core.csproj b/src/Squidex.Domain.Apps.Core/Squidex.Domain.Apps.Core.csproj index 1d5c16e35..d8e51a942 100644 --- a/src/Squidex.Domain.Apps.Core/Squidex.Domain.Apps.Core.csproj +++ b/src/Squidex.Domain.Apps.Core/Squidex.Domain.Apps.Core.csproj @@ -10,7 +10,6 @@ - diff --git a/src/Squidex.Domain.Apps.Read/GraphQl/ContentDataGraphType.cs b/src/Squidex.Domain.Apps.Read/GraphQl/ContentDataGraphType.cs new file mode 100644 index 000000000..610ce4a6f --- /dev/null +++ b/src/Squidex.Domain.Apps.Read/GraphQl/ContentDataGraphType.cs @@ -0,0 +1,37 @@ +// ========================================================================== +// SchemaGraphType.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using GraphQL.Resolvers; +using GraphQL.Types; +using Squidex.Domain.Apps.Core.Contents; +using Squidex.Infrastructure; +using Schema = Squidex.Domain.Apps.Core.Schemas.Schema; + +namespace Squidex.Domain.Apps.Read.GraphQl +{ + public sealed class ContentDataGraphType : ObjectGraphType + { + private static readonly IFieldResolver FieldResolver = + new FuncFieldResolver(c => c.Source.GetOrDefault(c.FieldName)); + + public ContentDataGraphType(Schema schema, IGraphQLContext context) + { + foreach (var field in schema.Fields) + { + var fieldName = field.Name; + + AddField(new FieldType + { + Name = fieldName, + Resolver = FieldResolver, + ResolvedType = new SchemaFieldGraphType(field, context), + }); + } + } + } +} diff --git a/src/Squidex.Domain.Apps.Read/GraphQl/ContentFieldGraphType.cs b/src/Squidex.Domain.Apps.Read/GraphQl/ContentFieldGraphType.cs new file mode 100644 index 000000000..22af68b94 --- /dev/null +++ b/src/Squidex.Domain.Apps.Read/GraphQl/ContentFieldGraphType.cs @@ -0,0 +1,36 @@ +// ========================================================================== +// SchemaGraphType.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using GraphQL.Types; +using Squidex.Domain.Apps.Core.Contents; +using Squidex.Domain.Apps.Core.Schemas; + +namespace Squidex.Domain.Apps.Read.GraphQl +{ + + public sealed class ContentFieldGraphType : ObjectGraphType + { + public ContentFieldGraphType(Field field, IGraphQLContext context) + { + var partition = context.ResolvePartition(field.Paritioning); + + foreach (var partitionItem in partition) + { + var fieldInfo = context.GetGraphType(field); + + AddField(new FieldType + { + Name = partitionItem.Key, + Resolver = fieldInfo.Resolver, + ResolvedType = fieldInfo.ResolveType, + Description = field.RawProperties.Hints + }); + } + } + } +} diff --git a/src/Squidex.Domain.Apps.Read/GraphQl/SchemaGraphType.cs b/src/Squidex.Domain.Apps.Read/GraphQl/ContentGraphType.cs similarity index 59% rename from src/Squidex.Domain.Apps.Read/GraphQl/SchemaGraphType.cs rename to src/Squidex.Domain.Apps.Read/GraphQl/ContentGraphType.cs index bbb5aca0e..b35cfe045 100644 --- a/src/Squidex.Domain.Apps.Read/GraphQl/SchemaGraphType.cs +++ b/src/Squidex.Domain.Apps.Read/GraphQl/ContentGraphType.cs @@ -8,20 +8,13 @@ using GraphQL.Resolvers; using GraphQL.Types; -using Squidex.Domain.Apps.Core; using Squidex.Domain.Apps.Core.Contents; -using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Read.Contents; using Squidex.Infrastructure; using Schema = Squidex.Domain.Apps.Core.Schemas.Schema; namespace Squidex.Domain.Apps.Read.GraphQl { - public interface IGraphQLContext : IGraphQLResolver - { - IFieldPartitioning ResolvePartition(Partitioning key); - } - public sealed class ContentGraphType : ObjectGraphType { private static readonly IFieldResolver DataResolver = @@ -58,42 +51,4 @@ namespace Squidex.Domain.Apps.Read.GraphQl }); } } - - public sealed class SchemaDataGraphType : ObjectGraphType - { - private static readonly IFieldResolver FieldResolver = - new FuncFieldResolver(c => c.Source.GetOrDefault(c.FieldName)); - - public SchemaDataGraphType(Schema schema, IGraphQLContext context) - { - foreach (var field in schema.Fields) - { - var fieldName = field.Name; - - AddField(new FieldType - { - Name = fieldName, - Resolver = FieldResolver, - ResolvedType = new SchemaFieldGraphType(field, context), - }); - } - } - } - - public sealed class SchemaFieldGraphType : ObjectGraphType - { - public SchemaFieldGraphType(Field field, IGraphQLContext context) - { - var partition = context.ResolvePartition(field.Paritioning); - - foreach (var partitionItem in partition) - { - AddField(new FieldType - { - Name = partitionItem.Key, - Resolver = new FuncFieldResolver() - } - } - } - } } diff --git a/src/Squidex.Domain.Apps.Read/GraphQl/GraphQLContext.cs b/src/Squidex.Domain.Apps.Read/GraphQl/GraphQLContext.cs new file mode 100644 index 000000000..3bf18433c --- /dev/null +++ b/src/Squidex.Domain.Apps.Read/GraphQl/GraphQLContext.cs @@ -0,0 +1,94 @@ +// ========================================================================== +// GraphQLContext.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using GraphQL.Resolvers; +using GraphQL.Types; +using Squidex.Domain.Apps.Core; +using Squidex.Domain.Apps.Core.Contents; +using Squidex.Domain.Apps.Core.Schemas; +using Squidex.Domain.Apps.Read.Contents; +using Squidex.Infrastructure; +using Schema = Squidex.Domain.Apps.Core.Schemas.Schema; + +namespace Squidex.Domain.Apps.Read.GraphQl +{ + public sealed class GraphQLContext : IGraphQLContext + { + private readonly PartitionResolver partitionResolver; + private readonly Dictionary fieldInfos; + + public GraphQLContext(PartitionResolver partitionResolver) + { + Guard.NotNull(partitionResolver, nameof(partitionResolver)); + + this.partitionResolver = partitionResolver; + + var defaultResolver = + new FuncFieldResolver(c => c.Source.GetOrDefault(c.FieldName)); + + fieldInfos = new Dictionary + { + { + typeof(StringField), + (new StringGraphType(), defaultResolver) + }, + { + typeof(BooleanField), + (new BooleanGraphType(), defaultResolver) + }, + { + typeof(NumberField), + (new FloatGraphType(), defaultResolver) + }, + { + typeof(DateTimeField), + (new FloatGraphType(), defaultResolver) + }, + { + typeof(JsonField), + (new ObjectGraphType(), defaultResolver) + }, + { + typeof(GeolocationField), + (new ObjectGraphType(), defaultResolver) + }, + { + typeof(AssetsField), + (new ListGraphType(), defaultResolver) + }, + { + typeof(ReferencesField), + (new ListGraphType(), defaultResolver) + } + }; + } + + public IGraphType GetSchemaListType(Schema schema) + { + throw new NotImplementedException(); + } + + public IGraphType GetSchemaListType(Guid schemaId) + { + throw new NotImplementedException(); + } + + public IFieldPartitioning ResolvePartition(Partitioning key) + { + return partitionResolver(key); + } + + public (IGraphType ResolveType, IFieldResolver Resolver) GetGraphType(Field field) + { + return fieldInfos[field.GetType()]; + } + } +} diff --git a/src/Squidex.Domain.Apps.Core/Schemas/IGraphQLResolver.cs b/src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLContext.cs similarity index 50% rename from src/Squidex.Domain.Apps.Core/Schemas/IGraphQLResolver.cs rename to src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLContext.cs index 35125b23a..6d7faa1c2 100644 --- a/src/Squidex.Domain.Apps.Core/Schemas/IGraphQLResolver.cs +++ b/src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLContext.cs @@ -1,22 +1,25 @@ // ========================================================================== -// IGraphQLResolver.cs +// SchemaGraphType.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== -using System; +using GraphQL.Resolvers; using GraphQL.Types; +using Squidex.Domain.Apps.Core; +using Squidex.Domain.Apps.Core.Schemas; +using Schema = Squidex.Domain.Apps.Core.Schemas.Schema; -namespace Squidex.Domain.Apps.Core.Schemas +namespace Squidex.Domain.Apps.Read.GraphQl { - public interface IGraphQLResolver + public interface IGraphQLContext { IGraphType GetSchemaListType(Schema schema); - IGraphType GetSchemaListType(Guid schemaId); + IFieldPartitioning ResolvePartition(Partitioning key); - IGraphType GetAssetListType(); + (IGraphType ResolveType, IFieldResolver Resolver) GetGraphType(Field field); } -} \ No newline at end of file +} diff --git a/src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLResolver.cs b/src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLResolver.cs deleted file mode 100644 index fa1536a55..000000000 --- a/src/Squidex.Domain.Apps.Read/GraphQl/IGraphQLResolver.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using GraphQL.Types; -using Schema = Squidex.Domain.Apps.Core.Schemas.Schema; - -namespace Squidex.Domain.Apps.Read.GraphQl -{ - public interface IGraphQLResolver - { - IGraphType GetSchemaListType(Schema schema); - - IGraphType GetSchemaListType(Guid schemaId); - - IGraphType GetAssetListType(); - } -} \ No newline at end of file