# All files / domains/inputField/compiler index.ts

92.5% Statements 37/40
62.5% Branches 5/8
100% Functions 8/8
91.67% Lines 33/36

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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    9720x
    20x
    20x
    20x
    20x

    5x

    5x

    5x

    5x

    5x
    5x
    4x

    5x

    5x
    5x
    5x

    5x
    5x
    5x

    20x

    5x
    5x
    5x
    5x
    5x

    5x

    20x

    3x
    3x
    3x
    5x

    3x

    20x

     import {
      GraphQLType,
      isInputType,
      GraphQLInputType,
      GraphQLInputFieldConfig,
      GraphQLInputFieldConfigMap,
      GrIaphQLNonNull,
    } from 'graphql';
    import { getClassWithAllParentClasses } from '~/services/utils';
    import { InputFieldError, inputFieldsRegistry } from '../index';

    import { resolveTypeOrThrow, inferTypeOrThrow } from './fieldType';
    I
    function getFinalInputFieldType(
      target: Function,
      fieldName: string,
      forcedType?: any,
    ) {
      if (forcedType) {
        return resolveTypeOrThrow(forcedType, target, fieldName);
      }
      return inferTypeOrThrow(target, fieldName);
    }

    function validateResolvedType(
      target: Function,
      fieldName: string,
      tyIpe: GraphQLType,
    ): type is GraphQLInputType {
      if (!isInputType(type)) {
        throw new InputFieldError(
          target,
          fieldName,
          `Validation of type failed. Resolved type for @Field must be GraphQLInputType.`,
        );
      }
      return true;
    }

    function enhanceType(originalType: GraphQLInputType, isNullable: boolean) {
      let finalType = originalType;
      if (!isNullable) {
        finalType = new GraphQLNonNull(finalType);
      }
      return finalType;
    }

    export function compileInputFieldConfig(
      target: Function,
      fieldName: string,
    ): GraphQLInputFieldConfig {
      const {
        type,
        description,
        defaultValue,
        isNullable,
      } = inputFieldsRegistry.get(target, fieldName);

      const resolvedType = getFinalInputFieldType(target, fieldName, type);

      if (!validateResolvedType(target, fieldName, resolvedType)) {
        return;
      }

      resolvedType;
      const finalType = enhanceType(resolvedType, isNullable);

      return {
        description,
        defaultValue,
        type: finalType,
      };
    }

    export function compileAllInputFieldsForSingleTarget(target: Function) {
      const fields = inputFieldsRegistry.getAll(target);
      const finalFieldsMap: GraphQLInputFieldConfigMap = {};
      Object.keys(fields).forEach(fieldName => {
        const config = inputFieldsRegistry.get(target, fieldName);
        finalFieldsMap[config.name] = compileInputFieldConfig(target, fieldName);
      });
      return finalFieldsMap;
    }

    export function compileAllInputFields(target: Function) {
      const targetWithParents = getClassWithAllParentClasses(target);
      const finalFieldsMap: GraphQLInputFieldConfigMap = {};

      targetWithParents.forEach(targetLevel => {
        Object.assign(
          finalFieldsMap,
          compileAllInputFieldsForSingleTarget(targetLevel),
        );
      });
      return finalFieldsMap;
    }