Skip to content

Commit

Permalink
[GraphQL] Should Filter On Multiple Indexes with the Same Alias (#3088)
Browse files Browse the repository at this point in the history
* Failing Test

* Adding missing index registration

* update id

* Maybe it does work!? and we jsut need examples
  • Loading branch information
Jetski5822 authored Jan 27, 2019
1 parent 4ceb1ce commit fe78d13
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using OrchardCore.ContentManagement.Records;
using YesSql;

namespace OrchardCore.ContentManagement.GraphQL.Queries.Predicates
Expand Down
138 changes: 129 additions & 9 deletions test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class ContentItemsFieldTypeTests : IDisposable
{
protected Store _store;

public ContentItemsFieldTypeTests() {
public ContentItemsFieldTypeTests()
{
_store = new Store(new Configuration().UseInMemory());

CreateTables();
Expand Down Expand Up @@ -60,21 +61,29 @@ private void CreateTables()
builder.CreateMapIndexTable(nameof(AnimalIndex), column => column
.Column<string>(nameof(AnimalIndex.Name))
);


builder.CreateMapIndexTable(nameof(AnimalTraitsIndex), column => column
.Column<bool>(nameof(AnimalTraitsIndex.IsHappy))
.Column<bool>(nameof(AnimalTraitsIndex.IsScary))
);
}

_store.RegisterIndexes<ContentItemIndexProvider>();
}


[Fact]
public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() {
_store.RegisterIndexes<ContentItemIndexProvider>();
_store.RegisterIndexes<MultipleIndexProvider>();
public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases()
{
_store.RegisterIndexes<AnimalIndexProvider>();

var services = new FakeServiceCollection();
services.Populate(new ServiceCollection());
services.Services.AddScoped<ISession>(x => new Session(_store, System.Data.IsolationLevel.Unspecified));
services.Services.AddScoped<IIndexProvider, ContentItemIndexProvider>();
services.Services.AddScoped<IIndexProvider, MultipleIndexProvider>();
services.Services.AddScoped<IIndexAliasProvider, MultipleIndexAliasProvider>();
services.Services.AddScoped<IIndexProvider, AnimalIndexProvider>();
services.Services.AddScoped<IIndexAliasProvider, MultipleAliasIndexProvider>();
services.Build();

var retrunType = new ListGraphType<StringGraphType>();
Expand All @@ -83,7 +92,8 @@ public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() {
var context = new ResolveFieldContext
{
Arguments = new Dictionary<string, object>(),
UserContext = new GraphQLContext {
UserContext = new GraphQLContext
{
ServiceProvider = services
},
ReturnType = retrunType
Expand Down Expand Up @@ -111,19 +121,76 @@ public async Task ShouldBeAbleToUseTheSameIndexForMultipleAliases() {
Assert.Single(dogs);
Assert.Equal("doug", dogs.First().As<Animal>().Name);
}

[Fact]
public async Task ShouldFilterOnMultipleIndexesOnSameAlias()
{
_store.RegisterIndexes<AnimalIndexProvider>();
_store.RegisterIndexes<AnimalTraitsIndexProvider>();

var services = new FakeServiceCollection();
services.Populate(new ServiceCollection());
services.Services.AddScoped<ISession>(x => new Session(_store, System.Data.IsolationLevel.Unspecified));
services.Services.AddScoped<IIndexProvider, ContentItemIndexProvider>();
services.Services.AddScoped<IIndexProvider, AnimalIndexProvider>();
services.Services.AddScoped<IIndexProvider, AnimalTraitsIndexProvider>();
services.Services.AddScoped<IIndexAliasProvider, MultipleIndexesIndexProvider>();
services.Build();

var retrunType = new ListGraphType<StringGraphType>();
retrunType.ResolvedType = new StringGraphType() { Name = "Animal" };

var context = new ResolveFieldContext
{
Arguments = new Dictionary<string, object>(),
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<ISession>();
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<IEnumerable<ContentItem>>)type.Resolver).Resolve(context);

Assert.Single(animals);
Assert.Equal("doug", animals.First().As<Animal>().Name);
Assert.True(animals.First().As<Animal>().IsScary);
Assert.False(animals.First().As<Animal>().IsHappy);
}
}

public class Animal : ContentPart
{
public string Name { get; set; }
public bool IsHappy { get; set; }
public bool IsScary { get; set; }
}

public class AnimalIndex : MapIndex
{
public string Name { get; set; }
}

public class MultipleIndexProvider : IndexProvider<ContentItem>
public class AnimalIndexProvider : IndexProvider<ContentItem>
{
public override void Describe(DescribeContext<ContentItem> context)
{
Expand All @@ -138,7 +205,30 @@ public override void Describe(DescribeContext<ContentItem> context)
}
}

public class MultipleIndexAliasProvider : IIndexAliasProvider
public class AnimalTraitsIndex : MapIndex
{
public bool IsHappy { get; set; }
public bool IsScary { get; set; }
}

public class AnimalTraitsIndexProvider : IndexProvider<ContentItem>
{
public override void Describe(DescribeContext<ContentItem> context)
{
context.For<AnimalTraitsIndex>()
.Map(contentItem =>
{
return new AnimalTraitsIndex
{
IsHappy = contentItem.As<Animal>().IsHappy,
IsScary = contentItem.As<Animal>().IsScary
};
});
}
}


public class MultipleAliasIndexProvider : IIndexAliasProvider
{
private static readonly IndexAlias[] _aliases = new[]
{
Expand All @@ -162,6 +252,36 @@ public IEnumerable<IndexAlias> GetAliases()
}
}

public class MultipleIndexesIndexProvider : IIndexAliasProvider
{
private static readonly IndexAlias[] _aliases = new[]
{
new IndexAlias
{
Alias = "animals.name",
Index = $"Name",
With = q => q.With<AnimalIndex>()
},
new IndexAlias
{
Alias = "animals.isHappy",
Index = $"IsHappy",
With = q => q.With<AnimalTraitsIndex>()
},
new IndexAlias
{
Alias = "animals.isScary",
Index = $"IsScary",
With = q => q.With<AnimalTraitsIndex>()
}
};

public IEnumerable<IndexAlias> GetAliases()
{
return _aliases;
}
}

public class FakeServiceCollection : IServiceProvider
{
private IServiceProvider _inner;
Expand Down

0 comments on commit fe78d13

Please sign in to comment.