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.

8
  • 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. Commented Apr 1, 2022 at 15:10
  • 1
    @glenatron Hey Glenatron! Thank you for your input, i updated the question with some code instead of images. Commented Apr 1, 2022 at 15:23
  • That's great! This looks a lot like an http headers problem - are you sending an accept header? ( relevant question: stackoverflow.com/questions/43209924/… ) 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. Commented Apr 1, 2022 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. Commented Apr 1, 2022 at 15:47
  • 1
    @glenatron posted an images of the headers in postman Commented Apr 1, 2022 at 15:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.