@pothos/core
Version:
Pothos (formerly GiraphQL) is a plugin based schema builder for creating code-first GraphQL schemas in typescript
76 lines (75 loc) • 2.88 kB
JavaScript
import { PothosSchemaError } from '../errors.js';
import { BaseTypeRef } from '../refs/base.js';
import { InputListRef } from '../refs/input-list.js';
import { ListRef } from '../refs/list.js';
export function unwrapOutputFieldType(type) {
if (type.kind === "List") {
return unwrapOutputFieldType(type.type);
}
return type.ref;
}
export function typeFromParam(param, configStore, nullableOption) {
const itemNullable = typeof nullableOption === "object" ? nullableOption.items : false;
const nullable = typeof nullableOption === "object" ? nullableOption.list : !!nullableOption;
if (Array.isArray(param)) {
return {
kind: "List",
type: typeFromParam(param[0], configStore, itemNullable),
nullable
};
}
if (param instanceof ListRef) {
return {
kind: "List",
type: typeFromParam(param.listType, configStore, param.nullable),
nullable
};
}
const ref = configStore.getOutputTypeRef(param);
const kind = ref instanceof BaseTypeRef ? ref.kind : configStore.getTypeConfig(ref).graphqlKind;
const name = ref instanceof BaseTypeRef ? ref.name : configStore.getTypeConfig(ref).name;
if (kind !== "InputObject" && kind !== "List" && kind !== "InputList") {
return {
kind,
ref,
nullable
};
}
throw new PothosSchemaError(`Expected input param ${name} to be an output type but got ${kind}`);
}
export function unwrapInputFieldType(type) {
if (type.kind === "List") {
return unwrapInputFieldType(type.type);
}
return type.ref;
}
export function inputTypeFromParam(param, configStore, requiredOption) {
const itemRequired = typeof requiredOption === "object" ? requiredOption.items : true;
const required = typeof requiredOption === "object" ? requiredOption.list : !!requiredOption;
if (Array.isArray(param)) {
return {
kind: "List",
type: inputTypeFromParam(param[0], configStore, itemRequired),
required
};
}
if (param instanceof InputListRef) {
return {
kind: "List",
type: inputTypeFromParam(param.listType, configStore, param.required),
required
};
}
const ref = configStore.getInputTypeRef(param);
const kind = ref instanceof BaseTypeRef ? ref.kind : configStore.getTypeConfig(ref).graphqlKind;
const name = ref instanceof BaseTypeRef ? ref.name : configStore.getTypeConfig(ref).name;
if (kind === "InputObject" || kind === "Enum" || kind === "Scalar") {
return {
kind,
ref,
required
};
}
throw new PothosSchemaError(`Expected input param ${name} to be an InputObject, Enum, or Scalar but got ${kind}`);
}
//# sourceMappingURL=params.js.map