All files / domains/inputField/compiler fieldType.ts

64.29% Statements 9/14
25% Branches 1/4
50% Functions 1/2
64.29% Lines 9/14

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 3919x 19x 19x 19x               19x   5x 5x     5x   19x                                      
import { GraphQLType } from 'graphql';
import { inferTypeByTarget } from '~/services/utils';
import { InputFieldError } from '../index';
 
import { resolveType } from '~/services/utils';

export function resolveTypeOrThrow(
  type: any,
  target: Function,
  fieldName: string,
): GraphQLType {
  const resolvedType = resolveType(type);
 
  if (!resolvedType) {
    Ithrow new InputFieldError(
      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);
  if (!inferedType) {
    throw new InputFieldError(
      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);
}