UNPKG

graphql

Version:

A Query Language and Runtime which can target any service.

56 lines (46 loc) 2.08 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'] = FragmentsOnCompositeType; var _error = require('../../error'); var _typeDefinition = require('../../type/definition'); var _errors = require('../errors'); /** * Fragments on composite type * * Fragments use a type condition to determine if they apply, since fragments * can only be spread into a composite type (object, interface, or union), the * type condition must also be a composite type. */ function FragmentsOnCompositeType(context) { return { InlineFragment: function InlineFragment(node) { var typeName = node.typeCondition.value; var type = context.getSchema().getType(typeName); var isCompositeType = type instanceof _typeDefinition.GraphQLObjectType || type instanceof _typeDefinition.GraphQLInterfaceType || type instanceof _typeDefinition.GraphQLUnionType; if (!isCompositeType) { return new _error.GraphQLError('Fragment cannot condition on non composite type "' + typeName + '".', [node.typeCondition]); } }, FragmentDefinition: function FragmentDefinition(node) { var typeName = node.typeCondition.value; var type = context.getSchema().getType(typeName); var isCompositeType = type instanceof _typeDefinition.GraphQLObjectType || type instanceof _typeDefinition.GraphQLInterfaceType || type instanceof _typeDefinition.GraphQLUnionType; if (!isCompositeType) { return new _error.GraphQLError((0, _errors.fragmentOnNonCompositeErrorMessage)(node.name.value, typeName), [node.typeCondition]); } } }; } module.exports = exports['default'];