From fe78d13b7f25b0599cf72d5160db2368c3be328c Mon Sep 17 00:00:00 2001 From: Nicholas Mayne Date: Sun, 27 Jan 2019 18:23:55 +0000 Subject: [PATCH] [GraphQL] Should Filter On Multiple Indexes with the Same Alias (#3088) * Failing Test * Adding missing index registration * update id * Maybe it does work!? and we jsut need examples --- .../Queries/Predicates/PredicateQuery.cs | 1 - .../GraphQL/ContentItemsFieldTypeTests.cs | 138 ++++++++++++++++-- 2 files changed, 129 insertions(+), 10 deletions(-) diff --git a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs index 73cb4dee875..5f2eab5aec2 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using OrchardCore.ContentManagement.Records; using YesSql; namespace OrchardCore.ContentManagement.GraphQL.Queries.Predicates diff --git a/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs index 2bb9bc88dc5..ecb2421da78 100644 --- a/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs +++ b/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs @@ -22,7 +22,8 @@ public class ContentItemsFieldTypeTests : IDisposable { protected Store _store; - public ContentItemsFieldTypeTests() { + public ContentItemsFieldTypeTests() + { _store = new Store(new Configuration().UseInMemory()); CreateTables(); @@ -60,21 +61,29 @@ private void CreateTables() builder.CreateMapIndexTable(nameof(AnimalIndex), column => column .Column(nameof(AnimalIndex.Name)) ); + + + builder.CreateMapIndexTable(nameof(AnimalTraitsIndex), column => column + .Column(nameof(AnimalTraitsIndex.IsHappy)) + .Column(nameof(AnimalTraitsIndex.IsScary)) + ); } + + _store.RegisterIndexes(); } [Fact] - public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() { - _store.RegisterIndexes(); - _store.RegisterIndexes(); + public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() + { + _store.RegisterIndexes(); var services = new FakeServiceCollection(); services.Populate(new ServiceCollection()); services.Services.AddScoped(x => new Session(_store, System.Data.IsolationLevel.Unspecified)); services.Services.AddScoped(); - services.Services.AddScoped(); - services.Services.AddScoped(); + services.Services.AddScoped(); + services.Services.AddScoped(); services.Build(); var retrunType = new ListGraphType(); @@ -83,7 +92,8 @@ public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() { var context = new ResolveFieldContext { Arguments = new Dictionary(), - UserContext = new GraphQLContext { + UserContext = new GraphQLContext + { ServiceProvider = services }, ReturnType = retrunType @@ -111,11 +121,68 @@ public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() { Assert.Single(dogs); Assert.Equal("doug", dogs.First().As().Name); } + + [Fact] + public async Task ShouldFilterOnMultipleIndexesOnSameAlias() + { + _store.RegisterIndexes(); + _store.RegisterIndexes(); + + var services = new FakeServiceCollection(); + services.Populate(new ServiceCollection()); + services.Services.AddScoped(x => new Session(_store, System.Data.IsolationLevel.Unspecified)); + services.Services.AddScoped(); + services.Services.AddScoped(); + services.Services.AddScoped(); + services.Services.AddScoped(); + services.Build(); + + var retrunType = new ListGraphType(); + retrunType.ResolvedType = new StringGraphType() { Name = "Animal" }; + + var context = new ResolveFieldContext + { + Arguments = new Dictionary(), + UserContext = new GraphQLContext + { + ServiceProvider = services + }, + ReturnType = retrunType + }; + + var ci = new ContentItem { ContentType = "Animal", Published = true, ContentItemId = "1", ContentItemVersionId = "1" }; + ci.Weld(new Animal { Name = "doug", IsHappy = true, IsScary = false }); + + var ci1 = new ContentItem { ContentType = "Animal", Published = true, ContentItemId = "2", ContentItemVersionId = "2" }; + ci1.Weld(new Animal { Name = "doug", IsHappy = false, IsScary = true }); + + var ci2 = new ContentItem { ContentType = "Animal", Published = true, ContentItemId = "3", ContentItemVersionId = "3" }; + ci2.Weld(new Animal { Name = "tommy", IsHappy = false, IsScary = true }); + + + var session = ((GraphQLContext)context.UserContext).ServiceProvider.GetService(); + session.Save(ci); + session.Save(ci1); + session.Save(ci2); + await session.CommitAsync(); + + var type = new ContentItemsFieldType("Animal", new Schema()); + + context.Arguments["where"] = JObject.Parse("{ animals: { name: \"doug\", isScary: true } }"); + var animals = await ((AsyncFieldResolver>)type.Resolver).Resolve(context); + + Assert.Single(animals); + Assert.Equal("doug", animals.First().As().Name); + Assert.True(animals.First().As().IsScary); + Assert.False(animals.First().As().IsHappy); + } } public class Animal : ContentPart { public string Name { get; set; } + public bool IsHappy { get; set; } + public bool IsScary { get; set; } } public class AnimalIndex : MapIndex @@ -123,7 +190,7 @@ public class AnimalIndex : MapIndex public string Name { get; set; } } - public class MultipleIndexProvider : IndexProvider + public class AnimalIndexProvider : IndexProvider { public override void Describe(DescribeContext context) { @@ -138,7 +205,30 @@ public override void Describe(DescribeContext context) } } - public class MultipleIndexAliasProvider : IIndexAliasProvider + public class AnimalTraitsIndex : MapIndex + { + public bool IsHappy { get; set; } + public bool IsScary { get; set; } + } + + public class AnimalTraitsIndexProvider : IndexProvider + { + public override void Describe(DescribeContext context) + { + context.For() + .Map(contentItem => + { + return new AnimalTraitsIndex + { + IsHappy = contentItem.As().IsHappy, + IsScary = contentItem.As().IsScary + }; + }); + } + } + + + public class MultipleAliasIndexProvider : IIndexAliasProvider { private static readonly IndexAlias[] _aliases = new[] { @@ -162,6 +252,36 @@ public IEnumerable GetAliases() } } + public class MultipleIndexesIndexProvider : IIndexAliasProvider + { + private static readonly IndexAlias[] _aliases = new[] + { + new IndexAlias + { + Alias = "animals.name", + Index = $"Name", + With = q => q.With() + }, + new IndexAlias + { + Alias = "animals.isHappy", + Index = $"IsHappy", + With = q => q.With() + }, + new IndexAlias + { + Alias = "animals.isScary", + Index = $"IsScary", + With = q => q.With() + } + }; + + public IEnumerable GetAliases() + { + return _aliases; + } + } + public class FakeServiceCollection : IServiceProvider { private IServiceProvider _inner;