UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

76 lines (65 loc) 2.2 kB
/* @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. */ 'use strict'; var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default']; _Object$defineProperty(exports, '__esModule', { value: true }); exports['default'] = NoUnusedVariables; var _error = require('../../error'); var _errors = require('../errors'); /** * No unused variables * * A GraphQL operation is only valid if all variables defined by an operation * are used, either directly or within a spread fragment. */ function NoUnusedVariables(context) { var visitedFragmentNames = {}; var variableDefs = []; var variableNameUsed = {}; return { // Visit FragmentDefinition after visiting FragmentSpread visitSpreadFragments: true, OperationDefinition: { enter: function enter() { visitedFragmentNames = {}; variableDefs = []; variableNameUsed = {}; }, leave: function leave() { var errors = variableDefs.filter(function (def) { return variableNameUsed[def.variable.name.value] !== true; }).map(function (def) { return new _error.GraphQLError((0, _errors.unusedVariableMessage)(def.variable.name.value), [def]); }); if (errors.length > 0) { return errors; } } }, VariableDefinition: function VariableDefinition(def) { variableDefs.push(def); // Do not visit deeper, or else the defined variable name will be visited. return false; }, Variable: function Variable(variable) { variableNameUsed[variable.name.value] = true; }, FragmentSpread: function FragmentSpread(spreadAST) { // Only visit fragments of a particular name once per operation if (visitedFragmentNames[spreadAST.name.value] === true) { return false; } visitedFragmentNames[spreadAST.name.value] = true; } }; } module.exports = exports['default'];