Search code examples
graphql-jsweb-api-testingrest-assured-jsonpath

Escape character formatting in graphql query


need help in formatting a graphql mutation in java code. In postman it looks like this->.postman and what I have tried is-> my code and the error I am getting is--> error I am getting. Thanks in advance :-)


Solution

  • Escaping will be language dependent and so I have provided a few examples below based on the sample you provided.

    Postman provides a code snippet that exports in various formats

    cURL

    curl --location --request GET '' \
    --header 'Content-Type: application/json' \
    --data-raw '{"query":"mutation{\n    Create(\n        request: \"Xboxx\",\n        GoalAmount: 123,\n        GoalTargetData: \"15/09/2021\",\n        ImagerRef:\"etst ref\"\n    ){\n        Id\n        Name\n        GoalAmount\n        GoalTargetDate\n        ImageRef\n    }\n}","variables":{}}'
    

    java

    Unirest.setTimeouts(0, 0);
    HttpResponse<String> response = Unirest.get("")
      .header("Content-Type", "application/json")
      .body("{\"query\":\"mutation{\\n    Create(\\n        request: \\\"Xboxx\\\",\\n        GoalAmount: 123,\\n        GoalTargetData: \\\"15/09/2021\\\",\\n        ImagerRef:\\\"etst ref\\\"\\n    ){\\n        Id\\n        Name\\n        GoalAmount\\n        GoalTargetDate\\n        ImageRef\\n    }\\n}\",\"variables\":{}}")
      .asString();
    
    

    javascript

    //javascript - fetch
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");
    
    var graphql = JSON.stringify({
      query: "mutation{\n    Create(\n        request: \"Xboxx\",\n        GoalAmount: 123,\n        GoalTargetData: \"15/09/2021\",\n        ImagerRef:\"etst ref\"\n    ){\n        Id\n        Name\n        GoalAmount\n        GoalTargetDate\n        ImageRef\n    }\n}",
      variables: {}
    })
    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      body: graphql,
      redirect: 'follow'
    };
    
    fetch("", requestOptions)
      .then(response => response.text())
      .then(result => console.log(result))
      .catch(error => console.log('error', error));
    

    Go

    package main
    
    import (
      "fmt"
      "strings"
      "net/http"
      "io/ioutil"
    )
    
    func main() {
    
      url := ""
      method := "GET"
    
      payload := strings.NewReader("{\"query\":\"mutation{\\n    Create(\\n        request: \\\"Xboxx\\\",\\n        GoalAmount: 123,\\n        GoalTargetData: \\\"15/09/2021\\\",\\n        ImagerRef:\\\"etst ref\\\"\\n    ){\\n        Id\\n        Name\\n        GoalAmount\\n        GoalTargetDate\\n        ImageRef\\n    }\\n}\",\"variables\":{}}")
    
      client := &http.Client {
      }
      req, err := http.NewRequest(method, url, payload)
    
      if err != nil {
        fmt.Println(err)
        return
      }
      req.Header.Add("Content-Type", "application/json")
    
      res, err := client.Do(req)
      if err != nil {
        fmt.Println(err)
        return
      }
      defer res.Body.Close()
    
      body, err := ioutil.ReadAll(res.Body)
      if err != nil {
        fmt.Println(err)
        return
      }
      fmt.Println(string(body))
    }