<commit_before>/**
 *  Copyright (c) 2015, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import {
  graphql,
  GraphQLSchema,
  GraphQLEnumType,
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLBoolean,
} from '../../';


describe('Type System: Enum Values', () => {

  const ColorType = new GraphQLEnumType({
    name: 'Color',
    values: {
      RED: { value: 0 },
      GREEN: { value: 1 },
      BLUE: { value: 2 },
    }
  });

  const Complex1 = { someRandomObject: 1 };
  const Complex2 = { someOtherRandomObject: 2 };

  const ComplexEnum = new GraphQLEnumType({
    name: 'Complex',
    values: {
      ONE: { value: Complex1 },
      TWO: { value: Complex2 },
    }
  });

  const QueryType = new GraphQLObjectType({
    name: 'Query',
    fields: {
      colorEnum: {
        type: ColorType,
        args: {
          fromEnum: { type: ColorType },
          fromInt: { type: GraphQLInt },
          fromString: { type: GraphQLString },
        },
        resolve(value, { fromEnum, fromInt, fromString }) {
          return fromInt !== undefined ? fromInt :
            fromString !== undefined ? fromString :
            fromEnum;
        }
      },
      colorInt: {
        type: GraphQLInt,
        args: {
          fromEnum: { type: ColorType },
          fromInt: { type: GraphQLInt },
        },
        resolve(value, { fromEnum, fromInt }) {
          return fromInt !== undefined ? fromInt : fromEnum;
        }
      },
      complexEnum: {
        type: ComplexEnum,
        args: {
          fromEnum: { type: ComplexEnum },
          provideBadValue: { type: GraphQLBoolean }
        },
        resolve(value, { fromEnum, provideBadValue }) {
          if (provideBadValue) {
            // Note: similar shape, but not the same *reference*
            // as Complex1 above. Enum internal values require === equality.
            return { someRandomObject: 1 };
          }
          return fromEnum || Complex1;
        }
      }
    }
  });

  const MutationType = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      favoriteEnum: {
        type: ColorType,
        args: { color: { type: ColorType } },
        resolve(value, { color }) { return color; }
      }
    }
  });

  const SubscriptionType = new GraphQLObjectType({
    name: 'Subscription',
    fields: {
      subscribeToEnum: {
        type: ColorType,
        args: { color: { type: ColorType } },
        resolve(value, { color }) { return color; }
      }
    }
  });

  const schema = new GraphQLSchema({
    query: QueryType,
    mutation: MutationType,<commit_msg>[test] Illustrate use of defaultValue for Enum-typed arguments<commit_after>/**
 *  Copyright (c) 2015, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import {
  graphql,
  GraphQLSchema,
  GraphQLEnumType,
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLBoolean,
} from '../../';


describe('Type System: Enum Values', () => {

  const ColorType = new GraphQLEnumType({
    name: 'Color',
    values: {
      RED: { value: 0 },
      GREEN: { value: 1 },
      BLUE: { value: 2 },
    }
  });

  const Complex1 = { someRandomObject: 1 };
  const Complex2 = { someOtherRandomObject: 2 };

  const ComplexEnum = new GraphQLEnumType({
    name: 'Complex',
    values: {
      ONE: { value: Complex1 },
      TWO: { value: Complex2 },
    }
  });

  const QueryType = new GraphQLObjectType({
    name: 'Query',
    fields: {
      colorEnum: {
        type: ColorType,
        args: {
          fromEnum: { type: ColorType },
          fromInt: { type: GraphQLInt },
          fromString: { type: GraphQLString },
        },
        resolve(value, { fromEnum, fromInt, fromString }) {
          return fromInt !== undefined ? fromInt :
            fromString !== undefined ? fromString :
            fromEnum;
        }
      },
      colorInt: {
        type: GraphQLInt,
        args: {
          fromEnum: { type: ColorType },
          fromInt: { type: GraphQLInt },
        },
        resolve(value, { fromEnum, fromInt }) {
          return fromInt !== undefined ? fromInt : fromEnum;
        }
      },
      complexEnum: {
        type: ComplexEnum,
        args: {
          fromEnum: {
            type: ComplexEnum,
            // Note: defaultValue is provided an *internal* representation for
            // Enums, rather than the string name.
            defaultValue: Complex1
          },
          provideBadValue: { type: GraphQLBoolean }
        },
        resolve(value, { fromEnum, provideBadValue }) {
          if (provideBadValue) {
            // Note: similar shape, but not the same *reference*
            // as Complex1 above. Enum internal values require === equality.
            return { someRandomObject: 1 };
          }
          return fromEnum;
        }
      }
    }
  });

  const MutationType = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
      favoriteEnum: {
        type: ColorType,
        args: { color: { type: ColorType } },
        resolve(value, { color }) { return color; }
      }
    }
  });

  const SubscriptionType = new GraphQLObjectType({
    name: 'Subscription',
    fields: {
      subscribeToEnum: {
        type: ColorType,
        args: { color: { type: ColorType } },
        resolve(value, { color }) { return color; }
      }
    }
  });

  const schema = new GraphQLSchema({
    query: QueryType,
    mutation: MutationType,
{'fraction_non_alphanumeric': 0.11508398469981707, 'fraction_numerical': 0.005321802760685182, 'mean_word_length': 2.0160481444333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7373461', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1720, 'n_words': 497}