UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

1 lines 5.37 kB
{"version":3,"file":"typeFromAST.js","sourceRoot":"","sources":["../../src/utilities/typeFromAST.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,IAAI,EAAE,8BAA6B;AAO5C,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,+BAA8B;AAmHpE,MAAM,UAAU,WAAW,CACzB,MAAqB,EACrB,QAAkB;IAElB,QAAQ,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtB,KAAK,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;YACpB,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;YACrD,OAAO,SAAS,IAAI,IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;QACjD,CAAC;QACD,KAAK,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC;YACxB,MAAM,SAAS,GAAG,WAAW,CAAC,MAAM,EAAE,QAAQ,CAAC,IAAI,CAEtC,CAAC;YACd,OAAO,SAAS,IAAI,IAAI,cAAc,CAAC,SAAS,CAAC,CAAC;QACpD,CAAC;QACD,KAAK,IAAI,CAAC,UAAU;YAClB,OAAO,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC","sourcesContent":["/** @category Values */\n\nimport type {\n ListTypeNode,\n NamedTypeNode,\n NonNullTypeNode,\n TypeNode,\n} from '../language/ast.ts';\nimport { Kind } from '../language/kinds.ts';\n\nimport type {\n GraphQLNamedType,\n GraphQLNullableType,\n GraphQLType,\n} from '../type/definition.ts';\nimport { GraphQLList, GraphQLNonNull } from '../type/definition.ts';\nimport type { GraphQLSchema } from '../type/schema.ts';\n\n/**\n * Given a Schema and an AST node describing a type, return a GraphQLType\n * definition which applies to that type. For example, if provided the parsed\n * AST node for `[User]`, a GraphQLList instance will be returned, containing\n * the type called \"User\" found in the schema. If a type called \"User\" is not\n * found in the schema, then undefined will be returned.\n * @param schema - GraphQL schema to use.\n * @param typeNode - The GraphQL type AST node to resolve.\n * @returns The GraphQL type referenced by the AST node, or undefined if it cannot be resolved.\n * @example\n * ```ts\n * import { parseType } from 'graphql/language';\n * import { buildSchema, typeFromAST } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n * type Query {\n * name: String\n * }\n * `);\n *\n * typeFromAST(schema, parseType('String'))?.toString(); // => 'String'\n * typeFromAST(schema, parseType('Missing')); // => undefined\n * ```\n */\nexport function typeFromAST(\n schema: GraphQLSchema,\n typeNode: NamedTypeNode,\n): GraphQLNamedType | undefined;\n/**\n * Resolves a list type AST node against a schema.\n * @param schema - GraphQL schema to use.\n * @param typeNode - The list type AST node to resolve.\n * @returns The GraphQL list type referenced by the AST node, or undefined if\n * it cannot be resolved.\n * @example\n * ```ts\n * import { parseType } from 'graphql/language';\n * import { buildSchema, typeFromAST } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n * type Query {\n * tags: [String]\n * }\n * `);\n *\n * typeFromAST(schema, parseType('[String]'))?.toString(); // => '[String]'\n * typeFromAST(schema, parseType('[Missing]')); // => undefined\n * ```\n */\nexport function typeFromAST(\n schema: GraphQLSchema,\n typeNode: ListTypeNode,\n): GraphQLList<any> | undefined;\n/**\n * Resolves a non-null type AST node against a schema.\n * @param schema - GraphQL schema to use.\n * @param typeNode - The non-null type AST node to resolve.\n * @returns The GraphQL non-null type referenced by the AST node, or undefined\n * if it cannot be resolved.\n * @example\n * ```ts\n * import { parseType } from 'graphql/language';\n * import { buildSchema, typeFromAST } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n * type Query {\n * name: String!\n * }\n * `);\n *\n * typeFromAST(schema, parseType('String!'))?.toString(); // => 'String!'\n * typeFromAST(schema, parseType('[String!]!'))?.toString(); // => '[String!]!'\n * ```\n */\nexport function typeFromAST(\n schema: GraphQLSchema,\n typeNode: NonNullTypeNode,\n): GraphQLNonNull<any> | undefined;\n/**\n * Resolves a type AST node against a schema.\n * @param schema - GraphQL schema to use.\n * @param typeNode - The GraphQL type AST node to resolve.\n * @returns The GraphQL type referenced by the AST node, or undefined if it\n * cannot be resolved.\n * @example\n * ```ts\n * import { parseType } from 'graphql/language';\n * import { buildSchema, typeFromAST } from 'graphql/utilities';\n *\n * const schema = buildSchema(`\n * type User {\n * name: String\n * }\n *\n * type Query {\n * users: [User!]!\n * }\n * `);\n *\n * typeFromAST(schema, parseType('User'))?.toString(); // => 'User'\n * typeFromAST(schema, parseType('[User!]!'))?.toString(); // => '[User!]!'\n * typeFromAST(schema, parseType('Missing')); // => undefined\n * ```\n */\nexport function typeFromAST(\n schema: GraphQLSchema,\n typeNode: TypeNode,\n): GraphQLType | undefined;\n/**\n * Resolves a type AST node against a schema.\n * @internal\n */\nexport function typeFromAST(\n schema: GraphQLSchema,\n typeNode: TypeNode,\n): GraphQLType | undefined {\n switch (typeNode.kind) {\n case Kind.LIST_TYPE: {\n const innerType = typeFromAST(schema, typeNode.type);\n return innerType && new GraphQLList(innerType);\n }\n case Kind.NON_NULL_TYPE: {\n const innerType = typeFromAST(schema, typeNode.type) as\n | GraphQLNullableType\n | undefined;\n return innerType && new GraphQLNonNull(innerType);\n }\n case Kind.NAMED_TYPE:\n return schema.getType(typeNode.name.value);\n }\n}\n"]}