Skip to content

Commit fbf1b9e

Browse files
committed
ts:gql-codegen:split-schema-into-two-files
1 parent 9dfcf2d commit fbf1b9e

10 files changed

Lines changed: 41 additions & 18 deletions

File tree

typescript/gql-codegen/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ dist
33
gql-codegen.mts
44
check
55
schema.json
6+
client-schema.json
7+
server-schema.json
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export { Actor, ActorContext, Config } from './config.js'
22
export { run } from './main.js'
3-
export { loadSchemaFromFile } from './schema/utils.js'
3+
export {
4+
loadClientSchemaFromFile,
5+
loadServerSchemaFromFile
6+
} from './schema/utils.js'
47
export * as actors from './actors/index.js'

typescript/gql-codegen/src/schema/client/root.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export const clientSchema = z.object({
88
operations: z.record(z.string(), operationSchema),
99
directives: z.record(z.string(), directiveSchema)
1010
})
11+
export type ClientSchema = z.infer<typeof clientSchema>;
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { z } from 'zod/v4'
2-
import { serverSchema } from './server.js'
3-
import { clientSchema } from './client/root.js'
1+
import { ClientSchema } from './client/root.js';
2+
import { ServerSchema } from './server.js';
43

5-
export const rootSchema = z.object({
6-
server: serverSchema,
7-
client: clientSchema
8-
}).strict()
9-
10-
export type RootSchema = z.infer<typeof rootSchema>
4+
export interface RootSchema {
5+
server: ServerSchema
6+
client: ClientSchema
7+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { describe, it, expect } from 'vitest'
2-
import testJSON from './test.json' with { type: 'json' }
3-
import { rootSchema } from './root.js'
2+
import testServerJSON from './test-server.json' with { type: 'json' }
3+
import testClientJSON from './test-client.json' with { type: 'json' }
4+
import { serverSchema } from './server.js'
5+
import { clientSchema } from './client/root.js'
46

5-
describe('Schema', () => {
7+
describe('Server schema', () => {
68
it('Should parse ok', () => {
7-
const result = rootSchema.safeParse(testJSON)
9+
const result = serverSchema.safeParse(testServerJSON)
10+
expect(result.success, result.error?.message || '').toBe(true)
11+
})
12+
})
13+
14+
describe('Client schema', () => {
15+
it('Should parse ok', () => {
16+
const result = clientSchema.safeParse(testClientJSON)
817
expect(result.success, result.error?.message || '').toBe(true)
918
})
1019
})

typescript/gql-codegen/src/schema/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,4 @@ export const serverSchema = z.object({
106106
scalars: z.array(z.string()),
107107
inputs: z.record(z.string(), inputSchema)
108108
})
109+
export type ServerSchema = z.infer<typeof serverSchema>;

typescript/gql-codegen/src/schema/test-client.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

typescript/gql-codegen/src/schema/test-server.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

typescript/gql-codegen/src/schema/test.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { PathOrFileDescriptor, readFileSync } from 'fs';
2-
import { rootSchema, RootSchema } from './root.js';
2+
import { ServerSchema, serverSchema } from './server.js';
3+
import { ClientSchema, clientSchema } from './client/root.js';
34

4-
export function loadSchemaFromFile(p: PathOrFileDescriptor): RootSchema {
5-
return rootSchema.parse(JSON.parse(readFileSync(p).toString()))
5+
export function loadServerSchemaFromFile(
6+
p: PathOrFileDescriptor
7+
): ServerSchema {
8+
return serverSchema.parse(JSON.parse(readFileSync(p).toString()))
9+
}
10+
11+
export function loadClientSchemaFromFile(
12+
p: PathOrFileDescriptor
13+
): ClientSchema {
14+
return clientSchema.parse(JSON.parse(readFileSync(p).toString()))
615
}

0 commit comments

Comments
 (0)