@nestjs/graphql
Version:
GraphQL for NestJS: code-first and schema-first APIs, resolvers as providers, subscriptions, federation and generated SDL, on Apollo or Mercurius.
25 lines (24 loc) • 1.23 kB
JavaScript
import { isUndefined } from '@nestjs/common/utils/shared.utils.js';
import { isEqual } from 'es-toolkit';
import { DefaultValuesConflictError } from '../errors/default-values-conflict.error.js';
export function getDefaultValue(instance, options, key, typeName) {
// Ignore members inherited from "Object.prototype" (e.g., "toString",
// "constructor") so they are not mistaken for field initializers.
const initializerValue = Object.prototype.hasOwnProperty.call(instance, key) ||
!(key in Object.prototype)
? instance[key]
: undefined;
if (isUndefined(options.defaultValue)) {
// A "null" initializer (e.g., inherited from a base class in which the
// field is nullable) is not a default of a non-nullable field: graphql-js
// would no longer require the value and pass "null" to the resolver.
return initializerValue === null && !options.nullable
? undefined
: initializerValue;
}
if (!isEqual(options.defaultValue, initializerValue) &&
!isUndefined(initializerValue)) {
throw new DefaultValuesConflictError(typeName, key, options.defaultValue, initializerValue);
}
return options.defaultValue;
}