UNPKG

2.57 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.KnownTypeNamesRule = KnownTypeNamesRule;
7
8var _didYouMean = _interopRequireDefault(require("../../jsutils/didYouMean.js"));
9
10var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList.js"));
11
12var _GraphQLError = require("../../error/GraphQLError.js");
13
14var _predicates = require("../../language/predicates.js");
15
16var _scalars = require("../../type/scalars.js");
17
18var _introspection = require("../../type/introspection.js");
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22/**
23 * Known type names
24 *
25 * A GraphQL document is only valid if referenced types (specifically
26 * variable definitions and fragment conditions) are defined by the type schema.
27 */
28function 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
63var standardTypeNames = [].concat(_scalars.specifiedScalarTypes, _introspection.introspectionTypes).map(function (type) {
64 return type.name;
65});
66
67function isStandardTypeName(typeName) {
68 return standardTypeNames.indexOf(typeName) !== -1;
69}
70
71function isSDLNode(value) {
72 return !Array.isArray(value) && ((0, _predicates.isTypeSystemDefinitionNode)(value) || (0, _predicates.isTypeSystemExtensionNode)(value));
73}