Frank Mwale
Frank Mwale

Reputation: 71

How to upload images OR files from flutter_graphql to Strapi CMS and Cloudinary?

I have created a Graphql API with Strapi; using Cloudinary as its Media Provider to store images and documents. Everything - the uploads - is working when using the Strapi admin panel. The images are being saved in Cloudinary. I am having a problem sending the data from my Flutter code using the graphQL plugin (https://pub.dev/packages/graphql).

I have the image as Uint8List? type in Flutter when I pick it from storage.

here is my graphQL mutation. The other fields are uploading to the database when I remove the image projectLogo. The app crashes when I include the image in the mutation - perhaps there's something wrong with the syntax of this mutation.

''' 
    mutation {
      createProject(data: {
        title: "$name",
        description: "$description",
        completed: $completed,
        start_date: "$startDate",      
        end_date: "$endDate",
        project_logo: upload(file: $projectLogo){
          name
        },
        publishedAt: "${publishedAt}Z"
      }){
        data{
          id,
          attributes{
            title,
            description,
            completed,
            start_date,
            end_date
          }
        }
      }
    }
''';

when I Try-Catch the error, this is what is showing

EXCEPTION=> Error on line 8, column 29: Expected an object field name
  ╷
8 │         project_logo: upload(file: [137, 80.....])

in implementing my solution I followed an example here done in React js for Strapi Graphql upload mutation.

There is also a solution here, also in a js framework. The author of the question answered it, but I can't understand it.

My question is: what is the proper syntax to upload files in strapi using graphQL?

My understanding is that Strapi gets the image and uploads it to Cloudinary; Cloudinary returns the ID of the image; Strapi then saves the image ID. Am I needed to manually upload the image to Cloudinary and then save the ID with strapi?

Upvotes: 1

Views: 307

Answers (0)

Related Questions