blips
Version:
State management for the GraphQL heads
78 lines (65 loc) • 2.5 kB
JavaScript
;
exports.__esModule = true;
exports.singleFieldOnlyMessage = singleFieldOnlyMessage;
exports.anonOperationNotAloneMessage = anonOperationNotAloneMessage;
exports.duplicateFragmentNameMessage = duplicateFragmentNameMessage;
exports.singleFieldSubscriptions = singleFieldSubscriptions;
exports.loneAnonymousOperation = loneAnonymousOperation;
exports.uniqueFragmentNames = uniqueFragmentNames;
var _graphql = require('graphql');
var _isFragmentDefinition = require('./isFragmentDefinition');
var _getOperationName = require('./getOperationName');
function singleFieldOnlyMessage(name) {
return (name ? 'Subscription "' + name + '" ' : 'Anonymous Subscription ') + 'must select only one top level field.';
}
function anonOperationNotAloneMessage() {
return 'This anonymous operation must be the only defined operation.';
}
function duplicateFragmentNameMessage(fragName) {
return 'There can be only one fragment named "' + fragName + '".';
}
/**
* Subscriptions must only include one field.
*
* A GraphQL subscription is valid only if it contains a single root field.
*/
function singleFieldSubscriptions(node) {
if (node.operation === 'subscription') {
if (node.selectionSet.selections.length !== 1) {
return new _graphql.GraphQLError(singleFieldOnlyMessage(node.name && node.name.value), node.selectionSet.selections.slice(1));
}
}
}
/**
* Lone anonymous operation
*
* A GraphQL document is only valid if when it contains an anonymous operation
* (the query short-hand) that it contains only that one operation definition.
*/
/**
* 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.
*
*
*/
function loneAnonymousOperation(node, operationCount) {
if (!node.name && operationCount > 1) {
return new _graphql.GraphQLError(anonOperationNotAloneMessage(), [node]);
}
}
/**
* Unique fragment names
*
* A GraphQL document is only valid if all defined fragments have unique names.
*/
function uniqueFragmentNames(node, knownFragmentNames) {
if (!(0, _isFragmentDefinition.isFragmentDefinition)(node)) return;
var fragmentName = (0, _getOperationName.getOperationName)(node);
if (knownFragmentNames[fragmentName]) {
return new _graphql.GraphQLError(duplicateFragmentNameMessage(fragmentName), [knownFragmentNames[fragmentName], node.name]);
} else {
knownFragmentNames[fragmentName] = node.name;
}
}