'How to execute a mutation for Graphql in a Java Client Code?
This is my schema
type Mutation{
createUser(username: String!, email: String!, tempPassword: String!): ComplexCallResult
@aws_iam
}
I am using AmazoneWebServicesClient to execute queries. For query which is already defined in the schema, I gave input in the post request body as below
private String queryAllUsers = "{\n" +
" \"query\": \"{listUsers {name}}\",\n" +
" \"variables\": null,\n" +
" \"operationsName\": null\n" +
"}";
Now I want to run mutation similar to above. My input query was
{ "mutation":"{createUser(email: "[email protected]", tempPassword: "LPwd!", username: "testUser1") { Exception Success}}" }
In Java code:
private String testCreateUserInputQuery= "{\n" +
" \"mutation\":\"{createUser(email: \\\"[email protected]\\\", tempPassword: \\\"LPwd!\\\", username: \\\"testUser10\\\") { Exception Success}}\"\n" +
"}";
But this is not working. Any leads please
Edit: I am updating what I have done here:
This is my latest try which is not working.
private String testCreateUserInputQuery= "{\n" +
" \"query\":\"{mutation($username:String!,$email:String!,$tempPassword:String!) {createUser(username:$username,email:$email,tempPassword:$tempPassword) { Exception Success }}}\", \"variables\":{\"input\":{\"username\":\"testUser11\",\"email\":\"[email protected]\",\"tempPassword\":\"L123!\" } }\n" +
" }";
Tried like this too:
private String testCreateUserInputQuery= "{\n" +
" \"query\":\"{mutation createUser($username:String!,$email:String!,$tempPassword:String!) {createUser(username:$username,email:$email,tempPassword:$tempPassword) { Exception Success }}}\", \"variables\":{\"input\":{\"username\":\"testUser11\",\"email\":\"[email protected]\",\"tempPassword\":\"L123!\" } }\n" +
" }";
I am getting exception as below
{"errors":[{"message":"Unable to parse GraphQL query.","errorType":"MalformedHttpRequestException"}]} (Service: appsync; Status Code: 400; Error Code: MalformedHttpRequestException; Request ID: af42c6eb-dd5f-401e-9cac-530e9c62df71; Proxy: null)
Not able to find what is the mistake and no clue. I am exhausted with my search.
Not able to use Postman or Insomnia to try this API as it is my organization's work and the API is secured by AWS IAM. I am yet to be given credentials to test this in the Postman or Insomnia. I can test it only through code.
Solution 1:[1]
I had to struggle unnecessarily to do this operation. Insomnia tool is automatically converting the GraphQL query to JSON query. Copy and paste or write your GraphQL query, keeping the input mode format as GraphQL. After you are done with that, select JSON. Your GraphQL query will be converted automatically to the JSON query format.
Thanks to the help provided in the video: https://youtu.be/AaD03fv6q-o. Just for the people who need help as I got.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
