Skip to content

Instantly share code, notes, and snippets.

@GiladShoham
Created December 20, 2020 11:06
Show Gist options
  • Save GiladShoham/a53405103e352865f23b4d34ea8a7a22 to your computer and use it in GitHub Desktop.
Save GiladShoham/a53405103e352865f23b4d34ea8a7a22 to your computer and use it in GitHub Desktop.
scope-creator
const { GraphQLClient, gql } = require('graphql-request');
const scopes = ['your-scopes'];
const uri = `https://symphony.bit.dev/graphql`;
const token = 'your-token';
const graphQLClient = new GraphQLClient(uri, {
headers: {
authorization: `Bearer ${token}`
}
});
const deleteScopeQuery = gql`
mutation deleteScope($scopeId: String!){
deleteScope(id: $scopeId)
}
`;
const createScopeQuery = gql`
mutation createScope($scope: String!, $visibility: Boolean!, $org: String!){
createScope(input:
{
scope: $scope,
visibility: $visibility,
org:$org
}){
id{owner,scopeName}
}
}
`;
function deleteScopes(scopes){
const promises = scopes.map(scope => deleteOneScope(scope));
return Promise.all(promises);
}
function createScopes(scopes){
const promises = scopes.map(scope => createOneScope(scope));
return Promise.all(promises);
}
function deleteOneScope(scope) {
const variables = { scopeId: scope };
return graphQLClient.request(deleteScopeQuery, variables);
}
function createOneScope(scope) {
const [org, scopeName] = scope.split('.');
const variables = {scope: scopeName, visibility:true, org};
return graphQLClient.request(createScopeQuery, variables);
}
async function deleteAndCreateOneScopeGracefully(scope) {
console.log('deleteAndCreate', scope);
try {
await deleteOneScope(scope);
} catch (err) {
console.log(`failed deleting ${scope}`, err)
}
try {
await createOneScope(scope);
return true;
} catch (err) {
console.log(`failed creating ${scope}`, err);
return false;
}
}
async function deleteAndCreateAllGracefully() {
const promises = scopes.map(scope => deleteAndCreateOneScopeGracefully(scope));
return Promise.all(promises);
}
// deleteScopes(scopes)
// .then(res => {
// console.log(res)
// }).catch(err => {
// console.log('got error')
// console.log(err)
// })
// return createScopes(scopes)
// .then(res => {
// console.log(res)
// }).catch(err => {
// console.log('got error')
// console.log(err)
// });
deleteAndCreateAllGracefully().then(results => {
console.log('results', results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment