Skip to content

Instantly share code, notes, and snippets.

@maggiben
Created November 28, 2020 03:39
Show Gist options
  • Save maggiben/3c03f552319f58089abb4fb9ae9150f6 to your computer and use it in GitHub Desktop.
Save maggiben/3c03f552319f58089abb4fb9ae9150f6 to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
const ALLOWED_MUTATIONS = [
'manage_splash',
'create_public_api_credential',
'delete_public_api_credential'
];
const loadSchema = async (file) => {
try {
const data = await fs.readFile(file);
return JSON.parse(data);
} catch (error) {
console.error(error);
throw error;
}
};
const sanitizeSchema = async (schema) => {
const { mutationType, types } = schema;
const replace = (array, index, ...items) => [...array.slice(0, index), ...items, ...array.slice(index + 1)];
const mutations = types.find(type => type.name === mutationType.name);
const index = types.findIndex(type => type.name === mutationType.name);
if (mutations && index !== -1) {
const { fields } = mutations;
const result = fields.filter(field => ALLOWED_MUTATIONS.includes(field.name));
const patchedMutations = {...mutations, fields: result};
const patchedTypes = replace(types, index, patchedMutations);
return {
data: {
__schema: {
...schema,
types: patchedTypes
}
}
};
}
return schema;
};
const saveSchema = (file) => {
return async (schema) => {
try {
const data = JSON.stringify(schema, null, 4);
return await fs.writeFile(file, data, 'utf8');
} catch (error) {
console.error(error);
throw error;
}
};
};
loadSchema('schema.json').then(sanitizeSchema).then(saveSchema('output.json'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment