import fs from 'fs';
import path from 'path';
import glob from 'glob';
interface GraphQLOperation {
name: string;
type: 'query' | 'mutation';
variablesType: string;
responseType: string;
}
export function crawlGraphQL(rootPath: string): GraphQLOperation[] {
const files = glob.sync('**/*.{graphql,gql}', { cwd: rootPath });
const ops: GraphQLOperation[] = [];
files.forEach((file: any) => {
const content = fs.readFileSync(path.join(rootPath, file), 'utf-8');
const matches = [...content.matchAll(/(query|mutation)\s+(\w+)\s*(\([^\)]*\))?/g)];
matches.forEach((m) => {
const type = m[1] as 'query' | 'mutation';
const name = m[2];
const varsRaw = m[3] || '';
const variablesType = varsRaw ? `${name}Variables` : 'void';
const responseType = `${name}Response`;
ops.push({ name, type, variablesType, responseType });
});
});
return ops;
}