graphql-codegen-typescript-validation-schema
Version:
GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema
26 lines (25 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildMaybeLazy = buildMaybeLazy;
const graphql_1 = require("graphql");
const graphql_js_1 = require("./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 visitor - Type lookup used to distinguish scalars/enums from complex types.
* @param type - GraphQL type node for the schema expression.
* @param schema - Generated schema expression to wrap when needed.
* @param lazyWrapper - e.g. `(s) => \`z.lazy(() => ${s})\``
*/
function buildMaybeLazy(visitor, type, schema, lazyWrapper) {
if (!(0, graphql_js_1.isNamedType)(type))
return schema;
const schemaType = visitor.getType(type.name.value);
const isComplexType = !(0, graphql_1.isScalarType)(schemaType) && !(0, graphql_1.isEnumType)(schemaType);
return isComplexType ? lazyWrapper(schema) : schema;
}