Skip to content

Commit 7e8e57b

Browse files
Kevin Lackerleebyron
authored andcommitted
buildSchema helper function (#471)
* buildSchema helper function * import -> import type
1 parent e834097 commit 7e8e57b

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ export {
160160
// Build a GraphQLSchema from a parsed GraphQL Schema language AST.
161161
buildASTSchema,
162162

163+
// Build a GraphQLSchema from a GraphQL schema language document.
164+
buildSchema,
165+
163166
// Extends an existing GraphQLSchema from a parsed GraphQL Schema
164167
// language AST.
165168
extendSchema,

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { expect } from 'chai';
1111
import { describe, it } from 'mocha';
1212
import { parse } from '../../language';
1313
import { printSchema } from '../schemaPrinter';
14-
import { buildASTSchema } from '../buildASTSchema';
14+
import { buildASTSchema, buildSchema } from '../buildASTSchema';
1515
import {
1616
graphql,
1717
GraphQLSkipDirective,
@@ -50,6 +50,17 @@ describe('Schema Builder', () => {
5050
expect(result.data).to.deep.equal({ str: '123' });
5151
});
5252

53+
it('can build a schema directly from the source', async () => {
54+
const schema = buildSchema(`
55+
type Query {
56+
add(x: Int, y: Int): Int
57+
}
58+
`);
59+
expect(await graphql(
60+
schema, '{ add(x: 34, y: 55) }', { add: ({x, y}) => x + y }
61+
)).to.deep.equal({ data: { add: 89 }});
62+
});
63+
5364
it('Simple type', () => {
5465
const body = `
5566
schema {

src/utilities/buildASTSchema.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import invariant from '../jsutils/invariant';
1313
import keyValMap from '../jsutils/keyValMap';
1414
import { valueFromAST } from './valueFromAST';
1515
import { TokenKind } from '../language/lexer';
16+
import { parse } from '../language/parser';
17+
import type { Source } from '../language/source';
1618
import { getArgumentValues } from '../execution/values';
1719

1820
import {
@@ -130,9 +132,8 @@ function getNamedTypeAST(typeAST: Type): NamedType {
130132
* If no schema definition is provided, then it will look for types named Query
131133
* and Mutation.
132134
*
133-
* Given that AST it constructs a GraphQLSchema. As constructed
134-
* they are not particularly useful for non-introspection queries
135-
* since they have no resolve methods.
135+
* Given that AST it constructs a GraphQLSchema. The resulting schema
136+
* has no resolve methods, so execution will use default resolvers.
136137
*/
137138
export function buildASTSchema(ast: Document): GraphQLSchema {
138139
if (!ast || ast.kind !== DOCUMENT) {
@@ -507,6 +508,14 @@ export function getDescription(node: { loc?: Location }): ?string {
507508
.join('\n');
508509
}
509510

511+
/**
512+
* A helper function to build a GraphQLSchema directly from a source
513+
* document.
514+
*/
515+
export function buildSchema(source: string | Source): GraphQLSchema {
516+
return buildASTSchema(parse(source));
517+
}
518+
510519
// Count the number of spaces on the starting side of a string.
511520
function leadingSpaces(str) {
512521
let i = 0;

src/utilities/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export { getOperationAST } from './getOperationAST';
1717
// Build a GraphQLSchema from an introspection result.
1818
export { buildClientSchema } from './buildClientSchema';
1919

20-
// Build a GraphQLSchema from a parsed GraphQL Schema language AST.
21-
export { buildASTSchema } from './buildASTSchema';
20+
// Build a GraphQLSchema from GraphQL Schema language.
21+
export { buildASTSchema, buildSchema } from './buildASTSchema';
2222

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

0 commit comments

Comments
 (0)