@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
33 lines (32 loc) • 1.69 kB
JavaScript
/**
* The API surface of this module has been heavily inspired by the "type-graphql" library (https://github.com/MichalLytek/type-graphql), originally designed & released by Michal Lytek.
* In the v6 major release of NestJS, we introduced the code-first approach as a compatibility layer between this package and the `@nestjs/graphql` module.
* Eventually, our team decided to reimplement all the features from scratch due to a lack of flexibility.
* To avoid numerous breaking changes, the public API is backward-compatible and may resemble "type-graphql".
*/
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage.js';
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage.js';
/**
* Creates a GraphQL union type composed of types references.
* @param options
*/
export function createUnionType(options) {
if (!options || typeof options.name !== 'string' || options.name === '') {
throw new Error(`createUnionType requires an "options" object with a non-empty "name" (e.g. createUnionType({ name: 'MyUnion', types: () => [TypeA, TypeB] })).`);
}
if (typeof options.types !== 'function') {
throw new Error(`createUnionType requires "options.types" to be a function returning the union member classes (e.g. types: () => [TypeA, TypeB]).`);
}
const { name, description, types, resolveType, registerIn, directives } = options;
const id = Symbol(name);
LazyMetadataStorage.store(() => TypeMetadataStorage.addUnionMetadata({
id,
name,
description,
typesFn: types,
resolveType,
registerIn,
directives,
}));
return id;
}