Example: Creating and Deleting Authorizations (GraphQL)

The sample code below creates and optionally deletes an authorization for any user and any app in your Hub. The authorization can be created in an app in any team or personal account context.

๐Ÿ“˜

Primarily GraphQL

This example primarily uses the GraphQL Platform API, though it also uses the REST Platform API.

This code uses a combination of the GraphQL and REST Platform APIs. Because it uses on-behalf-of functionality of the REST Platform API, you must configure the config.json object with a X-RapidAPI-Key from a team that has enterprise access to the platform API.

The following field in the config.json file can be configured:

  • create_auth_app_id - Set this to the app id (project id) of any team or personal account app that the user has access to.
  • delete_auth_after_creation - Set to true or false. If set to true, the authorization that is created will be immediately deleted and you will see a deleted: true field in the results.

Place the createAuthAndOptionallyDeleteGQL.js file in the same directory as config.json. The admin_user_id must be the user ID of a user with enterprise access to the Platform API.

You can set the reportStatsToConsole variable to true to see more details on the type and duration of the API calls made.

const reportStatsToConsole = false;
let totalRestAPICalls = 0;
let totalTimeOnRestCalls = 0;
let totalGQLAPICalls = 0;
const axios = require("axios").default;

let optionsCommonRest;
let optionsCommonGql;

let userKeyCache = {};

let resultsLog = []; //used to log messages and a subset of the data obtained

// set specific user email address, Hub urls, key, category and preferences in config.json
let config = require("./config.json");

optionsCommonRest = {
  headers: {
    "x-rapidapi-host": config.rapidapi_host_rest,
    "x-rapidapi-key": config.key,
  },
};

optionsCommonGql = {
  method: "POST",
  url: config.base_url_gql,
  headers: {
    "x-rapidapi-host": config.rapidapi_host_gql,
    "x-rapidapi-key": config.key,
    "content-type": "application/json",
  },
};

runAll();

async function runAll() {
  console.log("working...");
  if (reportStatsToConsole) console.time("total execution time");
  if (reportStatsToConsole) console.time("getUserByEmail execution time");
  let results = {};
  results.aUser = await getUserByEmail(config.useremail, config.admin_user_id);
  if (results.aUser) {
    results.aUser.email = config.useremail;
    if (reportStatsToConsole) console.timeEnd("getUserByEmail execution time");
    if (reportStatsToConsole) console.log("userid: " + results.aUser.id);

    if (reportStatsToConsole) console.time("createAuth execution time");
    results = await createAuth(config.create_auth_app_id, results);
    if (reportStatsToConsole) console.timeEnd("createAuth execution time");

    if (config.delete_auth_after_creation && results.newAuth?.id) {
      results = await deleteAuth(results.newAuth.id, results);
    }

    logResults(results);
    if (reportStatsToConsole) console.timeEnd("total execution time");
    if (reportStatsToConsole)
      console.log(
        `total time on REST calls: ${Math.round(totalTimeOnRestCalls) / 1000}s`
      );
    if (reportStatsToConsole)
      console.log(`total number of REST calls: ${totalRestAPICalls}`);
    if (reportStatsToConsole)
      console.log(`total number GQL calls: ${totalGQLAPICalls}`);
  } else {
    resultsLog.push(`User ${config.useremail} not found!`);
    displayResultsLog();
  }
}

async function getUserByEmail(email, adminUserId) {
  totalGQLAPICalls++;
  const optionsUnique = {
    data: {
      query: `query Users($where: UserWhereInput!) {
        users(where: $where) {
            id
            username
            name
            email
          }
      }`,
      variables: {
        where: {
          email: email,
        },
      },
    },
  };
  const optionsCommonClone = JSON.parse(JSON.stringify(optionsCommonGql)); // deep clone
  let options = { ...optionsCommonClone, ...optionsUnique };
  options = await transformOptionsForOnBehalfOf(options, adminUserId, "user");

  let response = await axios.request(options);
  return response?.data?.data?.users[0];
}

async function createAuth(app, results) {
  const optionsUnique = {
    data: {
      query: `mutation CreateApplicationAuthorization($input: AppAuthorizationCreateInput!) {
        createApplicationAuthorization(input: $input) {
          id
          name
          key
          applicationId
          status
          createdAt
          authorizationType
          authorizationValues
        }
      }`,
      variables: {
        input: {
          projectId: app,
          name: "auth_" + new Date().toLocaleString().replace(",", ""),
          authorizationType: "RAPIDAPI",
        },
      },
    },
  };
  const optionsCommonClone = JSON.parse(JSON.stringify(optionsCommonGql)); // deep clone
  let options = { ...optionsCommonClone, ...optionsUnique };
  options = await transformOptionsForOnBehalfOf(
    options,
    results.aUser.id,
    "user"
  );

  totalGQLAPICalls++;
  let response = await axios.request(options);
  results.newAuth = response?.data?.data?.createApplicationAuthorization;
  return results;
}

