0

I managed to use similar code below for query GraphQL using Google App Script. However, it's not working when I changed to use on mutation (code below).

Error code is 400 with text: {"errors":[{"message":"GraphQL operations must contain a non-empty query or a persistedQuery extension.","extensions":{"code":"INTERNAL_SERVER_ERROR"}}]}

I just started to learn Google App Script & Javascript, not sure how to resolve this issue after search few hours on the net. Some help would be appreciate. Thanks in advance.

function get_auth_headers(){
  email = "test@mail.com"
  password ="test"

  var query = "mutation={ \
                userLogin(input:{email:\"" + email + "\",password:\"" + password + "\"}){ \
                  token \
                } \
              }";

  Logger.log(query)
  var url1 = "https://server.matters.news/graphql?";
  url = encodeURI(url1 + query)
  Logger.log(url)
  var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true });
  Logger.log("code: " + response.getResponseCode());
  Logger.log("text: " + response.getContentText());
  var response = UrlFetchApp.fetch(url,{method: 'GET', headers: {'Content-Type': 'application/json'}});
  var lists = JSON.parse((response.getContentText()));
  Logger.log(lists);
}
user8729759
  • 3
  • 1
  • 2

1 Answers1

0

Managed to get answer from https://speckle.guide/dev/js-app-script.html Refers code below for your reference if you have same issue.

function get_auth_headers() {
  //email = "test@gmail.com"
  //password ="test"

  let url = "https://server.matters.news/graphql";
  let graphql = JSON.stringify({
    query: `mutation($input: UserLoginInput!) {userLogin(input: $input) {token}}`,
    variables: {
      input: {
        email: email,
        password: password,
      },
    },
  });

  let params = {
    method: "POST",
    payload: graphql,
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer YOUR_PERSONAL_TOKEN",
    },
  };

  var response = UrlFetchApp.fetch(url, params);
  var lists = JSON.parse((response.getContentText()));
  //Logger.log(lists['data']['userLogin']['token']);
  return lists['data']['userLogin']['token'];
}
user8729759
  • 3
  • 1
  • 2
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 26 '22 at 19:57