@vladimirdev635/gql-codegen 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/schema.js ADDED
@@ -0,0 +1,122 @@
1
+ /* eslint-disable max-lines */
2
+ import { z } from "zod";
3
+ export const objectTypeSchema = z.discriminatedUnion("_type", [
4
+ z.object({
5
+ _type: z.literal("ObjectType"),
6
+ name: z.string(),
7
+ $ref: z.string()
8
+ }),
9
+ z.object({
10
+ _type: z.literal("Scalar"),
11
+ name: z.string()
12
+ }),
13
+ z.object({
14
+ _type: z.literal("Union"),
15
+ name: z.string(),
16
+ $ref: z.string()
17
+ })
18
+ ]);
19
+ export const inputTypeSchema = z.discriminatedUnion("_type", [
20
+ z.object({
21
+ _type: z.literal("InputType"),
22
+ name: z.string(),
23
+ $ref: z.string()
24
+ }),
25
+ z.object({
26
+ _type: z.literal("Scalar"),
27
+ name: z.string()
28
+ }),
29
+ z.object({
30
+ _type: z.literal("Enum"),
31
+ name: z.string(),
32
+ $ref: z.string()
33
+ }),
34
+ ]);
35
+ export const objectLiteralSpecSchema = z.object({
36
+ _type: z.literal("literal"),
37
+ type: objectTypeSchema
38
+ });
39
+ export const objectArraySpecSchema = z.object({
40
+ _type: z.literal("array"),
41
+ nullable: z.boolean(),
42
+ type: objectTypeSchema
43
+ });
44
+ export const inputLiteralSpecSchema = z.object({
45
+ _type: z.literal("literal"),
46
+ type: inputTypeSchema
47
+ });
48
+ export const inputArraySpecSchema = z.object({
49
+ _type: z.literal("array"),
50
+ nullable: z.boolean(),
51
+ type: inputTypeSchema
52
+ });
53
+ export const inputFieldSpecSchema = z.discriminatedUnion("_type", [
54
+ inputLiteralSpecSchema,
55
+ inputArraySpecSchema,
56
+ ]);
57
+ export const objectNonCallableFieldSpecSchema = z.discriminatedUnion("_type", [
58
+ objectLiteralSpecSchema,
59
+ objectArraySpecSchema,
60
+ ]);
61
+ export const inputFieldSchema = z.object({
62
+ nullable: z.boolean(),
63
+ spec: inputFieldSpecSchema
64
+ });
65
+ export const callableSpecSchema = z.object({
66
+ _type: z.literal("callable"),
67
+ returnType: objectNonCallableFieldSpecSchema,
68
+ arguments: z.record(z.string(), inputFieldSchema)
69
+ });
70
+ export const objectFieldSpecSchema = z.discriminatedUnion("_type", [
71
+ objectLiteralSpecSchema,
72
+ objectArraySpecSchema,
73
+ callableSpecSchema
74
+ ]);
75
+ export const objectFieldSchema = z.object({
76
+ nullable: z.boolean(),
77
+ spec: objectFieldSpecSchema
78
+ });
79
+ export const objectSchema = z.object({
80
+ name: z.string(),
81
+ implements: z.record(z.string(), z.string()),
82
+ fields: z.record(z.string(), objectFieldSchema)
83
+ });
84
+ export const inputSchema = z.object({
85
+ name: z.string(),
86
+ fields: z.record(z.string(), inputFieldSchema)
87
+ });
88
+ export const directiveLocationEnum = z.enum([
89
+ 'SCHEMA',
90
+ 'SCALAR',
91
+ 'OBJECT',
92
+ 'FIELD_DEFINITION',
93
+ 'ARGUMENT_DEFINITION',
94
+ 'INTERFACE',
95
+ 'UNION',
96
+ 'ENUM',
97
+ 'ENUM_VALUE',
98
+ 'INPUT_OBJECT',
99
+ 'INPUT_FIELD_DEFINITION',
100
+ ]);
101
+ export const directiveSchema = z.object({
102
+ name: z.string(),
103
+ locations: z.array(directiveLocationEnum)
104
+ });
105
+ export const unionSchema = z.object({
106
+ name: z.string(),
107
+ items: z.record(z.string(), z.string())
108
+ });
109
+ export const enumSchema = z.object({
110
+ name: z.string(),
111
+ values: z.array(z.string())
112
+ });
113
+ export const rootSchema = z.object({
114
+ server: z.object({
115
+ objects: z.record(z.string(), objectSchema),
116
+ directives: z.record(z.string(), directiveSchema),
117
+ unions: z.record(z.string(), unionSchema),
118
+ enums: z.record(z.string(), enumSchema),
119
+ scalars: z.array(z.string()),
120
+ inputs: z.record(z.string(), inputSchema)
121
+ }),
122
+ }).strict();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import testJSON from './test.json' with { type: 'json' };
3
+ import { rootSchema } from './schema.js';
4
+ describe("Schema", () => {
5
+ it('Should parse ok', () => {
6
+ const result = rootSchema.safeParse(testJSON);
7
+ expect(result.success, result.error?.message || '').toBe(true);
8
+ });
9
+ });
@@ -0,0 +1,3 @@
1
+ import { PathOrFileDescriptor } from "fs";
2
+ import { RootSchema } from "./schema.js";
3
+ export declare function loadSchemaFromFile(p: PathOrFileDescriptor): RootSchema;
@@ -0,0 +1,5 @@
1
+ import { readFileSync } from "fs";
2
+ import { rootSchema } from "./schema.js";
3
+ export function loadSchemaFromFile(p) {
4
+ return rootSchema.parse(JSON.parse(readFileSync(p).toString()));
5
+ }