public static IFieldMiddlewareBuilder Use(this IFieldMiddlewareBuilder builder, System.Type middleware)
        {
            return(builder.Use(next =>
            {
                var methods = middleware.GetMethods(BindingFlags.Instance | BindingFlags.Public);
                var invokeMethods = methods.Where(m => string.Equals(m.Name, InvokeMethodName, StringComparison.Ordinal)).ToArray();
                if (invokeMethods.Length > 1)
                {
                    throw new InvalidOperationException($"There should be only a single method named {InvokeMethodName}. Middleware actually has {invokeMethods.Length} methods.");
                }

                if (invokeMethods.Length == 0)
                {
                    throw new InvalidOperationException($"Could not find a method named {InvokeMethodName}. Middleware must have a public instance method named {InvokeMethodName}.");
                }

                var methodInfo = invokeMethods[0];
                if (!typeof(Task <object>).IsAssignableFrom(methodInfo.ReturnType))
                {
                    throw new InvalidOperationException($"The {InvokeMethodName} method should return a Task<object>.");
                }

                var parameters = methodInfo.GetParameters();
                if (parameters.Length != 2 || parameters[0].ParameterType != typeof(ResolveFieldContext) || parameters[1].ParameterType != typeof(FieldMiddlewareDelegate))
                {
                    throw new InvalidOperationException($"The {InvokeMethodName} method of middleware should take a parameter of type {nameof(ResolveFieldContext)} as the first parameter and a parameter of type {nameof(FieldMiddlewareDelegate)} as the second parameter.");
                }

                var instance = Activator.CreateInstance(middleware);

                return context => (Task <object>)methodInfo.Invoke(instance, new object[] { context, next });
            }));
        }
示例#2
0
 public IFieldMiddlewareBuilder Use(Func <FieldMiddlewareDelegate, FieldMiddlewareDelegate> middleware)
 {
     return(overriddenBuilder.Use(middleware));
 }
 /// <summary>
 /// Adds the specified delegate to the list of delegates that will be applied to the schema when invoking <see cref="IFieldMiddlewareBuilder.ApplyTo(ISchema)"/>.
 /// <br/><br/>
 /// This is a compatibility shim when compiling delegates without schema specified.
 /// </summary>
 /// <param name="builder">Interface for connecting middlewares to a schema.</param>
 /// <param name="middleware">Middleware delegate.</param>
 /// <returns>Reference to the same <see cref="IFieldMiddlewareBuilder"/>.</returns>
 public static IFieldMiddlewareBuilder Use(this IFieldMiddlewareBuilder builder, Func <FieldMiddlewareDelegate, FieldMiddlewareDelegate> middleware)
 => builder.Use((_, next) => middleware(next));
 /// <summary>
 /// Adds middleware to the list of delegates that will be applied to the schema when invoking <see cref="IFieldMiddlewareBuilder.ApplyTo(ISchema)"/>.
 /// </summary>
 /// <param name="builder">Interface for connecting middlewares to a schema.</param>
 /// <param name="middleware">Middleware instance.</param>
 /// <returns>Reference to the same <see cref="IFieldMiddlewareBuilder"/>.</returns>
 public static IFieldMiddlewareBuilder Use(this IFieldMiddlewareBuilder builder, IFieldMiddleware middleware)
 => builder.Use(next => context => middleware.Resolve(context, next));