|
| 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 | +}); |
0 commit comments