@pothos/core
Version:
Pothos (formerly GiraphQL) is a plugin based schema builder for creating code-first GraphQL schemas in typescript
129 lines (124 loc) • 4.4 kB
JavaScript
import { getArgumentValues } from 'graphql';
import { PothosSchemaError, PothosValidationError } from '../errors.js';
import { InputListRef } from '../refs/input-list.js';
import { ListRef } from '../refs/list.js';
import { typeBrandKey } from '../types/index.js';
export * from './base64.js';
export * from './context-cache.js';
export * from './enums.js';
export * from './input.js';
export * from './params.js';
export * from './sort-classes.js';
export function assertNever(value) {
throw new TypeError(`Unexpected value: ${value}`);
}
export function assertArray(value) {
if (!Array.isArray(value)) {
throw new PothosValidationError("List resolvers must return arrays");
}
return true;
}
export function isThenable(value) {
return !!(value && (typeof value === "object" || typeof value === "function") && typeof value.then === "function");
}
export function verifyRef(ref) {
if (ref === undefined) {
throw new PothosSchemaError(`Received undefined as a type ref.
This is often caused by a circular import
If this ref is imported from a file that re-exports it (like index.ts)
you may be able to resolve this by importing it directly from the file that defines it.
`);
}
}
export function verifyInterfaces(interfaces) {
if (!interfaces || typeof interfaces === "function") {
return;
}
if (!Array.isArray(interfaces)) {
throw new PothosSchemaError("interfaces must be an array or function");
}
for (const iface of interfaces) {
if (iface === undefined) {
throw new PothosSchemaError(`Received undefined in list of interfaces.
This is often caused by a circular import
If this ref is imported from a file that re-exports it (like index.ts)
you may be able to resolve this by importing it directly from the file that defines it.
Alternatively you can define interfaces with a function that will be lazily evaluated,
which may resolver issues with circular dependencies:
Example:
builder.objectType('MyObject', {
interface: () => [Interface1, Interface2],
...
});
`);
}
}
}
export function brandWithType(val, type) {
if (typeof val !== "object" || val === null) {
return;
}
Object.defineProperty(val, typeBrandKey, {
enumerable: false,
value: type
});
}
export function getTypeBrand(val) {
if (typeof val === "object" && val !== null && typeBrandKey in val) {
return val[typeBrandKey];
}
return null;
}
export function unwrapListParam(param) {
if (Array.isArray(param)) {
return unwrapListParam(param[0]);
}
if (param instanceof ListRef || param instanceof InputListRef) {
return unwrapListParam(param.listType);
}
return param;
}
export function unwrapOutputListParam(param) {
if (Array.isArray(param)) {
return unwrapOutputListParam(param[0]);
}
if (param instanceof ListRef) {
return unwrapOutputListParam(param.listType);
}
return param;
}
export function unwrapInputListParam(param) {
if (Array.isArray(param)) {
return unwrapInputListParam(param[0]);
}
if (param instanceof InputListRef) {
return unwrapInputListParam(param.listType);
}
return param;
}
/**
* Helper for allowing plugins to fulfill the return of the `next` resolver, without paying the cost of the
* Promise if not required.
*/ export function completeValue(valOrPromise, onSuccess, onError) {
if (isThenable(valOrPromise)) {
return Promise.resolve(valOrPromise).then(onSuccess, onError);
}
// No need to handle onError, this should just be a try/catch inside the `onSuccess` block
const result = onSuccess(valOrPromise);
// If the result of the synchronous call is a promise like, convert to a promise
// for consistency
if (isThenable(result)) {
return Promise.resolve(result);
}
return result;
}
export function getMappedArgumentValues(def, node, context, info) {
var _def_extensions;
const args = getArgumentValues(def, node, info.variableValues);
const mappers = (_def_extensions = def.extensions) === null || _def_extensions === void 0 ? void 0 : _def_extensions.pothosArgMappers;
if (mappers && mappers.length > 0) {
return mappers.reduce((acc, argMapper) => argMapper(acc, context, info), args);
}
return args;
}
//# sourceMappingURL=index.js.map