0

I have deployed a Mutation in a MicroProfile container (WildFly) and I'm able to execute it in the GraphQL UI:

mutation Add {
  createOrder(order: {
      id: 1,
      type: "car"
      model: "Ferrari"
      price: 500
    }
  )
  {
    type
    model
    price
  }
}

However, when I try to execute the same mutation using cURL:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{ "query": "mutation Add {
  createOrder(order: {
      id: 1,
      type: \"car\"
      model: \"Ferrari\"
      price: 500
    }
  )
  {
    type
    model
    price
  }
}
" }' http://localhost:8080/demo/graphql

However it fails with:

javax.json.stream.JsonParsingException: Unexpected char 10 at (line no=1, column no=27, offset=26)

I haven't found many examples of running Mutation with command line tools like cURL and don't know where the error is. Any help? Thanks

Carla
  • 3,064
  • 8
  • 36
  • 65

2 Answers2

1

You are sending json data through curl, and Json specs do not allow line breaks. They need to be replaced by a '\n' character.

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{ "query": "mutation Add { createOrder(order: {id: 1, type: \"car\", model: \"Ferrari\", price: 500}){type\n model\n price\n}}" }' http://localhost:8080/demo/graphql

See this question for other suggestions: Correct format for multiline '--data' in curl query to graphql endpoint?

AllirionX
  • 1,073
  • 6
  • 13
0

Here I have a working mutation using curl, newline quote escape and indention. It sure doesn't look fancy but somewhat more maintainable than a oneliner: https://stackoverflow.com/a/65837497/835098

I've reformatted your curl invocation and it should work now. Notice the input and output parameters nicely indented for your mutation. You only have to worry about adding a backslash at the end of each line and escaping the backslashes and quotes in the "query" string.

Oh, and don't forget to add commas after every input argument. I think those were missing in your original request.

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data \
"{ \
    \"query\": \"mutation Add { \
        createOrder(order: { \
            id: 1, \
            type: \\\"car\\\", \
            model: \\\"Ferrari\\\", \
            price: 500 \
        }){ \
            type \
            model \
            price \
        } \
    } \
} \
" http://localhost:8080/demo/graphql
Konrad Kleine
  • 4,275
  • 3
  • 27
  • 35