@@ -45,6 +45,9 @@ This package is designed for ASP.NET Core (2.1 through 6.0) to facilitate easy s
4545over HTTP. The code is designed to be used as middleware within the ASP.NET Core pipeline,
4646serving GET, POST or WebSocket requests. GET requests process requests from the query string.
4747POST requests can be in the form of JSON requests, form submissions, or raw GraphQL strings.
48+ Form submissions either accepts ` query ` , ` operationName ` , ` variables ` and ` extensions ` parameters,
49+ or ` operations ` and ` map ` parameters along with file uploads as defined in the
50+ [ GraphQL multipart request spec] ( https://github.com/jaydenseric/graphql-multipart-request-spec ) .
4851WebSocket requests can use the ` graphql-ws ` or ` graphql-transport-ws ` WebSocket sub-protocol,
4952as defined in the [ apollographql/subscriptions-transport-ws] ( https://github.com/apollographql/subscriptions-transport-ws )
5053and [ enisdenjo/graphql-ws] ( https://github.com/enisdenjo/graphql-ws ) repositories, respectively.
@@ -660,6 +663,8 @@ methods allowing for different options for each configured endpoint.
660663| `HandleGet ` | Enables handling of GET requests . | True |
661664| `HandlePost ` | Enables handling of POST requests . | True |
662665| `HandleWebSockets ` | Enables handling of WebSockets requests . | True |
666+ | `MaximumFileSize ` | Sets the maximum file size allowed for GraphQL multipart requests . | unlimited |
667+ | `MaximumFileCount ` | Sets the maximum number of files allowed for GraphQL multipart requests . | unlimited |
663668| `ReadExtensionsFromQueryString ` | Enables reading extensions from the query string . | True |
664669| `ReadFormOnPost ` | Enables parsing of form data for POST requests (may have security implications ). | True |
665670| `ReadQueryStringOnPost ` | Enables parsing the query string on POST requests . | True |
@@ -918,6 +923,24 @@ security risk. However, GraphQL query operations usually do not alter data, and
918923Additionally , the response is not expected to be readable in the browser (unless CORS checks are successful ),
919924which helps alleviate this concern .
920925
926+ GraphQL .NET Server supports two formats of `application / x - www - form - urlencoded ` or `multipart / form - data ` requests :
927+
928+ 1 . The following keys are read from the form data and used to populate the GraphQL request :
929+ - `query `: The GraphQL query string .
930+ - `operationName `: The name of the operation to execute .
931+ - `variables `: A JSON - encoded object containing the variables for the operation .
932+ - `extensions `: A JSON - encoded object containing the extensions for the operation .
933+
934+ 2 . The following keys are read from the form data and used to populate the GraphQL request :
935+ - `operations `: A JSON - encoded object containing the GraphQL request , in the same format as typical
936+ requests sent via `application / json `. This can be a single object or an array of objects if batching
937+ is enabled .
938+ - `map `: An optional JSON - encoded map of file keys to file objects . This is used to map attached files
939+ into the GraphQL request 's variables property . See the section below titled 'File uploading /downloading ' and the
940+ [GraphQL multipart request specification](https :// github.com/jaydenseric/graphql-multipart-request-spec)
941+ for additional details . Since `application /x -www -form -urlencoded ` cannot transmit files , this feature
942+ is only available for `multipart /form -data ` requests .
943+
921944### Excessive `OperationCanceledException`s
922945
923946When hosting a WebSockets endpoint , it may be common for clients to simply disconnect rather
@@ -956,10 +979,26 @@ security complications, especially when used with JWT bearer authentication.
956979This answer often works well for GraphQL queries , but may not be desired during
957980uploads (mutations ).
958981
959- An option for uploading is to upload file data alongside a mutation with the `multipart/form-data`
960- content type. Please see [Issue 307](https:// github.com/graphql-dotnet/server/issues/307) and
961- [FileUploadTests.cs](https:// github.com/graphql-dotnet/server/blob/master/tests/Transports.AspNetCore.Tests/Middleware/FileUploadTests.cs)
962- for discussion and demonstration of this capability.
982+ An option for uploading is to upload file data alongside a mutation with the
983+ `multipart /form -data ` content type as described by the
984+ [GraphQL multipart request specification ](https :// github.com/jaydenseric/graphql-multipart-request-spec).
985+ Uploaded files are mapped into the GraphQL request 's variables as `IFormFile ` objects .
986+ You can use the provided `FormFileGraphType ` scalar graph type in your GraphQL schema
987+ to access these files . The `AddFormFileGraphType ()` builder extension method adds this scalar
988+ to the DI container and configures a CLR type mapping for it to be used for `IFormFile ` objects .
989+
990+ ```csharp
991+ services .AddGraphQL (b => b
992+ .AddAutoSchema <Query >()
993+ .AddFormFileGraphType ()
994+ .AddSystemTextJson ());
995+ ```
996+
997+ Please see the 'Upload' sample for a demonstration of this technique . Note that
998+ using the `FormFileGraphType ` scalar requires that the uploaded files be sent only
999+ via the `multipart / form - data ` content type as attached files . If you wish to also
1000+ allow clients to send files as base - 64 encoded strings , you can write a custom scalar
1001+ better suited to your needs .
9631002
9641003## Samples
9651004
@@ -968,16 +1007,17 @@ typical ASP.NET Core scenarios.
9681007
9691008| Name | Framework | Description |
9701009| ---------------- - | -------------------------- | ------------ - |
971- | Authorization | .NET 6 Minimal | Based on the VS template, demonstrates authorization functionality with cookie-based authentication |
972- | Basic | .NET 6 Minimal | Demonstrates simplest possible implementation |
973- | Complex | .NET 3.1 / 5 / 6 | Demonstrates older Program/Startup files and various configuration options, and multiple UI endpoints |
974- | Controller | .NET 6 Minimal | MVC implementation; does not include WebSocket support |
975- | Cors | .NET 6 Minimal | Demonstrates configuring a GraphQL endpoint to use a specified CORS policy |
976- | EndpointRouting | .NET 6 Minimal | Demonstrates configuring GraphQL through endpoint routing |
977- | Jwt | .NET 6 Minimal | Demonstrates authenticating GraphQL requests with a JWT bearer token over HTTP POST and WebSocket connections |
978- | MultipleSchemas | .NET 6 Minimal | Demonstrates configuring multiple schemas within a single server |
1010+ | Authorization | .NET 8 Minimal | Based on the VS template , demonstrates authorization functionality with cookie - based authentication |
1011+ | Basic | .NET 8 Minimal | Demonstrates simplest possible implementation |
1012+ | Complex | .NET 3 . 1 / 6 / 8 | Demonstrates older Program / Startup files and various configuration options , and multiple UI endpoints |
1013+ | Controller | .NET 8 Minimal | MVC implementation ; does not include WebSocket support |
1014+ | Cors | .NET 8 Minimal | Demonstrates configuring a GraphQL endpoint to use a specified CORS policy |
1015+ | EndpointRouting | .NET 8 Minimal | Demonstrates configuring GraphQL through endpoint routing |
1016+ | Jwt | .NET 8 Minimal | Demonstrates authenticating GraphQL requests with a JWT bearer token over HTTP POST and WebSocket connections |
1017+ | MultipleSchemas | .NET 8 Minimal | Demonstrates configuring multiple schemas within a single server |
9791018| Net48 | .NET Core 2 . 1 / .NET 4 . 8 | Demonstrates configuring GraphQL on .NET 4 . 8 / Core 2 . 1 |
980- | Pages | .NET 6 Minimal | Demonstrates configuring GraphQL on top of a Razor Pages template |
1019+ | Pages | .NET 8 Minimal | Demonstrates configuring GraphQL on top of a Razor Pages template |
1020+ | Upload | .NET 8 Minimal | Demonstrates uploading files via the `multipart / form - data ` content type |
9811021
9821022Most of the above samples rely on a sample " Chat" schema .
9831023Below are some basic requests you can use to test the schema :
0 commit comments