Skip to content

Commit 6e521d0

Browse files
authored
Merge pull request #157 from octokit/query-builder-error
Fixing another query builder aliasing error
2 parents 40f2529 + 96ef03c commit 6e521d0

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

Octokit.GraphQL.Core.UnitTests/QueryBuilderTests.cs

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using Octokit.GraphQL.Core.Builders;
34
using Octokit.GraphQL.Core.UnitTests.Models;
45
using Xunit;
@@ -668,9 +669,9 @@ public void Repository_Select_Object()
668669
{
669670
var expected = @"query {
670671
repository(owner: ""foo"", name: ""bar"") {
671-
forkCount
672-
name
673-
description
672+
intField1: forkCount
673+
stringField1: name
674+
stringField2: description
674675
}
675676
}";
676677

@@ -724,9 +725,9 @@ public void Repository_Select_Object_Fragment()
724725
}
725726
}
726727
fragment repositoryName on Repository {
727-
forkCount
728-
name
729-
description
728+
intField1: forkCount
729+
stringField1: name
730+
stringField2: description
730731
}";
731732

732733
var fragment = new Fragment<Repository, TestModelObject>("repositoryName", repository => new TestModelObject
@@ -786,9 +787,9 @@ public void Repository_Select_Use_Object_Fragment_Twice()
786787
}
787788
}
788789
fragment repositoryName on Repository {
789-
forkCount
790-
name
791-
description
790+
intField1: forkCount
791+
stringField1: name
792+
stringField2: description
792793
}";
793794

794795
var repositoryName = new Fragment<Repository, TestModelObject>("repositoryName", repository => new TestModelObject
@@ -877,12 +878,92 @@ fragment issueTitle on Issue {
877878
Assert.Equal(expected, query.ToString(2), ignoreLineEndingDifferences: true);
878879
}
879880

881+
[Fact]
882+
public void Issue_Select_Two_In_Anon_Object()
883+
{
884+
var expected = @"query {
885+
repository(owner: ""foo"", name: ""bar"") {
886+
someData: issues(labels: [""asdf""]) {
887+
nodes {
888+
title
889+
}
890+
}
891+
someData2: issues(labels: [""asdf""]) {
892+
nodes {
893+
title
894+
}
895+
}
896+
}
897+
}";
898+
899+
Arg<IEnumerable<string>>? labels = new[] { "asdf" };
900+
901+
var expression = new Query().Repository("foo", "bar").Select(repository => new
902+
{
903+
SomeData = repository.Issues(null, null, null, null, labels)
904+
.Nodes
905+
.Select(issue => new { issue.Title })
906+
.ToList(),
907+
SomeData2 = repository.Issues(null, null, null, null, labels)
908+
.Nodes
909+
.Select(issue => new { issue.Title })
910+
.ToList(),
911+
});
912+
913+
var query = expression.Compile();
914+
915+
Assert.Equal(expected, query.ToString(2), ignoreLineEndingDifferences: true);
916+
}
917+
918+
[Fact]
919+
public void Issue_Select_Two_In_Object()
920+
{
921+
var expected = @"query {
922+
repository(owner: ""foo"", name: ""bar"") {
923+
someData: issues(labels: [""asdf""]) {
924+
nodes {
925+
title
926+
}
927+
}
928+
someData2: issues(labels: [""asdf""]) {
929+
nodes {
930+
title
931+
}
932+
}
933+
}
934+
}";
935+
936+
Arg<IEnumerable<string>>? labels = new[] { "asdf" };
937+
938+
var expression = new Query().Repository("foo", "bar").Select(repository => new TestIssueSets
939+
{
940+
SomeData = repository.Issues(null, null, null, null, labels)
941+
.Nodes
942+
.Select(issue => new { issue.Title })
943+
.ToList(),
944+
SomeData2 = repository.Issues(null, null, null, null, labels)
945+
.Nodes
946+
.Select(issue => new { issue.Title })
947+
.ToList(),
948+
});
949+
950+
var query = expression.Compile();
951+
952+
Assert.Equal(expected, query.ToString(2), ignoreLineEndingDifferences: true);
953+
}
954+
880955
class TestModelObject
881956
{
882957
public string StringField1;
883958
public string StringField2;
884959
public int IntField1;
885960
public int IntField2;
886961
}
962+
963+
public class TestIssueSets
964+
{
965+
public IReadOnlyList<object> SomeData { get; set; }
966+
public IReadOnlyList<object> SomeData2 { get; set; }
967+
}
887968
}
888969
}

Octokit.GraphQL.Core.UnitTests/ResponseDeserializerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public void Repository_Issue_Select_Multiple_Members_To_Named_Class()
170170
""data"":{
171171
""repository"": {
172172
""issue"":{
173-
""title"": ""Hello World!"",
174-
""body"": ""Goodbye cruel world""
173+
""name"": ""Hello World!"",
174+
""description"": ""Goodbye cruel world""
175175
}
176176
}
177177
}

Octokit.GraphQL.Core/Core/Builders/QueryBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ protected override Expression VisitMember(MemberExpression node)
255255

256256
protected override MemberAssignment VisitMemberAssignment(MemberAssignment node)
257257
{
258-
var expression = BookmarkAndVisit(node.Expression).AddCast(node.Expression.Type);
258+
var nodeExpression = AliasedExpression.WrapIfNeeded(node.Expression, node.Member);
259+
var expression = BookmarkAndVisit(nodeExpression).AddCast(node.Expression.Type);
259260
return Expression.Bind(node.Member, expression);
260261
}
261262

0 commit comments

Comments
 (0)