Skip to content

Commit 7da4b09

Browse files
committed
getting employee and departments
1 parent e1a6471 commit 7da4b09

5 files changed

Lines changed: 101 additions & 47 deletions

File tree

src/NetCore2Blockly/GraphQLDemo/InitDb.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static void Initialize(IServiceProvider serviceProvider)
2525
new Department { Iddepartment = 2, Name = "Accounting" }
2626

2727

28+
);
29+
30+
context.Employee.AddRange(
31+
new Employee { Iddepartment = 1, Name = "andrei" },
32+
new Employee { Iddepartment = 1, Name = "cosmin" }
2833
);
2934
context.SaveChanges();
3035
}

src/NetCore2Blockly/NetCore2Blockly/GraphQL/GraphQLActionInfo.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Linq;
78
using System.Text;
89

910
using System.Text.Json;
@@ -14,27 +15,43 @@ namespace NetCore2Blockly.GraphQL
1415
class GraphQLActionInfo : ActionInfo
1516
{
1617
private Field f;
18+
private GraphQLTypeArgument returnType;
19+
List<Arg> args;
20+
private GraphQLTypeArgument[] allTypesInGraph;
1721

18-
public GraphQLActionInfo(Field f)
22+
23+
public GraphQLActionInfo(Field f, GraphQLTypeArgument[] allTypesInGraph)
1924
{
2025
this.f = f;
21-
26+
this.allTypesInGraph = allTypesInGraph;
2227
}
2328

2429
internal void Init()
2530
{
2631
ActionName = f.Name;
27-
RelativeRequestUrl = "/graphql?query={" + f.Name + "{iddepartment}}";
32+
var retName = f.Type?.OfType?.Name ?? f.Type.Name;
33+
returnType = allTypesInGraph.FirstOrDefault(it => it.Name == retName);
34+
string ret = "";
35+
if(returnType != null)
36+
{
37+
ret = string.Join(" ",
38+
returnType.GetProperties().Select(it => it.Name)
39+
);
40+
}
41+
42+
43+
RelativeRequestUrl = "/graphql?query={" + f.Name + "{"+ ret +"}}";
2844
Verb = "GET";
45+
46+
args = f.Args;
2947

30-
31-
//if (f.Args?.Count > 0)
32-
//{
33-
// foreach (var arg in f.Args)
34-
// {
35-
// action.Params.Add(arg.Name, (BlocklyType.CreateValue('string'), BindingSourceDefinition.Query));
36-
// }
37-
//}
48+
if (f.Args?.Count > 0)
49+
{
50+
foreach (var arg in f.Args)
51+
{
52+
//Params.Add(arg.Name, (BlocklyType.CreateValue('string'), BindingSourceDefinition.Query));
53+
}
54+
}
3855
ReturnType = BlocklyType.CreateValue(null);
3956

4057

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NetCore2Blockly.GraphQL
2+
{
3+
class GraphQLPropertyBase : PropertyBase
4+
{
5+
public override bool IsArray => false;
6+
}
7+
}
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
using System;
1+
using NetCore2Blockly.Swagger;
2+
using System;
23
using System.Collections.Generic;
34
using System.Reflection.Metadata.Ecma335;
45
using System.Text;
5-
6-
6+
using System.Text.Json;
77

88
namespace NetCore2Blockly.GraphQL
99
{
1010
class GraphQLTypeArgument : TypeArgumentBase
1111
{
12-
public GraphQLTypeArgument(string id) : base(id)
12+
private JsonElement it;
13+
private PropertyBase[] properties;
14+
15+
public GraphQLTypeArgument(JsonElement it):base(it.GetProperty("name").GetString())
1316
{
14-
17+
List<PropertyBase> props = new List<PropertyBase>();
18+
this.it = it;
19+
var fields = it.GetProperty("fields");
20+
var l=fields.GetArrayLength();
21+
for(var i = 0; i < l; i++)
22+
{
23+
var current = fields[i];
24+
var prop = new GraphQLPropertyBase();
25+
prop.Name = current.GetProperty("name").GetString();
26+
//put here real name
27+
prop.PropertyType = BlocklyType.CreateValue(null);
28+
29+
props.Add(prop);
30+
}
31+
properties = props.ToArray();
1532
}
1633

1734
/// <summary>
1835
/// Gets the OGT name. ie DepartmentOGT
1936
/// </summary>
20-
public override string FullName => id;
37+
public override string FullName => it.GetProperty("name").GetString();
2138

2239
public override bool IsEnum => false;
2340

@@ -32,7 +49,7 @@ public override bool ConvertibleToBlocklyType()
3249

3350
public override PropertyBase[] GetProperties()
3451
{
35-
return null;
52+
return properties;
3653
}
3754

3855
public override Dictionary<string, object> GetValuesForEnum()
@@ -42,17 +59,17 @@ public override Dictionary<string, object> GetValuesForEnum()
4259

4360
public override string TranslateToBlocklyBlocksType()
4461
{
45-
throw new NotImplementedException();
62+
return $"TranslateToBlocklyBlocksType=>{id}";
4663
}
4764

4865
public override string TranslateToBlocklyType()
4966
{
50-
throw new NotImplementedException();
67+
return $"TranslateToBlocklyType=>{id}";
5168
}
5269

5370
public override string TranslateToNewTypeName()
5471
{
55-
throw new NotImplementedException();
72+
return $"TranslateToNewTypeName=>{id}";
5673
}
5774
}
5875
}

src/NetCore2Blockly/NetCore2Blockly/GraphQL/GraphqlGenerator.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,53 @@ public async Task<List<ActionInfo>> GetIntrospection()
4545
.Where(condition => condition.GetProperty("name").GetString().Equals(queryTypeName.GetString()));
4646
var schemaObjectsToString = string.Join("", schemaObjects);
4747

48-
/*
49-
* Getting the OGT Fields
50-
*/
51-
var typesOGT = allTypes
52-
.Where(condition => condition.GetProperty("kind").GetString().Equals("OBJECT"))
53-
.Where(condition => condition.GetProperty("name").GetString().Contains("OGT"));// for the moment, hardcoded
54-
5548
//Console.WriteLine(schemaObjectsToString);
5649
var obj = Root.FromJson(schemaObjectsToString);//works
50+
51+
var nameObjectReturns = obj.Fields
52+
.Where(it => it.Type?.OfType?.Name != null)
53+
.Select(it => it.Type.OfType)
54+
.Select(it => it.Name)
55+
.ToArray();
56+
57+
var nameObjectArgs = obj.Fields
58+
.Where(it => it.Args?.Count > 0)
59+
.SelectMany(it => it.Args)
60+
.Select(it => it?.Type?.Name)
61+
.Where(it=>it != null)
62+
.ToArray();
63+
64+
//remove from args the returns
65+
nameObjectArgs =
66+
nameObjectArgs.Where(it =>
67+
!nameObjectReturns.Contains(it)).ToArray();
68+
69+
var allObjectsNames = nameObjectReturns
70+
.Union(nameObjectArgs)
71+
.ToArray();
72+
// finally get the json
73+
var typesWithFields = allTypes
74+
.Where(condition => condition.GetProperty("kind").GetString().Equals("OBJECT"))
75+
.Where(condition => allObjectsNames.Contains( condition.GetProperty("name").GetString()))
76+
.ToArray();
77+
78+
var allTypesInGraph = typesWithFields
79+
.Select(it => new GraphQLTypeArgument(it))
80+
.ToArray();
81+
82+
5783
string controllerName = obj.Name;
5884
foreach(var f in obj.Fields)
5985
{
6086
// generate GraphQLActionInfo
61-
var action = new GraphQLActionInfo(f);
87+
var action = new GraphQLActionInfo(f, allTypesInGraph);
6288
action.ControllerName = controllerName;
6389
action.Init();
6490

6591
arrayOfActions.Add(action);
6692

6793
}
6894

69-
//there are a few ogts
70-
foreach (var ogt in typesOGT)
71-
{
72-
var myOGTsToStr = string.Join("", ogt);
73-
var objOGT = Root.FromJson(myOGTsToStr);
74-
string controllerNameOGT = obj.Name;
75-
76-
foreach (var field in objOGT.Fields)
77-
{
78-
var action = new GraphQLActionInfo(field);
79-
action.ControllerName = controllerNameOGT;
80-
action.Init();
81-
arrayOfActions.Add(action);
82-
}
83-
84-
85-
86-
}
8795

8896
}
8997
//return the array of ACtion Info

0 commit comments

Comments
 (0)