-
Notifications
You must be signed in to change notification settings - Fork 14
/
codegen.js
62 lines (54 loc) · 1.46 KB
/
codegen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable @typescript-eslint/no-var-requires */
const { writeFileSync } = require('fs');
const { generate } = require('@graphql-codegen/cli');
const {
buildClientSchema,
getIntrospectionQuery,
printSchema,
} = require('graphql');
const fetch = require('node-fetch');
const SCHEMA_LOCATION = './tmp-schema.graphql';
const fetchSchema = async () => {
const response = await fetch('http://localhost:20002/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'da2-fakeApiId123456',
},
body: JSON.stringify({ query: getIntrospectionQuery() }),
});
const data = await response.json();
const schema = buildClientSchema(data.data);
writeFileSync(SCHEMA_LOCATION, printSchema(schema));
};
const codegen = async () => {
try {
await fetchSchema();
const graphqlFiles = './src/graphql/**/*.graphql';
generate({
schema: SCHEMA_LOCATION,
documents: graphqlFiles,
generates: {
'./src/graphql/generated.ts': {
plugins: [
'typescript',
'typescript-operations',
'typescript-react-apollo',
],
},
},
config: {
scalars: {
AWSDateTime: 'string',
AWSEmail: 'string',
AWSURL: 'string',
AWSTimestamp: 'number',
},
},
watch: graphqlFiles,
});
} catch (error) {
console.error('Error when generating types: ', error);
}
};
codegen();