Skip to content
This repository was archived by the owner on Apr 15, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b107e91

Browse files
committedMar 10, 2020
feat(mapSchema): initial version
mapSchema enables transformation of schema objects without modification of the original schema. See graphql/graphql-js#1199
1 parent db837f7 commit b107e91

File tree

3 files changed

+545
-0
lines changed

3 files changed

+545
-0
lines changed
 

‎src/test/testMapSchema.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { expect } from 'chai';
2+
import { GraphQLSchema, GraphQLObjectType, graphqlSync } from 'graphql';
3+
4+
import { makeExecutableSchema, mapSchema } from '../index';
5+
import { MapperKind } from '../utils/map';
6+
7+
describe('mapSchema', () => {
8+
it('does not throw', () => {
9+
const schema = makeExecutableSchema({
10+
typeDefs: `
11+
type Query {
12+
version: String
13+
}
14+
`,
15+
});
16+
17+
const newSchema = mapSchema(schema, {});
18+
expect(newSchema).to.be.instanceOf(GraphQLSchema);
19+
});
20+
21+
it('can add a resolver', () => {
22+
const schema = makeExecutableSchema({
23+
typeDefs: `
24+
type Query {
25+
version: Int
26+
}
27+
`,
28+
});
29+
30+
const newSchema = mapSchema(schema, {
31+
[MapperKind.QUERY]: type => {
32+
const queryConfig = type.toConfig();
33+
queryConfig.fields.version.resolve = () => 1;
34+
return new GraphQLObjectType(queryConfig);
35+
},
36+
});
37+
38+
expect(newSchema).to.be.instanceOf(GraphQLSchema);
39+
40+
const result = graphqlSync(newSchema, '{ version }');
41+
expect(result.data.version).to.equal(1);
42+
});
43+
44+
it('can change the root query name', () => {
45+
const schema = makeExecutableSchema({
46+
typeDefs: `
47+
type Query {
48+
version: Int
49+
}
50+
`,
51+
});
52+
53+
const newSchema = mapSchema(schema, {
54+
[MapperKind.QUERY]: type => {
55+
const queryConfig = type.toConfig();
56+
queryConfig.name = 'RootQuery';
57+
return new GraphQLObjectType(queryConfig);
58+
},
59+
});
60+
61+
expect(newSchema).to.be.instanceOf(GraphQLSchema);
62+
expect(newSchema.getQueryType().name).to.equal('RootQuery');
63+
});
64+
});

‎src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export {
2727
export { appendFields, removeFields } from './fields';
2828
export { createNamedStub } from './stub';
2929
export { graphqlVersion } from './graphqlVersion';
30+
export { mapSchema } from './map';

0 commit comments

Comments
 (0)