<commit_before>import {GraphQLUnionType, GraphQLObjectType} from "graphql"

import invariant from "@octetstream/invariant"

import isPlainObject from "../util/internal/isPlainObject"
import isObjectType from "../util/internal/isObjectType"
import omitNullish from "../util/internal/omitNullish"
import apply from "../util/internal/selfInvokingClass"
import isString from "../util/internal/isString"
import typeOf from "../util/internal/typeOf"
import proxy from "../util/internal/proxy"

import Base from "./Base"

const isArray = Array.isArray

/**
 * Defines a new Unit type
 *
 * @example
 *
 * const TSearchable = Union("Searchable", [TBook, TMovie])
 *   .match(({author}) => author && TBook)
 *   .match(({director}) => director && TMovie)
 * .end()
 */
@proxy({apply})
class Union extends Base {
  constructor(name, description, types, astNode) {
    if (isPlainObject(name)) {
      [name, description, types] = [
        name.name, name.description, name.types, name.astNode
      ]
    } else if (isArray(description) || isObjectType(description)) {
      [astNode, types, description] = [types, description, undefined]
    }

    invariant(!name, "Union type constructor requires a name.")

    invariant(
      !isString(name), TypeError,
      "The name should be a string. Received %s", typeOf(name)
    )

    super(name, description)

    invariant(!types, "Types list required.")

    this.__types = isArray(types) ? types : [types]
    this.__astNode = astNode

    this.__predicates = []
  }

  __resolveType = async (source, ctx, info) => {
    for (const predicate of this.__predicates) {
      const resolvedType = await predicate(source, ctx, info)

      if (resolvedType instanceof GraphQLObjectType || isString(resolvedType)) {
        return resolvedType
      }
    }
  }

  match = (predicate, ctx = null) => {
    this.__predicates.push(ctx ? predicate.bind(ctx) : predicate)

    return this
  }

  end() {
    return new GraphQLUnionType(omitNullish({
      name: this._name,
      description: this._description,
      types: this.__types,
      resolveType: this.__resolveType
    }))
  }
}

export default Union<commit_msg>Add checking of a predicate type in Union#match method.<commit_after>import {GraphQLUnionType, GraphQLObjectType} from "graphql"

import invariant from "@octetstream/invariant"

import isPlainObject from "../util/internal/isPlainObject"
import isObjectType from "../util/internal/isObjectType"
import isFunction from "../util/internal/isFunction"
import omitNullish from "../util/internal/omitNullish"
import apply from "../util/internal/selfInvokingClass"
import isString from "../util/internal/isString"
import typeOf from "../util/internal/typeOf"
import proxy from "../util/internal/proxy"

import Base from "./Base"

const isArray = Array.isArray

/**
 * Defines a new Unit type
 *
 * @example
 *
 * const TSearchable = Union("Searchable", [TBook, TMovie])
 *   .match(({author}) => author && TBook)
 *   .match(({director}) => director && TMovie)
 * .end()
 */
@proxy({apply})
class Union extends Base {
  constructor(name, description, types, astNode) {
    if (isPlainObject(name)) {
      [name, description, types] = [
        name.name, name.description, name.types, name.astNode
      ]
    } else if (isArray(description) || isObjectType(description)) {
      [astNode, types, description] = [types, description, undefined]
    }

    invariant(!name, "Union type constructor requires a name.")

    invariant(
      !isString(name), TypeError,
      "The name should be a string. Received %s", typeOf(name)
    )

    super(name, description)

    invariant(!types, "Types list required.")

    this.__types = isArray(types) ? types : [types]
    this.__astNode = astNode

    this.__predicates = []
  }

  __resolveType = async (source, ctx, info) => {
    for (const predicate of this.__predicates) {
      const resolvedType = await predicate(source, ctx, info)

      if (resolvedType instanceof GraphQLObjectType || isString(resolvedType)) {
        return resolvedType
      }
    }
  }

  match = (predicate, ctx = null) => {
    invariant(
      !isFunction(predicate), TypeError,
      "Predicate should be a function. Received %s", typeOf(predicate)
    )

    this.__predicates.push(ctx ? predicate.bind(ctx) : predicate)

    return this
  }

  end() {
    return new GraphQLUnionType(omitNullish({
      name: this._name,
      description: this._description,
      types: this.__types,
      resolveType: this.__resolveType,
      astNode: this.__astNode
    }))
  }
}

export default Union
{'fraction_non_alphanumeric': 0.12762237762237763, 'fraction_numerical': 0.0, 'mean_word_length': 3.409441233140655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621482', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1335, 'n_words': 362}