123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true,
- });
- exports.VariablesInAllowedPositionRule = VariablesInAllowedPositionRule;
- var _inspect = require('../../jsutils/inspect.js');
- var _GraphQLError = require('../../error/GraphQLError.js');
- var _kinds = require('../../language/kinds.js');
- var _definition = require('../../type/definition.js');
- var _typeComparators = require('../../utilities/typeComparators.js');
- var _typeFromAST = require('../../utilities/typeFromAST.js');
- function VariablesInAllowedPositionRule(context) {
- let varDefMap = Object.create(null);
- return {
- OperationDefinition: {
- enter() {
- varDefMap = Object.create(null);
- },
- leave(operation) {
- const usages = context.getRecursiveVariableUsages(operation);
- for (const { node, type, defaultValue } of usages) {
- const varName = node.name.value;
- const varDef = varDefMap[varName];
- if (varDef && type) {
-
-
-
-
-
- const schema = context.getSchema();
- const varType = (0, _typeFromAST.typeFromAST)(schema, varDef.type);
- if (
- varType &&
- !allowedVariableUsage(
- schema,
- varType,
- varDef.defaultValue,
- type,
- defaultValue,
- )
- ) {
- const varTypeStr = (0, _inspect.inspect)(varType);
- const typeStr = (0, _inspect.inspect)(type);
- context.reportError(
- new _GraphQLError.GraphQLError(
- `Variable "$${varName}" of type "${varTypeStr}" used in position expecting type "${typeStr}".`,
- {
- nodes: [varDef, node],
- },
- ),
- );
- }
- }
- }
- },
- },
- VariableDefinition(node) {
- varDefMap[node.variable.name.value] = node;
- },
- };
- }
- function allowedVariableUsage(
- schema,
- varType,
- varDefaultValue,
- locationType,
- locationDefaultValue,
- ) {
- if (
- (0, _definition.isNonNullType)(locationType) &&
- !(0, _definition.isNonNullType)(varType)
- ) {
- const hasNonNullVariableDefaultValue =
- varDefaultValue != null && varDefaultValue.kind !== _kinds.Kind.NULL;
- const hasLocationDefaultValue = locationDefaultValue !== undefined;
- if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) {
- return false;
- }
- const nullableLocationType = locationType.ofType;
- return (0, _typeComparators.isTypeSubTypeOf)(
- schema,
- varType,
- nullableLocationType,
- );
- }
- return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, locationType);
- }
|