|
| 1 | +import { capitalize } from '../jsutils/capitalize'; |
1 | 2 | import { inspect } from '../jsutils/inspect';
|
2 | 3 | import type { Maybe } from '../jsutils/Maybe';
|
3 | 4 |
|
@@ -112,37 +113,25 @@ class SchemaValidationContext {
|
112 | 113 |
|
113 | 114 | function validateRootTypes(context: SchemaValidationContext): void {
|
114 | 115 | const schema = context.schema;
|
115 |
| - const queryType = schema.getQueryType(); |
116 |
| - if (!queryType) { |
| 116 | + |
| 117 | + if (schema.getQueryType() == null) { |
117 | 118 | context.reportError('Query root type must be provided.', schema.astNode);
|
118 |
| - } else if (!isObjectType(queryType)) { |
119 |
| - context.reportError( |
120 |
| - `Query root type must be Object type, it cannot be ${inspect( |
121 |
| - queryType, |
122 |
| - )}.`, |
123 |
| - getOperationTypeNode(schema, OperationTypeNode.QUERY) ?? |
124 |
| - (queryType as any).astNode, |
125 |
| - ); |
126 | 119 | }
|
127 | 120 |
|
128 |
| - const mutationType = schema.getMutationType(); |
129 |
| - if (mutationType && !isObjectType(mutationType)) { |
130 |
| - context.reportError( |
131 |
| - 'Mutation root type must be Object type if provided, it cannot be ' + |
132 |
| - `${inspect(mutationType)}.`, |
133 |
| - getOperationTypeNode(schema, OperationTypeNode.MUTATION) ?? |
134 |
| - (mutationType as any).astNode, |
135 |
| - ); |
136 |
| - } |
| 121 | + for (const operationType of Object.values(OperationTypeNode)) { |
| 122 | + const rootType = schema.getRootType(operationType); |
137 | 123 |
|
138 |
| - const subscriptionType = schema.getSubscriptionType(); |
139 |
| - if (subscriptionType && !isObjectType(subscriptionType)) { |
140 |
| - context.reportError( |
141 |
| - 'Subscription root type must be Object type if provided, it cannot be ' + |
142 |
| - `${inspect(subscriptionType)}.`, |
143 |
| - getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ?? |
144 |
| - (subscriptionType as any).astNode, |
145 |
| - ); |
| 124 | + if (rootType != null && !isObjectType(rootType)) { |
| 125 | + const operationTypeStr = capitalize(operationType); |
| 126 | + const rootTypeStr = inspect(rootType); |
| 127 | + context.reportError( |
| 128 | + operationType === OperationTypeNode.QUERY |
| 129 | + ? `${operationTypeStr} root type must be Object type, it cannot be ${rootTypeStr}.` |
| 130 | + : `${operationTypeStr} root type must be Object type if provided, it cannot be ${rootTypeStr}.`, |
| 131 | + getOperationTypeNode(schema, operationType) ?? |
| 132 | + (rootType as any).astNode, |
| 133 | + ); |
| 134 | + } |
146 | 135 | }
|
147 | 136 | }
|
148 | 137 |
|
|
0 commit comments