1

I have endpoint where my request works when it is:

query {
    getItem(dictionaryType: "test1") {
        code
        name
        description
    }
}

It works fine, see:

enter image description here

I want to test variable - so i want to change it to sth like:

query {
    getItem($dictionaryType: String) {
        code
        name
        description
    }
}
variables {
    dictionaryType: "test1"
}

I do not want to use any other tool than postman, or i would rather not use other format than text. When executing the second ouput i get following error:

"errors": [
    {
        "message": "Invalid Syntax",
        "locations": [
            {
                "line": 2,
                "column": 9,
                "sourceName": null
            }
        ],

How to fix the syntax of the request?

enter image description here

Edit: I have even problem with request with syntax like here: https://stackoverflow.com/a/50043390/4983983 query { getDataTypes } To translate it to json For example: {"query": "{getDataTypes}"} Does not work and gives JSON parse error: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofjava.lang.Stringout of START_OBJECT token\n at [Source: (PushbackInputStream error.

See: enter image description here enter image description here

Currently the code for Posts endpoints looks like:

@PostMapping("graphql")
public ResponseEntity<Object> getResource(@RequestBody String query) {
    ExecutionResult result = graphQL.execute(query);
    return new ResponseEntity<Object>(result, HttpStatus.OK);
}

If i change it to:

@PostMapping("graphql")
public ResponseEntity<Object> getResource(@RequestBody Object query) { // String query
    ExecutionResult result;
    if (query instanceof String) {
        result = graphQL.execute(query.toString());
    } else{
        Map b = (HashMap) query;
        result = graphQL.execute(b.get("query").toString());
    }
    return new ResponseEntity<Object>(result, HttpStatus.OK);
}

Now it seems only json versions works. Cause when i use text i get:

"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/graphql"

Is there other config option for that matter?? I don't know if the variables will be well handled in the last example.

tryingHard
  • 1,794
  • 4
  • 35
  • 74
  • You need to send a proper JSON request, something like `{"query": "...", "variables": "..."}` – DavidG Jan 21 '19 at 09:57
  • Isn't the syntax for variable replacement something like `{{dictionaryType}}` ? – Arnaud Jan 21 '19 at 09:57
  • Why not text? For other request it works. – tryingHard Jan 21 '19 at 09:58
  • @Arnaud see here: https://graphql.org/learn/queries/#variables – tryingHard Jan 21 '19 at 09:58
  • Note that example I give - the `...` in the query will contain the actual query. – DavidG Jan 21 '19 at 09:59
  • Yeah - i understood that. But why if other request as `plain text` works - now when i add `variables` it stop to work? It should work - but my syntax is probably wrong. – tryingHard Jan 21 '19 at 10:00
  • Because the variables request needs to be able to be split out from the main query, you can't just tag it on to the end. – DavidG Jan 21 '19 at 10:01
  • So there is not possible for me to do a `POST` as text? Could you post an `answer` providing how this `json` should look like? I tried it myself - but i did not work. I updated the question with image - to show you - that request before works with plain text. – tryingHard Jan 21 '19 at 10:02
  • I have even problem with request like ```query { getDataTypes }``` To translate it to `json` For example: ```{"query": "getDataTypes"}``` Does not work and gives ```JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token\n at [Source: (PushbackInputStream``` error. – tryingHard Jan 21 '19 at 10:06
  • Possible duplicate of [How to send graphql query by postman?](https://stackoverflow.com/questions/42520663/how-to-send-graphql-query-by-postman) – DavidG Jan 21 '19 at 10:07
  • This is other issue - cause i get an exception when trying to get it to `json` and i ask for plain text answer. I edited the question. – tryingHard Jan 21 '19 at 10:09
  • What content type did you send with the request? – DavidG Jan 21 '19 at 10:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187042/discussion-between-yami-and-davidg). – tryingHard Jan 21 '19 at 10:19
  • I found the reason and updated the question - how can i configure it nicely ? – tryingHard Jan 21 '19 at 13:49

2 Answers2

3

You need to update postman version to 7.2. Postman now supports graphql requests.

https://blog.getpostman.com/2019/06/18/postman-v7-2-supports-graphql/

refer to Image below.

enter image description here

Naveen
  • 360
  • 1
  • 8
  • 23
1

You can try with similar to below sample

{ "Query":"query interview($id:ID!){ interview(interviewGuid: $id) { interviewGuid interviewCode interviewTitle interviewOwnerGuid interviewStartDate interviewEndDate}}", "Variables":{"id":"725000ae-ba39-45a5-a10f-79e5b27fe747"} }

enter image description here