All files / domains/field/compiler fieldType.ts

90.91% Statements 20/22
80% Branches 8/10
100% Functions 3/3
90.91% Lines 20/22

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 6520x 20x 20x   9x 9x 1x   8x   20x   81x 81x     81x   20x   4x 4x 1x   3x 1x   2x 2x       20x                                                                
import { GraphQLType } from 'graphql';
import { inferTypeByTarget, resolveType } from '~/services/utils';
import { FieldError } from '../index';
 
export function resolveTypeOrThrow(
  type: any,
  target: Function,
  fieldName: string,
): GraphQLType {
  const resolvedType = resolveType(type);
 
  if (!resolvedType) {
    throw new FieldError(
    I  target,
      fieldName,
      `Forced type is incorrect. Make sure to use either native graphql type or class that is registered with @Type decorator`,
    );
  }
 
  return resolvedType;
}
 
export function inferTypeOrThrow(
  target: Function,
  fieldName: string,
): GraphQLType {
  const inferedType = inferTypeByTarget(target.prototype, fieldName);
  ifE (!inferedType) {
    throw new FieldError(
      target,
      fieldName,
      `Could not infer return type and no type is forced. In case of circular dependencies make sure to force types of instead of infering them.`,
    );
  }
  return resolveType(inferedType);
}
 
export function validateNotInferableField(target: Function, fieldName: string) {
  const inferedType = inferTypeByTarget(target.prototype, fieldName);
  if (inferedType === Array) {
    throw new FieldError(
      target,
      fieldName,
      `Field returns list so it's required to explicitly set list item type. You can set list type like: @Field({ type: [ItemType] })`,
    );
  }
 
  if (inferedType === Promise) {
    throw new FieldError(
      target,
      fieldName,
      `Field returns Promise so it's required to explicitly set resolved type as it's not possible to guess it. You can set resolved type like: @Field({ type: ItemType })`,
    );
  }
 
  if (inferedType === Object) {
    throw new FieldError(
      target,
      fieldName,
      `It was not possible to guess type of this field. It might be because it returns Promise, Array etc. In such case it's needed to explicitly declare type of field like: @Field({ type: ItemType })`,
    );
  }
  return true;
}