All files / domains/arg compiler.ts

93.75% Statements 45/48
90.63% Branches 29/32
100% Functions 8/8
93.48% Lines 43/46
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 1159x 9x 9x 9x 9x 9x 9x   17x 17x 20x 20x     20x   17x 20x       17x 20x 20x 1x   19x     19x 1x     15x     15x 15x 15x 15x 3x   12x 12x 18x 18x   18x 9x   9x         12x     37x 37x   37x 20x   17x 17x     15x   9x                                                                                        
import {
  GraphQLFieldConfigArgumentMap,
  GraphQLType,
  GraphQLInputType,
  isInputType,
} from 'graphql';
import { resolveType } from 'services/utils';
import { injectorRegistry } from 'domains/inject';
import { ArgsIndex, argRegistry } from './registry';
import { ArgError } from './error';
import { getParameterNames } from 'services/utils';
I
function compileInferedAndRegisterdArgs(infered: any[], registeredArgs: ArgsIndex = {}) {
  const argsMerged = infered.map((inferedType, index) => {
    const registered = registeredArgs[index];
    if (registered && registered.type) {
      return registered.type;
    }
    return inferedType;
  });
 
  return argsMerged.map((rawType, index) => {
    return resolveType(rawType);
  });
}
 
functionI validateArgs(
  target: Function,
  fieldName: string,
  types: GraphQLType[],
): types is GraphQLInputType[] {
  types.forEach((argType, argIndex) => {
    const isInjectedArg = injectorRegistry.has(target, fieldName, argIndex);
 
    if (!isInjectedArg && !argType) {
      throw new ArgError(target, fieldName, argIndex, `Could not infer type of argument`);
    }
 
    if (!isInjectedArg && !isInputType(argType)) {
      throw new ArgError(
        target,
        fieldName,
        argIndex,
        `Argument must be of type GraphQLInputType`,
      );
    }
 
    if (isInjectedArg && argRegistry.has(target, fieldName, argIndex)) {
      throw new ArgError(
        target,
        fieldName,
        argIndex,
        `Argument cannot be marked wiht both @Arg and @Inject or custom injector`,
      );
    }
  });
  return true;
}
 
function convertArgsArrayToArgsMap(
  target: Function,
  fieldName: string,
  argsTypes: GraphQLInputType[],
  registeredArgs: ArgsIndex = {},
): GraphQLFieldConfigArgumentMap {
  coInst functionDefinition = target.prototype[fieldName];
  const argNames = getParameterNames(functionDefinition);
 
  if (!argNames || !argNames.length) {
    return {};
  }
 
  const argsMap: GraphQLFieldConfigArgumentMap = {};
  argNames.forEach((argName, index) => {
    const argConfig = registeredArgs[index];
    const argType = argsTypes[index];
 
    // don't publish args marked as auto Injected
    if (injectorRegistry.has(target, fieldName, index)) {
      return;
    }
 
    argsMap[argName] = {
      type: argType,
      description: argConfig && argConfig.description,
    };
  });
  return argsMap;
}
 
export function compileFieldArgs(
  target: Function,
  fieldName: string,
): GraphQLFieldConfigArgumentMap {
  const registeredArgs = argRegistry.getAll(target)[fieldName];
  const inferedRawArgs = Reflect.getMetadata(
    'design:paramtypes',
    target.prototype,
    fieldName,
  );
 
  // There are no arguments
  if (!inferedRawArgs) {
    return {};
  }
 
  const argTypes = compileInferedAndRegisterdArgs(inferedRawArgs, registeredArgs);
 
  if (!validateArgs(target, fieldName, argTypes)) {
    return;
  }
 
  return convertArgsArrayToArgsMap(target, fieldName, argTypes, registeredArgs);
}