I'm currently working on a React Native application where I need to upload images to my GraphQL server using Apollo Client and react-native-image-picker. However, I'm encountering an issue where I consistently receive an "Upload value invalid" error from the server when attempting to upload the image.
- I've verified that the file object is constructed correctly with the appropriate URI, type, and name properties.
- The MIME type (type property) of the file object is set to image/jpeg or image/png based on the selected image.
- My GraphQL server is configured to accept file uploads, and the server-side mutation handling the file upload appears to be correctly implemented.
- I've tested with static files and encountered the same error, ruling out issues with the file selection process.
const handleImage = async () => {
const options: ImageLibraryOptions = {
mediaType: 'photo',// Limit selection to one image
};
launchImageLibrary(options, (response: any) => {
if (response.didCancel) {
console.log('user cancel the image pikker');
return;
} else if (response.errorCode) {
console.log(response.errorMessage);
return;
}
console.log('image ', response?.assets);`your text`
if (response.assets) {
const file = {
uri: response.assets[0].uri,
type: response.assets[0].type || 'image/jpeg', // Default to JPEG if type is not provided
name: response.assets[0].fileName || 'image.jpg',
}
setProfile(file);
}
});
};
calling the api
variables: {
email: route?.params ? route.params.data.email : email,
fullname: name,
password: route.params?.data?.password,
nikname: nikname,
image :profile
},
});
export const client = new ApolloClient({
uri: `${env.server}/graphql`,
cache: new InMemoryCache(),
link: createUploadLink({
uri: `${env.server}/graphql`,
headers: {
'apollo-require-preflight': 'true',
},
})
});