Skip to content

Commit d374cbb

Browse files
authored
Merge pull request #492 from code0-tech/491-type-graphql-ids
Move to TypeScript codegen configuration and make scalars more type safe
2 parents 55ef77e + e17c90b commit d374cbb

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

.gitlab-ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,15 @@ npm-types:generate-graphql:
190190
- .rails
191191
stage: build
192192
script:
193-
- bundle exec rake graphql:schema:idl
193+
- bundle exec rake graphql:schema:dump
194194
artifacts:
195195
expire_in: 7 days
196196
paths:
197197
- tmp/schema.graphql
198+
- tmp/schema.json
198199
- tooling/graphql/types/
199200

201+
200202
.npm-types:publish-graphql:
201203
image: node:22.16.0
202204
needs:
@@ -218,6 +220,10 @@ npm-types:publish-graphql-dry-run:
218220
script:
219221
- !reference [.npm-types:publish-graphql, script]
220222
- npm publish --dry-run
223+
artifacts:
224+
expire_in: 7 days
225+
paths:
226+
- tooling/graphql/types/index.d.ts
221227
rules:
222228
- if: $C0_GH_REF_NAME != "main"
223229

tooling/graphql/types/codegen.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type {CodegenConfig} from '@graphql-codegen/cli';
2+
import {readFileSync} from "node:fs";
3+
4+
type Schema = {
5+
data: {
6+
__schema: {
7+
types: Array<{ kind: string; name: string; }>;
8+
};
9+
};
10+
}
11+
12+
const schemaData = readFileSync('../../../tmp/schema.json', 'utf-8');
13+
const schema: Schema = JSON.parse(schemaData);
14+
15+
const globalIds = schema.data.__schema.types
16+
.filter(type => type.kind === 'SCALAR' && type.name.endsWith('ID'))
17+
18+
const scalars = {
19+
JSON: {
20+
input: 'any',
21+
output: 'any',
22+
},
23+
Time: {
24+
input: 'string',
25+
output: 'string',
26+
}
27+
}
28+
29+
30+
globalIds.forEach(type => {
31+
const typeConfig = `\`gid://sagittarius/${type.name.replace(/ID$/, '')}/\${number}\``
32+
33+
scalars[type.name] = {
34+
input: typeConfig,
35+
output: typeConfig,
36+
};
37+
});
38+
39+
40+
const config: CodegenConfig = {
41+
schema: "../../../tmp/schema.graphql",
42+
generates: {
43+
'./index.d.ts': {
44+
plugins: [
45+
"typescript"
46+
],
47+
config: {
48+
scalars,
49+
strictScalars: true,
50+
declarationKind: 'interface',
51+
}
52+
},
53+
},
54+
};
55+
56+
57+
export default config;

tooling/graphql/types/codegen.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

tooling/graphql/types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"types": "index.d.ts",
77
"scripts": {
8-
"generate": "graphql-codegen --config codegen.yml"
8+
"generate": "graphql-codegen --config codegen.ts"
99
},
1010
"repository": {
1111
"type": "git",

0 commit comments

Comments
 (0)