Skip to content

Commit

Permalink
Allow buildSchema() to take options. (#1249)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leebyron authored Feb 16, 2018
1 parent 31ae8a8 commit 86d33b4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ export {
} from './utilities';

export type {
BuildSchemaOptions,
BreakingChange,
DangerousChange,
IntrospectionOptions,
Expand Down
20 changes: 12 additions & 8 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { valueFromAST } from './valueFromAST';
import blockStringValue from '../language/blockStringValue';
import { TokenKind } from '../language/lexer';
import { parse } from '../language/parser';
import type { ParseOptions } from '../language/parser';
import type { Source } from '../language/source';
import { getDirectiveValues } from '../execution/values';
import { Kind } from '../language/kinds';
Expand Down Expand Up @@ -72,7 +73,7 @@ import type {
GraphQLFieldConfig,
} from '../type/definition';

type Options = {|
export type BuildSchemaOptions = {
...GraphQLSchemaValidationOptions,

/**
Expand All @@ -83,7 +84,7 @@ type Options = {|
* Default: false
*/
commentDescriptions?: boolean,
|};
};

function buildWrappedType(
innerType: GraphQLType,
Expand Down Expand Up @@ -128,7 +129,7 @@ function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode {
*/
export function buildASTSchema(
ast: DocumentNode,
options?: Options,
options?: BuildSchemaOptions,
): GraphQLSchema {
if (!ast || ast.kind !== Kind.DOCUMENT) {
throw new Error('Must provide a document ast.');
Expand Down Expand Up @@ -246,13 +247,13 @@ type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType;

export class ASTDefinitionBuilder {
_typeDefinitionsMap: TypeDefinitionsMap;
_options: ?Options;
_options: ?BuildSchemaOptions;
_resolveType: TypeResolver;
_cache: ObjMap<GraphQLNamedType>;

constructor(
typeDefinitionsMap: TypeDefinitionsMap,
options: ?Options,
options: ?BuildSchemaOptions,
resolveType: TypeResolver,
) {
this._typeDefinitionsMap = typeDefinitionsMap;
Expand Down Expand Up @@ -464,7 +465,7 @@ function getDeprecationReason(
*/
export function getDescription(
node: { +description?: StringValueNode, +loc?: Location },
options: ?Options,
options: ?BuildSchemaOptions,
): void | string {
if (node.description) {
return node.description.value;
Expand Down Expand Up @@ -503,6 +504,9 @@ function getLeadingCommentBlock(node): void | string {
* A helper function to build a GraphQLSchema directly from a source
* document.
*/
export function buildSchema(source: string | Source): GraphQLSchema {
return buildASTSchema(parse(source));
export function buildSchema(
source: string | Source,
options: BuildSchemaOptions & ParseOptions,
): GraphQLSchema {
return buildASTSchema(parse(source, options), options);
}
1 change: 1 addition & 0 deletions src/utilities/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export { buildClientSchema } from './buildClientSchema';

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

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

0 comments on commit 86d33b4

Please sign in to comment.