Upload media with GraphQL (createUploadFile)

System Information
  • Strapi v4:

Hello, I’m having trouble making a mutation to upload an image, before coding it in my app, I’m making the mutation in the GraphQL playground. There was a big change from v3 to v4, no tutorial can help me.

According to Schema, it should be something like this:

mutation {
   createUploadFile(data: {
     url: "file:///home/gabriel/Downloads/logo-login.png"
   }) {
     date {
       id
     }
   }
}

Here is an example of the type of arguments

type UploadFileInput {
   name: String
   alternativeText: String
   caption: String
   width: int
   height: int
   formats: JSON
   hash: String
   ext: String
   mime: String
   size: float
   url: String
   previewUrl: String
   provider: String
   provider_metadata: JSON
   folder: ID
   folderPath: String
}

as return, I get a “message”: “Internal Server Error”

How should I insert my image? is it correct to send as local file link?

1 Like

I am also looking for such graphql mutation. any luck in getting it working ?

Use Altair for File upload because default GQL playground doesnt support that

mutation SingleImageUpload($refId: ID, $ref: String, $field: String, $info: FileInfoInput, $file: Upload!) {
   upload(refId: $refId, ref: $ref, field: $field, file: $file, info: $info) {
     data {
       id
       attributes {
         name   
         createdAt
         updatedAt
       }
     }
   }
}
{
  "refId": "2",
  "ref": "api::physical-exchanger.physical-exchanger",
  "field": "photo",
  "info": {
      "name":"test",
      "alternativeText": "",
      "caption": "`"
  }
}

“refId”: “ID_OF_YOUR_ENTRY_IN_YOUR_COLLECTION_TYPE”,

“ref”: “YOUR_COLLECTION_TYPE_NAME”,

“field”: “FIELD_NAME”