This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathJObjectRawType.cs
182 lines (161 loc) · 7.24 KB
/
JObjectRawType.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina'</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using GraphQL;
using GraphQL.Types;
using Newtonsoft.Json.Linq;
using RawCMS.Library.Schema;
using RawCMS.Library.Service;
using RawCMS.Plugins.GraphQL.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using FieldType = GraphQL.Types.FieldType;
namespace RawCMS.Plugins.GraphQL.Types
{
public class JObjectRawType : ObjectGraphType<object>
{
private GraphQLService _graphQLService { get; set; }
private CollectionSchema _collectionSchema { get; set; }
private EntityService _entityService { get; set; }
private GraphQLQuery _query { get; set; }
public JObjectRawType(GraphQLQuery query, GraphQLService graphQLService, CollectionSchema collectionSchema, EntityService entityService)
{
_query = query;
_graphQLService = graphQLService;
_collectionSchema = collectionSchema;
_entityService = entityService;
Name = collectionSchema.CollectionName;
var fields = entityService.GetTypes();
foreach (Field field in collectionSchema.FieldSettings)
{
BuildGraphField(field);
}
TableArgs.Add(new QueryArgument<IntGraphType> { Name = "pageNumber" });
TableArgs.Add(new QueryArgument<IntGraphType> { Name = "pageSize" });
TableArgs.Add(new QueryArgument<StringGraphType> { Name = "rawQuery" });
}
private void BuildGraphField(Field field)
{
Type graphQLType;
var f = _entityService.GetTypes().FirstOrDefault(x => x.TypeName == field.Type);
if (f?.GraphType == FieldGraphType.Relation)
{
var collectionName = field.Options["Collection"].Value<string>();
var fieldType = _query.Fields.FirstOrDefault(x => x.Name == collectionName);
if (fieldType == null)
{
//Add related collection not registred on schema
var relatedObject = _entityService.GetCollectionSchemas().FirstOrDefault(x => x.CollectionName == collectionName);
var type = new JObjectRawType(_query, _graphQLService, relatedObject, _entityService);
var listType = new ListGraphType(type);
fieldType = new FieldType
{
Name = collectionName,
Type = listType.GetType(),
ResolvedType = listType,
Resolver = new JObjectFieldResolver(_graphQLService, _entityService),
Arguments = new QueryArguments(
type.TableArgs
)
};
_query.AddField(fieldType);
}
var subType = fieldType.GetType();
var subresolvedType = fieldType.ResolvedType;
//if (!field.Options["Multiple"].Value<bool>())
//{
// var relatedObject = _entityService.GetCollectionSchemas().FirstOrDefault(x => x.CollectionName == collectionName);
// subresolvedType = new JObjectRawType(_query, _graphQLService, relatedObject, _entityService);
// subType = subresolvedType.GetType();
//}
var subField = new FieldType
{
Name = fieldType.Name,
Type = subType,
ResolvedType = subresolvedType,
Resolver = new NameFieldResolver(),
Arguments = fieldType.Arguments
};
AddField(subField);
foreach (var arg in fieldType.Arguments.Where(x => !(new string[] { "pageNumber", "pageSize", "rawQuery", "_id" }.Contains(x.Name))).ToList())
{
arg.Name = $"{collectionName}_{arg.Name}";
TableArgs.Add(arg);
}
}
else
{
//graphQLType = (ResolveFieldMetaType(field.BaseType)).GetGraphTypeFromType(!field.Required);
graphQLType = (ResolveFieldMetaType(f?.GraphType ?? FieldGraphType.String)).GetGraphTypeFromType(true);
FieldType columnField = Field(
graphQLType,
field.Name);
columnField.Resolver = new NameFieldResolver();
FillArgs(field.Name, graphQLType);
}
}
public QueryArguments TableArgs
{
get; set;
}
private IDictionary<FieldGraphType, Type> _fieldTypeToSystemType;
protected IDictionary<FieldGraphType, Type> FieldTypeToSystemType
{
get
{
if (_fieldTypeToSystemType == null)
{
_fieldTypeToSystemType = new Dictionary<FieldGraphType, Type>
{
{ FieldGraphType.Boolean, typeof(bool) },
{ FieldGraphType.Date, typeof(DateTime) },
{ FieldGraphType.Float, typeof(float) },
{ FieldGraphType.Id, typeof(Guid) },
{ FieldGraphType.Int, typeof(int) },
{ FieldGraphType.String, typeof(string) },
{ FieldGraphType.BigInt, typeof(Int64) },
{ FieldGraphType.Byte, typeof(byte) },
{ FieldGraphType.DateTimeOffset, typeof(DateTimeOffset) },
{ FieldGraphType.Decimal, typeof(decimal) },
{ FieldGraphType.Long, typeof(long) },
{ FieldGraphType.SByte, typeof(sbyte) },
{ FieldGraphType.Short, typeof(short) },
{ FieldGraphType.TimeSpanSeconds, typeof(TimeSpan) },
{ FieldGraphType.Relation, typeof(JObject) }
};
}
return _fieldTypeToSystemType;
}
}
private Type ResolveFieldMetaType(FieldGraphType type)
{
if (FieldTypeToSystemType.ContainsKey(type))
{
return FieldTypeToSystemType[type];
}
return typeof(string);
}
private void FillArgs(string name, Type graphType)
{
if (TableArgs == null)
{
TableArgs = new QueryArguments(
new QueryArgument(graphType)
{
Name = name
}
);
}
else
{
TableArgs.Add(new QueryArgument(graphType) { Name = name });
}
}
}
}