function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * strict */ import { Kind } from '../language/kinds'; import { isObjectType, isInterfaceType, isEnumType, isInputObjectType, isListType, isCompositeType, isInputType, isOutputType, getNullableType, getNamedType } from '../type/definition'; import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from '../type/introspection'; import { typeFromAST } from './typeFromAST'; import find from '../jsutils/find'; /** * TypeInfo is a utility class which, given a GraphQL schema, can keep track * of the current field and type definitions at any point in a GraphQL document * AST during a recursive descent by calling `enter(node)` and `leave(node)`. */ export var TypeInfo = function () { function TypeInfo(schema, // NOTE: this experimental optional second parameter is only needed in order // to support non-spec-compliant codebases. You should never need to use it. getFieldDefFn, // Initial type may be provided in rare cases to facilitate traversals initialType) { _classCallCheck(this, TypeInfo); this._schema = schema; this._typeStack = []; this._parentTypeStack = []; this._inputTypeStack = []; this._fieldDefStack = []; this._directive = null; this._argument = null; this._enumValue = null; this._getFieldDef = getFieldDefFn || getFieldDef; if (initialType) { if (isInputType(initialType)) { this._inputTypeStack.push(initialType); } if (isCompositeType(initialType)) { this._parentTypeStack.push(initialType); } if (isOutputType(initialType)) { this._typeStack.push(initialType); } } } TypeInfo.prototype.getType = function getType() { if (this._typeStack.length > 0) { return this._typeStack[this._typeStack.length - 1]; } }; TypeInfo.prototype.getParentType = function getParentType() { if (this._parentTypeStack.length > 0) { return this._parentTypeStack[this._parentTypeStack.length - 1]; } }; TypeInfo.prototype.getInputType = function getInputType() { if (this._inputTypeStack.length > 0) { return this._inputTypeStack[this._inputTypeStack.length - 1]; } }; TypeInfo.prototype.getParentInputType = function getParentInputType() { if (this._inputTypeStack.length > 1) { return this._inputTypeStack[this._inputTypeStack.length - 2]; } }; TypeInfo.prototype.getFieldDef = function getFieldDef() { if (this._fieldDefStack.length > 0) { return this._fieldDefStack[this._fieldDefStack.length - 1]; } }; TypeInfo.prototype.getDirective = function getDirective() { return this._directive; }; TypeInfo.prototype.getArgument = function getArgument() { return this._argument; }; TypeInfo.prototype.getEnumValue = function getEnumValue() { return this._enumValue; }; // Flow does not yet handle this case. TypeInfo.prototype.enter = function enter(node /* ASTNode */) { var schema = this._schema; // Note: many of the types below are explicitly typed as "mixed" to drop // any assumptions of a valid schema to ensure runtime types are properly // checked before continuing since TypeInfo is used as part of validation // which occurs before guarantees of schema and document validity. switch (node.kind) { case Kind.SELECTION_SET: var namedType = getNamedType(this.getType()); this._parentTypeStack.push(isCompositeType(namedType) ? namedType : undefined); break; case Kind.FIELD: var parentType = this.getParentType(); var fieldDef = void 0; var fieldType = void 0; if (parentType) { fieldDef = this._getFieldDef(schema, parentType, node); if (fieldDef) { fieldType = fieldDef.type; } } this._fieldDefStack.push(fieldDef); this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined); break; case Kind.DIRECTIVE: this._directive = schema.getDirective(node.name.value); break; case Kind.OPERATION_DEFINITION: var type = void 0; if (node.operation === 'query') { type = schema.getQueryType(); } else if (node.operation === 'mutation') { type = schema.getMutationType(); } else if (node.operation === 'subscription') { type = schema.getSubscriptionType(); } this._typeStack.push(isObjectType(type) ? type : undefined); break; case Kind.INLINE_FRAGMENT: case Kind.FRAGMENT_DEFINITION: var typeConditionAST = node.typeCondition; var outputType = typeConditionAST ? typeFromAST(schema, typeConditionAST) : getNamedType(this.getType()); this._typeStack.push(isOutputType(outputType) ? outputType : undefined); break; case Kind.VARIABLE_DEFINITION: var inputType = typeFromAST(schema, node.type); this._inputTypeStack.push(isInputType(inputType) ? inputType : undefined); break; case Kind.ARGUMENT: var argDef = void 0; var argType = void 0; var fieldOrDirective = this.getDirective() || this.getFieldDef(); if (fieldOrDirective) { argDef = find(fieldOrDirective.args, function (arg) { return arg.name === node.name.value; }); if (argDef) { argType = argDef.type; } } this._argument = argDef; this._inputTypeStack.push(isInputType(argType) ? argType : undefined); break; case Kind.LIST: var listType = getNullableType(this.getInputType()); var itemType = isListType(listType) ? listType.ofType : listType; this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined); break; case Kind.OBJECT_FIELD: var objectType = getNamedType(this.getInputType()); var inputFieldType = void 0; if (isInputObjectType(objectType)) { var inputField = objectType.getFields()[node.name.value]; if (inputField) { inputFieldType = inputField.type; } } this._inputTypeStack.push(isInputType(inputFieldType) ? inputFieldType : undefined); break; case Kind.ENUM: var enumType = getNamedType(this.getInputType()); var enumValue = void 0; if (isEnumType(enumType)) { enumValue = enumType.getValue(node.value); } this._enumValue = enumValue; break; } }; TypeInfo.prototype.leave = function leave(node) { switch (node.kind) { case Kind.SELECTION_SET: this._parentTypeStack.pop(); break; case Kind.FIELD: this._fieldDefStack.pop(); this._typeStack.pop(); break; case Kind.DIRECTIVE: this._directive = null; break; case Kind.OPERATION_DEFINITION: case Kind.INLINE_FRAGMENT: case Kind.FRAGMENT_DEFINITION: this._typeStack.pop(); break; case Kind.VARIABLE_DEFINITION: this._inputTypeStack.pop(); break; case Kind.ARGUMENT: this._argument = null; this._inputTypeStack.pop(); break; case Kind.LIST: case Kind.OBJECT_FIELD: this._inputTypeStack.pop(); break; case Kind.ENUM: this._enumValue = null; break; } }; return TypeInfo; }(); /** * Not exactly the same as the executor's definition of getFieldDef, in this * statically evaluated environment we do not always have an Object type, * and need to handle Interface and Union types. */ function getFieldDef(schema, parentType, fieldNode) { var name = fieldNode.name.value; if (name === SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { return SchemaMetaFieldDef; } if (name === TypeMetaFieldDef.name && schema.getQueryType() === parentType) { return TypeMetaFieldDef; } if (name === TypeNameMetaFieldDef.name && isCompositeType(parentType)) { return TypeNameMetaFieldDef; } if (isObjectType(parentType) || isInterfaceType(parentType)) { return parentType.getFields()[name]; } }