Skip to content

Commit 63b4f6c

Browse files
jMarkPkfcampbell
andauthored
Fix GraphQL entity names in subqueries (#299)
* Regenerate model * Add annotations to generated types To make sure subqueries get generated with the correct GraphQL entity names * Regenerate model with new attribute * Add test and fix compilation * Fix unit tests --------- Co-authored-by: Keegan Campbell <[email protected]>
1 parent 36708fa commit 63b4f6c

File tree

100 files changed

+1137
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1137
-95
lines changed

Octokit.GraphQL.Core.Generation.UnitTests/InterfaceGenerationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class InterfaceGenerationTests : TestBase
1818
using Octokit.GraphQL.Core;
1919
using Octokit.GraphQL.Core.Builders;
2020
21+
[GraphQLIdentifier(""Entity"")]
2122
public interface IEntity : IQueryableValue<IEntity>, IQueryableInterface
2223
{{
2324
{0}
@@ -32,6 +33,7 @@ namespace Test.Internal
3233
using Octokit.GraphQL.Core;
3334
using Octokit.GraphQL.Core.Builders;
3435
36+
[GraphQLIdentifier(""Entity"")]
3537
internal class StubIEntity : QueryableValue<StubIEntity>, IEntity
3638
{{
3739
internal StubIEntity(Expression expression) : base(expression)

Octokit.GraphQL.Core.Generation/EntityGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public static string Generate(
1616
string queryType,
1717
string modifiers = "public ",
1818
bool generateDocComments = true,
19-
string entityNamespace = null)
19+
string entityNamespace = null,
20+
string graphQlModelType = null)
2021
{
2122
var className = TypeUtilities.GetClassName(type);
2223
var pagingConnectionNodeType = GetPagingConnectionNodeType(type);
24+
var annotations = TypeUtilities.GetGraphQlIdentifierAttribute(graphQlModelType);
2325

2426
return $@"namespace {rootNamespace}
2527
{{
@@ -29,7 +31,7 @@ public static string Generate(
2931
using Octokit.GraphQL.Core;
3032
using Octokit.GraphQL.Core.Builders;
3133
32-
{GenerateDocComments(type, generateDocComments)}{modifiers}class {className} : QueryableValue<{className}>{GenerateImplementedInterfaces(type, pagingConnectionNodeType)}
34+
{GenerateDocComments(type, generateDocComments)}{annotations}{modifiers}class {className} : QueryableValue<{className}>{GenerateImplementedInterfaces(type, pagingConnectionNodeType)}
3335
{{
3436
internal {className}(Expression expression) : base(expression)
3537
{{

Octokit.GraphQL.Core.Generation/InterfaceGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal static class InterfaceGenerator
1111
public static string Generate(TypeModel type, string entityNamespace, string queryType)
1212
{
1313
var className = TypeUtilities.GetInterfaceName(type);
14+
var annotations = TypeUtilities.GetGraphQlIdentifierAttribute(type.Name);
1415

1516
return $@"namespace {entityNamespace}
1617
{{
@@ -22,7 +23,7 @@ public static string Generate(TypeModel type, string entityNamespace, string que
2223
using Octokit.GraphQL.Core;
2324
using Octokit.GraphQL.Core.Builders;
2425
25-
{GenerateDocComments(type)}public interface {className} : IQueryableValue<{className}>, IQueryableInterface
26+
{GenerateDocComments(type)}{annotations}public interface {className} : IQueryableValue<{className}>, IQueryableInterface
2627
{{{GenerateFields(type)}
2728
}}
2829
}}
@@ -215,7 +216,7 @@ private static string GenerateStub(TypeModel type, string entityNamespace, strin
215216
stubType.Name = "Stub" + TypeUtilities.GetInterfaceName(type);
216217
stubType.Kind = TypeKind.Object;
217218
stubType.Interfaces = new[] { type };
218-
return EntityGenerator.Generate(stubType, entityNamespace + ".Internal", queryType, entityNamespace: entityNamespace, modifiers: "internal ", generateDocComments: false);
219+
return EntityGenerator.Generate(stubType, entityNamespace + ".Internal", queryType, entityNamespace: entityNamespace, modifiers: "internal ", generateDocComments: false, graphQlModelType:type.Name);
219220
}
220221
}
221222
}

Octokit.GraphQL.Core.Generation/Utilities/TypeUtilities.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public static TypeModel ReduceType(TypeModel type)
113113

114114
return type;
115115
}
116+
117+
public static string GetGraphQlIdentifierAttribute(string graphQlModelType)
118+
{
119+
if (graphQlModelType == null)
120+
{
121+
return "";
122+
}
123+
124+
return $"[GraphQLIdentifier(\"{graphQlModelType}\")]\n ";
125+
}
116126

117127
private static string GetCSharpType(TypeModel type, bool nullableType, bool returnType)
118128
{

Octokit.GraphQL.UnitTests/ExpressionRewiterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void Repository_Details_With_Viewer()
129129
{
130130
var query = new Query()
131131
.Select(x => x.RepositoryOwner("foo")
132-
.Repositories(30, null, null, null, null, null, null, null, null, null)
132+
.Repositories(30, null, null, null, null, null, null, null, null, null, null)
133133
.Edges
134134
.Select(y => y.Node)
135135
.Select(z => new

Octokit.GraphQL.UnitTests/QueryBuilderTests.cs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Octokit.GraphQL.Core;
25
using Octokit.GraphQL.Model;
36
using Xunit;
47

@@ -117,7 +120,7 @@ public void RepositoryOwner_Repositories_Query_Viewer()
117120
var expression = new Query()
118121
.Select(root => root
119122
.RepositoryOwner("foo")
120-
.Repositories(30, null, null, null, null, null, null, null, null, null)
123+
.Repositories(30, null, null, null, null, null, null, null, null, null, null)
121124
.Edges.Select(x => x.Node)
122125
.Select(x => new
123126
{
@@ -218,7 +221,7 @@ public void Repository_Details_With_Viewer()
218221

219222
var expression = new Query()
220223
.Select(x => x.RepositoryOwner("foo")
221-
.Repositories(30, null, null, null, null, null, null, null, null, null)
224+
.Repositories(30, null, null, null, null, null, null, null, null, null, null)
222225
.Edges
223226
.Select(y => y.Node)
224227
.Select(y => new
@@ -338,5 +341,58 @@ public void DateTimeOffsetVariable()
338341

339342
Assert.Equal(expected, query.ToString(), ignoreLineEndingDifferences: true);
340343
}
344+
345+
346+
[Fact]
347+
public void TestAllPagesSubqueryUsesCorrectEntityName()
348+
{
349+
var expectedMasterQuery = @"query {
350+
repositoryOwner(login: ""foo"") {
351+
id
352+
repositories(first: 100) {
353+
pageInfo {
354+
hasNextPage
355+
endCursor
356+
}
357+
nodes {
358+
name
359+
}
360+
}
361+
}
362+
}";
363+
364+
// The actual type that we map RepositoryOwner to is called StubIRepositoryOwner
365+
// So make sure it gets serialized to the query correctly.
366+
var expectedSubQuery = @"query($__id: ID!, $__after: String) {
367+
node(id: $__id) {
368+
__typename
369+
... on RepositoryOwner {
370+
repositories(first: 100, after: $__after) {
371+
pageInfo {
372+
hasNextPage
373+
endCursor
374+
}
375+
nodes {
376+
name
377+
}
378+
}
379+
}
380+
}
381+
}";
382+
383+
var query = new Query()
384+
.RepositoryOwner("foo")
385+
.Repositories()
386+
.AllPages(100)
387+
.Select(x => x.Name)
388+
.Compile();
389+
390+
var subQuery = (query as PagedQuery<IEnumerable<string>>).Subqueries.First();
391+
392+
Assert.Equal(expectedMasterQuery, query.ToString(), ignoreLineEndingDifferences: true);
393+
Assert.Equal(expectedSubQuery, subQuery.ToString(), ignoreLineEndingDifferences: true);
394+
395+
396+
}
341397
}
342398
}

Octokit.GraphQL/Model/Actor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Octokit.GraphQL.Model
1111
/// <summary>
1212
/// Represents an object which can take actions on GitHub. Typically a User or Bot.
1313
/// </summary>
14+
[GraphQLIdentifier("Actor")]
1415
public interface IActor : IQueryableValue<IActor>, IQueryableInterface
1516
{
1617
/// <summary>
@@ -44,6 +45,7 @@ namespace Octokit.GraphQL.Model.Internal
4445
using Octokit.GraphQL.Core;
4546
using Octokit.GraphQL.Core.Builders;
4647

48+
[GraphQLIdentifier("Actor")]
4749
internal class StubIActor : QueryableValue<StubIActor>, IActor
4850
{
4951
internal StubIActor(Expression expression) : base(expression)

Octokit.GraphQL/Model/AnnouncementBanner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Octokit.GraphQL.Model
1111
/// <summary>
1212
/// Represents an announcement banner.
1313
/// </summary>
14+
[GraphQLIdentifier("AnnouncementBanner")]
1415
public interface IAnnouncementBanner : IQueryableValue<IAnnouncementBanner>, IQueryableInterface
1516
{
1617
/// <summary>
@@ -38,6 +39,7 @@ namespace Octokit.GraphQL.Model.Internal
3839
using Octokit.GraphQL.Core;
3940
using Octokit.GraphQL.Core.Builders;
4041

42+
[GraphQLIdentifier("AnnouncementBanner")]
4143
internal class StubIAnnouncementBanner : QueryableValue<StubIAnnouncementBanner>, IAnnouncementBanner
4244
{
4345
internal StubIAnnouncementBanner(Expression expression) : base(expression)

Octokit.GraphQL/Model/Assignable.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Octokit.GraphQL.Model
1111
/// <summary>
1212
/// An object that can have users assigned to it.
1313
/// </summary>
14+
[GraphQLIdentifier("Assignable")]
1415
public interface IAssignable : IQueryableValue<IAssignable>, IQueryableInterface
1516
{
1617
/// <summary>
@@ -32,6 +33,7 @@ namespace Octokit.GraphQL.Model.Internal
3233
using Octokit.GraphQL.Core;
3334
using Octokit.GraphQL.Core.Builders;
3435

36+
[GraphQLIdentifier("Assignable")]
3537
internal class StubIAssignable : QueryableValue<StubIAssignable>, IAssignable
3638
{
3739
internal StubIAssignable(Expression expression) : base(expression)

Octokit.GraphQL/Model/AuditEntry.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Octokit.GraphQL.Model
1111
/// <summary>
1212
/// An entry in the audit log.
1313
/// </summary>
14+
[GraphQLIdentifier("AuditEntry")]
1415
public interface IAuditEntry : IQueryableValue<IAuditEntry>, IQueryableInterface
1516
{
1617
/// <summary>
@@ -88,6 +89,7 @@ namespace Octokit.GraphQL.Model.Internal
8889
using Octokit.GraphQL.Core;
8990
using Octokit.GraphQL.Core.Builders;
9091

92+
[GraphQLIdentifier("AuditEntry")]
9193
internal class StubIAuditEntry : QueryableValue<StubIAuditEntry>, IAuditEntry
9294
{
9395
internal StubIAuditEntry(Expression expression) : base(expression)

0 commit comments

Comments
 (0)