Skip to content

Commit 56df03d

Browse files
committed
graphql go
1 parent 83036cd commit 56df03d

File tree

7 files changed

+128
-36
lines changed

7 files changed

+128
-36
lines changed

Scripts/graphql/.vscode/launch.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/graphql.dll",
13+
"args": ["243"],
14+
"cwd": "${workspaceFolder}",
15+
"console": "internalConsole",
16+
"stopAtEntry": false,
17+
"internalConsoleOptions": "openOnSessionStart"
18+
},
19+
{
20+
"name": ".NET Core Attach",
21+
"type": "coreclr",
22+
"request": "attach",
23+
"processId": "${command:pickProcess}"
24+
}
25+
]
26+
}

Scripts/graphql/.vscode/tasks.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/graphql.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

Scripts/graphql/ILeetcode.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Refit;
4+
namespace graphql
5+
{
6+
public interface ILeetcode
7+
{
8+
[Get("/api/problems/all")]
9+
Task<QuestionStat> GetAllQuestionStat();
10+
}
11+
}

Scripts/graphql/Leetcode.cs

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using GraphQL.Client;
5+
using GraphQL.Common.Request;
6+
using Newtonsoft.Json;
7+
using Refit;
28
namespace graphql
39
{
410
public class Leetcode
511
{
6-
public void GetAll()
12+
public string BaseUrl => "https://leetcode.com";
13+
public async Task<QuestionStat> GetAllAsync()
714
{
8-
var url = "https://leetcode.com/api/problems/all/";
15+
var leetcodeApi = RestService.For<ILeetcode>(BaseUrl);
16+
17+
var questionStat = await leetcodeApi.GetAllQuestionStat();
18+
return questionStat;
19+
}
20+
21+
public async Task<QuestionDetail> GetLeetcodeAsync(string titleSlug)
22+
{
23+
var heroAndFriendsRequest = new GraphQLRequest
24+
{
25+
Query = @"
26+
query getQuestionDetail($titleSlug: String!) {
27+
question(titleSlug: $titleSlug) {
28+
questionId
29+
questionTitle
30+
questionTitleSlug
31+
content
32+
difficulty
33+
categoryTitle
34+
codeDefinition
35+
}
36+
37+
}",
38+
OperationName = "getQuestionDetail",
39+
Variables = new
40+
{
41+
titleSlug = titleSlug
42+
}
43+
};
44+
var graphQLClient = new GraphQLClient($"{BaseUrl}/graphql");
45+
var graphQLResponse = await graphQLClient.GetAsync(heroAndFriendsRequest);
46+
var questionDetail = graphQLResponse.GetDataFieldAs<QuestionDetail>("question");
47+
return questionDetail;
948
}
1049
}
1150
}

Scripts/graphql/Program.cs

+30-34
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,50 @@
11
using System;
2-
using GraphQL.Client;
3-
using GraphQL.Common.Request;
42
using System.Threading.Tasks;
53
using System.Collections.Generic;
64
using Newtonsoft.Json;
7-
5+
using System.Linq;
86
namespace graphql
97
{
108
class Program
119
{
10+
private static readonly Leetcode lc = new Leetcode();
1211
static void Main(string[] args)
1312
{
14-
try
13+
var questionId = 0L;
14+
if(args.Length > 0)
1515
{
16-
GetLeetcodeAsync().Wait();
16+
long.TryParse(args[0], out questionId);
1717
}
18-
catch (Exception ex)
18+
if(questionId <= 0)
1919
{
20-
Console.WriteLine($"There was an exception: {ex.ToString()}");
20+
var s = "";
21+
do
22+
{
23+
Console.WriteLine("Please enter a QuestionId:");
24+
s = Console.ReadLine();
25+
long.TryParse(s, out questionId);
26+
27+
} while (questionId <= 0);
2128
}
22-
}
23-
static private async Task GetLeetcodeAsync()
24-
{
25-
var heroAndFriendsRequest = new GraphQLRequest
29+
try
2630
{
27-
Query = @"
28-
query getQuestionDetail($titleSlug: String!) {
29-
question(titleSlug: $titleSlug) {
30-
questionId
31-
questionTitle
32-
questionTitleSlug
33-
content
34-
difficulty
35-
categoryTitle
36-
codeDefinition
37-
}
38-
39-
}",
40-
OperationName = "getQuestionDetail",
41-
Variables = new
31+
var questionStat = lc.GetAllAsync().Result;
32+
if(questionStat != null && questionStat.StatStatusPairs.Any())
4233
{
43-
titleSlug = "house-robber"
34+
var statStatusPair = questionStat.StatStatusPairs.Where(x=>x.Stat.QuestionId==questionId).FirstOrDefault();
35+
if(statStatusPair != null)
36+
{
37+
var QuestionDetail = lc.GetLeetcodeAsync(statStatusPair.Stat.QuestionTitleSlug).Result;
38+
Console.WriteLine(QuestionDetail.ToJson());
39+
}
4440
}
45-
};
46-
var graphQLClient = new GraphQLClient("https://leetcode.com/graphql");
47-
var graphQLResponse = await graphQLClient.GetAsync(heroAndFriendsRequest);
48-
var questionDetail = graphQLResponse.GetDataFieldAs<QuestionDetail>("question");
49-
Console.WriteLine(questionDetail.ToJson());
50-
var codeDefinitions = JsonConvert.DeserializeObject<List<CodeDefinition>>(questionDetail.CodeDefinition);
51-
Console.WriteLine(codeDefinitions[0].ToJson());
41+
42+
}
43+
catch (Exception ex)
44+
{
45+
Console.WriteLine($"There was an exception: {ex.ToString()}");
46+
}
5247
}
48+
5349
}
5450
}

Scripts/graphql/QuestionDetail.cs

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public partial class QuestionDetail
2828

2929
[JsonProperty("codeDefinition")]
3030
public string CodeDefinition { get; set; }
31+
32+
[JsonProperty("questionDetailUrl")]
33+
public string QuestionDetailUrl{get;set;}
34+
public List<CodeDefinition> CodeDefinitions => JsonConvert.DeserializeObject<List<CodeDefinition>>(CodeDefinition);
3135
}
3236

3337
public partial class QuestionDetail

Scripts/graphql/graphql.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<PackageReference Include="GraphQL.Client" Version="1.0.3" />
1010
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
11+
<PackageReference Include="refit" Version="4.3.0" />
1112
</ItemGroup>
1213

1314
</Project>

0 commit comments

Comments
 (0)