Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
feat(mapSchema): initial version
Browse files Browse the repository at this point in the history
mapSchema enables transformation of schema objects without modification of the original schema.

See graphql/graphql-js#1199
  • Loading branch information
yaacovCR committed Mar 9, 2020
1 parent 4535008 commit dfe07b6
Show file tree
Hide file tree
Showing 3 changed files with 545 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/test/testMapSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect } from 'chai';
import { GraphQLSchema, GraphQLObjectType, graphqlSync } from 'graphql';

import { makeExecutableSchema, mapSchema } from '../index';
import { MapperKind } from '../utils/map';

describe('mapSchema', () => {
it('does not throw', () => {
const schema = makeExecutableSchema({
typeDefs: `
type Query {
version: String
}
`,
});

const newSchema = mapSchema(schema, {});
expect(newSchema).to.be.instanceOf(GraphQLSchema);
});

it('can add a resolver', () => {
const schema = makeExecutableSchema({
typeDefs: `
type Query {
version: Int
}
`,
});

const newSchema = mapSchema(schema, {
[MapperKind.QUERY]: type => {
const queryConfig = type.toConfig();
queryConfig.fields.version.resolve = () => 1;
return new GraphQLObjectType(queryConfig);
},
});

expect(newSchema).to.be.instanceOf(GraphQLSchema);

const result = graphqlSync(newSchema, '{ version }');
expect(result.data.version).to.equal(1);
});

it('can change the root query name', () => {
const schema = makeExecutableSchema({
typeDefs: `
type Query {
version: Int
}
`,
});

const newSchema = mapSchema(schema, {
[MapperKind.QUERY]: type => {
const queryConfig = type.toConfig();
queryConfig.name = 'RootQuery';
return new GraphQLObjectType(queryConfig);
},
});

expect(newSchema).to.be.instanceOf(GraphQLSchema);
expect(newSchema.getQueryType().name).to.equal('RootQuery');
});
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export {
export { appendFields, removeFields } from './fields';
export { createNamedStub } from './stub';
export { graphqlVersion } from './graphqlVersion';
export { mapSchema } from './map';
Loading

0 comments on commit dfe07b6

Please sign in to comment.