@nestjs/graphql
Version:
GraphQL for NestJS: code-first and schema-first APIs, resolvers as providers, subscriptions, federation and generated SDL, on Apollo or Mercurius.
138 lines (137 loc) • 7.56 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { Injectable } from '@nestjs/common';
import { isUndefined } from '@nestjs/common/utils/shared.utils.js';
import { isEqual } from 'es-toolkit';
import { isEqualType, } from 'graphql';
import { CannotDetermineArgTypeError } from '../errors/cannot-determine-arg-type.error.js';
import { ConflictingArgsError } from '../errors/conflicting-args.error.js';
import { getDefaultValue } from '../helpers/get-default-value.helper.js';
import { mergeInheritedFields } from '../helpers/merge-inherited-fields.helper.js';
import { normalizeEnumDefaultValue } from '../helpers/normalize-enum-default-value.helper.js';
import { TypeMetadataStorage } from '../storages/type-metadata.storage.js';
import { InputTypeFactory } from './input-type.factory.js';
import { AstDefinitionNodeFactory } from './ast-definition-node.factory.js';
let ArgsFactory = class ArgsFactory {
constructor(inputTypeFactory, astDefinitionNodeFactory) {
this.inputTypeFactory = inputTypeFactory;
this.astDefinitionNodeFactory = astDefinitionNodeFactory;
}
create(args, options) {
const fieldConfigMap = {};
const declarationsBySchemaName = new Map();
const declarationsByKey = new Map();
// Every parameter of a method reads from the same arguments object, so an
// argument declared by several parameters must be declared identically,
// and no two arguments may end up under the same key once the renamed
// properties of the `@ArgsType()` classes are mapped back to their names.
const addDeclaration = (declaration) => {
const { param, schemaName, key } = declaration;
const host = `"${param.target.name}#${param.methodName}"`;
const sameSchemaName = declarationsBySchemaName.get(schemaName);
if (sameSchemaName && sameSchemaName.param !== param) {
if (!isEqualType(sameSchemaName.config.type, declaration.config.type) ||
!isEqual(sameSchemaName.config.defaultValue, declaration.config.defaultValue)) {
throw new ConflictingArgsError(`Argument "${schemaName}" of ${host} is declared by more than one @Args() parameter with a different type or default value.`);
}
if (sameSchemaName.key !== key) {
throw new ConflictingArgsError(`Argument "${schemaName}" of ${host} is passed to the resolver both as the ${sameSchemaName.origin} and as the ${declaration.origin}.`);
}
}
const sameKey = declarationsByKey.get(key);
if (sameKey &&
sameKey.param !== param &&
sameKey.schemaName !== schemaName) {
const [renamed, other] = declaration.schemaName !== key
? [declaration, sameKey]
: [sameKey, declaration];
throw new ConflictingArgsError(`The ${renamed.origin} is named like the ${other.origin} of ${host}, so both cannot be passed to the resolver.`);
}
declarationsBySchemaName.set(schemaName, declaration);
declarationsByKey.set(key, declaration);
fieldConfigMap[schemaName] = declaration.config;
};
args.forEach((param) => {
if (param.kind === 'arg') {
const typeRef = param.typeFn();
addDeclaration({
param,
schemaName: param.name,
key: param.name,
origin: `"${param.name}" argument`,
config: {
description: param.description,
deprecationReason: param.deprecationReason,
type: this.inputTypeFactory.create(param.name, typeRef, options, param.options),
defaultValue: normalizeEnumDefaultValue(param.options.defaultValue, typeRef, TypeMetadataStorage.getEnumsMetadata()),
},
});
}
else if (param.kind === 'args') {
const argumentTypes = TypeMetadataStorage.getArgumentsMetadata();
const hostType = param.typeFn();
const argumentType = argumentTypes.find((item) => item.target === hostType);
if (!argumentType) {
throw new CannotDetermineArgTypeError(hostType.name || hostType, param);
}
// Farthest ancestor first, so that each class overrides what it
// re-declares from the classes above it.
const inheritanceChain = [argumentType];
let parent = Object.getPrototypeOf(argumentType.target);
while (!isUndefined(parent.prototype)) {
const parentArgType = argumentTypes.find((item) => item.target === parent);
if (parentArgType) {
inheritanceChain.unshift(parentArgType);
}
parent = Object.getPrototypeOf(parent);
}
this.createInheritedArgs(inheritanceChain, options).forEach(({ field, argType, config }) => addDeclaration({
param,
schemaName: field.schemaName,
key: field.name,
origin: field.name === field.schemaName
? `"${field.name}" property of "${argType.name}"`
: `"${field.name}" property of "${argType.name}" (exposed as "${field.schemaName}")`,
config,
}));
}
});
return fieldConfigMap;
}
createInheritedArgs(inheritanceChain, options) {
const argTypeByField = new Map();
const instanceByArgType = new Map();
inheritanceChain.forEach((argType) => argType.properties.forEach((field) => argTypeByField.set(field, argType)));
const fields = mergeInheritedFields(inheritanceChain.map((argType) => argType.properties));
return fields.map((field) => {
const argType = argTypeByField.get(field);
if (!instanceByArgType.has(argType)) {
instanceByArgType.set(argType, new argType.target());
}
field.options.defaultValue = getDefaultValue(instanceByArgType.get(argType), field.options, field.name, argType.name);
const typeRef = field.typeFn();
const type = this.inputTypeFactory.create(field.name, typeRef, options, field.options);
return {
field,
argType,
config: {
description: field.description,
deprecationReason: field.deprecationReason,
type,
defaultValue: normalizeEnumDefaultValue(field.options.defaultValue, typeRef, TypeMetadataStorage.getEnumsMetadata()),
extensions: field.extensions,
/**
* AST node has to be manually created in order to define directives
* (more on this topic here: https://github.com/graphql/graphql-js/issues/1343)
*/
astNode: this.astDefinitionNodeFactory.createArgNode(field.name, type, field.directives),
},
};
});
}
};
ArgsFactory = __decorate([
Injectable(),
__metadata("design:paramtypes", [InputTypeFactory,
AstDefinitionNodeFactory])
], ArgsFactory);
export { ArgsFactory };