MCPcopy
hub / github.com/graphql/graphql-js / validateRootTypes

Function validateRootTypes

src/type/validate.ts:167–209  ·  view source on GitHub ↗
(context: SchemaValidationContext)

Source from the content-addressed store, hash-verified

165}
166
167function validateRootTypes(context: SchemaValidationContext): void {
168 const schema = context.schema;
169
170 if (schema.getQueryType() == null) {
171 context.reportError('Query root type must be provided.', schema.astNode);
172 }
173
174 const rootTypesMap = new AccumulatorMap<
175 GraphQLObjectType,
176 OperationTypeNode
177 >();
178 for (const operationType of Object.values(OperationTypeNode)) {
179 const rootType = schema.getRootType(operationType);
180
181 if (rootType != null) {
182 if (!isObjectType(rootType)) {
183 const operationTypeStr = capitalize(operationType);
184 const rootTypeStr = inspect(rootType);
185 context.reportError(
186 operationType === OperationTypeNode.QUERY
187 ? `${operationTypeStr} root type must be Object type, it cannot be ${rootTypeStr}.`
188 : `${operationTypeStr} root type must be Object type if provided, it cannot be ${rootTypeStr}.`,
189 getOperationTypeNode(schema, operationType) ??
190 (rootType as any).astNode,
191 );
192 } else {
193 rootTypesMap.add(rootType, operationType);
194 }
195 }
196 }
197
198 for (const [rootType, operationTypes] of rootTypesMap) {
199 if (operationTypes.length > 1) {
200 const operationList = andList(operationTypes);
201 context.reportError(
202 `All root types must be different, "${rootType}" type is used as ${operationList} root types.`,
203 operationTypes.map((operationType) =>
204 getOperationTypeNode(schema, operationType),
205 ),
206 );
207 }
208 }
209}
210
211function getOperationTypeNode(
212 schema: GraphQLSchema,

Callers 1

validateSchemaFunction · 0.85

Calls 9

addMethod · 0.95
isObjectTypeFunction · 0.90
capitalizeFunction · 0.90
inspectFunction · 0.90
andListFunction · 0.90
getOperationTypeNodeFunction · 0.85
getQueryTypeMethod · 0.80
getRootTypeMethod · 0.80
reportErrorMethod · 0.45

Tested by

no test coverage detected