protected Expression <Func <ObjectGraphType <TEntity> > > BuildEntityGraphTypeFactory <TEntity>(params IPropertyMap[] properties)
            where TEntity : class
        {
            var entityGraphType = typeof(ObjectGraphType <TEntity>);

            // collect the body
            var bodyExprs = new List <Expression>();

            // var entityGraphType = new ObjectGraphType<TEntity>();
            var entityGraphTypeVariableExpr       = Expression.Variable(entityGraphType, "entityGraphType");
            var newEntityGraphTypeExpr            = Expression.New(entityGraphType);
            var assignEntityGraphTypeVariableExpr = Expression.Assign(entityGraphTypeVariableExpr, newEntityGraphTypeExpr);

            bodyExprs.Add(assignEntityGraphTypeVariableExpr);

            // entityGraphType.Name = $"{EntityName}_Type";
            var assignEntityGraphTypeNameExpr = GraphTypeBuilder.CreateObjectGraphAssignPropertyExpression(
                entityGraphTypeVariableExpr, entityGraphType, nameof(ObjectGraphType.Name), () => GetEntityGraphTypeName <TEntity>());

            bodyExprs.Add(assignEntityGraphTypeNameExpr);

            if (properties.IsNotEmpty())
            {
                foreach (var property in properties.Where(p => !p.Ignored))
                {
                    var propertyType = property.PropertyInfo.PropertyType;

                    // entityGraphType.Field(entity => entity.Property)
                    var objectGraphFieldByPropertyMethod           = GetObjectGraphFieldByPropertyExpressionMethod <TEntity>().MakeGenericMethod(propertyType);
                    var objectGraphFieldByPropertyMethodParamExprs =
                        BuildObjectGraphFieldByPropertyMethodParamExpressions <TEntity>(objectGraphFieldByPropertyMethod, property);
                    var fieldBuilderExpr = Expression.Call(entityGraphTypeVariableExpr, objectGraphFieldByPropertyMethod,
                                                           objectGraphFieldByPropertyMethodParamExprs);

                    // ObjectGraphType.Field(entity => entity.Property).Description("The PropertyName of the EntityName.")
                    var fieldBuilderDescriptionMethod = GetFieldBuilderDescriptionMethod <TEntity>(propertyType);
                    var fieldDescriptionExpr          = Expression.Constant(GetEntityGraphTypeFieldDescription <TEntity>(property));
                    fieldBuilderExpr = Expression.Call(fieldBuilderExpr, fieldBuilderDescriptionMethod, fieldDescriptionExpr);

                    bodyExprs.Add(fieldBuilderExpr);
                }
            }

            // code: return (ObjectGraphType<TEntity>)entityGraphType;
            var castResultExpr = Expression.Convert(entityGraphTypeVariableExpr, entityGraphType);

            bodyExprs.Add(castResultExpr);

            var factoryBodyExpr = Expression.Block(
                entityGraphType, /* return type */
                new[] { entityGraphTypeVariableExpr } /* local variables */,
                bodyExprs /* body expressions */);

            return(Expression.Lambda <Func <ObjectGraphType <TEntity> > >(factoryBodyExpr));
        }
