UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

79 lines (63 loc) 2.45 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 _slicedToArray = require('babel-runtime/helpers/sliced-to-array')['default']; var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default']; var _Object$keys = require('babel-runtime/core-js/object/keys')['default']; _Object$defineProperty(exports, '__esModule', { value: true }); exports['default'] = NoInputTypesAsOutputFields; var _error = require('../../error'); var _definition = require('../definition'); function NoInputTypesAsOutputFields(context) { var schema = context.getSchema(); var typeMap = schema.getTypeMap(); var errors = []; var queryType = schema.getQueryType(); if (queryType) { var queryError = operationMayNotBeInputType(queryType, 'query'); if (queryError !== null) { errors.push(queryError); } } var mutationType = schema.getMutationType(); if (mutationType) { var mutationError = operationMayNotBeInputType(mutationType, 'mutation'); if (mutationError !== null) { errors.push(mutationError); } } _Object$keys(typeMap).map(function (typeName) { return [typeName, typeMap[typeName]]; }).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2); var typeName = _ref2[0]; var type = _ref2[1]; if (!(type instanceof _definition.GraphQLObjectType) && !(type instanceof _definition.GraphQLInterfaceType)) { return; } var fields = type.getFields(); _Object$keys(fields).forEach(function (fieldName) { var field = fields[fieldName]; if (field.type instanceof _definition.GraphQLInputObjectType) { errors.push(new _error.GraphQLError('Field ' + typeName + '.' + field.name + ' is of type ' + ('' + field.type.name + ', which is an input type, but field types ') + 'must be output types!')); } }); }); return errors.length > 0 ? errors : null; } function operationMayNotBeInputType(type, operation) { if (!(0, _definition.isOutputType)(type)) { return new _error.GraphQLError('Schema ' + operation + ' type ' + type + ' must be an object type!'); } return null; } module.exports = exports['default'];