UNPKG

2.72 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 * @format
9 */
10'use strict';
11
12function validateOrThrow(document, schema, rules) {
13 var validationErrors = require("graphql").validate(schema, document, rules);
14
15 if (validationErrors && validationErrors.length > 0) {
16 var formattedErrors = validationErrors.map(require("graphql").formatError);
17 var errorMessages = validationErrors.map(function (e) {
18 return e.toString();
19 });
20 var error = new Error(require("util").format('You supplied a GraphQL document with validation errors:\n%s', errorMessages.join('\n')));
21 error.validationErrors = formattedErrors;
22 throw error;
23 }
24}
25
26module.exports = {
27 GLOBAL_RULES: [require("graphql").KnownArgumentNamesRule,
28 /* Some rules are not enabled (potentially non-exhaustive)
29 *
30 * - KnownFragmentNamesRule: RelayClassic generates fragments at runtime,
31 * so RelayCompat queries might reference fragments unknown in build time.
32 * - NoFragmentCyclesRule: Because of @argumentDefinitions, this validation
33 * incorrectly flags a subset of fragments using @include/@skip as
34 * recursive.
35 * - NoUndefinedVariablesRule: Because of @argumentDefinitions, this
36 * validation incorrectly marks some fragment variables as undefined.
37 * - NoUnusedFragmentsRule: Queries generated dynamically with RelayCompat
38 * might use unused fragments.
39 * - OverlappingFieldsCanBeMergedRule: RelayClassic auto-resolves
40 * overlapping fields by generating aliases.
41 */
42 require("graphql").NoUnusedVariablesRule, require("graphql").UniqueArgumentNamesRule, require("graphql").UniqueFragmentNamesRule, require("graphql").UniqueInputFieldNamesRule, require("graphql").UniqueOperationNamesRule, require("graphql").UniqueVariableNamesRule],
43 LOCAL_RULES: [
44 /* Some rules are not enabled (potentially non-exhaustive)
45 *
46 * - FieldsOnCorrectTypeRule: is not aware of @fixme_fat_interface.
47 * - KnownDirectivesRule: doesn't pass with @arguments and other Relay
48 * directives.
49 * - ScalarLeafsRule: is violated by the @match directive since these rules
50 * run before any transform steps.
51 */
52 require("graphql").FragmentsOnCompositeTypesRule, require("graphql").KnownTypeNamesRule, require("graphql").LoneAnonymousOperationRule, require("graphql").PossibleFragmentSpreadsRule, require("graphql").ValuesOfCorrectTypeRule, require("graphql").VariablesAreInputTypesRule, require("graphql").VariablesInAllowedPositionRule],
53 validate: require("./GraphQLCompilerProfiler").instrument(validateOrThrow, 'GraphQLValidator.validate')
54};
\No newline at end of file