Skip to content

Commit fee66ad

Browse files
authored
Merge branch 'master' into v0.1.0
2 parents ffe74f6 + aee4754 commit fee66ad

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/build/__tests__/build-test.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,26 @@ import {
88
GraphQLNonNull,
99
GraphQLBoolean,
1010
GraphQLID,
11+
DirectiveLocation,
12+
GraphQLDirective,
1113
} from 'graphql/type';
1214
import { parse } from 'graphql/language';
1315
import { expectTypesEqual, expectSchemasEqual } from './comparators';
14-
import build, { buildTypes } from '../';
16+
import build, { buildTypes, SCHEMA_CONFIG_KEY } from '../';
1517

18+
const CustomDirective = new GraphQLDirective({
19+
name: 'CustomDirective',
20+
locations: [DirectiveLocation.FIELD],
21+
});
1622
const querySource = `
17-
type Query {
18-
ok: Boolean!
19-
}
23+
type Query {
24+
ok: Boolean!
25+
}
26+
`;
27+
const queryWithDirectiveSource = `
28+
type Query {
29+
ok: Boolean! @CustomDirective
30+
}
2031
`;
2132
const generateQuery = (resolve) => new GraphQLObjectType({
2233
name: 'Query',
@@ -28,6 +39,10 @@ const schemaSource = `
2839
}
2940
`;
3041
const Schema = new GraphQLSchema({ query: generateQuery() });
42+
const SchemaWithDirective = new GraphQLSchema({
43+
directives: [CustomDirective],
44+
query: generateQuery(),
45+
});
3146
const timestampSource = `
3247
scalar Timestamp
3348
`;
@@ -149,4 +164,11 @@ describe('build()', () => {
149164
Timestamp: { serialize },
150165
}, undefined, false)).forEach((type, i) => expectTypesEqual(type, [Record, Timestamp][i]));
151166
});
167+
168+
it('should allow schema configuration using SCHEMA_CONFIG_KEY', () => {
169+
const config = {};
170+
config[SCHEMA_CONFIG_KEY] = { directives: [CustomDirective] };
171+
const target = build(schemaSource + queryWithDirectiveSource, config);
172+
expectSchemasEqual(target, SchemaWithDirective);
173+
});
152174
});

src/build/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const buildMap = {
1818
[Kind.INPUT_OBJECT_TYPE_DEFINITION]: buildInputObject,
1919
};
2020

21+
export const SCHEMA_CONFIG_KEY = '__schema';
22+
2123
export const ensureNoDuplicateTypes = (types) => {
2224
types.forEach((typeA) => {
2325
if (types.some((typeB) => typeA !== typeB && typeA.name === typeB.name)) {
@@ -93,7 +95,7 @@ export default function build(source, config = {}, typeDeps = [], infer = true)
9395
}
9496
return buildSchema(
9597
schemaASTs[0],
96-
undefined,
98+
config[SCHEMA_CONFIG_KEY],
9799
() => ensureNoDuplicateTypes([...types, ...typeDeps]),
98100
);
99101
}

0 commit comments

Comments
 (0)