-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIND-12462: Support link type for Link query
- New class LinkQueryBuilder use for build a link query - Add unit test
- Loading branch information
1 parent
18a3fa7
commit 7100f9d
Showing
7 changed files
with
260 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
APIs/src/EpiServer.ContentGraph/Api/Querying/LinkQueryBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EPiServer.ContentGraph.Api.Querying | ||
{ | ||
public interface ILinkQueryBuilder : ITypeQueryBuilder | ||
{ | ||
string GetLinkType(); | ||
} | ||
|
||
public class LinkQueryBuilder<T> : TypeQueryBuilder<T>, ILinkQueryBuilder | ||
{ | ||
private string _type; | ||
public LinkQueryBuilder() : base() | ||
{ | ||
} | ||
public LinkQueryBuilder(string linkType) : base() | ||
{ | ||
_type = linkType; | ||
} | ||
public LinkQueryBuilder<T> WithLinkType(string linkType) | ||
{ | ||
if (_type == null) | ||
{ | ||
_type = linkType; | ||
} | ||
return this; | ||
} | ||
public string GetLinkType() | ||
{ | ||
return _type; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
APIs/src/Testing/EpiServer.ContentGraph.UnitTests/GenerateLinkQueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using EpiServer.ContentGraph.UnitTests.QueryTypeObjects; | ||
using EPiServer.ContentGraph.Api.Filters; | ||
using EPiServer.ContentGraph.Api.Querying; | ||
using Xunit; | ||
|
||
namespace EpiServer.ContentGraph.UnitTests | ||
{ | ||
public class GenerateLinkQueryTests | ||
{ | ||
private TypeQueryBuilder<RequestTypeObject> typeQueryBuilder; | ||
public GenerateLinkQueryTests() | ||
{ | ||
typeQueryBuilder = new TypeQueryBuilder<RequestTypeObject>(); | ||
} | ||
|
||
[Fact] | ||
public void LinkQueryTests() | ||
{ | ||
string childQuery = "SubTypeObject(where:{SubProperty:{match: \"test\"}}){items{SubProperty} facets{Property3{name count}}}"; | ||
string expectedFields = $"items{{Property1 Property2 _link(type:CUSTOMERREFERENCES) {{{childQuery}}}}}"; | ||
string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; | ||
var linkQuery = new LinkQueryBuilder<SubTypeObject>("CUSTOMERREFERENCES") | ||
.Field(x => x.SubProperty) | ||
.Where(x => x.SubProperty, new StringFilterOperators().Match("test")) | ||
.Facet(x => x.Property3); | ||
|
||
typeQueryBuilder | ||
.Field(x => x.Property1) | ||
.Field(x => x.Property2) | ||
.Link(linkQuery) | ||
.Facet(x => x.Property3.NestedProperty); | ||
GraphQueryBuilder query = typeQueryBuilder.ToQuery(); | ||
|
||
Assert.NotNull(query.GetQuery()); | ||
Assert.Contains(expectedFacets, query.GetQuery().Query); | ||
Assert.Contains(expectedFields, query.GetQuery().Query); | ||
Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); | ||
} | ||
|
||
[Fact] | ||
public void multiple_links_query_should_generate_correct_query() | ||
{ | ||
string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; | ||
string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1} facets{Property3{name count}}}"; | ||
string expectedFields = $"items{{Property1 Property2 _link(type:CUSTOMERREFERENCES) {{{expectedLink1}}} _link(type:DEFAULT) {{{expectedLink2}}}}}"; | ||
string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; | ||
var linkQuery1 = new LinkQueryBuilder<SubTypeObject>("CUSTOMERREFERENCES") | ||
.Field(x => x.SubProperty) | ||
.Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) | ||
.Facet(x => x.Property3); | ||
|
||
var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") | ||
.Field(x => x.Property1) | ||
.Where(x => x.Property1, new StringFilterOperators().Match("test2")) | ||
.Facet(x => x.Property3); | ||
|
||
typeQueryBuilder | ||
.Field(x => x.Property1) | ||
.Field(x => x.Property2) | ||
.Link(linkQuery1) | ||
.Link(linkQuery2) | ||
.Facet(x => x.Property3.NestedProperty); | ||
GraphQueryBuilder query = typeQueryBuilder.ToQuery(); | ||
|
||
Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); | ||
Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); | ||
|
||
Assert.Contains(expectedFacets, query.GetQuery().Query); | ||
Assert.Contains(expectedFields, query.GetQuery().Query); | ||
Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); | ||
} | ||
|
||
[Fact] | ||
public void nested_links_query_should_generate_nested_link_query() | ||
{ | ||
string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; | ||
string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1 _link(type:CUSTOMERREFERENCES) {" + expectedLink1 + "}} facets{Property3{name count}}}"; | ||
string expectedFields = $"items{{Property1 Property2 _link(type:DEFAULT) {{{expectedLink2}}}}}"; | ||
string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; | ||
var linkQuery1 = new LinkQueryBuilder<SubTypeObject>() | ||
.WithLinkType("CUSTOMERREFERENCES") | ||
.Field(x => x.SubProperty) | ||
.Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) | ||
.Facet(x => x.Property3); | ||
|
||
var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") | ||
.Field(x => x.Property1) | ||
.Where(x => x.Property1, new StringFilterOperators().Match("test2")) | ||
.Facet(x => x.Property3) | ||
.Link(linkQuery1); | ||
|
||
typeQueryBuilder | ||
.Field(x => x.Property1) | ||
.Field(x => x.Property2) | ||
.Link(linkQuery2) | ||
.Facet(x => x.Property3.NestedProperty); | ||
GraphQueryBuilder query = typeQueryBuilder.ToQuery(); | ||
|
||
Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); | ||
Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); | ||
|
||
Assert.Contains(expectedFacets, query.GetQuery().Query); | ||
Assert.Contains(expectedFields, query.GetQuery().Query); | ||
Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); | ||
} | ||
[Fact] | ||
public void nested_link_query_with_aliases() | ||
{ | ||
string expectedLink1 = "SubTypeObject(where:{SubProperty:{match: \"test1\"}}){items{SubProperty} facets{Property3{name count}}}"; | ||
string expectedLink2 = "SubTypeObject(where:{Property1:{match: \"test2\"}}){items{Property1 mylink1:_link(type:CUSTOMERREFERENCES) {" + expectedLink1 + "}} facets{Property3{name count}}}"; | ||
string expectedFields = $"items{{Property1 Property2 mylink2:_link(type:DEFAULT) {{{expectedLink2}}}}}"; | ||
string expectedFacets = @"facets{Property3{NestedProperty{name count}}}"; | ||
var linkQuery1 = new LinkQueryBuilder<SubTypeObject>() | ||
.WithLinkType("CUSTOMERREFERENCES") | ||
.Field(x => x.SubProperty) | ||
.Where(x => x.SubProperty, new StringFilterOperators().Match("test1")) | ||
.Facet(x => x.Property3); | ||
|
||
var linkQuery2 = new LinkQueryBuilder<SubTypeObject>("DEFAULT") | ||
.Field(x => x.Property1) | ||
.Where(x => x.Property1, new StringFilterOperators().Match("test2")) | ||
.Facet(x => x.Property3) | ||
.Link(linkQuery1, "mylink1"); | ||
|
||
typeQueryBuilder | ||
.Field(x => x.Property1) | ||
.Field(x => x.Property2) | ||
.Link(linkQuery2, "mylink2") | ||
.Facet(x => x.Property3.NestedProperty); | ||
GraphQueryBuilder query = typeQueryBuilder.ToQuery(); | ||
|
||
Assert.Equal(linkQuery1.GetQuery().Query, expectedLink1); | ||
Assert.Equal(linkQuery2.GetQuery().Query, expectedLink2); | ||
|
||
Assert.Contains(expectedFacets, query.GetQuery().Query); | ||
Assert.Contains(expectedFields, query.GetQuery().Query); | ||
Assert.Equal($"RequestTypeObject{{{expectedFields} {expectedFacets}}}", query.GetQuery().Query); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters