UNPKG

3.54 kBTypeScriptView Raw
1import Maybe from '../tsutils/Maybe';
2import { SchemaDefinitionNode, SchemaExtensionNode } from '../language/ast';
3import { GraphQLDirective } from './directives';
4import {
5 GraphQLType,
6 GraphQLNamedType,
7 GraphQLAbstractType,
8 GraphQLObjectType,
9} from './definition';
10
11/**
12 * Test if the given value is a GraphQL schema.
13 */
14export function isSchema(schema: any): schema is GraphQLSchema;
15export function assertSchema(schema: any): GraphQLSchema;
16
17/**
18 * Schema Definition
19 *
20 * A Schema is created by supplying the root types of each type of operation,
21 * query and mutation (optional). A schema definition is then supplied to the
22 * validator and executor.
23 *
24 * Example:
25 *
26 * const MyAppSchema = new GraphQLSchema({
27 * query: MyAppQueryRootType,
28 * mutation: MyAppMutationRootType,
29 * })
30 *
31 * Note: If an array of `directives` are provided to GraphQLSchema, that will be
32 * the exact list of directives represented and allowed. If `directives` is not
33 * provided then a default set of the specified directives (e.g. @include and
34 * @skip) will be used. If you wish to provide *additional* directives to these
35 * specified directives, you must explicitly declare them. Example:
36 *
37 * const MyAppSchema = new GraphQLSchema({
38 * ...
39 * directives: specifiedDirectives.concat([ myCustomDirective ]),
40 * })
41 *
42 */
43export class GraphQLSchema {
44 extensions: Maybe<Readonly<Record<string, any>>>;
45 astNode: Maybe<SchemaDefinitionNode>;
46 extensionASTNodes: Maybe<ReadonlyArray<SchemaExtensionNode>>;
47
48 constructor(config: GraphQLSchemaConfig);
49
50 getQueryType(): Maybe<GraphQLObjectType>;
51 getMutationType(): Maybe<GraphQLObjectType>;
52 getSubscriptionType(): Maybe<GraphQLObjectType>;
53 getTypeMap(): TypeMap;
54 getType(name: string): Maybe<GraphQLNamedType>;
55 getPossibleTypes(
56 abstractType: GraphQLAbstractType,
57 ): ReadonlyArray<GraphQLObjectType>;
58
59 isPossibleType(
60 abstractType: GraphQLAbstractType,
61 possibleType: GraphQLObjectType,
62 ): boolean;
63
64 getDirectives(): ReadonlyArray<GraphQLDirective>;
65 getDirective(name: string): Maybe<GraphQLDirective>;
66
67 toConfig(): GraphQLSchemaConfig & {
68 types: GraphQLNamedType[];
69 directives: GraphQLDirective[];
70 extensions: Maybe<Readonly<Record<string, any>>>;
71 extensionASTNodes: ReadonlyArray<SchemaExtensionNode>;
72 assumeValid: boolean;
73 allowedLegacyNames: ReadonlyArray<string>;
74 };
75}
76
77type TypeMap = { [key: string]: GraphQLNamedType };
78
79export interface GraphQLSchemaValidationOptions {
80 /**
81 * When building a schema from a GraphQL service's introspection result, it
82 * might be safe to assume the schema is valid. Set to true to assume the
83 * produced schema is valid.
84 *
85 * Default: false
86 */
87 assumeValid?: boolean;
88
89 /**
90 * If provided, the schema will consider fields or types with names included
91 * in this list valid, even if they do not adhere to the specification's
92 * schema validation rules.
93 *
94 * This option is provided to ease adoption and will be removed in v15.
95 */
96 allowedLegacyNames?: Maybe<ReadonlyArray<string>>;
97}
98
99export interface GraphQLSchemaConfig extends GraphQLSchemaValidationOptions {
100 query: Maybe<GraphQLObjectType>;
101 mutation?: Maybe<GraphQLObjectType>;
102 subscription?: Maybe<GraphQLObjectType>;
103 types?: Maybe<GraphQLNamedType[]>;
104 directives?: Maybe<GraphQLDirective[]>;
105 extensions?: Maybe<Readonly<Record<string, any>>>;
106 astNode?: Maybe<SchemaDefinitionNode>;
107 extensionASTNodes?: Maybe<ReadonlyArray<SchemaExtensionNode>>;
108}