@vladimirdev635/gql-codegen 0.0.91 → 0.0.93

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.
@@ -23,12 +23,14 @@ export function extractFragmentNamesInSpec(schema, fragmentSpec) {
23
23
  return fragmentSpec.selections.map((s) => {
24
24
  if (s._type === 'SpreadSelection') {
25
25
  const fragment = schema.client.fragments[s.fragment];
26
+ assert(fragment.spec !== undefined);
26
27
  return [
27
28
  s.fragment,
28
29
  ...extractFragmentNamesInSpec(schema, fragment.spec)
29
30
  ];
30
31
  }
31
- if (s._type === 'FieldSelection' && s.selection !== null) {
32
+ if (s._type === 'FieldSelection' && s.selection != null) {
33
+ assert(s.selection !== undefined);
32
34
  return extractFragmentNamesInSpec(schema, s.selection);
33
35
  }
34
36
  return [];
@@ -58,7 +60,7 @@ function generateZodObjectSelection(scalarsMapping, schema, objectType, selectio
58
60
  case 'FieldSelection': {
59
61
  const fieldSpec = objectType.fields[selection.name];
60
62
  let expression;
61
- if (selection.selection === null) {
63
+ if (selection.selection == null) {
62
64
  expression = generateZodObjectFieldSpec(scalarsMapping, fieldSpec);
63
65
  }
64
66
  else {
@@ -15,28 +15,32 @@ function generateZodInputTypeSpec(scalarsMapping, type) {
15
15
  }
16
16
  }
17
17
  }
18
- function generateZodInputFieldSpec(scalarsMapping, field) {
19
- let expression;
20
- switch (field.spec._type) {
18
+ function generateZodInputFieldSpec(scalarsMapping, spec) {
19
+ switch (spec._type) {
21
20
  case 'array': {
22
- expression = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [generateZodInputTypeSpec(scalarsMapping, field.spec.type)]);
23
- break;
21
+ return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [generateZodInputFieldSpec(scalarsMapping, spec.type)]);
24
22
  }
25
23
  case 'literal': {
26
- expression = generateZodInputTypeSpec(scalarsMapping, field.spec.type);
24
+ return generateZodInputTypeSpec(scalarsMapping, spec.type);
27
25
  }
28
26
  }
27
+ }
28
+ function generateZodInputField(scalarsMapping, field) {
29
+ const expression = generateZodInputFieldSpec(scalarsMapping, field.spec);
29
30
  if (!field.nullable) {
30
31
  return expression;
31
32
  }
32
33
  return invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
33
34
  }
34
35
  function isFieldLazy(spec) {
35
- return spec.type._type === 'InputType';
36
+ switch (spec._type) {
37
+ case 'literal': return spec.type._type === 'InputType';
38
+ case 'array': return isFieldLazy(spec.type);
39
+ }
36
40
  }
37
41
  export function generateInputTypeDefinitionFields(scalarsMapping, fields) {
38
42
  return Object.entries(fields).map(([name, field]) => {
39
- const fieldSpec = generateZodInputFieldSpec(scalarsMapping, field);
43
+ const fieldSpec = generateZodInputField(scalarsMapping, field);
40
44
  if (isFieldLazy(field.spec)) {
41
45
  return ts.factory.createGetAccessorDeclaration([], name, [], undefined, ts.factory.createBlock([
42
46
  ts.factory.createReturnStatement(fieldSpec)
@@ -2,7 +2,6 @@
2
2
  import { getScalarSpecFromMapping } from './scalars/mapping.js';
3
3
  import ts from 'typescript';
4
4
  import { generateSchemaName, generateZodInferTypeAlias } from './shared.js';
5
- import { invokeMethod } from '../../../shared.js';
6
5
  import { assertUnreachable } from '../../../../../utils.js';
7
6
  function generateZodObjectTypeSpec(scalarsMapping, type) {
8
7
  switch (type._type) {
@@ -25,7 +24,7 @@ function generateZodObjectTypeSpec(scalarsMapping, type) {
25
24
  export function generateObjectNonCallableFieldSpec(scalarsMapping, spec) {
26
25
  switch (spec._type) {
27
26
  case 'array': {
28
- return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [generateZodObjectTypeSpec(scalarsMapping, spec.type)]);
27
+ return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('z'), 'array'), undefined, [generateObjectNonCallableFieldSpec(scalarsMapping, spec.type)]);
29
28
  }
30
29
  case 'literal': {
31
30
  return generateZodObjectTypeSpec(scalarsMapping, spec.type);
@@ -51,10 +50,7 @@ export function generateZodObjectFieldSpec(scalarsMapping, field) {
51
50
  assertUnreachable(field.spec);
52
51
  }
53
52
  }
54
- if (!field.nullable) {
55
- return expression;
56
- }
57
- return invokeMethod(invokeMethod(expression, 'nullable', []), 'optional', []);
53
+ return expression;
58
54
  }
59
55
  const lazyTypes = [
60
56
  'ObjectType',
@@ -63,9 +59,9 @@ const lazyTypes = [
63
59
  ];
64
60
  function isFieldLazy(spec) {
65
61
  switch (spec._type) {
66
- case 'literal':
67
- case 'array': return lazyTypes.includes(spec.type._type);
68
- case 'callable': return lazyTypes.includes(spec.returnType.type._type);
62
+ case 'literal': return lazyTypes.includes(spec.type._type);
63
+ case 'array': return isFieldLazy(spec.type);
64
+ case 'callable': return isFieldLazy(spec.returnType);
69
65
  }
70
66
  }
71
67
  function generateObjectTypeDefinitionFields(scalarsMapping, fields, typename) {
@@ -2,7 +2,7 @@ import ts from 'typescript';
2
2
  import { z } from 'zod/v4';
3
3
  import { objectNonCallableFieldSpecSchema } from '../../../../../schema/server.js';
4
4
  import { inputFieldSpecSchema } from '../../../../../schema/shared.js';
5
- export declare function generateNonCallableFieldSpec(scalars: string[], spec: z.infer<typeof objectNonCallableFieldSpecSchema> | z.infer<typeof inputFieldSpecSchema>): ts.TypeReferenceNode | ts.IndexedAccessTypeNode | ts.ArrayTypeNode;
5
+ export declare function generateNonCallableFieldSpec(scalars: string[], spec: z.infer<typeof objectNonCallableFieldSpecSchema> | z.infer<typeof inputFieldSpecSchema>): ts.TypeNode;
6
6
  export declare function wrapInMaybeIfNullable(spec: ts.TypeNode, nullable: boolean): ts.TypeNode;
7
7
  export declare function generateZodInferTypeAlias(inferType: 'input' | 'output', name: string, typeName: string): ts.TypeAliasDeclaration;
8
8
  export declare function generateSchemaName(name: string): string;
@@ -3,7 +3,7 @@ import { generateTypeReferenceNode } from '../shared.js';
3
3
  export function generateNonCallableFieldSpec(scalars, spec) {
4
4
  switch (spec._type) {
5
5
  case 'array':
6
- return ts.factory.createArrayTypeNode(generateTypeReferenceNode(scalars, spec.type.name));
6
+ return ts.factory.createArrayTypeNode(generateNonCallableFieldSpec(scalars, spec.type));
7
7
  case 'literal':
8
8
  return generateTypeReferenceNode(scalars, spec.type.name);
9
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vladimirdev635/gql-codegen",
3
- "version": "0.0.91",
3
+ "version": "0.0.93",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -21,19 +21,8 @@ export declare const directiveSchema: z.ZodObject<{
21
21
  }, z.core.$strip>, z.ZodObject<{
22
22
  _type: z.ZodLiteral<"array">;
23
23
  nullable: z.ZodBoolean;
24
- type: z.ZodDiscriminatedUnion<[z.ZodObject<{
25
- _type: z.ZodLiteral<"InputType">;
26
- name: z.ZodString;
27
- $ref: z.ZodString;
28
- }, z.core.$strip>, z.ZodObject<{
29
- _type: z.ZodLiteral<"Scalar">;
30
- name: z.ZodString;
31
- }, z.core.$strip>, z.ZodObject<{
32
- _type: z.ZodLiteral<"Enum">;
33
- name: z.ZodString;
34
- $ref: z.ZodString;
35
- }, z.core.$strip>]>;
36
24
  defaultValue: z.ZodNullable<z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodArray<z.ZodInt>, z.ZodArray<z.ZodFloat32>, z.ZodArray<z.ZodBoolean>]>>>;
25
+ type: z.ZodDiscriminatedUnion<[typeof import("../shared.js").inputLiteralSpecSchema, typeof import("../shared.js").inputArraySpecSchema]>;
37
26
  }, z.core.$strip>]>;
38
27
  }, z.core.$strip>>;
39
28
  locations: z.ZodArray<z.ZodEnum<{
@@ -26,19 +26,8 @@ export declare const operationSchema: z.ZodObject<{
26
26
  }, z.core.$strip>, z.ZodObject<{
27
27
  _type: z.ZodLiteral<"array">;
28
28
  nullable: z.ZodBoolean;
29
- type: z.ZodDiscriminatedUnion<[z.ZodObject<{
30
- _type: z.ZodLiteral<"InputType">;
31
- name: z.ZodString;
32
- $ref: z.ZodString;
33
- }, z.core.$strip>, z.ZodObject<{
34
- _type: z.ZodLiteral<"Scalar">;
35
- name: z.ZodString;
36
- }, z.core.$strip>, z.ZodObject<{
37
- _type: z.ZodLiteral<"Enum">;
38
- name: z.ZodString;
39
- $ref: z.ZodString;
40
- }, z.core.$strip>]>;
41
29
  defaultValue: z.ZodNullable<z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodArray<z.ZodInt>, z.ZodArray<z.ZodFloat32>, z.ZodArray<z.ZodBoolean>]>>>;
30
+ type: z.ZodDiscriminatedUnion<[typeof import("../shared.js").inputLiteralSpecSchema, typeof import("../shared.js").inputArraySpecSchema]>;
42
31
  }, z.core.$strip>]>;
43
32
  }, z.core.$strip>>;
44
33
  fragmentSpec: z.ZodDiscriminatedUnion<[z.ZodObject<{
@@ -39,19 +39,8 @@ export declare const clientSchema: z.ZodObject<{
39
39
  }, z.core.$strip>, z.ZodObject<{
40
40
  _type: z.ZodLiteral<"array">;
41
41
  nullable: z.ZodBoolean;
42
- type: z.ZodDiscriminatedUnion<[z.ZodObject<{
43
- _type: z.ZodLiteral<"InputType">;
44
- name: z.ZodString;
45
- $ref: z.ZodString;
46
- }, z.core.$strip>, z.ZodObject<{
47
- _type: z.ZodLiteral<"Scalar">;
48
- name: z.ZodString;
49
- }, z.core.$strip>, z.ZodObject<{
50
- _type: z.ZodLiteral<"Enum">;
51
- name: z.ZodString;
52
- $ref: z.ZodString;
53
- }, z.core.$strip>]>;
54
42
  defaultValue: z.ZodNullable<z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodArray<z.ZodInt>, z.ZodArray<z.ZodFloat32>, z.ZodArray<z.ZodBoolean>]>>>;
43
+ type: z.ZodDiscriminatedUnion<[typeof import("../shared.js").inputLiteralSpecSchema, typeof import("../shared.js").inputArraySpecSchema]>;
55
44
  }, z.core.$strip>]>;
56
45
  }, z.core.$strip>>;
57
46
  fragmentSpec: z.ZodDiscriminatedUnion<[z.ZodObject<{
@@ -87,19 +76,8 @@ export declare const clientSchema: z.ZodObject<{
87
76
  }, z.core.$strip>, z.ZodObject<{
88
77
  _type: z.ZodLiteral<"array">;
89
78
  nullable: z.ZodBoolean;
90
- type: z.ZodDiscriminatedUnion<[z.ZodObject<{
91
- _type: z.ZodLiteral<"InputType">;
92
- name: z.ZodString;
93
- $ref: z.ZodString;
94
- }, z.core.$strip>, z.ZodObject<{
95
- _type: z.ZodLiteral<"Scalar">;
96
- name: z.ZodString;
97
- }, z.core.$strip>, z.ZodObject<{
98
- _type: z.ZodLiteral<"Enum">;
99
- name: z.ZodString;
100
- $ref: z.ZodString;
101
- }, z.core.$strip>]>;
102
79
  defaultValue: z.ZodNullable<z.ZodOptional<z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodArray<z.ZodInt>, z.ZodArray<z.ZodFloat32>, z.ZodArray<z.ZodBoolean>]>>>;
80
+ type: z.ZodDiscriminatedUnion<[typeof import("../shared.js").inputLiteralSpecSchema, typeof import("../shared.js").inputArraySpecSchema]>;
103
81
  }, z.core.$strip>]>;
104
82
  }, z.core.$strip>>;
105
83
  locations: z.ZodArray<z.ZodEnum<{