Apollo + Angular mutation is not working with variables

Viewed 751

I'm having troubles creating mutations with variables. I can make it work including the values directly, like this:

export const orderCancellation = gql`
mutation{
  cancelOrder(input: {id: 2649,  cancellationReasonId: 1 } ) {
    order{
      id,
      isCancelled
    }
  }
}`;

But when try to use variables nothing works:

export const orderCancellation = gql`
mutation cancelOrder($id: ID!, $cancellationReasonId: ID!) {
  cancelOrder(input: {id: $id,  cancellationReasonId: $cancellationReasonId }) {
    order {
      id,
      isCancelled
    }
  }
}`;

The custom error message from our server:

Didn't find any result for  'CancelOrderPayload' with the arguments '{:input=>#<#<Class:0x00005608dca67e90>:0x00005608e10cd790 @context=#<Query::Context ...>, @ruby_style_hash={:id=>nil, :cancellation_reason_id=>nil}, @arguments=#<GraphQL::Execution::Interpreter::Arguments @keyword_arguments={:id=>nil, :cancellation_reason_id=>nil}>, @maybe_lazies=[]>}.

And this is how I call the mutation:

this.apollo.mutate({
  mutation: orderCancellation,
  variables: {id: orderId, cancellationReasonId: reasonId}
});

Does any one has clue on what i'm doing wrong? Maybe it's a server side problem?

EDIT 1: Here is the request payload with variables implementation (took from Network tab on Chrome DevTools):

operationName: "cancelOrder"
query: "mutation cancelOrder($id: ID!, $cancellationReasonId: ID!) {↵  cancelOrder(input: {id: $id, cancellationReasonId: $cancellationReasonId}) {↵    order {↵      id↵      isCancelled↵      __typename↵    }↵    __typename↵  }↵}↵"
variables: {id: "3045", cancellationReasonId: "1"}

EDIT 2: Already tried to declare the mutation like this:

export const orderCancellation = gql`
mutation cancelOrder($input: CancelOrderInput!) {
  cancelOrder(input: $input) {
    order {
      id,
      isCancelled
    }
  }
}`;

this.apollo.mutate({
     mutation: orderCancellation,
     variables: {input: {id: orderId, cancellationReasonId: reasonId}}
  });
1 Answers

We figure it out what was the problem. Our server has declared the IDs for mutations as Integer!, instead of ID!. This is the cause of string ids was working with queries but not with mutations. Thanks for your effort!

Related