【发布时间】:2021-08-05 17:12:10
【问题描述】:
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": "santa@example.com",
"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);
}
上面的脚本是 GraphQL 中的一个示例突变请求。具体来说,这是与 Wave Accounting API 交互以为特定业务创建客户,令牌和业务 ID 出于显而易见的原因被审查,客户只是一个占位符。
这个脚本可以在 Wave API Playground 上完美运行,甚至可以在 Apollo 等第三方平台上运行。但是,当插入 Google App Script 时,我收到错误:
POST 正文丢失。你忘记使用 body-parser 中间件了吗?
如何将 body-parser 中间件集成到我的脚本中?我认为它只在 G.A.S. 上不起作用。因为我提到的其他两个服务具有可以与 Wave API 服务器交互的 body-parser 中间件。如果不是这样,我怎么能在 G.A.S. 中提出变异请求?
查询请求运行良好,唯一的问题是突变。
【问题讨论】:
标签: javascript google-apps-script graphql http-post graphql-mutation