private InputObjectGraphType GetSecondGraphType(ColumnMetadata columnMetadata, TableMetadata metaTable = null) { string key = $"{columnMetadata.Type.Name}_internal_input"; InputObjectGraphType objectGraphType = null; if (metaTable == null) { metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type.Name == columnMetadata.Type.Name); } if (!_tableNameLookup.ExistInputGraphType(key)) { //Creacion de instancia //var inherateListType = typeof(InputObjectGraphType<>).MakeGenericType(new Type[] { columnMetadata.Type }); objectGraphType = new InputObjectGraphType(); //objectGraphType = Activator.CreateInstance(inherateListType); objectGraphType.Name = key; foreach (var tableColumn in metaTable.Columns) { objectGraphType.Field( GraphUtils.ResolveGraphType(tableColumn.Type), tableColumn.ColumnName ); } } return(_tableNameLookup.GetOrInsertInputGraphType(key, objectGraphType)); }
private void InitGraphTableColumn(Type parentType, ColumnMetadata columnMetadata) { if (columnMetadata.Type == typeof(NetTopologySuite.Geometries.Point) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.Coordinate) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.LineString) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.MultiLineString)) { Field( typeof(string).GetGraphTypeFromType(true), columnMetadata.ColumnName, resolve: context => { var pi = parentType.GetProperty(columnMetadata.ColumnName); dynamic point = pi.GetValue(context.Source); if (point == null) { return(null); } return(JsonExtensions.SerializeWithGeoJson(point, formatting: Formatting.None)); } ); } else { Field( GraphUtils.ResolveGraphType(columnMetadata.Type), columnMetadata.ColumnName ); } }
private InputObjectGraphType GetInternalInstances(ColumnMetadata columnMetadata) { var metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type.Name == columnMetadata.Type.Name); string key = $"{metaTable.Type.Name.ToLower().ToSnakeCase()}_first_internal_input"; InputObjectGraphType objectGraphType = null; if (!_tableNameLookup.ExistInputGraphType(key)) { //var inherateListType = typeof(InputObjectGraphType<>).MakeGenericType(new Type[] { columnMetadata.Type }); objectGraphType = new InputObjectGraphType(); // Activator.CreateInstance(inherateListType); objectGraphType.Name = key; foreach (var tableColumn in metaTable.Columns) { objectGraphType.Field( GraphUtils.ResolveGraphType(tableColumn.Type), tableColumn.ColumnName ); } } return(_tableNameLookup.GetOrInsertInputGraphType(key, objectGraphType)); }
private void InitGraphTableColumn(ColumnMetadata columnMetadata, Type parentType) { //Console.WriteLine($"{columnMetadata.ColumnName} {columnMetadata.DataType}"); if (columnMetadata.DataType == "uniqueidentifier") { return; } if (columnMetadata.IsJson) // incluye litas de cada objeto { try { AddField(new FieldType { Name = columnMetadata.ColumnName, ResolvedType = GetInternalInstances(columnMetadata) }); } catch (Exception) { Field( typeof(string).GetGraphTypeFromType(true), columnMetadata.ColumnName ); } } else if (columnMetadata.IsList) // incluye litas de cada objeto { var listObjectGraph = GetInternalListInstances(columnMetadata); AddField(new FieldType { Name = columnMetadata.ColumnName, ResolvedType = listObjectGraph //Resolver = new CustomListResolver(mainTableColumn.Type, parentType, _httpContextAccessor) }); } else if (typeof(IBaseModel).IsAssignableFrom(columnMetadata.Type) || _dbMetadata.GetTableMetadatas().Any(x => x.Type == columnMetadata.Type)) { AddField(new FieldType { Name = columnMetadata.ColumnName, ResolvedType = GetInternalInstances(columnMetadata) }); } else if (columnMetadata.Type == typeof(NetTopologySuite.Geometries.Point) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.Coordinate) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.LineString) || columnMetadata.Type == typeof(NetTopologySuite.Geometries.MultiLineString)) { Field( typeof(string).GetGraphTypeFromType(true), columnMetadata.ColumnName ); } else if (columnMetadata.Type == typeof(TimeSpan)) { Field( typeof(string).GetGraphTypeFromType(true), columnMetadata.ColumnName ); } else if (columnMetadata.Type.IsEnum) { Field <IntGraphType>(columnMetadata.ColumnName, resolve: context => { var pi = parentType.GetProperty(columnMetadata.ColumnName); return((int)pi.GetValue(context.Source)); }); } else if (columnMetadata.Type != _optionsDelegate.CurrentValue.UserType && columnMetadata.Type != _optionsDelegate.CurrentValue.RoleType && columnMetadata.Type != _optionsDelegate.CurrentValue.UserRoleType) { Field( GraphUtils.ResolveGraphType(columnMetadata.Type), columnMetadata.ColumnName ); } }
public object Resolve(IResolveFieldContext context) { Type type = context.FieldDefinition.ResolvedType.GetType(); if (context.FieldAst.Name.Contains("_list")) { type = ((dynamic)context.FieldDefinition.ResolvedType).ResolvedType.GetType(); } if (type.GenericTypeArguments.Length > 0) { type = type.GetGenericArguments()[0]; } Type graphRepositoryType = typeof(IGraphRepository <>).MakeGenericType(new Type[] { type }); dynamic service = _httpContextAccessor.HttpContext.RequestServices.GetService(graphRepositoryType); var alias = string.IsNullOrEmpty(context.FieldAst.Alias) ? context.FieldAst.Name : context.FieldAst.Alias; var whereArgs = new StringBuilder(); var args = new List <object>(); var includes = new List <string>(); //Console.WriteLine($" ----------------------- alias {alias} Name {context.FieldAst.Name} type {type}"); try { //var listFieldType = ((dynamic)context.FieldDefinition.ResolvedType).ResolvedType.Fields; if (context.FieldAst.Name.Contains("_list")) { GraphUtils.DetectChild <TUser, TRole, TUserRole>(context.FieldAst.SelectionSet.Selections, includes, ((dynamic)context.FieldDefinition.ResolvedType).ResolvedType, args, whereArgs, arguments: context.Arguments, mainType: type); Console.WriteLine($"whereArgs list: {whereArgs} args {string.Join(", ", args)}"); return(service .GetAllAsync(alias, whereArgs: whereArgs.ToString(), take: context.GetArgument <int?>("first"), offset: context.GetArgument <int?>("page"), orderBy: context.GetArgument <string>("orderBy"), includeExpressions: includes, args: args.ToArray()) .Result); } else if (context.FieldAst.Name.Contains("_count")) { GraphUtils.DetectChild <TUser, TRole, TUserRole>(context.FieldAst.SelectionSet.Selections, includes, context.FieldDefinition.ResolvedType, args, whereArgs, arguments: context.Arguments, mainType: type); Console.WriteLine($"whereArgs count: {whereArgs}"); return(service.GetCountQuery(whereArgs: whereArgs.ToString(), includeExpressions: includes, args: args.ToArray())); } else if (context.FieldAst.Name.Contains("_sum")) { GraphUtils.DetectChild <TUser, TRole, TUserRole>(context.FieldAst.SelectionSet.Selections, includes, context.FieldDefinition.ResolvedType, args, whereArgs, arguments: context.Arguments, mainType: type); string param = ""; Console.WriteLine($"whereArgs sum: {whereArgs}"); if (context.FieldAst.SelectionSet.Selections != null) { foreach (Field field in context.FieldAst.SelectionSet.Selections) { //Console.WriteLine($"name {field.Name}"); param = field.Name; context.FieldAst.SelectionSet.Selections.Remove(field); break; } } if (param == null) { GetError(context); return(null); } return(service.GetSumQuery(param: param, whereArgs: whereArgs.ToString(), includeExpressions: includes, args: args.ToArray())); } else { var id = context.GetArgument <dynamic>("id"); GraphUtils.DetectChild <TUser, TRole, TUserRole>(context.FieldAst.SelectionSet.Selections, includes, context.FieldDefinition.ResolvedType, args, whereArgs, arguments: context.Arguments, mainType: type); Console.WriteLine($"whereArgs single obj: {whereArgs}"); var dbEntity = service .GetByIdAsync(alias, id, whereArgs: whereArgs.ToString(), includeExpressions: includes, args: args.ToArray()) .Result; if (dbEntity == null) { GetError(context); return(null); } return(dbEntity); } } catch (Exception e) { _logger.LogError(e.ToString()); var error = new ExecutionError(e.Message, e); context.Errors.Add(error); return(null); } }