0

I am using the fetch api to make a POST request sending a GraphQL

fetch( dbUrl, {
        method : 'post',
        body   : JSON.stringify( {
            graphql : "mutation M {saveUser(name:'Avraam')}"
        } )
    } )

On the request body I can see clearly:

enter image description here

On the server I am using restify and I have:

server.use( restify.bodyParser() );

and

import { GraphQLAnimationSchema } from '../schemas/GraphQLAnimationSchema';

const requestBuilder = query => graphql( GraphQLAnimationSchema, query )

...
...

export default {
    '/data' : {
        post : ( req, res ) => requestHandler( requestBuilder( req.body.graphql ), res )
};

The GraphQL response with {"errors":[{"message":"Syntax Error GraphQL request (1:1) Unexpected EOF\n\n1: \n ^\n"}]}

Should I use a specific kind of Headers, (I have tried to use various Headers) but it doesnt seem to solve the problem.

Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133

1 Answers1

3

Solved by using FormData

const data = new FormData()
data.append( 'graphql',  "mutation M {saveUser(name:'Avraam')}" )

fetch( dbUrl, {
        method  : 'post',
        body : data
    } )
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133