All files / domains/field/compiler services.ts

88.89% Statements 24/27
75% Branches 9/12
100% Functions 4/4
88.89% Lines 24/27

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 6619x 19x 19x 19x 19x   85x 9x   76x   19x   80x     80x   19x   80x 80x 1x   80x   19x   73x 73x     73x     73x 1x   72x   19x                                                  
import {
  isOutputType,
  GraphQLType,
  GraphQLOutputType,
  GraphQLNonNull,
} from 'graphql';
import { FieldError } from '../index';
 
import { resolveTypeOrThrow, inferTypeOrThrow } from './fieldType';
import {
  mutationFieldsRegistry,
  queryFieldsRegistry,
  isSchemaRoot,
} frIom '~/domains/schema';

export function resolveRegisteredOrInferedType(
  target: Function,
  fieldName: string,
  forcedType?: any,
) {
  if (forcedType) {
    return resolveTypeOrThrow(forcedType, target, fieldName);
  }
  return inferTypeOrThrow(target, fieldName);
}
 
export function validateResolvedType(
  target: Function,
  fieldName: string,
  tyIpe: GraphQLType,
): type is GraphQLOutputType {
  if (!isOutputType(type)) {
    Ithrow new FieldError(
      target,
      fieldName,
      `Validation of type failed. Resolved type for @Field must be GraphQLOutputType.`,
    );
  }
  return true;
}
 
export function enhanceType(
  originalType: GraphQLOutputType,
  isNullable: boolean,
) {
  let finalType = originalType;
  if (!isNullable) {
    finalType = new GraphQLNonNull(finalType);
  }
  return finalType;
}
 
export function isRootFieldOnNonRootBase(base: Function, fieldName: string) {
  const isRoot = isSchemaRoot(base);
  if (isRoot) {
    return false;
  }
  if (mutationFieldsRegistry.has(base, fieldName)) {
    return true;
  }
  if (queryFieldsRegistry.has(base, fieldName)) {
    return true;
  }
  return false;
}