UNPKG

6.05 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.specifiedDirectives = exports.GraphQLDeprecatedDirective = exports.DEFAULT_DEPRECATION_REASON = exports.GraphQLSkipDirective = exports.GraphQLIncludeDirective = exports.GraphQLDirective = undefined;
7exports.isDirective = isDirective;
8exports.isSpecifiedDirective = isSpecifiedDirective;
9
10var _wrappers = require('./wrappers');
11
12var _scalars = require('./scalars');
13
14var _instanceOf = require('../jsutils/instanceOf');
15
16var _instanceOf2 = _interopRequireDefault(_instanceOf);
17
18var _invariant = require('../jsutils/invariant');
19
20var _invariant2 = _interopRequireDefault(_invariant);
21
22var _directiveLocation = require('../language/directiveLocation');
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /**
27 * Copyright (c) 2015-present, Facebook, Inc.
28 *
29 * This source code is licensed under the MIT license found in the
30 * LICENSE file in the root directory of this source tree.
31 *
32 *
33 */
34
35// eslint-disable-next-line no-redeclare
36
37
38/**
39 * Test if the given value is a GraphQL directive.
40 */
41function isDirective(directive) {
42 return (0, _instanceOf2.default)(directive, GraphQLDirective);
43}
44
45/**
46 * Directives are used by the GraphQL runtime as a way of modifying execution
47 * behavior. Type system creators will usually not create these directly.
48 */
49
50var GraphQLDirective = exports.GraphQLDirective = function GraphQLDirective(config) {
51 _classCallCheck(this, GraphQLDirective);
52
53 this.name = config.name;
54 this.description = config.description;
55 this.locations = config.locations;
56 this.astNode = config.astNode;
57 !config.name ? (0, _invariant2.default)(0, 'Directive must be named.') : void 0;
58 !Array.isArray(config.locations) ? (0, _invariant2.default)(0, 'Must provide locations for directive.') : void 0;
59
60 var args = config.args;
61 if (!args) {
62 this.args = [];
63 } else {
64 !!Array.isArray(args) ? (0, _invariant2.default)(0, '@' + config.name + ' args must be an object with argument names as keys.') : void 0;
65 this.args = Object.keys(args).map(function (argName) {
66 var arg = args[argName];
67 return {
68 name: argName,
69 description: arg.description === undefined ? null : arg.description,
70 type: arg.type,
71 defaultValue: arg.defaultValue,
72 astNode: arg.astNode
73 };
74 });
75 }
76};
77
78/**
79 * Used to conditionally include fields or fragments.
80 */
81var GraphQLIncludeDirective = exports.GraphQLIncludeDirective = new GraphQLDirective({
82 name: 'include',
83 description: 'Directs the executor to include this field or fragment only when ' + 'the `if` argument is true.',
84 locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT],
85 args: {
86 if: {
87 type: (0, _wrappers.GraphQLNonNull)(_scalars.GraphQLBoolean),
88 description: 'Included when true.'
89 }
90 }
91});
92
93/**
94 * Used to conditionally skip (exclude) fields or fragments.
95 */
96var GraphQLSkipDirective = exports.GraphQLSkipDirective = new GraphQLDirective({
97 name: 'skip',
98 description: 'Directs the executor to skip this field or fragment when the `if` ' + 'argument is true.',
99 locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT],
100 args: {
101 if: {
102 type: (0, _wrappers.GraphQLNonNull)(_scalars.GraphQLBoolean),
103 description: 'Skipped when true.'
104 }
105 }
106});
107
108/**
109 * Constant string used for default reason for a deprecation.
110 */
111var DEFAULT_DEPRECATION_REASON = exports.DEFAULT_DEPRECATION_REASON = 'No longer supported';
112
113/**
114 * Used to declare element of a GraphQL schema as deprecated.
115 */
116var GraphQLDeprecatedDirective = exports.GraphQLDeprecatedDirective = new GraphQLDirective({
117 name: 'deprecated',
118 description: 'Marks an element of a GraphQL schema as no longer supported.',
119 locations: [_directiveLocation.DirectiveLocation.FIELD_DEFINITION, _directiveLocation.DirectiveLocation.ENUM_VALUE],
120 args: {
121 reason: {
122 type: _scalars.GraphQLString,
123 description: 'Explains why this element was deprecated, usually also including a ' + 'suggestion for how to access supported similar data. Formatted ' + 'in [Markdown](https://daringfireball.net/projects/markdown/).',
124 defaultValue: DEFAULT_DEPRECATION_REASON
125 }
126 }
127});
128
129/**
130 * The full list of specified directives.
131 */
132var specifiedDirectives = exports.specifiedDirectives = [GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective];
133
134function isSpecifiedDirective(directive) {
135 return specifiedDirectives.some(function (specifiedDirective) {
136 return specifiedDirective.name === directive.name;
137 });
138}
\No newline at end of file