Home > Net >  Converting GraphQL mutation to Google Apps Scripts
Converting GraphQL mutation to Google Apps Scripts

Time:08-25

I am looking to convert this specific GraphQL code snippet to GAS.

mutation {
  createReportExport(input: {
    reportId: "XXXX",
    fileContentType: CSV,
    frequency: ONCE,
    reportFilters: [
      {
        attributeName: "Sale Date",
        relativeDateQuery: {
          greaterEqual: "P14D"
        }
      }
    ]
  }) {
    reportExport {
      id
      fileUrl
    }
  }
}

Below is what I have tried in GAS

  var query = 'mutation {createReportExport(input: {reportId: "urn:abc:Report:3318979a-7628-44ab-aa0d-a822f856b908",fileContentType: CSV,frequency: ONCE,reportFilters: [{attributeName: "Sale Date",relativeDateQuery: {greaterEqual: "P10D"}}]}) {reportExport {idfileUrl}}}'
  
 var query2 = {
  'operationName': 'Mutation',
  'query': {query},
  'variables': {}
  }

  var ql = '{insert URL}';
  var content = {
    "method": 'POST', 
    "headers": {"Authorization": httpBasicHeader,
    "contentType": "application/json"},
    "payload": JSON.stringify(query2)
    };
  
  var response = UrlFetchApp.fetch(ql, content);
  var data = (response.getContentText());
  Logger.log(data);

I have two variables, 'query' and 'query2' that I have tried. I am getting a {"errors":[{"message":"No query document supplied"}]} error message when running it in GAS.

When I run the first code snippet in another environment, it runs successfully. I am looking to keep my project within GAS if possible, since I have figured out the rest of the problems with my project and this is the last thing holding me back.

CodePudding user response:

I think, the query property in query2 should not be wrapped in an object:

var query2 = {
  'operationName': 'Mutation',
  // no { } here:
  'query': query,
  'variables': {}
  }
  • Related