示例#1
0
        private void ConvertPropertyToGetByIdField(PropertyInfo field)
        {
            var genericArguments = field.PropertyType.GetGenericArguments();
            var entityType       = genericArguments[0];
            var idType           = genericArguments[1];

            // construct resolving method
            Func <ResolveFieldContext <object>, object> resolve = (context) =>
            {
                // get the connection delegate
                var method = ((Delegate)(field.GetValue(this)));

                // convert the global id to local id
                var globalId = context.GetArgument <string>(entityType.GetIdPropertyName().ToFirstLower());
                var id       = GlobalId.ToLocalId(idType, globalId);

                // invoke field with "context" and "id"
                return(method.DynamicInvoke(new object[] { context, id }));
            };

            // handle authentication and authorization
            var isAuthenticationRequired = field.GetAttribute <FieldAttribute>().IsAuthenticationRequired;
            var requiredRoles            = field.GetRequiredRoles();

            var graphQLField = this.AddSingleField(entityType, resolve, field.Name.ToFirstLower());

            graphQLField.RequiresRoles(requiredRoles);
            graphQLField.RequiresAuthentication(isAuthenticationRequired);
        }
示例#2
0
 public DeletePayload(DeleteInput input, bool ok)
 {
     this.Id = GlobalId.ToLocalId <TId>(input.Id);
     this.ClientMutationId = input.ClientMutationId;
     this.Viewer           = new TViewer();
     this.Ok = ok;
 }
        public static FieldType CreateDeleteMutation <TId, TEntity, TViewer>(this ObjectGraphType <object> type, Func <ResolveFieldContext <object>, object, object, object> resolve, string mutationName = null) where TEntity : class
        {
            // construct DeletePayloadType<Order>
            var entityType        = typeof(TEntity);
            var deletePayloadType = typeof(DeletePayloadType <,>).MakeGenericType(entityType, typeof(TViewer));

            deletePayloadType = deletePayloadType.ConvertToVirtualType();

            // construct mutation name (i.e. "deleteOrder")
            var fieldName = mutationName != null ? mutationName : $"delete{entityType.Name}";

            // construct NonNullGraphType<DeleteInputType<Order>>
            var deleteInputType      = typeof(DeleteInputType <>).MakeGenericType(entityType);
            var nonNullableGraphType = typeof(NonNullGraphType <>);

            nonNullableGraphType = nonNullableGraphType.MakeGenericType(deleteInputType);

            // construct arguments
            var argument = new QueryArgument(nonNullableGraphType)
            {
                Name = "input"
            };
            var arguments = new QueryArguments(argument);

            // construct resolver
            Func <ResolveFieldContext <object>, object> resolver = (context) =>
            {
                var input = context.GetArgument <DeleteInput>("input");
                var id    = GlobalId.ToLocalId <TId>(input.Id);

                return(resolve(context, id, input));
            };

            // call Field like type.Field<DeletePayloadType<Order>>("deleteOrder", args, resolve)
            var method = type.GetType().GetMethods().First(m => m.Name == "Field" && m.IsGenericMethod && m.ReturnType == typeof(FieldType));

            method = method.MakeGenericMethod(deletePayloadType);
            return((FieldType)method.Invoke(type, new object[] { fieldName, null, arguments, resolver, null }));
        }
示例#4
0
        private static T CloneProperties <T>(object subject, T clone, bool omitIdProperty)
        {
            var cloneType = clone.GetType();

            foreach (var property in subject.GetType().GetProperties())
            {
                // skip properties that do not have setters
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                var propertyValue = property.GetValue(subject, null);
                var cloneProperty = cloneType.GetProperty(property.Name);

                // handle id
                if (property.Name == "Id" && !omitIdProperty)
                {
                    var attribute  = subject.GetType().GetTypeInfo().GetCustomAttribute <TypeDescriptionProviderAttribute>();
                    var idTypeName = attribute.TypeName;

                    if (idTypeName.Contains("Guid"))
                    {
                        var idValue = Guid.Empty;

                        try
                        {
                            idValue = GlobalId.ToLocalId <Guid>((string)propertyValue);
                        }
                        catch
                        {
                            idValue = Guid.Empty;
                        }

                        cloneProperty.SetValue(clone, idValue);
                    }

                    if (idTypeName.Contains("Int32"))
                    {
                        var idValue = GlobalId.ToLocalId <int>((string)propertyValue);
                        cloneProperty.SetValue(clone, idValue);
                    }

                    continue;
                }

                // handle foreign keys
                if (property.Name.EndsWith("Id"))
                {
                    try
                    {
                        cloneProperty.SetValue(clone, propertyValue);
                    }
                    catch
                    {
                        var targetTypeName = cloneProperty.PropertyType.Name;

                        if (targetTypeName.Contains("Guid"))
                        {
                            var value = Guid.Empty;

                            try
                            {
                                value = GlobalId.ToLocalId <Guid>((string)propertyValue);
                            }
                            catch
                            {
                                value = Guid.Empty;
                            }

                            cloneProperty.SetValue(clone, value);
                        }

                        if (targetTypeName.Contains("Int32"))
                        {
                            var value = GlobalId.ToLocalId <int>((string)propertyValue);
                            cloneProperty.SetValue(clone, value);
                        }

                        continue;
                    }
                }

                // handle collections
                if (property.PropertyType.GetTypeInfo().GetInterface("ICollection") != null && property.PropertyType.IsGenericType())
                {
                    // skip properties without setters
                    if (cloneProperty.GetSetMethod() == null)
                    {
                        continue;
                    }

                    var collectionType = property.PropertyType.GetGenericTypeDefinition();
                    var parameterType  = property.PropertyType.GenericTypeArguments[0];

                    var cloneCollectionType = cloneProperty.PropertyType.GetGenericTypeDefinition();
                    var cloneParameterType  = cloneProperty.PropertyType.GenericTypeArguments[0];

                    var   collection      = (IList)propertyValue;
                    IList cloneCollection = null;

                    if (collection != null)
                    {
                        cloneCollection = (IList)Activator.CreateInstance(cloneProperty.PropertyType);

                        foreach (var item in collection)
                        {
                            cloneCollection.Add(item.CloneAs(cloneParameterType));
                        }
                    }

                    cloneProperty.SetValue(clone, cloneCollection);

                    continue;
                }

                // handle all other properties
                cloneProperty.SetValue(clone, propertyValue);
            }

            return(clone);
        }
示例#5
0
 public TId GetBefore <TId>()
 {
     return(this.before != null?GlobalId.ToLocalId <TId>(this.before) : default(TId));
 }
示例#6
0
 public TId GetAfter <TId>()
 {
     return(this.after != null?GlobalId.ToLocalId <TId>(this.after) : default(TId));
 }