-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathlazy.ts
More file actions
29 lines (26 loc) · 946 Bytes
/
lazy.ts
File metadata and controls
29 lines (26 loc) · 946 Bytes
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
import type { TypeNode } from 'graphql';
import type { Visitor } from './visitor.js';
import { isEnumType, isScalarType } from 'graphql';
import { isNamedType } from './graphql.js';
/**
* Wraps a schema expression in a library-specific lazy reference when the type
* is a complex (non-scalar, non-enum) named type — avoiding issues with
* mutually-recursive input types.
*
* Each validation library has its own lazy syntax (z.lazy, v.lazy, etc.), so
* callers supply the wrapper function.
*
* @param lazyWrapper - e.g. `(s) => \`z.lazy(() => ${s})\``
*/
export function buildMaybeLazy(
visitor: Visitor,
type: TypeNode,
schema: string,
lazyWrapper: (schema: string) => string,
): string {
if (!isNamedType(type))
return schema;
const schemaType = visitor.getType(type.name.value);
const isComplexType = !isScalarType(schemaType) && !isEnumType(schemaType);
return isComplexType ? lazyWrapper(schema) : schema;
}