2

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

4
  • 1
    you can test mutation in graphiql using 'query variables' .... prepare entire input in js and pass as variable Commented Dec 22, 2020 at 17:48
  • thanks! but how do I declare the input param? ($input: JSON!) doesn't seem to work.. same 400 error ? Commented Dec 23, 2020 at 1:16
  • 1
    read API specs, must match BE declaration Commented Dec 23, 2020 at 1:40
  • 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 Commented Dec 23, 2020 at 13:14

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

if in API input id is type of ID! then variable for this id must be declared as $id: ID!, not $id: ID
@xadm do you know why it has to be $id: ID! and not $addressId: ID!, it is so weird, that should be just parameters name but it causes it to break
case? ... it should work as $startedNameVariableCanBeAnything is just name of variable, dynamic part, just defines dictionary element, 'consumed' at parsing step (shopify specific requirement?) ... input name (left part of someInputId: $someVariableId) must match (API defined input name) on 'consuming part', in query 'body'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.