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

91.43% Statements 32/35
75% Branches 6/8
100% Functions 5/5
90.63% Lines 29/32

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
    7720x
    20x
    20x
    20x
    20x
    20x
    20x

    94x
    94x
    90x

    89x

    85x

    85x
    85x

    20x

    64x
    64x
    64x
    77x
    1x

    76x
    76x

    54x

    60x
    60x
    60x
    64x

    50x

    20x

     import { GraphQLFieldConfig, GraphQLFieldConfigMap } from 'graphql';
    import { getClassWithAllParentClasses } from '~/services/utils/inheritance';
    import { FieldError, fieldsRegistry } from '../index';

    import { compileFieldResolver } from './resolver';
    import { compileFieldArgs } from '~/domains/arg';
    import {
      enhanceType,
      isRootFieldOnNonRootBase,
      resolveRegisteredOrInferedType,
      validateResolvedType,
    } from './services';
    I
    import { validateNotInferableField } from './fieldType';

    export function compileFieldConfig(
      taIrget: Function,
      fieldName: string,
    ): GraphQLFieldConfig<any, any, any> {
      const { type, description, isNullable } = fieldsRegistry.get(
        target,
        fieldName,
      );
      const args = compileFieldArgs(target, fieldName);

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

      // if was not able to resolve type, try to show some helpful information about it
      if (!resolvedType && !validateNotInferableField(target, fieldName)) {
        return;
      }

      // show error about being not able to resolve field type
      if (!validateResolvedType(target, fieldName, resolvedType)) {
        validateNotInferableField(target, fieldName);
        return;
      }

      const finalType = enhanceType(resolvedType, isNullable);

      return {
        description,
        type: finalType,
        resolve: compileFieldResolver(target, fieldName),
        args,
      };
    }

    function getAllFields(target: Function) {
      const fields = fieldsRegistry.getAll(target);
      const finalFieldsMap: GraphQLFieldConfigMap<any, any> = {};
      Object.keys(fields).forEach(fieldName => {
        if (isRootFieldOnNonRootBase(target, fieldName)) {
          throw new FieldError(
            target,
            fieldName,
            `Given field is root field (@Query or @Mutation) not registered inside @Schema type. `,
          );
        }

        const config = fieldsRegistry.get(target, fieldName);
        finalFieldsMap[config.name] = compileFieldConfig(target, fieldName);
      });
      return finalFieldsMap;
    }

    export function compileAllFields(target: Function) {
      const targetWithParents = getClassWithAllParentClasses(target);

      const finalFieldsMap: GraphQLFieldConfigMap<any, any> = {};

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