Skip to content

Commit 8e85c4f

Browse files
committed
feat(cli): generate mutation names typing
1 parent 599e209 commit 8e85c4f

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

lib/cli.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import * as codegen from 'apollo-codegen';
44
import * as yargs from 'yargs';
55
import * as path from 'path';
6+
import * as fs from 'fs';
7+
import { fstat } from 'fs';
8+
import { generateMutationTypesDef, extractMutationsNames } from './utils';
69

710
process.on('unhandledRejection', (error) => { throw error });
811
process.on('uncaughtException' as any, handleError);
@@ -57,6 +60,20 @@ yargs
5760
console.log('[1/1] downloadSchema ...');
5861
await codegen.downloadSchema(url, output, header, insecure, method);
5962
console.log('[2/2] generate mutations enum type ...');
63+
try {
64+
const mutationNames = extractMutationsNames(output);
65+
if (mutationNames) {
66+
fs.writeFileSync(
67+
path.resolve('./', 'mutations.d.ts'),
68+
generateMutationTypesDef(mutationNames)
69+
)
70+
} else {
71+
console.error('Failed to generate mutations typing');
72+
}
73+
} catch (error) {
74+
console.error(error)
75+
}
76+
console.log('Done.');
6077
}
6178
)
6279
.fail(function (message, error) {

lib/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as fs from 'fs';
2+
import { IntrospectionQuery, IntrospectionObjectType } from 'graphql';
3+
4+
export const extractMutationsNames = (filePath: string): string[] | void => {
5+
const content = fs.readFileSync(filePath);
6+
if (content) {
7+
const json = JSON.parse(content.toString()).data as IntrospectionQuery;
8+
const { name: mutationType } = json.__schema.mutationType;
9+
const mutations = json.__schema.types.find(t => t.name === mutationType) as (IntrospectionObjectType | undefined);
10+
return mutations ?
11+
mutations.fields.map(f => f.name) :
12+
[];
13+
} else {
14+
throw new Error(`Unable to read ${filePath}`);
15+
}
16+
}
17+
18+
export const generateMutationTypesDef = (mutations: string[]): string => {
19+
return (
20+
`
21+
/* this file is generated, do not edit and keep it in tsconfig.rootDir scope! */
22+
23+
declare type ApolloFormMutationNames = ${mutations.map(m => `'${m}'`).join(' | ')};
24+
`
25+
)
26+
}

mutations.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
/* this file is generated, do not edit and keep it in tsconfig.rootDir scope! */
3+
4+
declare type ApolloFormMutationNames = 'create_case' | 'create_chat' | 'create_chat_message' | 'create_community_list' | 'create_invitation' | 'create_milestone' | 'create_note' | 'create_post' | 'create_project' | 'create_space' | 'create_task' | 'destroy_case' | 'destroy_community_list' | 'destroy_milestone' | 'destroy_note' | 'destroy_planning' | 'destroy_post' | 'destroy_project' | 'extract_link_previews' | 'handle_visibility_case' | 'read_all_notifications' | 'read_chat' | 'read_notification' | 'typing_chat' | 'update_case' | 'update_community_list' | 'update_milestone' | 'update_note' | 'update_portfolio' | 'update_post' | 'update_project' | 'update_space' | 'update_task' | 'update_task_completion' | 'update_user';

0 commit comments

Comments
 (0)