Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 8a6737c

Browse files
Using the GraphQL Search for Issues and Pull Requests
1 parent 72a82e5 commit 8a6737c

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

src/GitHub.App/Services/IssuesAutoCompleteSource.cs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GitHub.Models;
99
using GitHub.Primitives;
1010
using Octokit.GraphQL;
11+
using Octokit.GraphQL.Model;
1112
using static Octokit.GraphQL.Variable;
1213

1314
namespace GitHub.Services
@@ -18,7 +19,7 @@ public class IssuesAutoCompleteSource : IAutoCompleteSource
1819
{
1920
readonly ITeamExplorerContext teamExplorerContext;
2021
readonly IGraphQLClientFactory graphqlFactory;
21-
ICompiledQuery<List<SuggestionItem>> query;
22+
ICompiledQuery<Page<SuggestionItem>> query;
2223

2324
[ImportingConstructor]
2425
public IssuesAutoCompleteSource(ITeamExplorerContext teamExplorerContext, IGraphQLClientFactory graphqlFactory)
@@ -38,32 +39,58 @@ public IObservable<AutoCompleteSuggestion> GetSuggestions()
3839
var owner = localRepositoryModel.Owner;
3940
var name = localRepositoryModel.Name;
4041

42+
string filter;
43+
string after;
44+
4145
if (query == null)
4246
{
43-
query = new Query().Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
44-
.Select(repository =>
45-
repository.Issues(null, null, null, null, null, null, null)
46-
.AllPages()
47-
.Select(issue => new SuggestionItem("#" + issue.Number, issue.Title)
48-
{
49-
LastModifiedDate = issue.LastEditedAt
50-
})
51-
.ToList())
52-
.Compile();
47+
query = new Query().Search(query: Var(nameof(filter)), SearchType.Issue, 100, after: Var(nameof(after)))
48+
.Select(item => new Page<SuggestionItem>
49+
{
50+
Items = item.Nodes.Select(searchResultItem =>
51+
searchResultItem.Switch<SuggestionItem>(selector => selector
52+
.Issue(i => new SuggestionItem("#" + i.Number, i.Title) { LastModifiedDate = i.LastEditedAt })
53+
.PullRequest(p => new SuggestionItem("#" + p.Number, p.Title) { LastModifiedDate = p.LastEditedAt }))
54+
).ToList(),
55+
EndCursor = item.PageInfo.EndCursor,
56+
HasNextPage = item.PageInfo.HasNextPage,
57+
TotalCount = item.IssueCount
58+
})
59+
.Compile();
5360
}
5461

55-
var variables = new Dictionary<string, object>
56-
{
57-
{nameof(owner), owner },
58-
{nameof(name), name },
59-
};
62+
filter = $"repo:{owner}/{name} is:open";
6063

6164
return Observable.FromAsync(async () =>
65+
{
66+
var results = new List<SuggestionItem>();
67+
68+
var variables = new Dictionary<string, object>
69+
{
70+
{nameof(filter), filter },
71+
};
72+
73+
var connection = await graphqlFactory.CreateConnection(hostAddress);
74+
var searchResults = await connection.Run(query, variables);
75+
76+
results.AddRange(searchResults.Items);
77+
78+
while (searchResults.HasNextPage)
6279
{
63-
var connection = await graphqlFactory.CreateConnection(hostAddress);
64-
var suggestions = await connection.Run(query, variables);
65-
return suggestions.Select(suggestion => new IssueAutoCompleteSuggestion(suggestion, Prefix));
66-
}).SelectMany(enumerable => enumerable);
80+
variables[nameof(after)] = searchResults.EndCursor;
81+
searchResults = await connection.Run(query, variables);
82+
83+
results.AddRange(searchResults.Items);
84+
}
85+
86+
return results.Select(item => new IssueAutoCompleteSuggestion(item, Prefix));
87+
88+
}).SelectMany(observable => observable);
89+
}
90+
91+
class SearchResult
92+
{
93+
public SuggestionItem SuggestionItem { get; set; }
6794
}
6895

6996
public string Prefix

submodules/octokit.graphql.net

0 commit comments

Comments
 (0)