1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.KnownTypeNamesRule = KnownTypeNamesRule;
|
7 |
|
8 | var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean.js"));
|
9 |
|
10 | var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList.js"));
|
11 |
|
12 | var _GraphQLError = require("../../error/GraphQLError.js");
|
13 |
|
14 | var _predicates = require("../../language/predicates.js");
|
15 |
|
16 | var _scalars = require("../../type/scalars.js");
|
17 |
|
18 | var _introspection = require("../../type/introspection.js");
|
19 |
|
20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function KnownTypeNamesRule(context) {
|
29 | var schema = context.getSchema();
|
30 | var existingTypesMap = schema ? schema.getTypeMap() : Object.create(null);
|
31 | var definedTypes = Object.create(null);
|
32 |
|
33 | for (var _i2 = 0, _context$getDocument$2 = context.getDocument().definitions; _i2 < _context$getDocument$2.length; _i2++) {
|
34 | var def = _context$getDocument$2[_i2];
|
35 |
|
36 | if ((0, _predicates.isTypeDefinitionNode)(def)) {
|
37 | definedTypes[def.name.value] = true;
|
38 | }
|
39 | }
|
40 |
|
41 | var typeNames = Object.keys(existingTypesMap).concat(Object.keys(definedTypes));
|
42 | return {
|
43 | NamedType: function NamedType(node, _1, parent, _2, ancestors) {
|
44 | var typeName = node.name.value;
|
45 |
|
46 | if (!existingTypesMap[typeName] && !definedTypes[typeName]) {
|
47 | var _ancestors$;
|
48 |
|
49 | var definitionNode = (_ancestors$ = ancestors[2]) !== null && _ancestors$ !== void 0 ? _ancestors$ : parent;
|
50 | var isSDL = definitionNode != null && isSDLNode(definitionNode);
|
51 |
|
52 | if (isSDL && isStandardTypeName(typeName)) {
|
53 | return;
|
54 | }
|
55 |
|
56 | var suggestedTypes = (0, _suggestionList.default)(typeName, isSDL ? standardTypeNames.concat(typeNames) : typeNames);
|
57 | context.reportError(new _GraphQLError.GraphQLError("Unknown type \"".concat(typeName, "\".") + (0, _didYouMean.default)(suggestedTypes), node));
|
58 | }
|
59 | }
|
60 | };
|
61 | }
|
62 |
|
63 | var standardTypeNames = [].concat(_scalars.specifiedScalarTypes, _introspection.introspectionTypes).map(function (type) {
|
64 | return type.name;
|
65 | });
|
66 |
|
67 | function isStandardTypeName(typeName) {
|
68 | return standardTypeNames.indexOf(typeName) !== -1;
|
69 | }
|
70 |
|
71 | function isSDLNode(value) {
|
72 | return !Array.isArray(value) && ((0, _predicates.isTypeSystemDefinitionNode)(value) || (0, _predicates.isTypeSystemExtensionNode)(value));
|
73 | }
|