All files / src/definitions inputobjecttype.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 2/2
100% Lines 8/8
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  8x 8x 8x 8x 4x 4x 3x         3x                                                            
/**
 * 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.
 *
 */
import { GraphQLInputObjectType } from "graphql";
import {
  UnmountedInputFieldMap,
  getInputFields,
  assertInputFields,
  setGraphQLType,
  getDescription,
  mountInputFields
} from "./../reflection";
 
// The provided configuration type when creating an InputObjectType.
export type InputObjectTypeConfig = {
  name?: string;
  description?: string;
};
 
export const InputObjectType = (opts: InputObjectTypeConfig = {}) => <
  T extends { new (...args: any[]): any }
>(
  target: T
): T => {
  const fields: UnmountedInputFieldMap = getInputFields(target);
  assertInputFields(target, fields);
 
  setGraphQLType(
    target,
    new GraphQLInputObjectType({
      name: opts.name || target.name,
      description: opts.description || getDescription(target),
      fields: mountInputFields(fields)
    })
  );
 
  return target;
};