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I,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;YAC7B,OAAO,eAAe,CAAC,MAAM,EAAE,YAAY,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;QACxE,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;QAE7B,OAAO,KAAK,CAAC;IACf,CAAC;IAID,OAAO,CACL,cAAc,CAAC,SAAS,CAAC;QACzB,CAAC,eAAe,CAAC,YAAY,CAAC,IAAI,YAAY,CAAC,YAAY,CAAC,CAAC;QAC7D,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,YAAY,CAAC,CAC1C,CAAC;AACJ,CAAC;AA4CD,MAAM,UAAU,cAAc,CAC5B,MAAqB,EACrB,KAA2B,EAC3B,KAA2B;IAG3B,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;YAG1B,OAAO,MAAM;iBACV,gBAAgB,CAAC,KAAK,CAAC;iBACvB,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;QACnD,CAAC;QAED,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAED,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAE1B,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;IAGD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/** @category Type Comparisons */\n\nimport type { GraphQLCompositeType, GraphQLType } from '../type/definition.ts';\nimport {\n  isAbstractType,\n  isInterfaceType,\n  isListType,\n  isNonNullType,\n  isObjectType,\n} from '../type/definition.ts';\nimport type { GraphQLSchema } from '../type/schema.ts';\n\n/**\n * Provided two types, return true if the types are equal (invariant).\n * @param typeA - The first GraphQL type to compare.\n * @param typeB - The second GraphQL type to compare.\n * @returns True when both types are equal.\n * @example\n * ```ts\n * import { GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql/type';\n * import { isEqualType } from 'graphql/utilities';\n *\n * isEqualType(GraphQLString, GraphQLString); // => true\n * isEqualType(new GraphQLList(GraphQLString), new GraphQLList(GraphQLString)); // => true\n * isEqualType(new GraphQLNonNull(GraphQLString), GraphQLString); // => false\n * ```\n */\nexport function isEqualType(typeA: GraphQLType, typeB: GraphQLType): boolean {\n  // Equivalent types are equal.\n  if (typeA === typeB) {\n    return true;\n  }\n\n  // If either type is non-null, the other must also be non-null.\n  if (isNonNullType(typeA) && isNonNullType(typeB)) {\n    return isEqualType(typeA.ofType, typeB.ofType);\n  }\n\n  // If either type is a list, the other must also be a list.\n  if (isListType(typeA) && isListType(typeB)) {\n    return isEqualType(typeA.ofType, typeB.ofType);\n  }\n\n  // Otherwise the types are not equal.\n  return false;\n}\n\n/**\n * Provided a type and a super type, return true if the first type is either\n * equal or a subset of the second super type (covariant).\n * @param schema - GraphQL schema to use.\n * @param maybeSubType - The possible subtype to compare.\n * @param superType - The possible supertype to compare.\n * @returns True when `maybeSubType` is equal to or a subtype of `superType`.\n * @example\n * ```ts\n * import { buildSchema } from 'graphql/utilities';\n * import {\n *   GraphQLNonNull,\n *   assertInterfaceType,\n *   assertObjectType,\n * } from 'graphql/type';\n * import { isTypeSubTypeOf } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n *   interface Node {\n *     id: ID!\n *   }\n *\n *   type User implements Node {\n *     id: ID!\n *   }\n *\n *   type Query {\n *     node: Node\n *   }\n * `);\n * const Node = assertInterfaceType(schema.getType('Node'));\n * const User = assertObjectType(schema.getType('User'));\n *\n * isTypeSubTypeOf(schema, User, Node); // => true\n * isTypeSubTypeOf(schema, new GraphQLNonNull(User), Node); // => true\n * isTypeSubTypeOf(schema, Node, User); // => false\n * ```\n */\nexport function isTypeSubTypeOf(\n  schema: GraphQLSchema,\n  maybeSubType: GraphQLType,\n  superType: GraphQLType,\n): boolean {\n  // Equivalent type is a valid subtype\n  if (maybeSubType === superType) {\n    return true;\n  }\n\n  // If superType is non-null, maybeSubType must also be non-null.\n  if (isNonNullType(superType)) {\n    if (isNonNullType(maybeSubType)) {\n      return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);\n    }\n    return false;\n  }\n  if (isNonNullType(maybeSubType)) {\n    // If superType is nullable, maybeSubType may be non-null or nullable.\n    return isTypeSubTypeOf(schema, maybeSubType.ofType, superType);\n  }\n\n  // If superType type is a list, maybeSubType type must also be a list.\n  if (isListType(superType)) {\n    if (isListType(maybeSubType)) {\n      return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType);\n    }\n    return false;\n  }\n  if (isListType(maybeSubType)) {\n    // If superType is not a list, maybeSubType must also be not a list.\n    return false;\n  }\n\n  // If superType type is an abstract type, check if it is super type of maybeSubType.\n  // Otherwise, the child type is not a valid subtype of the parent type.\n  return (\n    isAbstractType(superType) &&\n    (isInterfaceType(maybeSubType) || isObjectType(maybeSubType)) &&\n    schema.isSubType(superType, maybeSubType)\n  );\n}\n\n/**\n * Provided two composite types, determine if they \"overlap\". Two composite\n * types overlap when the Sets of possible concrete types for each intersect.\n *\n * This is often used to determine if a fragment of a given type could possibly\n * be visited in a context of another type.\n *\n * This function is commutative.\n * @param schema - GraphQL schema to use.\n * @param typeA - The first GraphQL type to compare.\n * @param typeB - The second GraphQL type to compare.\n * @returns True when the two composite types can apply to at least one common object type.\n * @example\n * ```ts\n * import { buildSchema } from 'graphql/utilities';\n * import { assertObjectType, assertUnionType } from 'graphql/type';\n * import { doTypesOverlap } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n *   type Photo {\n *     url: String!\n *   }\n *\n *   type Video {\n *     url: String!\n *   }\n *\n *   union Media = Photo | Video\n *   union StillImage = Photo\n *\n *   type Query {\n *     media: [Media]\n *   }\n * `);\n * const Media = assertUnionType(schema.getType('Media'));\n * const StillImage = assertUnionType(schema.getType('StillImage'));\n * const Video = assertObjectType(schema.getType('Video'));\n *\n * doTypesOverlap(schema, Media, StillImage); // => true\n * doTypesOverlap(schema, StillImage, Video); // => false\n * ```\n */\nexport function doTypesOverlap(\n  schema: GraphQLSchema,\n  typeA: GraphQLCompositeType,\n  typeB: GraphQLCompositeType,\n): boolean {\n  // Equivalent types overlap\n  if (typeA === typeB) {\n    return true;\n  }\n\n  if (isAbstractType(typeA)) {\n    if (isAbstractType(typeB)) {\n      // If both types are abstract, then determine if there is any intersection\n      // between possible concrete types of each.\n      return schema\n        .getPossibleTypes(typeA)\n        .some((type) => schema.isSubType(typeB, type));\n    }\n    // Determine if the latter type is a possible concrete type of the former.\n    return schema.isSubType(typeA, typeB);\n  }\n\n  if (isAbstractType(typeB)) {\n    // Determine if the former type is a possible concrete type of the latter.\n    return schema.isSubType(typeB, typeA);\n  }\n\n  // Otherwise the types do not overlap.\n  return false;\n}\n"]}