0

I've been having trouble sending GraphQL queries to my GraphQLJS backend, I copy and pasted the query that I use in GraphiQL (that works fine) but it's causing a syntax error: {"errors":[{"message":"Syntax Error: Expected :, found String \": \"","locations":[{"line":2,"column":45}]}]}

graphQLTest = async () => {
  var data = {
    query: `mutation {
      createAuctionOffering(features:"{\"year\": \"2002\", \"model\":{\"make\":\"honda\", \"selectedModel\":\"civic\"}}", start_time:"2019-03-15 19:05:45.109+00", duration:"2019-03-15 19:05:45.109+00"){
        auc_id
      }
    }`,
  }
  // data = await JSON.stringify(data)
  var token = jwt.sign({name:data}, 'LIdXNnmK2qJNyTGs456bR0iebf9eGZV7', {expiresIn: '10s'});
  let response = await fetch('http://localhost:8000/graphql', {
    method: 'post',
    headers: {
      "User": "charlesdsmith25@gmail.com",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({name:token})
  })
  let jsonResponse = await response.text()
  console.log(jsonResponse)

}

I've also been using JWT but that isn't an issue seeing as the query makes it through to the backend and raises the syntax error. This is the query I'm trying to send:

var data = {
    query: `mutation {
      createAuctionOffering(features:"{\"year\": \"2002\", \"model\":{\"make\":\"honda\", \"selectedModel\":\"civic\"}}", start_time:"2019-03-15 19:05:45.109+00", duration:"2019-03-15 19:05:45.109+00"){
        auc_id
      }
    }`,
  }

It seems like the problems lies with the quotes

Amon
  • 2,725
  • 5
  • 30
  • 52
  • 1
    test with empty `features` ... use `graphql-tag` ... use variables ... test first in postman ... test querying first then mutations ... look [here](https://stackoverflow.com/q/51935902/6124657) for some inspirations – xadm Jun 05 '19 at 20:46
  • hey sorry for the really late reply, I've been playing around with it a lot and still nothing. this is the current error: `{"errors":[{"message":"Unexpected token e in JSON at position 1","locations":[{"line":3,"column":7}],"path":["createAuctionOffering"]}],"data":{"createAuctionOffering":null}}` – Amon Jun 10 '19 at 16:00
  • it's expecting JSON for some reason? Like it's expecting the graphql query to be JSON, but if create a syntax error on purpose, like for example if I try to query a non-existent field I get the proper "cannot query that field" error or w.e. So it's interpreting it as graphql but still giving me errors as if it were JSON? – Amon Jun 10 '19 at 16:02
  • 1
    inspect headers and request details in network tab while working with graphiql – xadm Jun 10 '19 at 16:56
  • thanks for the help. I inspected graphiql and there doesnt seem to be anything that stands out. the content type is application/json which is what I have. there's other headers like `accept-encoding`. there's also a cookies object that is being passed too. But would that cause the problem? – Amon Jun 10 '19 at 17:19
  • 1
    did you try query/not mutations? are you using `gql`? update question, give more details – xadm Jun 10 '19 at 17:23
  • sorry I just tried queries, forgot to do that. And yes it does work, that's so odd. No im not using gql – Amon Jun 10 '19 at 18:57
  • I got it working by using variables – Amon Jun 26 '19 at 16:13

0 Answers0