Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/jsutils/__tests__/capitalize-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { capitalize } from '../capitalize';

describe('capitalize', () => {
it('Converts the first character of string to upper case and the remaining to lower case', () => {
expect(capitalize('')).to.equal('');

expect(capitalize('a')).to.equal('A');
expect(capitalize('A')).to.equal('A');

expect(capitalize('ab')).to.equal('Ab');
expect(capitalize('aB')).to.equal('Ab');
expect(capitalize('Ab')).to.equal('Ab');
expect(capitalize('AB')).to.equal('Ab');

expect(capitalize('platypus')).to.equal('Platypus');
expect(capitalize('PLATYPUS')).to.equal('Platypus');
});
});
6 changes: 6 additions & 0 deletions src/jsutils/capitalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Converts the first character of string to upper case and the remaining to lower case.
*/
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
43 changes: 16 additions & 27 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { capitalize } from '../jsutils/capitalize';
import { inspect } from '../jsutils/inspect';
import type { Maybe } from '../jsutils/Maybe';

Expand Down Expand Up @@ -112,37 +113,25 @@ class SchemaValidationContext {

function validateRootTypes(context: SchemaValidationContext): void {
const schema = context.schema;
const queryType = schema.getQueryType();
if (!queryType) {

if (schema.getQueryType() == null) {
context.reportError('Query root type must be provided.', schema.astNode);
} else if (!isObjectType(queryType)) {
context.reportError(
`Query root type must be Object type, it cannot be ${inspect(
queryType,
)}.`,
getOperationTypeNode(schema, OperationTypeNode.QUERY) ??
(queryType as any).astNode,
);
}

const mutationType = schema.getMutationType();
if (mutationType && !isObjectType(mutationType)) {
context.reportError(
'Mutation root type must be Object type if provided, it cannot be ' +
`${inspect(mutationType)}.`,
getOperationTypeNode(schema, OperationTypeNode.MUTATION) ??
(mutationType as any).astNode,
);
}
for (const operationType of Object.values(OperationTypeNode)) {
const rootType = schema.getRootType(operationType);

const subscriptionType = schema.getSubscriptionType();
if (subscriptionType && !isObjectType(subscriptionType)) {
context.reportError(
'Subscription root type must be Object type if provided, it cannot be ' +
`${inspect(subscriptionType)}.`,
getOperationTypeNode(schema, OperationTypeNode.SUBSCRIPTION) ??
(subscriptionType as any).astNode,
);
if (rootType != null && !isObjectType(rootType)) {
const operationTypeStr = capitalize(operationType);
const rootTypeStr = inspect(rootType);
context.reportError(
operationType === OperationTypeNode.QUERY
? `${operationTypeStr} root type must be Object type, it cannot be ${rootTypeStr}.`
: `${operationTypeStr} root type must be Object type if provided, it cannot be ${rootTypeStr}.`,
getOperationTypeNode(schema, operationType) ??
(rootType as any).astNode,
);
}
}
}

Expand Down