-
-
Save spences10/84278d8b9630d004acc88ec350284276 to your computer and use it in GitHub Desktop.
A utility Script for cleaning out tables in graphcool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const { GraphQLClient } = require('graphql-request'); | |
const client = new GraphQLClient(process.env.ENDPOINT, { headers: { | |
'Authorization': `Bearer ${process.env.ACCESS_TOKEN}` | |
}}); | |
const query = T => ` | |
query Get${T}s { | |
all${T}s { | |
id | |
} | |
} | |
`; | |
const getAll = async (T) => { | |
const res = await client.request(query(T)) | |
return toIds(res[`all${T}s`]); | |
} | |
const del = async (T, accounts) => { | |
await Promise.all(accounts.map(id => { | |
const query = ` | |
mutation Delete${T}($id: ID!) { | |
delete${T}(id: $id) { | |
id | |
} | |
} | |
`; | |
const variables = { id }; | |
return client.request(query, variables); | |
})); | |
} | |
const toIds = array => array.map(e => e.id); | |
const detroyAllDataIn = async T => { | |
const allAccounts = await getAll(T); | |
await del(T, allAccounts); | |
console.log(`Deleted: ${allAccounts.length} ${T}s`); | |
} | |
async function main() { | |
await Promise.all([ | |
detroyAllDataIn('Account'), | |
detroyAllDataIn('User') | |
]); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment