public async Task TooLargeFile() { // Arrange var operations = new StringContent(@"{""query"": ""mutation($file: Upload) { singleUpload(file: $file) }"", ""variables"": {""file"": null } }"); var map = new StringContent(@"{ ""0"": [""variables.file""] }"); var fileA = CreatePlainTextFile("t"); var fileB = CreatePlainTextFile("test"); var multipartContent = new MultipartFormDataContent { { operations, "operations" }, { map, "map" }, { fileA, "0", "a.txt" }, { fileB, "1", "b.txt" } }; var options = new GraphQLUploadOptions { MaximumFileSize = 2 }; using (var server = CreateServer(options)) { // Act var client = server.CreateClient(); var response = await client.PostAsync("/graphql", multipartContent); // Assert Assert.Equal(System.Net.HttpStatusCode.RequestEntityTooLarge, response.StatusCode); } }
/// <summary> /// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests. /// </summary> public static IApplicationBuilder UseGraphQLUpload <TSchema>(this IApplicationBuilder builder, PathString path, Action <GraphQLUploadOptions> configure) where TSchema : ISchema { var options = new GraphQLUploadOptions(); configure(options); return(builder.UseGraphQLUpload <TSchema>(path, options)); }
public TestServer CreateServer(GraphQLUploadOptions options = null) { var path = Assembly.GetAssembly(typeof(TestBase)).Location; var hostBuilder = new WebHostBuilder() .UseContentRoot(Path.GetDirectoryName(path)) .ConfigureServices(services => services.AddSingleton <IDocumentExecuter, DocumentExecuter>() .AddSingleton <IDocumentWriter, DocumentWriter>() .AddSingleton <ISchema, TestSchema>() .AddGraphQLUpload() ) .Configure(app => app.UseGraphQLUpload <ISchema>("/graphql", options ?? new GraphQLUploadOptions()) ); return(new TestServer(hostBuilder)); }
/// <summary> /// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests. /// </summary> public static IApplicationBuilder UseGraphQLUpload <TSchema>(this IApplicationBuilder builder, PathString path, GraphQLUploadOptions options) where TSchema : ISchema { if (options is null) { throw new ArgumentNullException(nameof(options)); } return(builder.UseWhen(context => context.Request.Path.StartsWithSegments(path), branch => branch.UseMiddleware <GraphQLUploadMiddleware <TSchema> >(options))); }
/// <summary> /// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests. /// </summary> /// <typeparam name="TSchema">The implementation of <see cref="ISchema"/> to use</typeparam> /// <param name="builder">The application builder</param> /// <param name="path">The path to the GraphQL endpoint</param> /// <param name="options">The options used to configure the <see cref="GraphQLUploadMiddleware{TSchema}"/></param> /// <returns>The <see cref="IApplicationBuilder"/> received as parameter</returns>> public static IApplicationBuilder UseGraphQLUpload <TSchema>(this IApplicationBuilder builder, PathString path, GraphQLUploadOptions options) where TSchema : ISchema { if (options is null) { throw new ArgumentNullException(nameof(options)); } return(builder.UseMiddleware <GraphQLUploadMiddleware <TSchema> >(options, path)); }
/// <summary> /// Adds the <see cref="GraphQLUploadMiddleware{TSchema}"/> to handle file uploads in GraphQL requests. /// </summary> public static IApplicationBuilder UseGraphQLUpload <TSchema>(this IApplicationBuilder builder, PathString path, GraphQLUploadOptions options) where TSchema : ISchema { if (options is null) { throw new ArgumentNullException(nameof(options)); } var schema = (TSchema)builder.ApplicationServices.GetService(typeof(TSchema)); if (schema == null) { throw new ArgumentException($"No service type for {typeof(TSchema).FullName} registered."); } schema.RegisterValueConverter(new FormFileConverter()); return(builder.UseWhen(context => context.Request.Path.StartsWithSegments(path), branch => branch.UseMiddleware <GraphQLUploadMiddleware <ISchema> >(options))); }