Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Syntax Error: Expected Name, found String \"query\"" in GraphQL

I'm trying to test the GraphQL server I built, by sending GraphQL queries to the server using Postman.

It works when I'm using raw radio button, but when I'm trying to use GraphQL radio button, it returns "message": "Syntax Error: Expected Name, found String \"query\"".

I have tried to change the syntax: mainly add or delete curly braces but nothing happened.

The query I sent in raw mode (working):

{
    person(id:"123456789") {
        personal_info {
            address
        }
    }
} 

The query I sent in GraphQL mode:

QUERY:

query getPerson ($id: String){
    person(id: $id){
        personal_info {
            address
        }
    }
}

GRAPHQL VARIABLES:

{
    "id": "123456789"
}

I expect to get the data I asked for, but I get the error message:

{
    "errors": [
        {
            "message": "Syntax Error: Expected Name, found String \"query\"",
            "locations": [
                {
                    "line": 1,
                    "column": 2
                }
            ]
        }
    ]
}
like image 917
liadperetz Avatar asked Aug 27 '19 19:08

liadperetz


People also ask

Is there a structure to errors in GraphQL?

This makes one more involved in debugging errors, but thankfully there's a structure to the errors in GraphQL and we will look into it to see how to debug and fix them. Let's take a typical GraphQL query: The response of the above query can have the following objects: both.

What are the status codes for network errors in GraphQL?

When there is a network error while trying to contact a GraphQL server, due to either the server being down or timeouts etc, then the response will be a status code of 4xx or 5xx. If the server responds anything other than 200, the response is not successful due to either being a:

How do you know if a GraphQL query is correct?

Let's take a typical GraphQL query: The response of the above query can have the following objects: both. When the response contains the errors object along with the data object, it could mean a partially correct response for the request. Digging into the errors object will give a better idea into what part of the query went wrong.

What are GraphQL query and mutation names?

In the same way, GraphQL query and mutation names, along with fragment names, can be a useful debugging tool on the server side to identify different GraphQL requests. So far, we have been writing all of our arguments inside the query string.


1 Answers

I had the same problem. During researching I have found the next answer on stackoverflow, thanks @gbenga_ps.

Resolved by adding the correct header to Postman request: Adding header Content-Type: application/json

Body of request should be something like next:

{
    courses {
        title
    }
}

Request example

If incorrect content-type set, error like next happened:

{
    "errors": [
        {
            "message": "Syntax Error: Expected Name, found String \"query\"",
            "locations": [
                {
                    "line": 1,
                    "column": 2
                }
            ]
        }
    ]
}

Error response example

like image 91
Vaha Avatar answered Sep 25 '22 23:09

Vaha