Esempio n. 2
0
        protected Expression <Func <InputObjectGraphType> > BuildEntityOrderByGraphTypeFactory <TEntity>(params IPropertyMap[] properties)
            where TEntity : class
        {
            var entityOrderByGraphType = typeof(InputObjectGraphType);

            // collect the body
            var bodyExprs = new List <Expression>();

            // var entityOrderByType = new InputObjectGraphType();
            var entityOrderByTypeVariableExpr       = Expression.Variable(entityOrderByGraphType, "entityOrderByType");
            var newEntityOrderByGraphTypeExpr       = Expression.New(entityOrderByGraphType);
            var assignEntityOrderByTypeVariableExpr = Expression.Assign(entityOrderByTypeVariableExpr, newEntityOrderByGraphTypeExpr);

            bodyExprs.Add(assignEntityOrderByTypeVariableExpr);

            // entityOrderByType.Name = $"{EntityName}_OrderBy_Type";
            var assignEntityOrderByTypeNameExpr = GraphTypeBuilder.CreateObjectGraphAssignPropertyExpression(
                entityOrderByTypeVariableExpr, entityOrderByGraphType, nameof(InputObjectGraphType.Name), () => GetEntityOrderByTypeName <TEntity>());

            bodyExprs.Add(assignEntityOrderByTypeNameExpr);

            // entityOrderByType.Description = $"ordering options when selecting data from {EntityName}";
            var assignEntityOrderByTypeDescriptionExpr = GraphTypeBuilder.CreateObjectGraphAssignPropertyExpression(
                entityOrderByTypeVariableExpr, entityOrderByGraphType,
                nameof(InputObjectGraphType.Description), () => GetEntityOrderByTypeDescription <TEntity>());

            bodyExprs.Add(assignEntityOrderByTypeDescriptionExpr);

            if (properties.IsNotEmpty())
            {
                foreach (var property in properties.Where(p => !p.Ignored && !p.IsReadOnly))
                {
                    // entityOrderByType.Field<OrderBy_Type>(property.Name)
                    var fieldTypeExpr = GraphTypeBuilder.CreateObjectGraphFieldByGraphExpression(
                        entityOrderByTypeVariableExpr, typeof(OrderBy_Type), property);
                    bodyExprs.Add(fieldTypeExpr);
                }
            }

            // code: return (InputObjectGraphType)entityOrderByType;
            var castResultExpr = Expression.Convert(entityOrderByTypeVariableExpr, entityOrderByGraphType);

            bodyExprs.Add(castResultExpr);

            var factoryBodyExpr = Expression.Block(
                entityOrderByGraphType, /* return type */
                new[] { entityOrderByTypeVariableExpr } /* local variables */,
                bodyExprs /* body expressions */);

            return(Expression.Lambda <Func <InputObjectGraphType> >(factoryBodyExpr));
        }
        protected Expression <Func <InputObjectGraphType> > BuildEntityPredicateGraphTypeFactory <TEntity>(params IPropertyMap[] properties)
            where TEntity : class
        {
            var entityPredicateGraphType = typeof(InputObjectGraphType);

            // collect the body
            var bodyExprs = new List <Expression>();

            // var entityPredicateType = new InputObjectGraphType();
            var entityPredicateTypeVariableExpr       = Expression.Variable(entityPredicateGraphType, "entityPredicateType");
            var newEntityPredicateGraphTypeExpr       = Expression.New(entityPredicateGraphType);
            var assignEntityPredicateTypeVariableExpr = Expression.Assign(entityPredicateTypeVariableExpr, newEntityPredicateGraphTypeExpr);

            bodyExprs.Add(assignEntityPredicateTypeVariableExpr);

            // entityPredicateType.Name = $"{EntityName}_Predicate_Type";
            var assignEntityPredicateTypeNameExpr = GraphTypeBuilder.CreateObjectGraphAssignPropertyExpression(
                entityPredicateTypeVariableExpr, entityPredicateGraphType, nameof(InputObjectGraphType.Name), () => GetEntityPredicateTypeName <TEntity>());

            bodyExprs.Add(assignEntityPredicateTypeNameExpr);

            // entityPredicateType.Description = $"Boolean expression to filter rows from the resource {EntityName}. All fields are combined with a logical 'AND'.";
            var assignEntityPredicateTypeDescriptionExpr = GraphTypeBuilder.CreateObjectGraphAssignPropertyExpression(
                entityPredicateTypeVariableExpr, entityPredicateGraphType,
                nameof(InputObjectGraphType.Description), () => GetEntityPredicateTypeDescription <TEntity>());

            bodyExprs.Add(assignEntityPredicateTypeDescriptionExpr);

            // entityPredicateType.Field("_and", new ListGraphType(entityPredicateType));
            var newListEntityPredicateTypeExpr             = Expression.New(typeof(ListGraphType).GetConstructors()[0], entityPredicateTypeVariableExpr);
            var objectGraphFieldByGraphExtMethodParamExprs = BuildObjectGraphFieldByGraphExtMethodParamExpressions(_objectGraphFieldByGraphExtensionMethod,
                                                                                                                   entityPredicateTypeVariableExpr, Expression.Constant("_and"), newListEntityPredicateTypeExpr);
            var predicateAndFieldTypeExpr = Expression.Call(_objectGraphFieldByGraphExtensionMethod, objectGraphFieldByGraphExtMethodParamExprs);

            bodyExprs.Add(predicateAndFieldTypeExpr);

            // entityPredicateType.Field("_or", new ListGraphType(entityPredicateType));
            objectGraphFieldByGraphExtMethodParamExprs = BuildObjectGraphFieldByGraphExtMethodParamExpressions(_objectGraphFieldByGraphExtensionMethod,
                                                                                                               entityPredicateTypeVariableExpr, Expression.Constant("_or"), newListEntityPredicateTypeExpr);
            var predicateOrFieldTypeExpr = Expression.Call(_objectGraphFieldByGraphExtensionMethod, objectGraphFieldByGraphExtMethodParamExprs);

            bodyExprs.Add(predicateOrFieldTypeExpr);

            if (properties.IsNotEmpty())
            {
                foreach (var property in properties.Where(p => !p.Ignored && !p.IsReadOnly))
                {
                    // entityPredicateType.Field<ComparisonExpr_Type>(property.Name)
                    var propertyComparisonExprGraphType = property.PropertyInfo.PropertyType.GetComparisonExprGraphType();

                    var predicateComparisonFieldTypeExpr = GraphTypeBuilder.CreateObjectGraphFieldByGraphExpression(
                        entityPredicateTypeVariableExpr, propertyComparisonExprGraphType, property);
                    bodyExprs.Add(predicateComparisonFieldTypeExpr);
                }
            }

            // code: return (InputObjectGraphType)entityPredicateType;
            var castResultExpr = Expression.Convert(entityPredicateTypeVariableExpr, entityPredicateGraphType);

            bodyExprs.Add(castResultExpr);

            var factoryBodyExpr = Expression.Block(
                entityPredicateGraphType, /* return type */
                new[] { entityPredicateTypeVariableExpr } /* local variables */,
                bodyExprs /* body expressions */);

            return(Expression.Lambda <Func <InputObjectGraphType> >(factoryBodyExpr));
        }