2

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.

Linus
  • 103
  • 1
  • 9
  • Welcome to Stack Overflow. This is a good first question - way better than average- but for future reference it's really useful if you paste the relevant parts of your code into your question rather than using images of it. It means that people searching for the same error you had can find your question and that if someone wants to reproduce your problem they don't have to type it out again. – glenatron Apr 01 '22 at 15:10
  • 1
    @glenatron Hey Glenatron! Thank you for your input, i updated the question with some code instead of images. – Linus Apr 01 '22 at 15:23
  • That's great! This looks a lot like an http headers problem - are you sending an `accept` header? ( relevant question: https://stackoverflow.com/questions/43209924/rest-api-use-the-accept-application-json-http-header ) I would start by getting it working in Postman and then try and reproduce it in your code. Once it works somewhere you have a frame of reference. I have even used a packet sniffer like Fiddler to intercept calls from my code so I can try them again from Postman. Http can be very sensitive to headers, line breaks and encoding. – glenatron Apr 01 '22 at 15:33
  • Yeah, that's what i've been trying to do when it didn't work on the client side.. I added manually in postman on Headers: key Accept, value application/json. Still.. same issue. – Linus Apr 01 '22 at 15:47
  • 1
    @glenatron posted an images of the headers in postman – Linus Apr 01 '22 at 15:48
  • Is it having a problem with the stringification of `"query"`? What happens if you take off the quotes to get `{ query: { GetAllUsers: username }}` ? – glenatron Apr 01 '22 at 15:50
  • Interesting.. this works: query { GetAllUsers { username } }, but complaining of course because it not a plain json-object, but works. But that seems to be the same type of string on the clientside and works on client-side too IF i don't use JSON.stringify(). Confused why this works, when its not a a "real" JSON-object. – Linus Apr 01 '22 at 16:06
  • I would look at your endpoint- do you need to do something to parse the json on that side? That would be my first guess from where you are now. I'm done for the week, but I think you're barking up the right tree here. Good luck. – glenatron Apr 01 '22 at 16:10

0 Answers0