├── GraphQL.cs ├── LICENSE └── README.md /GraphQL.cs: -------------------------------------------------------------------------------- 1 | using Newtonsoft.Json; 2 | using Newtonsoft.Json.Linq; 3 | using System; 4 | using System.IO; 5 | using System.Net; 6 | using System.Text; 7 | 8 | namespace GraphQL 9 | { 10 | public class GraphQLClient 11 | { 12 | private class GraphQLQuery 13 | { 14 | // public string OperationName { get; set; } 15 | public string query { get; set; } 16 | public object variables { get; set; } 17 | } 18 | public class GraphQLQueryResult 19 | { 20 | private string raw; 21 | private JObject data; 22 | private Exception Exception; 23 | public GraphQLQueryResult(string text, Exception ex = null) 24 | { 25 | Exception = ex; 26 | raw = text; 27 | data = text != null ? JObject.Parse(text) : null; 28 | } 29 | public Exception GetException() 30 | { 31 | return Exception; 32 | } 33 | public string GetRaw() 34 | { 35 | return raw; 36 | } 37 | public T Get(string key) 38 | { 39 | if (data == null) return default(T); 40 | try 41 | { 42 | return JsonConvert.DeserializeObject(this.data["data"][key].ToString()); 43 | } 44 | catch 45 | { 46 | return default(T); 47 | } 48 | } 49 | public dynamic Get(string key) 50 | { 51 | if (data == null) return null; 52 | try 53 | { 54 | return JsonConvert.DeserializeObject(this.data["data"][key].ToString()); 55 | } 56 | catch 57 | { 58 | return null; 59 | } 60 | } 61 | public dynamic GetData() 62 | { 63 | if (data == null) return null; 64 | try 65 | { 66 | return JsonConvert.DeserializeObject(this.data["data"].ToString()); 67 | } 68 | catch 69 | { 70 | return null; 71 | } 72 | } 73 | } 74 | private string url; 75 | public GraphQLClient(string url) 76 | { 77 | this.url = url; 78 | } 79 | public dynamic Query(string query, object variables) 80 | { 81 | var fullQuery = new GraphQLQuery() 82 | { 83 | query = query, 84 | variables = variables, 85 | }; 86 | string jsonContent = JsonConvert.SerializeObject(fullQuery); 87 | 88 | Console.WriteLine(jsonContent); 89 | HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 90 | request.Method = "POST"; 91 | 92 | UTF8Encoding encoding = new UTF8Encoding(); 93 | Byte[] byteArray = encoding.GetBytes(jsonContent.Trim()); 94 | 95 | request.ContentLength = byteArray.Length; 96 | request.ContentType = @"application/json"; 97 | 98 | using (Stream dataStream = request.GetRequestStream()) 99 | { 100 | dataStream.Write(byteArray, 0, byteArray.Length); 101 | } 102 | long length = 0; 103 | try 104 | { 105 | using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 106 | { 107 | length = response.ContentLength; 108 | using (Stream responseStream = response.GetResponseStream()) 109 | { 110 | StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 111 | var json = reader.ReadToEnd(); 112 | return new GraphQLQueryResult(json); 113 | } 114 | } 115 | } 116 | catch (WebException ex) 117 | { 118 | WebResponse errorResponse = ex.Response; 119 | using (Stream responseStream = errorResponse.GetResponseStream()) 120 | { 121 | StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); 122 | String errorText = reader.ReadToEnd(); 123 | Console.WriteLine(errorText); 124 | return new GraphQLQueryResult(null, ex); 125 | } 126 | } 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Benjamin Kniffler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # graphql-net-client 2 | Very simple GraphQL client for .NET/C#. 3 | Requires JSON.NET! 4 | 5 | ## Typed result 6 | ```cs 7 | class GqlObj 8 | { 9 | public string name { get; set; } 10 | public string id { get; set; } 11 | } 12 | 13 | var client = new GraphQLClient("https://mygraphql.endpoint"); 14 | var query = @" 15 | query($id: String) { 16 | someObject(id: $id) { 17 | id 18 | name 19 | } 20 | } 21 | "; 22 | var obj = client.Query(query, new { id = "123" }).Get("someObject"); 23 | if (obj != null) 24 | { 25 | Console.WriteLine(obj.name); 26 | } 27 | else 28 | { 29 | Console.WriteLine("Null :("); 30 | } 31 | ``` 32 | 33 | ## Dynamic result 34 | ```cs 35 | var client = new GraphQLClient("https://mygraphql.endpoint"); 36 | var query = @" 37 | query($id: String) { 38 | someObject(id: $id) { 39 | id 40 | name 41 | } 42 | } 43 | "; 44 | var obj = client.Query(query, new { id = "123" }).Get("someObject"); 45 | if (obj != null) 46 | { 47 | Console.WriteLine((string)obj.name); 48 | } 49 | else 50 | { 51 | Console.WriteLine("Null :("); 52 | } 53 | ``` 54 | --------------------------------------------------------------------------------