async function deleteAuth(auth, results) {
  const optionsUnique = {
    data: {
      query: `mutation deleteApplicationAuthorization($id: ID!) {
        deleteApplicationAuthorization(id: $id)
      }`,
      variables: {
        id: auth,
      },
    },
  };
  const optionsCommonClone = JSON.parse(JSON.stringify(optionsCommonGql)); // deep clone
  let options = { ...optionsCommonClone, ...optionsUnique };
  options = await transformOptionsForOnBehalfOf(
    options,
    results.aUser.id,
    "user"
  );

  totalGQLAPICalls++;
  let response = await axios.request(options);
  if (response?.data?.data?.deleteApplicationAuthorization) {
    results.newAuth.deleted = true;
  }
  return results;
}

async function getApps(entityID) {
  const start = performance.now();
  const optionsUnique = {
    method: "GET",
    url: `${config.base_url_rest}apps`,
  };
  const optionsCommonClone = JSON.parse(JSON.stringify(optionsCommonRest)); // deep clone
  const options = { ...optionsCommonClone, ...optionsUnique };
  options.headers["on-behalf-of"] = entityID;
  totalRestAPICalls++;
  let response = await axios.request(options);
  const end = performance.now();
  totalTimeOnRestCalls = totalTimeOnRestCalls + end - start;
  return response.data;
}

async function getAppKeys(entityID, projectID) {
  const start = performance.now();
  const optionsUnique = {
    method: "GET",
    url: `${config.base_url_rest}apps/${projectID}/keys`,
  };
  const optionsCommonClone = JSON.parse(JSON.stringify(optionsCommonRest)); // deep clone
  const options = { ...optionsCommonClone, ...optionsUnique };
  options.headers["on-behalf-of"] = entityID;
  totalRestAPICalls++;
  let response = await axios.request(options);
  // return only active production keys
  const validKeys = [];
  response.data.environments.forEach((env) => {
    if (
      env.applicationEnvironmentName === "Production" &&
      env.status === "ACTIVE"
    )
      validKeys.push(env);
  });
  const end = performance.now();
  totalTimeOnRestCalls = totalTimeOnRestCalls + end - start;
  return validKeys;
}

async function getAPersonalKeyForUser(userId) {
  if (userKeyCache[userId]) {
    return userKeyCache[userId];
  }
  let personalApps = await getApps(userId);
  let keys = await getAppKeys(userId, personalApps[0].projectId);
  let key;
  keys.forEach((env) => {
    if (
      env.applicationEnvironmentName === "Production" &&
      env.status === "ACTIVE"
    )
      if (!key) {
        key = env.key; //return first valid key
        userKeyCache[userId] = key;
      }
  });
  return key || "no key found";
}

async function transformOptionsForOnBehalfOf(options, entityID, entityType) {
  let results = {};
  if (entityType === "user") {
    options.headers["x-rapidapi-identity-key"] = await getAPersonalKeyForUser(
      entityID
    );
  }
  return options;
}

function logResults(results) {
  resultsLog.push(`Userid for ${results.aUser.email} is ${results.aUser.id}`);
  if (!results.newAuth) {
    resultsLog.push(
      `Authorization not created. Is the config.create_auth_app_id valid and accessible to the user?`
    );
  } else {
    resultsLog.push(`Authorization created:`);
    resultsLog.push(JSON.stringify(results.newAuth));
  }

  displayResultsLog();
}

function displayResultsLog() {
  resultsLog.forEach((entry) => console.log(entry));
}
{
    "rapidapi_host_rest": "[HOST FROM REST SAMPLE CODE].rapidapi.com",
    "base_url_rest": "https://[URL FROM REST SAMPLE CODE].rapidapi.com/v1/",
    "rapidapi_host_gql": "[HOST FROM GQL SAMPLE CODE].rapidapi.com",
    "base_url_gql": "[URL FROM REST SAMPLE CODE].rapidapi.com/",
    "key": "[KEY FROM TEAM WITH ENTERPRISE PLATFORM API ACCESS]",
    "useremail": "[email protected]",
    "admin_user_id": "5713300",
    "create_auth_app_id": "4912903",
    "delete_auth_after_creation": true
}