Skip to content

Experimental middleware and an Upload scalar to add support for GraphQL multipart requests for ASP.NET Core

License

Notifications You must be signed in to change notification settings

fubar-coder/graphql-dotnet-upload

 
 

Repository files navigation

graphql-dotnet-upload

Build Status NuGet

This repository contains an experimental implementation of the GraphQL multipart request spec based on ASP.NET Core.

Installation

You can install the latest version via NuGet.

PM> Install-Package GraphQL.Upload.AspNetCore

Preview versions from the develop branch are available via GitHub Packages.

Usage

Register the middleware in your Startup.cs.

This middleware implementation only parses multipart requests. That's why we're using additional middleware (graphql-dotnet/server) to handle other request types.

public void ConfigureServices(IServiceCollection services)
{
  services.AddSingleton<MySchema>()
          .AddGraphQLUpload()
          .AddGraphQL();
}

public void Configure(IApplicationBuilder app)
{
  app.UseGraphQLUpload<MySchema>()
     .UseGraphQL<MySchema>();
}

Register a FormFileConverter within your MySchema.cs.

public class MySchema : Schema
{
    public MySchema()
    {
        Query = new Query();
        Mutation = new Mutation();
        RegisterValueConverter(new FormFileConverter());
    }
}

Use the upload scalar in your resolvers. Files are exposed as IFormFile.

Field<StringGraphType>(
    "singleUpload",
    arguments: new QueryArguments(
        new QueryArgument<UploadGraphType> { Name = "file" }),
    resolve: context =>
    {
        var file = context.GetArgument<IFormFile>("file");
        return file.FileName;
    });

Testing

Take a look at the tests and the sample and run some of the cURL requests the spec lists if you are curious.

CMD:

curl localhost:54234/graphql ^
	-F operations="{ \"query\": \"mutation ($file: Upload!) { singleUpload(file: $file) { name } }\", \"variables\": { \"file\": null } }" ^
	-F map="{ \"0\": [\"variables.file\"] }" ^
	-F 0=@a.txt

Bash:

curl localhost:54234/graphql \
	-F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) { name } }", "variables": { "file": null } }' \
	-F map='{ "0": ["variables.file"] }' \
	-F 0=@a.txt

About

Experimental middleware and an Upload scalar to add support for GraphQL multipart requests for ASP.NET Core

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%