Server
In your schema you need to define the file argument as type Upload this is a built in type provided by Apollo
const { gql } = require('apollo-server');
const TypeDef = gql `
type Mutation {
upload(file: Upload!)
}
`
If you are getting an error about type Upload not being a type you can also define it in your schema ( I had this issue )
const { gql } = require('apollo-server');
const TypeDef = gql `
scalar Upload
type Mutation {
upload(file: Upload!)
}
`
In your revolvers you can access the file in your args, One thing to note is that the file will be a promise;
const resolver = {
Mutation: {
async upload(obj, args, context, info) {
const file = await args.file;
// ... DO STUFF
},
}
}
Client
In the client you need to use the apollo-angular-link-http
package to help with file uploads.
In the context
of the request use need to set useMultipart : true
Query will look something like this
const uploadFileMutation = gql`
mutation UploadMutation(
$file: Upload!
) {
upload(file: $file) {
YOUR RESPONSE
}
}
}
`
The Apollo request will look like this
constructor(
private Apollo: Apollo
) { }
fileUpload(file){
this.Apollo.mutate<any>({
mutation: uploadFileMutation,
variables: {
file: this.file
},
context: {
useMultipart: true
}
}).subscribe(({ data }) => {
this.response = data.upload
});
}
I hope this helps or at least gets you on the right track.