Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Network request failed when uploading images with apollo client react native android

I am using ApolloClient to send mutation that contains files (images) but I am getting

Error: Network request failed

this what I have done to create links

import { createUploadLink } from 'apollo-upload-client' v ==>> "^15.0.0";

const uploadLink = createUploadLink({
        uri: API_URL,
        headers: {
          "Authorization": `Bearer ${token}`,
          'Content-Type': 'multipart/form-data',
          "Accept":"application/json"
        },
      });

this to handle errors

import { onError } from "@apollo/client/link/error"; v ==>> "^3.3.20"

   const errorLink = onError(({ graphQLErrors, networkError }) => {
        if (graphQLErrors)
          graphQLErrors.forEach(({ message, locations, path }) =>
            console.log(
              `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
            ),
          );
      
        if (networkError) console.log(`[Network zaid error]: ${networkError}`);
      });

then :

const client = new ApolloClient({
        cache: new InMemoryCache(),
        link: from([errorLink,uploadLink]),
        defaultOptions: {
          watchQuery: {
            fetchPolicy: 'cache-and-network',
            errorPolicy: 'none'
          },
          mutate: {
            mutation: "DocumentNode",
            errorPolicy: 'none'
          },
        },
      });

then I called the mutation :

       await client.mutate({

        mutation:
        gql`  
         mutation($data: PostCreatInput!, $files: [CustomCreateImages!]!) {
           createpost(data: $data, files: $files) {
              id
          }}`,

        variables: {

          data: {****},

          files:[{file:ReactNativeFile}]

         }
      
      
      }).then(response => {
        console.log(response);
        return response
      }).catch(response => {
        console.log(response);
        return response
      })

i used ReactNativeFile generated by apollo-upload-client

          new ReactNativeFile({
            uri:  "file:///storage/***.jpg",
            name: "a.jpg",
            type: "image/jpeg"
          });

I am using react native "react-native": "0.62.2" and I have a live server not using localhost I did check the server logs this request never left the mobile; there was no record of it on the server.

been stuck all day on it, any help would be highly appreciated!

like image 651
Zaid Raddad Avatar asked May 05 '26 08:05

Zaid Raddad


1 Answers

bug with React Native 0.62+ that messes up the configuration for multiform requests. It can be fixed by commenting out line 43 in android/app/src/debug/java/com/maxyride/app/drivers/ReactNativeFlipper.java:

//builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin)); 
like image 60
Zaid Raddad Avatar answered May 06 '26 20:05

Zaid Raddad