Skip to content

Commit 839f244

Browse files
validateSchema: unify check of root types (#3566)
1 parent 97c9512 commit 839f244

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
4+
import { capitalize } from '../capitalize';
5+
6+
describe('capitalize', () => {
7+
it('Converts the first character of string to upper case and the remaining to lower case', () => {
8+
expect(capitalize('')).to.equal('');
9+
10+
expect(capitalize('a')).to.equal('A');
11+
expect(capitalize('A')).to.equal('A');
12+
13+
expect(capitalize('ab')).to.equal('Ab');
14+
expect(capitalize('aB')).to.equal('Ab');
15+
expect(capitalize('Ab')).to.equal('Ab');
16+
expect(capitalize('AB')).to.equal('Ab');
17+
18+
expect(capitalize('platypus')).to.equal('Platypus');
19+
expect(capitalize('PLATYPUS')).to.equal('Platypus');
20+
});
21+
});

src/jsutils/capitalize.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Converts the first character of string to upper case and the remaining to lower case.
3+
*/
4+
export function capitalize(str: string): string {
5+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
6+
}

src/type/validate.ts

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { capitalize } from '../jsutils/capitalize';
12
import { inspect } from '../jsutils/inspect';
23
import type { Maybe } from '../jsutils/Maybe';
34

@@ -112,37 +113,25 @@ class SchemaValidationContext {
112113

113114
function validateRootTypes(context: SchemaValidationContext): void {
114115
const schema = context.schema;
115-
const queryType = schema.getQueryType();
116-
if (!queryType) {
116+
117+
if (schema.getQueryType() == null) {
117118
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-
);
126119
}
127120

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);
137123

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+
}
146135
}
147136
}
148137

0 commit comments

Comments
 (0)