Last active
June 14, 2020 11:31
-
-
Save cosminpopescu14/89d45582ef1fc7a92bdfe7d436b85bca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Runtime.CompilerServices; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
namespace JsonPlay | |
{ | |
class Program | |
{ | |
static readonly HttpClient client = new HttpClient(); | |
private const string EndPoint = "https://localhost:5001/graphql?query="; | |
static async Task Main(string[] args) | |
{ | |
var schema = "{\r\n __schema {\r\n queryType {\r\n name\r\n }\r\n mutationType {\r\n name\r\n }\r\n subscriptionType {\r\n name\r\n }\r\n types {\r\n ...FullType\r\n }\r\n directives {\r\n name\r\n description\r\n locations\r\n args {\r\n ...InputValue\r\n }\r\n }\r\n }\r\n}\r\n\r\nfragment FullType on __Type {\r\n kind\r\n name\r\n description\r\n fields(includeDeprecated: true) {\r\n name\r\n description\r\n args {\r\n ...InputValue\r\n }\r\n type {\r\n ...TypeRef\r\n }\r\n isDeprecated\r\n deprecationReason\r\n }\r\n inputFields {\r\n ...InputValue\r\n }\r\n interfaces {\r\n ...TypeRef\r\n }\r\n enumValues(includeDeprecated: true) {\r\n name\r\n description\r\n isDeprecated\r\n deprecationReason\r\n }\r\n possibleTypes {\r\n ...TypeRef\r\n }\r\n}\r\n\r\nfragment InputValue on __InputValue {\r\n name\r\n description\r\n type {\r\n ...TypeRef\r\n }\r\n defaultValue\r\n}\r\n\r\nfragment TypeRef on __Type {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}"; | |
var typesKindName = @"{ | |
__schema { | |
types { | |
name kind | |
} | |
} | |
}"; | |
var schemaQuery = @"{ | |
__schema { | |
queryType { | |
name kind | |
} | |
} | |
}"; | |
#region schemaQuery | |
var response = await client.GetAsync($"{EndPoint + schemaQuery}"); | |
response.EnsureSuccessStatusCode(); | |
string responseBody = await response.Content.ReadAsStringAsync(); | |
string nameQuery = ""; | |
using (JsonDocument doc = JsonDocument.Parse(responseBody)) | |
{ | |
JsonElement root = doc.RootElement; | |
JsonElement info = root; | |
var types = info.GetProperty("data").GetProperty("__schema").GetProperty("queryType"); | |
nameQuery = types.GetProperty("name").GetString(); | |
} | |
#endregion | |
#region typesKindName | |
var response1 = await client.GetAsync($"{EndPoint + typesKindName}"); | |
response1.EnsureSuccessStatusCode(); | |
string responseBody1 = await response1.Content.ReadAsStringAsync(); | |
using (JsonDocument doc = JsonDocument.Parse(responseBody1)) | |
{ | |
JsonElement root = doc.RootElement; | |
JsonElement info = root; | |
var types = info.GetProperty("data").GetProperty("__schema").GetProperty("types"); | |
var x = types.EnumerateArray().ToArray();//because our collection is an array | |
var ourTypes = x.Where(condition => condition.GetProperty("kind").GetString().Equals("OBJECT")) | |
.Where(condition => condition.GetProperty("name").GetString() == nameQuery); | |
ourTypes.ToList().ForEach(res => Console.WriteLine(res)); | |
} | |
#endregion | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text.Json; | |
namespace JsonPlay | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var schema = "{\"_queryType\":\"GetAllQuery\",\"_mutationType\":null,\"_subscriptionType\":null,\"_directives\":[\"@include\",\"@skip\",\"@deprecated\"],\"_typeMap\":{\"String\":\"String\",\"Boolean\":\"Boolean\",\"Float\":\"Float\",\"Int\":\"Int\",\"ID\":\"ID\",\"Date\":\"Date\",\"DateTime\":\"DateTime\",\"DateTimeOffset\":\"DateTimeOffset\",\"Seconds\":\"Seconds\",\"Milliseconds\":\"Milliseconds\",\"Decimal\":\"Decimal\",\"__Schema\":\"__Schema\",\"__Type\":\"__Type\",\"__TypeKind\":\"__TypeKind\",\"__Field\":\"__Field\",\"__InputValue\":\"__InputValue\",\"__EnumValue\":\"__EnumValue\",\"__Directive\":\"__Directive\",\"__DirectiveLocation\":\"__DirectiveLocation\",\"GetAllQuery\":\"GetAllQuery\",\"DepartmentOGT\":\"DepartmentOGT\"},\"_subTypeMap\":{},\"_implementationsMap\":{}}\r\n"; | |
using (JsonDocument doc = JsonDocument.Parse(schema)) | |
{ | |
JsonElement root = doc.RootElement; | |
JsonElement info = root; | |
Console.WriteLine(info.GetProperty("_typeMap").GetProperty("DepartmentOGT")); | |
} | |
Console.ReadLine(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using QuickType; | |
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Runtime.CompilerServices; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
namespace JsonPlay | |
{ | |
class Program | |
{ | |
static readonly HttpClient client = new HttpClient(); | |
private const string EndPoint = "https://localhost:5001/graphql?query="; | |
static async Task Main(string[] args) | |
{ | |
var schema = "{\r\n __schema {\r\n queryType {\r\n name\r\n }\r\n mutationType {\r\n name\r\n }\r\n subscriptionType {\r\n name\r\n }\r\n types {\r\n ...FullType\r\n }\r\n directives {\r\n name\r\n description\r\n locations\r\n args {\r\n ...InputValue\r\n }\r\n }\r\n }\r\n}\r\n\r\nfragment FullType on __Type {\r\n kind\r\n name\r\n description\r\n fields(includeDeprecated: true) {\r\n name\r\n description\r\n args {\r\n ...InputValue\r\n }\r\n type {\r\n ...TypeRef\r\n }\r\n isDeprecated\r\n deprecationReason\r\n }\r\n inputFields {\r\n ...InputValue\r\n }\r\n interfaces {\r\n ...TypeRef\r\n }\r\n enumValues(includeDeprecated: true) {\r\n name\r\n description\r\n isDeprecated\r\n deprecationReason\r\n }\r\n possibleTypes {\r\n ...TypeRef\r\n }\r\n}\r\n\r\nfragment InputValue on __InputValue {\r\n name\r\n description\r\n type {\r\n ...TypeRef\r\n }\r\n defaultValue\r\n}\r\n\r\nfragment TypeRef on __Type {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n ofType {\r\n kind\r\n name\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}"; | |
var typesKindName = @"{ | |
__schema { | |
types { | |
name kind | |
} | |
} | |
}"; | |
var schemaQuery = @"{ | |
__schema { | |
queryType { | |
name kind | |
} | |
} | |
}"; | |
#region schemaQuery | |
var response = await client.GetAsync($"{EndPoint + schemaQuery}"); | |
response.EnsureSuccessStatusCode(); | |
string responseBody = await response.Content.ReadAsStringAsync(); | |
string nameQuery = ""; | |
using (JsonDocument doc = JsonDocument.Parse(responseBody)) | |
{ | |
JsonElement root = doc.RootElement; | |
JsonElement info = root; | |
var types = info.GetProperty("data").GetProperty("__schema").GetProperty("queryType"); | |
nameQuery = types.GetProperty("name").GetString(); | |
} | |
#endregion | |
#region typesKindName | |
var response1 = await client.GetAsync($"{EndPoint + typesKindName}"); | |
response1.EnsureSuccessStatusCode(); | |
string responseBody1 = await response1.Content.ReadAsStringAsync(); | |
using (JsonDocument doc = JsonDocument.Parse(responseBody1)) | |
{ | |
JsonElement root = doc.RootElement; | |
JsonElement info = root; | |
var types = info.GetProperty("data").GetProperty("__schema").GetProperty("types"); | |
var x = types.EnumerateArray().ToArray();//because our collection is an array | |
var ourTypes = x.Where(condition => condition.GetProperty("kind").GetString().Equals("OBJECT")) | |
.Where(condition => condition.GetProperty("name").GetString() == nameQuery); | |
//ourTypes.ToList().ForEach(res => Console.WriteLine(res)); | |
} | |
#endregion | |
var fullResponse = await client.GetAsync($"{EndPoint + schema}"); | |
fullResponse.EnsureSuccessStatusCode(); | |
string fullResponseBody = await fullResponse.Content.ReadAsStringAsync(); | |
using (JsonDocument doc = JsonDocument.Parse(fullResponseBody)) | |
{ | |
// JsonElement root = doc.RootElement; | |
JsonElement info = doc.RootElement; | |
var allTypes = info.GetProperty("data").GetProperty("__schema").GetProperty("types").EnumerateArray() | |
.ToArray(); | |
var queryTypeName = info.GetProperty("data").GetProperty("__schema") | |
.GetProperty("queryType").GetProperty("name"); | |
var schemaObjects = allTypes | |
.Where(condition => condition.GetProperty("kind").GetString().Equals("OBJECT")) | |
.Where(condition => condition.GetProperty("name").GetString().Equals(queryTypeName.GetString())); | |
var schemaObjectsToString = string.Join("", schemaObjects); | |
Console.WriteLine(schemaObjectsToString); | |
var obj = Root.FromJson(schemaObjectsToString);//works | |
Console.WriteLine(obj.Name); | |
} | |
Console.ReadLine(); | |
} | |
private static T GetJsonFromGenericType<T>(string json) | |
{ | |
var type = JsonSerializer.Deserialize<T>(json); | |
return (T)Convert.ChangeType(type, typeof(T)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment