Zennichimaro
Zennichimaro

Reputation: 5306

pass variable to `mutation` apollographql server

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

Upvotes: 2

Views: 385

Answers (1)

Jenny Kim
Jenny Kim

Reputation: 1565

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

Upvotes: 2

Related Questions