@backland/schema
Version:
TypeScript schema declaration and validation library with static type inference
65 lines (64 loc) • 2.15 kB
JavaScript
import { CircularDeps } from '../CircularDeps';
import { ObjectType } from '../ObjectType';
import { getObjectDefinitionId } from '../fields/MetaFieldField';
import { parseObjectField } from '../parseObjectDefinition';
// used to lazy parse args to improve circular types usage
export function lazyCreateGraphTypeInitPayload(args, onLoad) {
var payload;
var id = undefined;
var definitionInput;
var idFromArgs;
if (args.length === 2) {
idFromArgs = id = args[0];
definitionInput = args[1];
} else {
definitionInput = args[0];
}
function initializer(self) {
var _field$utils;
if (payload) return payload;
var def = typeof definitionInput === 'function' ? definitionInput(CircularDeps) : definitionInput;
var field = parseObjectField('temp', def, {
returnInstance: true
});
var objectType = ObjectType.is(field === null || field === void 0 ? void 0 : (_field$utils = field.utils) === null || _field$utils === void 0 ? void 0 : _field$utils.object) ? field.utils.object : undefined;
if (objectType) {
if (id && objectType.id && objectType.id !== id) {
field.utils.object = objectType.clone(el => el.objectType(id));
} else if (id) {
field.utils.object.identify(id);
}
}
if (!id && objectType) {
id = getObjectDefinitionId(objectType.definition, true // make nullable, the error below about undefined name is more clear
);
}
if (id && !field.id) {
field.id = id;
}
payload = {
definition: field.asFinalFieldDef,
definitionInput,
field,
// id can be from inner type, like an object type with id or defined in an argument of createType
id,
idFromArgs,
objectType: objectType
};
if (id) {
self.identify(id);
}
var res = onLoad === null || onLoad === void 0 ? void 0 : onLoad(payload);
if (res !== undefined) {
return res;
}
return payload;
}
return {
// id can also be from inner type, like an object type with id
definitionInput,
idFromArgs,
initializer
};
}
//# sourceMappingURL=lazyCreateGraphTypeInitPayload.js.map