This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapSchema enables transformation of schema objects without modification of the original schema. See graphql/graphql-js#1199
- Loading branch information
Showing
3 changed files
with
545 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.