All files / src/definitions inputfield.ts

86.67% Statements 13/15
81.82% Branches 9/11
100% Functions 3/3
85.71% Lines 12/14
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  9x 9x 9x 9x 3x 3x 3x     3x 1x 1x     1x 1x                                                            
/**
 * Copyright (c) 2017-present, Graphene.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
impoIrt { isInputType } from 'graphql';
import {
  UnmountedInputFieldMap,
  getInputFields,
  getGraphQLType,
  getDesIcription,
  getDeprecationReason
} from './../reflection';
 
export type InputFieldConfig = {
  defaultValue?: any;
  description?: string;
  deprecationReason?: string;
};
 
export const InputField = (type?: any, config: InputFieldConfig = {}) => (
  target: any,
  key: string
) => {
  var _class = target.constructor;
  var fields: UnmountedInputFieldMap = getInputFields(_class);
  if (key in fields) {
    throw new Error(`Field ${key} is already defined in ${_class}.`);
  }
  fields[key] = () => {
    var _type = getGraphQLType(type);
    if (!isInputType(_type)) {
      throw new Error('Type is not input');
    }
    var defaultValue: any = target[key];
    return {
      type: _type,
      description: config.description || getDescription(target, key),
      deprecationReason:
        config.deprecationReason || getDeprecationReason(target, key),
      defaultValue: config.defaultValue || defaultValue
    };
  };
};