/// <summary> /// Checks if the given argument exists, if it exists, the value of the argument is set into value. /// </summary> public static bool TryGetArgument <T>(this IResolveFieldContext <object> context, string field, out T value) { if (context.HasArgument(field)) { value = context.GetArgument <T>(field); return(true); } value = default; return(false); }
private async Task <object> resolveOrders(IResolveFieldContext <object> context) { try { // Throw a random exception. if (_random.NextDouble() < 0.2) { throw new Exception("Random Exception"); } // Check skip value and add ValidationError. if (context.HasArgument("skip") && context.GetArgument <int>("skip") < 0) { context.Errors.Add(new ValidationError( context.Document.OriginalQuery, "skip", "The argument 'skip' can not be less than 0")); return(null); } // Return with the list of orders. return(await _dbContext .Orders .Skip(context.GetArgument <int>("skip")) .Take(context.GetArgument <int>("take")) .ToListAsync()); } catch (Exception ex) { string errorMessage = $"Failed to get the orders: {ex.Message}."; context.Errors.Add(new ExecutionError(errorMessage, ex)); _logger.LogError(errorMessage, ex); } return(null); }
public dynamic Resolve(IResolveFieldContext context) { //Console.WriteLine($"----------------------Alias: {_type} {context.FieldAst.Alias} NAME {context.FieldAst.Name} "); dynamic entity = context.GetArgument(_type, _type.Name.ToLower().ToSnakeCase(), defaultValue: null); Type graphRepositoryType = typeof(IGraphRepository <>).MakeGenericType(new Type[] { _type }); dynamic service = _httpContextAccessor.HttpContext.RequestServices.GetService(graphRepositoryType); dynamic id = null; var sendObjFirebase = context.GetArgument <bool?>("sendObjFirebase") ?? true; dynamic deleteId = null; if (context.HasArgument("id")) { id = context.GetArgument <object>("id"); if (id is int) { id = (int)id; } else if (id is int) { id = id.ToString(); } else if (id is Guid) { id = (Guid)deleteId; } } if (context.HasArgument($"{_type.Name.ToLower().ToSnakeCase()}Id")) { deleteId = context.GetArgument <object>($"{_type.Name.ToLower().ToSnakeCase()}Id"); if (deleteId is int) { deleteId = (int)deleteId; } else if (deleteId is int) { deleteId = deleteId.ToString(); } else if (deleteId is Guid) { deleteId = (Guid)deleteId; } } var alias = string.IsNullOrEmpty(context.FieldAst.Alias) ? context.FieldAst.Name : context.FieldAst.Alias; var mainType = _type; string model = ""; FieldType fieldType = null; List <string> includes = new(); dynamic resolvedType = context.FieldDefinition.ResolvedType; foreach (Field field in context.FieldAst.SelectionSet.Selections) { if (field.SelectionSet.Selections.Count > 0) { model = field.Name; try { fieldType = ((IEnumerable <FieldType>)resolvedType.Fields).SingleOrDefault(x => x.Name == field.Name); } catch (Exception) { } if (fieldType != null) { // detect if field is object if (fieldType.ResolvedType.GetType().IsGenericType&& fieldType.ResolvedType is not ListGraphType && fieldType.ResolvedType.GetType().GetGenericTypeDefinition() == typeof(ObjectGraphType <>)) { includes.Add(model); } } } } if (id != null) { var argName = context.FieldAst.Arguments.FirstOrDefault(x => x.Name == _type.Name.ToLower().ToSnakeCase()); object dbEntity = null; var variable = context.Variables.FirstOrDefault(x => x.Name == (string)argName.Value.Value); if (variable != null && variable.Value.GetType() == typeof(Dictionary <string, object>)) { dbEntity = service.Update(id, entity, (Dictionary <string, object>)variable.Value, alias, sendObjFirebase, includes); } else { dbEntity = service.Update(id, entity, (Dictionary <string, object>)argName.Value.Value, alias, sendObjFirebase, includes); } if (dbEntity == null) { GetError(context); return(null); } return(dbEntity); } if (deleteId != null) { var dbEntity = service.Delete(deleteId, alias, sendObjFirebase); if (dbEntity == null) { GetError(context); return(null); } return(dbEntity); } //var service = _httpContextAccessor.HttpContext.RequestServices.GetService<IGraphRepository<Permission>>(); return(service.Create(entity, alias, sendObjFirebase, includes)); }