function Run_Mutation(){
var token = "Bearer [Censored]"
var Business_ID = "[Censored]"
var url = "https://gql.waveapps.com/graphql/public";
var query =
"mutation Mutation($input: CustomerCreateInput!) {\
customerCreate(input: $input) {\
didSucceed\
}\
}";
var variables = {
"input": {
"businessId": Business_ID,
"name": "Santa",
"firstName": "Saint",
"lastName": "Nicholas",
"email": "[email protected]",
"address": {
"city": "North Pole",
"postalCode": "H0H 0H0",
"provinceCode": "CA-NU",
"countryCode": "CA"
},
"currency": "CAD"
}
};
var headers = {
'muteHttpExceptions' : true,
'contentType' : 'application/json',
'authorization' : token
}
var data = {
"operationName": "Mutation",
"query" : query,
"variables" : variables
}
var options = {
'method' : 'POST',
'headers' : headers,
'payload' : data
}
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
The script above is an example mutation request in GraphQL. Specifically this is interacting with the Wave Accounting API to create a customer for a specific business, the Token and Business ID are censored for obvious reasons, and the customer is just a placeholder.
This script functions perfectly on the Wave API Playground, and even on a third party platform like Apollo. However, when plugged into Google App Script, I get the error:
POST body missing. Did you forget use body-parser middleware?
How would I be able to integrate body-parser middleware into my script? I assume that it only doesn't work on G.A.S. because the other two services I mentioned have body-parser middleware that can interact with the Wave API server. And if not that, how would I be able to make a mutation request in G.A.S.?
Query requests work perfectly fine, mutations are the only issue.
In your script,
'contentType' : 'application/json'
and'muteHttpExceptions' : true
are used in the request header. And, at'payload' : data
, the object ofdata
is not converted to the string. When I saw the official document of GraphQL API, it seems that the data is sent asapplication/json
. Ref I thought that the reason of your error ofPOST body missing. Did you forget use body-parser middleware?
might be due to this.From this situation, how about the following modification?
From:
To:
Note:
If you want to set
query
as follows,Please modify as follows.
From
To
or
Note:
data
) and your token are valid. Please be careful this. When an error occurs by above modification, can you confirm your request body and your token, again.References: