-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheckGraphQLSchema.mjs
More file actions
29 lines (24 loc) · 836 Bytes
/
checkGraphQLSchema.mjs
File metadata and controls
29 lines (24 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// @ts-check
import { GraphQLSchema, validateSchema } from "graphql";
import createHttpError from "http-errors";
import GraphQLAggregateError from "./GraphQLAggregateError.mjs";
/**
* Validates a GraphQL schema.
* @param {GraphQLSchema} schema GraphQL schema.
* @param {string} errorMessagePrefix Error message prefix.
*/
export default function checkGraphQLSchema(schema, errorMessagePrefix) {
if (!(schema instanceof GraphQLSchema))
throw createHttpError(
500,
`${errorMessagePrefix} GraphQL schema must be a \`GraphQLSchema\` instance.`
);
const schemaValidationErrors = validateSchema(schema);
if (schemaValidationErrors.length)
throw new GraphQLAggregateError(
schemaValidationErrors,
`${errorMessagePrefix} has GraphQL schema validation errors.`,
500,
false
);
}