From 663b58707291e6b1b8e8d0043442f1845f2dfc4b Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Tue, 12 Sep 2017 08:19:20 +0200 Subject: [PATCH] GraphQL fix for empty queries. --- .../Contents/GraphQL/CachingGraphQLService.cs | 5 +++++ .../Contents/GraphQL/Types/ContentGraphType.cs | 16 ++++++++++------ .../Contents/GraphQLTests.cs | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/Squidex.Domain.Apps.Read/Contents/GraphQL/CachingGraphQLService.cs b/src/Squidex.Domain.Apps.Read/Contents/GraphQL/CachingGraphQLService.cs index 16c1312d9..2c43a4c79 100644 --- a/src/Squidex.Domain.Apps.Read/Contents/GraphQL/CachingGraphQLService.cs +++ b/src/Squidex.Domain.Apps.Read/Contents/GraphQL/CachingGraphQLService.cs @@ -78,6 +78,11 @@ namespace Squidex.Domain.Apps.Read.Contents.GraphQL Guard.NotNull(app, nameof(app)); Guard.NotNull(query, nameof(query)); + if (string.IsNullOrWhiteSpace(query.Query)) + { + return (new object(), new object[0]); + } + var modelContext = await GetModelAsync(app); var queryContext = new QueryContext(app, assetRepository, contentQuery, urlGenerator, user); diff --git a/src/Squidex.Domain.Apps.Read/Contents/GraphQL/Types/ContentGraphType.cs b/src/Squidex.Domain.Apps.Read/Contents/GraphQL/Types/ContentGraphType.cs index 1505e5ea8..0ad086354 100644 --- a/src/Squidex.Domain.Apps.Read/Contents/GraphQL/Types/ContentGraphType.cs +++ b/src/Squidex.Domain.Apps.Read/Contents/GraphQL/Types/ContentGraphType.cs @@ -7,6 +7,7 @@ // ========================================================================== using System; +using System.Linq; using GraphQL.Resolvers; using GraphQL.Types; using Squidex.Domain.Apps.Read.Schemas; @@ -87,13 +88,16 @@ namespace Squidex.Domain.Apps.Read.Contents.GraphQL.Types Description = $"The url to the the {schemaName} content." }); - AddField(new FieldType + if (schema.SchemaDef.Fields.Any(x => !x.IsHidden)) { - Name = "data", - Resolver = Resolver(x => x.Data), - ResolvedType = new NonNullGraphType(new ContentDataGraphType(schema.SchemaDef, context)), - Description = $"The data of the {schemaName} content." - }); + AddField(new FieldType + { + Name = "data", + Resolver = Resolver(x => x.Data), + ResolvedType = new NonNullGraphType(new ContentDataGraphType(schema.SchemaDef, context)), + Description = $"The data of the {schemaName} content." + }); + } Description = $"The structure of a {schemaName} content type."; } diff --git a/tests/Squidex.Domain.Apps.Read.Tests/Contents/GraphQLTests.cs b/tests/Squidex.Domain.Apps.Read.Tests/Contents/GraphQLTests.cs index cbee5e2f3..ac5273792 100644 --- a/tests/Squidex.Domain.Apps.Read.Tests/Contents/GraphQLTests.cs +++ b/tests/Squidex.Domain.Apps.Read.Tests/Contents/GraphQLTests.cs @@ -86,6 +86,24 @@ namespace Squidex.Domain.Apps.Read.Contents sut = new CachingGraphQLService(cache, assetRepository, contentQuery, new FakeUrlGenerator(), schemaRepository); } + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public async Task Should_return_empty_object_for_empty_query(string query) + { + var result = await sut.QueryAsync(app, user, new GraphQLQuery { Query = query }); + + var expected = new + { + data = new + { + } + }; + + AssertJson(expected, new { data = result.Data }); + } + [Fact] public async Task Should_return_multiple_assets_when_querying_assets() {