UNPKG

1.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.assertValidName = assertValidName;
7exports.isValidNameError = isValidNameError;
8
9var _GraphQLError = require('../error/GraphQLError');
10
11var _invariant = require('../jsutils/invariant');
12
13var _invariant2 = _interopRequireDefault(_invariant);
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17/**
18 * Copyright (c) 2015-present, Facebook, Inc.
19 *
20 * This source code is licensed under the MIT license found in the
21 * LICENSE file in the root directory of this source tree.
22 *
23 *
24 */
25
26var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;
27
28/**
29 * Upholds the spec rules about naming.
30 */
31function assertValidName(name) {
32 var error = isValidNameError(name);
33 if (error) {
34 throw error;
35 }
36 return name;
37}
38
39/**
40 * Returns an Error if a name is invalid.
41 */
42function isValidNameError(name, node) {
43 !(typeof name === 'string') ? (0, _invariant2.default)(0, 'Expected string') : void 0;
44 if (name.length > 1 && name[0] === '_' && name[1] === '_' &&
45 // Note: this special case is not part of the spec and exists only to
46 // support legacy server configurations. Do not rely on this special case
47 // as it may be removed at any time.
48 name !== '__configs__') {
49 return new _GraphQLError.GraphQLError('Name "' + name + '" must not begin with "__", which is reserved by ' + 'GraphQL introspection.', node);
50 }
51 if (!NAME_RX.test(name)) {
52 return new _GraphQLError.GraphQLError('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "' + name + '" does not.', node);
53 }
54}
\No newline at end of file