示例#1
0
        public static Builders.FieldBuilder <IDictionary <string, object>, TProperty> EfField <TDbContext, TSource, TProperty>(this IEfGraph <TDbContext, TSource> graph, Expression <Func <TSource, TProperty> > expression, bool nullable = false, Type graphType = null) where TDbContext : DbContext where TSource : class
        {
            if (graph == null)
            {
                throw new ArgumentNullException(nameof(graph));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            //obtain the name
            string name;

            try
            {
                name = expression.NameOf();
            }
            catch
            {
                throw new ArgumentException(
                          $"Cannot infer a Field name from the expression: '{expression.Body.ToString()}' " +
                          $"on parent GraphQL type: '{graph.Name ?? graph.GetType().Name}'.");
            }

            return(EfField(graph, name, expression, nullable, graphType));
        }
示例#2
0
        private static Builders.FieldBuilder <IDictionary <string, object>, TReturn> EfFieldFromContext <TDbContext, TSource, TReturn>(this IEfGraph <TDbContext, TSource> graph, string name, Func <ResolveEfFieldContext <TDbContext, TSource>, LambdaExpression> expression, bool nullable = false, Type graphType = null) where TDbContext : DbContext where TSource : class
        {
            //obtain the type
            try
            {
                if (graphType == null)
                {
                    graphType = typeof(TReturn).GetGraphTypeFromType(nullable);
                }
            }
            catch (ArgumentOutOfRangeException exp)
            {
                throw new ArgumentException(
                          $"The GraphQL type for Field: '{name}' on parent type: '{graph.Name ?? graph.GetType().Name}' could not be derived implicitly. \n",
                          exp
                          );
            }

            var builder = FieldBuilder.Create <IDictionary <string, object>, TReturn>(graphType)
                          .Name(name)
                          .Resolve(EfGraphResolver)
                          //.Description(expression.DescriptionOf())
                          //.DeprecationReason(expression.DeprecationReasonOf())
                          //.DefaultValue(expression.DefaultValueOf())
            ;

            builder.FieldType.SetExpressionMetadata <TDbContext, TSource>(expression);
            graph.AddField(builder.FieldType);
            return(builder);
        }