|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import { describe, it } from 'mocha'; |
| 10 | +import { getOperationRootType } from '../getOperationRootType'; |
| 11 | +import { parse, GraphQLSchema, GraphQLObjectType, GraphQLString } from '../../'; |
| 12 | + |
| 13 | +const queryType = new GraphQLObjectType({ |
| 14 | + name: 'FooQuery', |
| 15 | + fields: () => ({ |
| 16 | + field: { type: GraphQLString }, |
| 17 | + }), |
| 18 | +}); |
| 19 | + |
| 20 | +const mutationType = new GraphQLObjectType({ |
| 21 | + name: 'FooMutation', |
| 22 | + fields: () => ({ |
| 23 | + field: { type: GraphQLString }, |
| 24 | + }), |
| 25 | +}); |
| 26 | + |
| 27 | +const subscriptionType = new GraphQLObjectType({ |
| 28 | + name: 'FooSubscription', |
| 29 | + fields: () => ({ |
| 30 | + field: { type: GraphQLString }, |
| 31 | + }), |
| 32 | +}); |
| 33 | + |
| 34 | +describe('getOperationRootType', () => { |
| 35 | + it('Gets a Query type for an unnamed OperationDefinitionNode', () => { |
| 36 | + const testSchema = new GraphQLSchema({ |
| 37 | + query: queryType, |
| 38 | + }); |
| 39 | + const doc = parse('{ field }'); |
| 40 | + expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal( |
| 41 | + queryType, |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Gets a Query type for an named OperationDefinitionNode', () => { |
| 46 | + const testSchema = new GraphQLSchema({ |
| 47 | + query: queryType, |
| 48 | + }); |
| 49 | + |
| 50 | + const doc = parse('query Q { field }'); |
| 51 | + expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal( |
| 52 | + queryType, |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Gets a type for OperationTypeDefinitionNodes', () => { |
| 57 | + const testSchema = new GraphQLSchema({ |
| 58 | + query: queryType, |
| 59 | + mutation: mutationType, |
| 60 | + subscription: subscriptionType, |
| 61 | + }); |
| 62 | + |
| 63 | + const doc = parse( |
| 64 | + 'schema { query: FooQuery mutation: FooMutation subscription: FooSubscription }', |
| 65 | + ); |
| 66 | + const operationTypes = doc.definitions[0].operationTypes; |
| 67 | + expect(getOperationRootType(testSchema, operationTypes[0])).to.equal( |
| 68 | + queryType, |
| 69 | + ); |
| 70 | + expect(getOperationRootType(testSchema, operationTypes[1])).to.equal( |
| 71 | + mutationType, |
| 72 | + ); |
| 73 | + expect(getOperationRootType(testSchema, operationTypes[2])).to.equal( |
| 74 | + subscriptionType, |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Gets a Mutation type for an OperationDefinitionNode', () => { |
| 79 | + const testSchema = new GraphQLSchema({ |
| 80 | + mutation: mutationType, |
| 81 | + }); |
| 82 | + |
| 83 | + const doc = parse('mutation { field }'); |
| 84 | + expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal( |
| 85 | + mutationType, |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Gets a Subscription type for an OperationDefinitionNode', () => { |
| 90 | + const testSchema = new GraphQLSchema({ |
| 91 | + subscription: subscriptionType, |
| 92 | + }); |
| 93 | + |
| 94 | + const doc = parse('subscription { field }'); |
| 95 | + expect(getOperationRootType(testSchema, doc.definitions[0])).to.equal( |
| 96 | + subscriptionType, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Throws when query type not defined in schema', () => { |
| 101 | + const testSchema = new GraphQLSchema({}); |
| 102 | + |
| 103 | + const doc = parse('query { field }'); |
| 104 | + expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw( |
| 105 | + 'Schema does not define the required query root type.', |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('Throws when mutation type not defined in schema', () => { |
| 110 | + const testSchema = new GraphQLSchema({}); |
| 111 | + |
| 112 | + const doc = parse('mutation { field }'); |
| 113 | + expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw( |
| 114 | + 'Schema is not configured for mutations.', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Throws when subscription type not defined in schema', () => { |
| 119 | + const testSchema = new GraphQLSchema({}); |
| 120 | + |
| 121 | + const doc = parse('subscription { field }'); |
| 122 | + expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw( |
| 123 | + 'Schema is not configured for subscriptions.', |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('Throws when operation not a valid operation kind', () => { |
| 128 | + const testSchema = new GraphQLSchema({}); |
| 129 | + |
| 130 | + const doc = parse('{ field }'); |
| 131 | + doc.definitions[0].operation = 'non_existent_operation'; |
| 132 | + expect(() => getOperationRootType(testSchema, doc.definitions[0])).to.throw( |
| 133 | + 'Can only have query, mutation and subscription operations.', |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments