Skip to content

Commit 86d33b4

Browse files
authored
Allow buildSchema() to take options. (#1249)
buildSchema() is essentially just short hand for buildASTSchema(parse()), however since both of these composed functions accept options, it's nice to allow buildSchema() to accept options as well - it accepts the combined set of options which it passes to both functions. This also names and exports the options for buildASTSchema() such that they can be used in the same way by third party code.
1 parent 31ae8a8 commit 86d33b4

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ export {
375375
} from './utilities';
376376

377377
export type {
378+
BuildSchemaOptions,
378379
BreakingChange,
379380
DangerousChange,
380381
IntrospectionOptions,

src/utilities/buildASTSchema.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { valueFromAST } from './valueFromAST';
1414
import blockStringValue from '../language/blockStringValue';
1515
import { TokenKind } from '../language/lexer';
1616
import { parse } from '../language/parser';
17+
import type { ParseOptions } from '../language/parser';
1718
import type { Source } from '../language/source';
1819
import { getDirectiveValues } from '../execution/values';
1920
import { Kind } from '../language/kinds';
@@ -72,7 +73,7 @@ import type {
7273
GraphQLFieldConfig,
7374
} from '../type/definition';
7475

75-
type Options = {|
76+
export type BuildSchemaOptions = {
7677
...GraphQLSchemaValidationOptions,
7778

7879
/**
@@ -83,7 +84,7 @@ type Options = {|
8384
* Default: false
8485
*/
8586
commentDescriptions?: boolean,
86-
|};
87+
};
8788

8889
function buildWrappedType(
8990
innerType: GraphQLType,
@@ -128,7 +129,7 @@ function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode {
128129
*/
129130
export function buildASTSchema(
130131
ast: DocumentNode,
131-
options?: Options,
132+
options?: BuildSchemaOptions,
132133
): GraphQLSchema {
133134
if (!ast || ast.kind !== Kind.DOCUMENT) {
134135
throw new Error('Must provide a document ast.');
@@ -246,13 +247,13 @@ type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType;
246247

247248
export class ASTDefinitionBuilder {
248249
_typeDefinitionsMap: TypeDefinitionsMap;
249-
_options: ?Options;
250+
_options: ?BuildSchemaOptions;
250251
_resolveType: TypeResolver;
251252
_cache: ObjMap<GraphQLNamedType>;
252253

253254
constructor(
254255
typeDefinitionsMap: TypeDefinitionsMap,
255-
options: ?Options,
256+
options: ?BuildSchemaOptions,
256257
resolveType: TypeResolver,
257258
) {
258259
this._typeDefinitionsMap = typeDefinitionsMap;
@@ -464,7 +465,7 @@ function getDeprecationReason(
464465
*/
465466
export function getDescription(
466467
node: { +description?: StringValueNode, +loc?: Location },
467-
options: ?Options,
468+
options: ?BuildSchemaOptions,
468469
): void | string {
469470
if (node.description) {
470471
return node.description.value;
@@ -503,6 +504,9 @@ function getLeadingCommentBlock(node): void | string {
503504
* A helper function to build a GraphQLSchema directly from a source
504505
* document.
505506
*/
506-
export function buildSchema(source: string | Source): GraphQLSchema {
507-
return buildASTSchema(parse(source));
507+
export function buildSchema(
508+
source: string | Source,
509+
options: BuildSchemaOptions & ParseOptions,
510+
): GraphQLSchema {
511+
return buildASTSchema(parse(source, options), options);
508512
}

src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export { buildClientSchema } from './buildClientSchema';
4949

5050
// Build a GraphQLSchema from GraphQL Schema language.
5151
export { buildASTSchema, buildSchema, getDescription } from './buildASTSchema';
52+
export type { BuildSchemaOptions } from './buildASTSchema';
5253

5354
// Extends an existing GraphQLSchema from a parsed GraphQL Schema language AST.
5455
export { extendSchema } from './extendSchema';

0 commit comments

Comments
 (0)