Executing a Mutation with GraphQL in Google App Script

774 Views Asked by At
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.

1

There are 1 best solutions below

0
On

In your script, 'contentType' : 'application/json' and 'muteHttpExceptions' : true are used in the request header. And, at 'payload' : data, the object of data is not converted to the string. When I saw the official document of GraphQL API, it seems that the data is sent as application/json. Ref I thought that the reason of your error of POST body missing. Did you forget use body-parser middleware? might be due to this.

From this situation, how about the following modification?

From:

  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
  }

To:

var headers = {'authorization': token}
var data = {
  "operationName": "Mutation",
  "query": query,
  "variables": variables
}
var options = {
  'method': 'POST',
  'headers': headers,
  'payload': JSON.stringify(data),
  'muteHttpExceptions': true,
  'contentType': 'application/json',
}

Note:

  • If you want to set query as follows,

      mutation Mutation($input: CustomerCreateInput!) {
        customerCreate(input: $input) {
          didSucceed
        }
      }
    
  • Please modify as follows.

    • From

          var query = 
         "mutation Mutation($input: CustomerCreateInput!) {\
          customerCreate(input: $input) {\
            didSucceed\
          }\
        }";
      
    • To

          var query = `mutation Mutation($input: CustomerCreateInput!) {
          customerCreate(input: $input) {
            didSucceed
          }
        }`;
      
    • or

        var query = "mutation Mutation($input: CustomerCreateInput!) {\n  customerCreate(input: $input) {\n    didSucceed\n  }\n}";
      

Note:

  • In this modification, it supposes that your request body (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: