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

Commit 8b0dde8

Browse files
Cleaning up graphql query
1 parent b746b8a commit 8b0dde8

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

src/GitHub.App/Services/IssuesAutoCompleteSource.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using GitHub.Models;
1010
using GitHub.Primitives;
1111
using Octokit.GraphQL;
12+
using static Octokit.GraphQL.Variable;
1213

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

2224
[ImportingConstructor]
2325
public IssuesAutoCompleteSource(ITeamExplorerContext teamExplorerContext, IGraphQLClientFactory graphqlFactory)
@@ -32,17 +34,32 @@ public IssuesAutoCompleteSource(ITeamExplorerContext teamExplorerContext, IGraph
3234
public IObservable<AutoCompleteSuggestion> GetSuggestions()
3335
{
3436
var localRepositoryModel = teamExplorerContext.ActiveRepository;
35-
var query = new Query().Repository(owner: localRepositoryModel.Owner, name: localRepositoryModel.Name)
36-
.Select(repository =>
37-
repository.Issues(null, null, null, null, null, null, null)
38-
.AllPages()
39-
.Select(issue => new SuggestionItem("#" + issue.Number, issue.Title))
40-
.ToList());
37+
38+
var hostAddress = HostAddress.Create(localRepositoryModel.CloneUrl.Host);
39+
var owner = localRepositoryModel.Owner;
40+
var name = localRepositoryModel.Name;
41+
42+
if (query == null)
43+
{
44+
query = new Query().Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
45+
.Select(repository =>
46+
repository.Issues(null, null, null, null, null, null, null)
47+
.AllPages()
48+
.Select(issue => new SuggestionItem("#" + issue.Number, issue.Title))
49+
.ToList())
50+
.Compile();
51+
}
52+
53+
var variables = new Dictionary<string, object>
54+
{
55+
{nameof(owner), owner },
56+
{nameof(name), name },
57+
};
4158

4259
return Observable.FromAsync(async () =>
4360
{
44-
var connection = await graphqlFactory.CreateConnection(HostAddress.Create(localRepositoryModel.CloneUrl.Host));
45-
var suggestions = await connection.Run(query);
61+
var connection = await graphqlFactory.CreateConnection(hostAddress);
62+
var suggestions = await connection.Run(query, variables);
4663
return suggestions.Select(suggestion => new IssueAutoCompleteSuggestion(suggestion, Prefix));
4764
}).SelectMany(enumerable => enumerable);
4865
}

src/GitHub.App/Services/MentionsAutoCompleteSource.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.Composition;
34
using System.Linq;
45
using System.Reactive.Linq;
@@ -10,6 +11,7 @@
1011
using GitHub.Models;
1112
using GitHub.Primitives;
1213
using Octokit.GraphQL;
14+
using static Octokit.GraphQL.Variable;
1315

1416
namespace GitHub.Services
1517
{
@@ -23,6 +25,7 @@ public class MentionsAutoCompleteSource : IAutoCompleteSource
2325
readonly ITeamExplorerContext teamExplorerContext;
2426
readonly IGraphQLClientFactory graphqlFactory;
2527
readonly IAvatarProvider avatarProvider;
28+
ICompiledQuery<List<SuggestionItem>> query;
2629

2730
[ImportingConstructor]
2831
public MentionsAutoCompleteSource(ITeamExplorerContext teamExplorerContext,
@@ -41,20 +44,35 @@ public MentionsAutoCompleteSource(ITeamExplorerContext teamExplorerContext,
4144
public IObservable<AutoCompleteSuggestion> GetSuggestions()
4245
{
4346
var localRepositoryModel = teamExplorerContext.ActiveRepository;
44-
var query = new Query().Repository(owner: localRepositoryModel.Owner, name: localRepositoryModel.Name)
47+
48+
var hostAddress = HostAddress.Create(localRepositoryModel.CloneUrl.Host);
49+
var owner = localRepositoryModel.Owner;
50+
var name = localRepositoryModel.Name;
51+
52+
if (query == null)
53+
{
54+
query = new Query().Repository(owner: Var(nameof(owner)), name: Var(nameof(name)))
4555
.Select(repository =>
4656
repository.MentionableUsers(null, null, null, null)
4757
.AllPages()
4858
.Select(sourceItem =>
4959
new SuggestionItem(sourceItem.Login,
5060
sourceItem.Name ?? "(unknown)",
5161
GetUrlSafe(sourceItem.AvatarUrl(null))))
52-
.ToList());
62+
.ToList())
63+
.Compile();
64+
}
65+
66+
var variables = new Dictionary<string, object>
67+
{
68+
{nameof(owner), owner },
69+
{nameof(name), name },
70+
};
5371

5472
return Observable.FromAsync(async () =>
5573
{
56-
var connection = await graphqlFactory.CreateConnection(HostAddress.Create(localRepositoryModel.CloneUrl.Host));
57-
var suggestions = await connection.Run(query);
74+
var connection = await graphqlFactory.CreateConnection(hostAddress);
75+
var suggestions = await connection.Run(query, variables);
5876
return suggestions.Select(suggestion => new AutoCompleteSuggestion(suggestion.Name,
5977
suggestion.Description,
6078
ResolveImage(suggestion.IconKey.ToString()),

src/GitHub.Exports.Reactive/ViewModels/GitHubPane/IPullRequestCreationViewModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public interface IPullRequestCreationViewModel : IPanePageViewModel
1717
ReactiveCommand<Unit, Unit> Cancel { get; }
1818
string PRTitle { get; set; }
1919
ReactivePropertyValidator TitleValidator { get; }
20-
IAutoCompleteAdvisor AutoCompleteAdvisor { get; }
2120

2221
Task InitializeAsync(LocalRepositoryModel repository, IConnection connection);
2322
}

0 commit comments

Comments
 (0)