Uploading Files
Vendure handles file uploads with the GraphQL multipart request specification. Internally, we use the graphql-upload package. Once uploaded, a file is known as an Asset. Assets are typically used for images, but can represent any kind of binary data such as PDF files or videos.
Upload clients
Here is a list of client implementations that will allow you to upload files using the spec. If you are using Apollo Client, then you should install the apollo-upload-client npm package.
For testing, it is even possible to use a plain curl request.
The createAssets mutation
The createAssets mutation in the Admin API is the only means of uploading files by default.
Here's an example of how a file upload would look using the apollo-upload-client package:
Custom upload mutations
How about if you want to implement a custom mutation for file uploads? Let's take an example where we want to allow customers to set an avatar image. To do this, we'll add a custom field to the Customer entity and then define a new mutation in the Shop API.
Configuration
Let's define a custom field to associate the avatar Asset with the Customer entity. To keep everything encapsulated, we'll do all of this in a plugin
Schema definition
Next, we will define the schema for the mutation:
Resolver
The resolver will make use of the built-in AssetService to handle the processing of the uploaded file into an Asset.
Complete Customer Avatar Plugin
Let's put all these parts together into the plugin:
Uploading a Customer Avatar
In our storefront, we would then upload a Customer's avatar like this: