1 year ago

#364108

test-img

Linus

GraphQL JSON-Http request towards Spring Boot parse failure why?

So, I'm using GraphQL in Java (Spring Boot) and running with Datafetchers. I'm trying to do a fetch with JSON but it seems like i never get it right. I can do a fetch in the client with Content-type: text/plain but not with 'application/json'.

WHY i use Java is because I'm doing school work based on Java and curious about graphql.

My query which works in postman and client with text/plain and been trying to add 'query' before 'GetAllUsers' and so on..

Query:

This is the request:

(I've been trying with axios also, with having no query type in body and just the query itself.. it runs the same error)

const PostData = () => {
  fetch('http://localhost:8080/api/allusersdata', {
    method: 'POST',
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(testquery),
  })
    .then(res => {
      return res.json();
    })
    .then(data => console.log(data));

I get this error,

 notprivacysafe.graphql.GraphQL           : Query failed to parse : '{"query":"\n{\n    GetAllUsers\n         { \n             username \n             } \n}\n"}'
ExecutionResultImpl{errors=[InvalidSyntaxError{ message=Invalid Syntax : offending token '"query"' at line 1 column 2 ,offendingToken="query" ,locations=[SourceLocation{line=1, column=2}] ,sourcePreview={"query":"\n{\n    GetAllUsers\n         { \n             username \n             } \n}\n"}
}], data=null, dataPresent=false, extensions=null}

This is the endpoint:

@PostMapping("/allusersdata")
    public ResponseEntity<Object> allusers(@RequestBody String query) {
        try {
            ExecutionResult execute = graphQLService.getGraphQL().execute(query);
            System.out.println(query);
            return new ResponseEntity<>(execute,HttpStatus.OK);
        } catch(Exception e) {
            System.out.print(e);
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

POSTMAN-request enter image description here

Headers in postman enter image description here

What do I miss?

Thank you in advance!

Different queries, different fetching-techniques.

javascript

java

spring

graphql

graphql-java

0 Answers

Your Answer

Accepted video resources