graphql
Version:
A Query Language and Runtime which can target any service.
74 lines (57 loc) • 2.22 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = VariablesAreInputTypes;
var _utilsInvariant = require('../../utils/invariant');
var _utilsInvariant2 = _interopRequireDefault(_utilsInvariant);
var _error = require('../../error');
var _languagePrinter = require('../../language/printer');
var _languageKinds = require('../../language/kinds');
var _typeDefinition = require('../../type/definition');
var _errors = require('../errors');
/**
* Variables are input types
*
* A GraphQL operation is only valid if all the variables it defines are of
* input types (scalar, enum, or input object).
*/
function VariablesAreInputTypes(context) {
return {
VariableDefinition: function VariableDefinition(node) {
var typeName = getTypeASTName(node.type);
var type = context.getSchema().getType(typeName);
var isInputType = type instanceof _typeDefinition.GraphQLScalarType || type instanceof _typeDefinition.GraphQLEnumType || type instanceof _typeDefinition.GraphQLInputObjectType;
if (!isInputType) {
var variableName = node.variable.name.value;
return new _error.GraphQLError((0, _errors.nonInputTypeOnVarMessage)(variableName, (0, _languagePrinter.print)(node.type)), [node.type]);
}
}
};
}
function getTypeASTName(_x) {
var _again = true;
_function: while (_again) {
var typeAST = _x;
_again = false;
if (typeAST.kind === _languageKinds.NAME) {
return typeAST.value;
}
(0, _utilsInvariant2['default'])(typeAST.type, 'Must be wrapping type');
_x = typeAST.type;
_again = true;
continue _function;
}
}
module.exports = exports['default'];