All files / src/definitions enum.ts

100% Statements 11/11
100% Branches 5/5
100% Functions 3/3
100% Lines 10/10
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  8x 8x 8x 8x 8x 6x 6x 12x           6x         6x                                                  
/**
 * 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 { GraphQLEnumValueConfigMap, GraphQLEnumType } from "graphql";
import {
  getDescription,
  getDeprecationReason,
  setGraphQLType
} from "./../reflection";
import { getStaticProperties } from "./utils";
 
// The provided configuration type when creating an EnumType.
export type EnumTypeConfig = {
  name?: string;
  description?: string;
};
 
export const EnumType = (opts: EnumTypeConfig = {}) => <
  T extends { new (...args: any[]): {}; [key: string]: any }
>(
  target: T
): T => {
  let values: GraphQLEnumValueConfigMap = {};
  getStaticProperties(target).forEach(name => {
    values[name] = {
      value: target[name],
      description: getDescription(target, name),
      deprecationReason: getDeprecationReason(target, name)
    };
  });
  setGraphQLType(
    target,
    new GraphQLEnumType({
      name: opts.name || target.name,
      description: opts.description || getDescription(target),
      values: values
    })
  );
  return target;
};