// Transform db => db.Entities.Where(args) into args => db => db.Entities.Where(args) public static void AddQuery <TContext, TEntity>(this GraphQLSchema <TContext> context, string name, Expression <Func <TContext, TEntity> > queryableGetter) { var queryables = typeof(TEntity).GetInterfaces().Concat(new[] { typeof(TEntity) }).Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IQueryable <>)).ToList(); if (queryables.Count > 1) { throw new Exception("Types inheriting IQueryable<T> more than once are not supported."); } if (queryables.Count == 1) { var entityType = queryables[0].GetGenericArguments()[0]; var method = typeof(SchemaExtensions).GetMethod("AddQueryToListSimple", BindingFlags.Static | BindingFlags.NonPublic); var genMethod = method.MakeGenericMethod(typeof(TContext), entityType); var getter = Expression.Lambda(typeof(Func <,>).MakeGenericType(typeof(TContext), queryables[0]), queryableGetter.Body, queryableGetter.Parameters[0]); genMethod.Invoke(null, new object[] { context, name, getter }); } else { var info = GetQueryInfo(queryableGetter); if (info.ResolutionType != ResolutionType.Unmodified) { context.AddQueryInternal(name, GetFinalQueryFunc <TContext, object, IQueryable <TEntity> >(info.BaseQuery), info.ResolutionType); } else { context.AddUnmodifiedQueryInternal(name, GetFinalQueryFunc <TContext, object, TEntity>(info.OriginalQuery)); } } }