# 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
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    6920x

    20x
    20x
    20x
    20x
    20x
    20x

    5x
    5x
    5x

    5x

    5x
    3x

    7x
    2x

    5x
    5x
    5x

    20x

    7x

    7x
    7x

    20x

     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;
    }

    funcItion createTypeInputFieldsGetter(
      target: Function,
    ): () => GraphQLInputFieldConfigMap {
      const targetWithParents = getClassWithAllParentClasses(target);
      const hasFields = targetWithParents.some(ancestor => {
        return !inputFieldsRegistry.isEmpty(ancestor);
      });

      if (!hasFields) {
        throw new InputObjectTypeError(
          target,
          `There are no fields inside this type.`,
        );
      }

      return createCachedThunk(() => {
        return compileAllInputFields(target);
      })I;
    }

    export function compileInputObjectTypeWithConfig(
      target: Function,
      config: TypeOptions,
    ): GraphQLInputObjectType {
      if (compileOutputTypeCache.has(target)) {
        return compileOutputTypeCache.get(target);
      }

      const 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();
    }