-
Notifications
You must be signed in to change notification settings - Fork 25
/
GraphQL.cs
129 lines (124 loc) · 4.06 KB
/
GraphQL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Text;
namespace GraphQL
{
public class GraphQLClient
{
private class GraphQLQuery
{
// public string OperationName { get; set; }
public string query { get; set; }
public object variables { get; set; }
}
public class GraphQLQueryResult
{
private string raw;
private JObject data;
private Exception Exception;
public GraphQLQueryResult(string text, Exception ex = null)
{
Exception = ex;
raw = text;
data = text != null ? JObject.Parse(text) : null;
}
public Exception GetException()
{
return Exception;
}
public string GetRaw()
{
return raw;
}
public T Get<T>(string key)
{
if (data == null) return default(T);
try
{
return JsonConvert.DeserializeObject<T>(this.data["data"][key].ToString());
}
catch
{
return default(T);
}
}
public dynamic Get(string key)
{
if (data == null) return null;
try
{
return JsonConvert.DeserializeObject<dynamic>(this.data["data"][key].ToString());
}
catch
{
return null;
}
}
public dynamic GetData()
{
if (data == null) return null;
try
{
return JsonConvert.DeserializeObject<dynamic>(this.data["data"].ToString());
}
catch
{
return null;
}
}
}
private string url;
public GraphQLClient(string url)
{
this.url = url;
}
public dynamic Query(string query, object variables)
{
var fullQuery = new GraphQLQuery()
{
query = query,
variables = variables,
};
string jsonContent = JsonConvert.SerializeObject(fullQuery);
Console.WriteLine(jsonContent);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(jsonContent.Trim());
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
long length = 0;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
length = response.ContentLength;
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
var json = reader.ReadToEnd();
return new GraphQLQueryResult(json);
}
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
Console.WriteLine(errorText);
return new GraphQLQueryResult(null, ex);
}
}
}
}
}