Skip to content

Instantly share code, notes, and snippets.

@mattpocock
Created May 30, 2019 13:06
Show Gist options
  • Save mattpocock/ee89876aaf47ed7613ea48ae8af80261 to your computer and use it in GitHub Desktop.
Save mattpocock/ee89876aaf47ed7613ea48ae8af80261 to your computer and use it in GitHub Desktop.
const file = require('file');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const getSchema = require('./getSchema');
const generateMocks = require('./generateMocks');
const typesDir = path.resolve('../JlrLmsUI/app/utils/fixtures/api');
if (fs.existsSync(typesDir)) {
execSync(`rm -rf ${typesDir}`);
}
fs.mkdirSync(typesDir);
file.walkSync(path.resolve(__dirname, '../contracts'), (dir, dirs, files) => {
files.forEach((fileName) => {
if (`${fileName}`.includes('.graphql')) {
const schema = getSchema(path.resolve(dir, fileName));
const mocks = generateMocks(schema.definitions);
const callName = `${path
.relative(
path.resolve(__dirname, '../contracts'),
path.resolve(__dirname, '../contracts', dir, fileName),
)
.replace(/\//g, '')
.replace(/\.graphql/g, '')}`;
if (mocks.Response && mocks.Response.Data) {
fs.writeFileSync(
path.resolve(typesDir, `${callName}Response.json`),
JSON.stringify(mocks.Response.Data, null, 2),
);
}
console.log(`${callName} complete`);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment