Browse Source

GraphQL fix for empty queries.

pull/111/head
Sebastian Stehle 9 years ago
parent
commit
663b587072
  1. 5
      src/Squidex.Domain.Apps.Read/Contents/GraphQL/CachingGraphQLService.cs
  2. 16
      src/Squidex.Domain.Apps.Read/Contents/GraphQL/Types/ContentGraphType.cs
  3. 18
      tests/Squidex.Domain.Apps.Read.Tests/Contents/GraphQLTests.cs

5
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);

16
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.";
}

18
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()
{

Loading…
Cancel
Save