UNPKG

3.38 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2import {
3 DocumentNode,
4 Location,
5 StringValueNode,
6 TypeDefinitionNode,
7 NamedTypeNode,
8 DirectiveDefinitionNode,
9 FieldDefinitionNode,
10 InputValueDefinitionNode,
11 EnumValueDefinitionNode,
12 TypeNode,
13} from '../language/ast';
14import {
15 GraphQLNamedType,
16 GraphQLFieldConfig,
17 GraphQLInputField,
18 GraphQLEnumValueConfig,
19 GraphQLType,
20 GraphQLArgumentConfig,
21 GraphQLInputFieldConfig,
22} from '../type/definition';
23import { GraphQLDirective } from '../type/directives';
24import { Source } from '../language/source';
25import { GraphQLSchema, GraphQLSchemaValidationOptions } from '../type/schema';
26import { ParseOptions } from '../language/parser';
27import { dedentBlockStringValue } from '../language/blockString';
28
29interface BuildSchemaOptions extends GraphQLSchemaValidationOptions {
30 /**
31 * Descriptions are defined as preceding string literals, however an older
32 * experimental version of the SDL supported preceding comments as
33 * descriptions. Set to true to enable this deprecated behavior.
34 * This option is provided to ease adoption and will be removed in v16.
35 *
36 * Default: false
37 */
38 commentDescriptions?: boolean;
39
40 /**
41 * Set to true to assume the SDL is valid.
42 *
43 * Default: false
44 */
45 assumeValidSDL?: boolean;
46}
47
48/**
49 * This takes the ast of a schema document produced by the parse function in
50 * src/language/parser.js.
51 *
52 * If no schema definition is provided, then it will look for types named Query
53 * and Mutation.
54 *
55 * Given that AST it constructs a GraphQLSchema. The resulting schema
56 * has no resolve methods, so execution will use default resolvers.
57 *
58 * Accepts options as a second argument:
59 *
60 * - commentDescriptions:
61 * Provide true to use preceding comments as the description.
62 *
63 */
64export function buildASTSchema(
65 documentAST: DocumentNode,
66 options?: BuildSchemaOptions,
67): GraphQLSchema;
68
69type TypeDefinitionsMap = { [key: string]: TypeDefinitionNode };
70type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType;
71
72export class ASTDefinitionBuilder {
73 constructor(options: Maybe<BuildSchemaOptions>, resolveType: TypeResolver);
74
75 getNamedType(node: NamedTypeNode): GraphQLNamedType;
76
77 getWrappedType(node: TypeNode): GraphQLType;
78
79 buildDirective(directive: DirectiveDefinitionNode): GraphQLDirective;
80
81 buildField(field: FieldDefinitionNode): GraphQLFieldConfig<any, any>;
82
83 buildArg(value: InputValueDefinitionNode): GraphQLArgumentConfig;
84
85 buildInputField(value: InputValueDefinitionNode): GraphQLInputFieldConfig;
86
87 buildEnumValue(value: EnumValueDefinitionNode): GraphQLEnumValueConfig;
88
89 buildType(node: NamedTypeNode | TypeDefinitionNode): GraphQLNamedType;
90}
91
92/**
93 * Given an ast node, returns its string description.
94 * @deprecated: provided to ease adoption and will be removed in v16.
95 *
96 * Accepts options as a second argument:
97 *
98 * - commentDescriptions:
99 * Provide true to use preceding comments as the description.
100 *
101 */
102export function getDescription(
103 node: { readonly description?: StringValueNode; readonly loc?: Location },
104 options: Maybe<BuildSchemaOptions>,
105): string | undefined;
106
107/**
108 * A helper function to build a GraphQLSchema directly from a source
109 * document.
110 */
111export function buildSchema(
112 source: string | Source,
113 options?: BuildSchemaOptions & ParseOptions,
114): GraphQLSchema;