Not able to run GraphQL mutations in Redux even though I am able to run it within the component [closed]

Not able to run GraphQL mutations in Redux even though I am able to run it within the component [closed]


-2

I am currently working on a project in which GraphQL is being implemented for the first time and initially I only had to run GraphQL mutations within the component (using Apollo-client and its hook – useMutation) that I was working on and all was well.

Now, I am trying to have the mutation in the action file, so that any component or container can use it. I am trying three different kinds of implementations. All three are working in my local system. But, once they are deployed and I am testing them in live version, none of them seem to be working right.

See code given below:

Client definition:

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: ApolloLink.from([
    new HttpLink({ uri: config.urls.THE_URL }),
    onError(({ graphQLErrors, networkError }) => {
      alert('network Error');
      if (networkError) {
        console.log(`[Network error]: ${networkError}`);
      }

      if (graphQLErrors) {
        alert('graphQL Error');
        graphQLErrors.forEach(({ message, locations, path }) =>

          console.log(
            `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
          )
        );
      }
    })
  ]),
});

simple function using client.mutate

export function editOrCreateMutation(payloadData) {
  try {
    console.log("Inside mutate try")
    client.mutate({
      mutation: EDIT_OR_CREATE,
      variables: payloadData.variables
    })
      .then(
        (response) => console.log("Response in client mutate:", response),
        (reject) => console.log("Reject in client mutate:", reject)
      )
      .catch((err) => console.error(err));
  } catch (error) {
    console.log("Reducer GraphQL mutate error: ", error)
  }
}

async function using client.mutate

export function editOrCreateMutationAsync(payloadData) {
  return async () => {
    try {
      console.log("Inside async mutate try")
      const result = await client.mutate({
        mutation: EDIT_OR_CREATE,
        variables: payloadData.variables
      })
        .then(
          (response) => console.log("Response in async client mutate:", response),
          (reject) => console.log("Reject in async client mutate:", reject)
        )
        .catch((err) => console.error(err));
    } catch (error) {
      console.log("Reducer GraphQL async mutate error: ", error)
    }
  }
}

mutation using fetch

export function editOrCreateUsingFetch(payloadData) {
  const plainQuery = `
  mutation EditOrCreate(
    $input: UpdateInput!
  ) {
    update(
      input: $input
    ) {
      data_point_1
      data_point_2
      data_point_3
    }
  }`;
  try {
    console.log("Inside fetch try")
    fetch(
      config.urls.THE_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        query: plainQuery,
        variables: payloadData.variables
      })
    }
    )
      .then(
        response => console.log("Response from mutation fetch: ", response),
        reject => console.log("Reject from mutation fetch: ", reject)
      )
      .then(
        data => console.log("Data from mutation fetch success: ", data),
        data => console.log("Data from mutation fetch failure: ", data)
      )
      .catch((err) => console.error(err))
  } catch (error) {
    console.log("Reducer GraphQL Fetch error: ", error)
  }
}

All of them are working in my local system. But, in the deployed live version, it is not working. I am getting the following logs in the deployed version.

response logs – 1

response logs – 2

I am getting a 201 response code for all three of them, even though I am getting a 200 response code in local.

Why is this happening? What am I doing wrong? Please help.

Also, please let me know if there is a more efficient way to solve this problem.

New contributor

Achu Anil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

  • Probably a CORS problem. Your JavaScript is now allowed to read responses from this server. Read up on CORS, and be aware: you cannot fix this in the client, your server has to support this.

    – phry

    yesterday

  • Please do not upload images of code/data/errors.

    – Heretic Monkey

    1 hour ago


Load 3 more related questions


Show fewer related questions

0

Leave a Reply

Your email address will not be published. Required fields are marked *