javagraphqlgraphql-mutation

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: "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.


Solution

  • 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" + " }";