All files / domains/inputObjectType/compiler objectType.ts

74.19% Statements 23/31
58.33% Branches 7/12
83.33% Functions 5/6
76.67% Lines 23/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 5817x               17x 17x 17x 17x 17x 17x   5x 5x 8x   5x     5x 3x       7x 2x   5x 5x 5x   17x   7x     7x 7x   17x                              
import { GraphQLInputObjectType, GraphQLInputFieldConfigMap } from 'graphql';
import { InputObjectTypeError, inputObjectTypeRegistry } from '../index';

import { compileAllInputFields, inputFieldsRegistry } from 'domains/inputField';
import { createCachedThunk, getClassWithAllParentClasses } from 'services/utils';
 
const compileOutputTypeCache = new WeakMap<Function, GraphQLInputObjectType>();
 
export interface TypeOptions {
  name: string;
  description?: string;
}
 
function createTypeInputFieldsGetter(target: Function): () => GraphQLInputFieldConfigMap {
  const targetWithParents = getClassWithAllParentClasses(target);
  const hasFields = targetWithParents.some(ancestor => {
    return !inputFieldsRegistry.isEmpty(ancestor);
  });
 
  ifI (!hasFields) {
    throw new InputObjectTypeError(target, `There are no fields inside this type.`);
  }
 
  return createCachedThunk(() => {
    return compileAllInputFields(target);
  });
}
 
export function compileInputObjectTypeWithConfig(
  target: Function,
  config: TypeOptions,
): GraphQLInputObjectType {
  if (compileOutputTypeCache.has(target)) {
    return compileOutputTypeCache.get(target);
  }
 
  coInst compiled = new GraphQLInputObjectType({
    ...config,
    fields: createTypeInputFieldsGetter(target),
  });
 
  compileOutputTypeCache.set(target, compiled);
  return compiled;
}
 
export function compileInputObjectType(target: Function) {
  if (!inputObjectTypeRegistry.has(target)) {
    throw new InputObjectTypeError(
      target,
      `Class is not registered. Make sure it's decorated with @InputObjectType decorator`,
    );
  }
 
  const compiler = inputObjectTypeRegistry.get(target);
 
  return compiler();
}