2
votes

I am trying to pass variable to mutation through apollo's javascript client, however, it always returns 400 error: [Error: Response not successful: Received status code 400] which indicates there's something wrong with my query

below is my query:

const SET_ADDRESS_LASTUSED = gql`
  mutation UpdateAddress($phone: String!, $name: String!) {
    updateAddress(input: {
      where: {
        phone: $phone
      },
      data: {
        name: $name
      }
    }) {
      address {
        name
        phone
      }
    }
  }
`;

I am calling the mutation like this:

const [updateLastUsed, updateLastUsedResult] = useMutation(SET_ADDRESS_LASTUSED);

          // sometime later,
          updateLastUsed({
            variables: {
              phone: "+12345678910",
              name: "WhyICanNotChangeMyName?",
            },
          });

is there something wrong that I didn't notice? I'd be able to successfully mutate if I hardcode the variable like this, but of course, I can't do it this way

const SET_ADDRESS_LASTUSED = gql`
  mutation {
    updateAddress(input: {
      where: {
        phone: "+12345678910"
      },
      data: {
        name: "ICanChangeMyName!"
      }
    }) {
      address {
        name
        phone
      }
    }
  }
`;

Please help. I've been debugging pointlessly for hours... I can always pass a variable to query using a similar way :(

Btw, I am using Strapi Server

Thanks in advance!

Reference: https://www.apollographql.com/docs/react/api/react/hooks/ https://strapi.io/documentation/developer-docs/latest/plugins/graphql.html#graphql

1
you can test mutation in graphiql using 'query variables' .... prepare entire input in js and pass as variable - xadm
thanks! but how do I declare the input param? ($input: JSON!) doesn't seem to work.. same 400 error ? - Zennichimaro
read API specs, must match BE declaration - xadm
Hey @xadm thanks a lot for the guide!! Even if it isn't clear initially as I am new to this, your comment really help! I never think it will be updateAddressInput like stated in the SDL hahaha - Zennichimaro

1 Answers

2
votes

You need to make sure your Query (including mutation) is the same (has the same variables) as what is stated in the SDL

If you're using Strapi, there is a bug where the param of ID type has to be specified as $id cannot be something else, not sure how it gets like this but if you found 400 errors where it shouldn't be, try checking if there is any ID type declared not exactly as $id, hope it helps someone