jaydenseric / graphql-upload

Middleware and a scalar Upload to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 132 forks source link

BadRequestError: File missing in the request. #232

Closed WakkoStar closed 3 years ago

WakkoStar commented 3 years ago

I use react native and I don't know if my file object is correct.

export const createFormData = async (nom, gout_array, difficulty, image) => {
  const form = new formData();`
  //operations
  const CREATE_COCKTAIL = `mutation ($nom: String!, $gout_array: [Int!]!, $difficulty: String!, $file: Upload) {
    createCocktail (nom: $nom, gout_array: $gout_array, difficulty: $difficulty, file: $file)
}`;
  const variables = {
    nom,
    gout_array,
    difficulty,
    file: null,
  };
  const operations = JSON.stringify({query: CREATE_COCKTAIL, variables});
  form.append('operations', operations);

  const body = new formData();
  body.append('operations', operations);
  body.append('map', '{ "0": ["variables.file"] }');

  const file = new File([b64toBlob(image.base64, image.type)], image.fileName, {
    type: image.type,
  });

  body.append('0', file, image.fileName);

  return body;
};

I've been stuck for 2 days.... Thank you !

WakkoStar commented 3 years ago

I found my problem !


const file = {uri: image.uri, name: image.fileName, type: image.type};
  body.append('0', file, image.fileName);