All files / services/utils/gql/types resolve.ts

93.62% Statements 44/47
89.47% Branches 34/38
80% Functions 4/5
93.02% Lines 40/43

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 9420x 20x 20x 20x               158x 158x 158x 1x   157x 105x   52x 4x   48x 2x   46x 3x   43x 2x   41x 21x   20x 1x   19x 1x   18x 6x   12x   20x   4x 4x 1x   3x 3x 3x     3x     2x 2x 2x 4x             20x                                                
import { isType, GraphQLType, GraphQLList, GraphQLNonNull } from 'graphql';
import { Thunk } from '~/services/types';
import {
  objectTypeRegistry,
  compileObjectType,
  inputObjectTypeRegistry,
  compileInputObjectType,
  enumsRegistry,
  unionRegistry,
} from '~/domains';
import { parseNativeTypeToGraphQL, isParsableScalar } from './parseNative';
 
/**
 *
 * @param input
 * @param allowThunk
 * @param preferInputType We want to be able to have single class used both for output and input type - thats why we need to be able to set resolve priority in different scenarios
 */
export function resolveType(
  input: any,
  preferInputType = false,
  allowThunk = true,
): GraphQLType {
  if (isType(input)) {
    return input;
  }
 
  if (isParsableScalar(input)) {
    return parseNativeTypeToGraphQL(input);
  }
 
  if (Array.isArray(input)) {
    return resolveListType(input);
  }
 
  if (enumsRegistry.has(input)) {
    return enumsRegistry.get(input);
  }
 
  if (unionRegistry.has(input)) {
    return unionRegistry.get(input)();
  }
 
  if (preferInputType && inputObjectTypeRegistry.has(input)) {
    return compileInputObjectType(input);
  }
 
  ifE (objectTypeRegistry.has(input)) {
    return compileObjectType(input);
  }
 
  if (inputObjectTypeRegistry.has(input)) {
    return compileInputObjectType(input);
  }I

  if (input === Promise) {
    return;
  }
 
  ifE (!allowThunk || typeof input !== 'function') {
    Ereturn;
  }
 
  return resolveType(input(), preferInputType, false);
}

function resolveListType(input: any[], preferInputType = false): GraphQLType {
  if (input.length !== 1) {
    return;
  }
  const [itemType] = input;
 
  const resolvedItemType = resolveType(itemType, preferInputType);
 
  if (!resolvedItemType) {
    return;
  }
  return new GraphQLList(new GraphQLNonNull(resolvedItemType));
}
 
export function resolveTypesList(
  types: Thunk<any[]>,
  preferInputType = false,
): GraphQLType[] {
  if (Array.isArray(types)) {
    return types.map(type => {
      return resolveType(type, preferInputType);
    });
  }
  return types().map(type => {
    return resolveType(type, preferInputType);
  });
}