0

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: "test@lb.de", tempPassword: "LPwd!", username: "testUser1") { Exception Success}}" }

In Java code:

  private String testCreateUserInputQuery= "{\n" +
            "    \"mutation\":\"{createUser(email: \\\"test@lb.de\\\", 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\":\"test@lb.de\",\"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\":\"test@lb.de\",\"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.

Manushi
  • 597
  • 1
  • 7
  • 26
  • use variables https://graphql.org/learn/queries/#variables ... test in playground ... look at request details, recreate in code ... use search before asking – xadm Jul 21 '21 at 12:06

2 Answers2

0
private String testCreateUserInputQuery=" {\n" +
            "    \"query\":\"mutation { createUser(username: \\\"testUser12\\\",email:\\\"test@lb.de\\\",tempPassword:\\\"tempPassword123!\\\") { Exception  Success }}\"\n" +
            "    }";

For query using variables, the following one works for me

private String testCreateUserInputQuery= "{\n" + " \"query\":\"mutation User($uname: String!,$mail:String!,$tempPwd:String!) { createUser(username: $uname,email:$mail,tempPassword:$tempPwd) { Exception Success }}\", \"variables\":{\"uname\":\"testUser20SK\",\"mail\":\"test@lb.de\",\"tempPwd\":\"LPassword123!\"} \n" + " }";
Manushi
  • 597
  • 1
  • 7
  • 26
  • follow 1st multiline example – xadm Jul 21 '21 at 15:47
  • private String testCreateUserInputQuery= "{\n" + " \"query\":\"mutation($uname:String!,$mail:String!,$tempPwd:String!) {createUser(username:$uname,email:$mail,tempPassword:$tempPwd) { Exception Success }}\", \"variables\":{\"input\":{\"$uname\":\"testUser15\",\"mail\":\"test@lB.de\",\"tempPwd\":\"LpASSWORD123!\" } }\n" + " }"; Getting error like variable uname has coerced null value for String!. I have passed value for that variable. – Manushi Jul 22 '21 at 07:33
  • no `input` 'level' required in variables as no 'input' variable – xadm Jul 22 '21 at 09:16
  • Do you pass the above mutation as a string or object. – Feezan Khattak Jan 25 '22 at 09:42
0

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.

Manushi
  • 597
  • 1
  • 7
  • 26