diff --git a/.babelrc b/.babelrc index d489eb6cc9..5fc1f02280 100644 --- a/.babelrc +++ b/.babelrc @@ -1,5 +1,5 @@ { - "presets": [["env", { + "presets": [["@babel/preset-env", { "modules": false, "targets": { "node": 4, @@ -16,12 +16,12 @@ "./resources/common-js-modules", "./resources/inline-invariant", "syntax-async-functions", - "syntax-async-generators", - "transform-class-properties", - "transform-flow-strip-types", - "transform-object-rest-spread", - ["transform-es2015-classes", {"loose": true}], - ["transform-es2015-destructuring", {"loose": true}], - ["transform-es2015-spread", {"loose": true}] + "@babel/plugin-syntax-async-generators", + "@babel/plugin-proposal-class-properties", + "@babel/plugin-transform-flow-strip-types", + "@babel/plugin-proposal-object-rest-spread", + ["@babel/plugin-transform-classes", {"loose": true}], + ["@babel/plugin-transform-destructuring", {"loose": true}], + ["@babel/plugin-transform-spread", {"loose": true}] ] } diff --git a/.gitignore b/.gitignore index 89f817e5df..b1ae3c0527 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,4 @@ node_modules coverage -dist npm diff --git a/dist/LICENSE b/dist/LICENSE new file mode 100644 index 0000000000..9e051010d8 --- /dev/null +++ b/dist/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2015-present, Facebook, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/dist/README.md b/dist/README.md new file mode 100644 index 0000000000..0148a8cbd1 --- /dev/null +++ b/dist/README.md @@ -0,0 +1,145 @@ +# GraphQL.js + +The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook. + +[![npm version](https://badge.fury.io/js/graphql.svg)](http://badge.fury.io/js/graphql) +[![Build Status](https://travis-ci.org/graphql/graphql-js.svg?branch=master)](https://travis-ci.org/graphql/graphql-js?branch=master) +[![Coverage Status](https://coveralls.io/repos/graphql/graphql-js/badge.svg?branch=master)](https://coveralls.io/r/graphql/graphql-js?branch=master) + +See more complete documentation at http://graphql.org/ and +http://graphql.org/graphql-js/. + +Looking for help? Find resources [from the community](http://graphql.org/community/). + + +## Getting Started + +An overview of GraphQL in general is available in the +[README](https://github.com/facebook/graphql/blob/master/README.md) for the +[Specification for GraphQL](https://github.com/facebook/graphql). That overview +describes a simple set of GraphQL examples that exist as [tests](src/__tests__) +in this repository. A good way to get started with this repository is to walk +through that README and the corresponding tests in parallel. + +### Using GraphQL.js + +Install GraphQL.js from npm + +With yarn: + +```sh +yarn add graphql +``` + +or alternatively using npm: + +```sh +npm install --save graphql +``` + +GraphQL.js provides two important capabilities: building a type schema, and +serving queries against that type schema. + +First, build a GraphQL type schema which maps to your code base. + +```js +import { + graphql, + GraphQLSchema, + GraphQLObjectType, + GraphQLString +} from 'graphql'; + +var schema = new GraphQLSchema({ + query: new GraphQLObjectType({ + name: 'RootQueryType', + fields: { + hello: { + type: GraphQLString, + resolve() { + return 'world'; + } + } + } + }) +}); +``` + +This defines a simple schema with one type and one field, that resolves +to a fixed value. The `resolve` function can return a value, a promise, +or an array of promises. A more complex example is included in the top +level [tests](src/__tests__) directory. + +Then, serve the result of a query against that type schema. + +```js +var query = '{ hello }'; + +graphql(schema, query).then(result => { + + // Prints + // { + // data: { hello: "world" } + // } + console.log(result); + +}); +``` + +This runs a query fetching the one field defined. The `graphql` function will +first ensure the query is syntactically and semantically valid before executing +it, reporting errors otherwise. + +```js +var query = '{ boyhowdy }'; + +graphql(schema, query).then(result => { + + // Prints + // { + // errors: [ + // { message: 'Cannot query field boyhowdy on RootQueryType', + // locations: [ { line: 1, column: 3 } ] } + // ] + // } + console.log(result); + +}); +``` + +### Want to ride the bleeding edge? + +The `npm` branch in this repository is automatically maintained to be the last +commit to `master` to pass all tests, in the same form found on npm. It is +recommended to use builds deployed to npm for many reasons, but if you want to use +the latest not-yet-released version of graphql-js, you can do so by depending +directly on this branch: + +``` +npm install graphql@git://github.com/graphql/graphql-js.git#npm +``` + +### Using in a Browser + +GraphQL.js is a general purpose library and can be used both in a Node server +and in the browser. As an example, the [GraphiQL](https://github.com/graphql/graphiql/) +tool is built with GraphQL.js! + +Building a project using GraphQL.js with [webpack](https://webpack.js.org) or +[rollup](https://github.com/rollup/rollup) should just work and only include +the portions of the library you use. This works because GraphQL.js is distributed +with both CommonJS (`require()`) and ESModule (`import`) files. Ensure that any +custom build configurations look for `.mjs` files! + +### Contributing + +We actively welcome pull requests, learn how to +[contribute](https://github.com/graphql/graphql-js/blob/master/.github/CONTRIBUTING.md). + +### Changelog + +Changes are tracked as [GitHub releases](https://github.com/graphql/graphql-js/releases). + +### License + +GraphQL.js is [MIT-licensed](https://github.com/graphql/graphql-js/blob/master/LICENSE). diff --git a/dist/error/GraphQLError.js b/dist/error/GraphQLError.js new file mode 100644 index 0000000000..f9d5347083 --- /dev/null +++ b/dist/error/GraphQLError.js @@ -0,0 +1,146 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.GraphQLError = GraphQLError; + +var _printError = require("./printError"); + +var _location = require("../language/location"); + +/** + * 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. + * + * strict + */ +function GraphQLError( // eslint-disable-line no-redeclare +message, nodes, source, positions, path, originalError, extensions) { + // Compute list of blame nodes. + var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions. + + + var _source = source; + + if (!_source && _nodes) { + var node = _nodes[0]; + _source = node && node.loc && node.loc.source; + } + + var _positions = positions; + + if (!_positions && _nodes) { + _positions = _nodes.reduce(function (list, node) { + if (node.loc) { + list.push(node.loc.start); + } + + return list; + }, []); + } + + if (_positions && _positions.length === 0) { + _positions = undefined; + } + + var _locations; + + if (positions && source) { + _locations = positions.map(function (pos) { + return (0, _location.getLocation)(source, pos); + }); + } else if (_nodes) { + _locations = _nodes.reduce(function (list, node) { + if (node.loc) { + list.push((0, _location.getLocation)(node.loc.source, node.loc.start)); + } + + return list; + }, []); + } + + var _extensions = extensions || originalError && originalError.extensions; + + Object.defineProperties(this, { + message: { + value: message, + // By being enumerable, JSON.stringify will include `message` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: true, + writable: true + }, + locations: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _locations || undefined, + // By being enumerable, JSON.stringify will include `locations` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_locations) + }, + path: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: path || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(path) + }, + nodes: { + value: _nodes || undefined + }, + source: { + value: _source || undefined + }, + positions: { + value: _positions || undefined + }, + originalError: { + value: originalError + }, + extensions: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _extensions || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_extensions) + } + }); // Include (non-enumerable) stack trace. + + if (originalError && originalError.stack) { + Object.defineProperty(this, 'stack', { + value: originalError.stack, + writable: true, + configurable: true + }); + } else if (Error.captureStackTrace) { + Error.captureStackTrace(this, GraphQLError); + } else { + Object.defineProperty(this, 'stack', { + value: Error().stack, + writable: true, + configurable: true + }); + } +} + +GraphQLError.prototype = Object.create(Error.prototype, { + constructor: { + value: GraphQLError + }, + name: { + value: 'GraphQLError' + }, + toString: { + value: function toString() { + return (0, _printError.printError)(this); + } + } +}); \ No newline at end of file diff --git a/dist/error/GraphQLError.js.flow b/dist/error/GraphQLError.js.flow new file mode 100644 index 0000000000..ecd7997c3e --- /dev/null +++ b/dist/error/GraphQLError.js.flow @@ -0,0 +1,221 @@ +/** + * 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. + * + * @flow strict + */ + +import { printError } from './printError'; +import { getLocation } from '../language/location'; +import type { SourceLocation } from '../language/location'; +import type { ASTNode } from '../language/ast'; +import type { Source } from '../language/source'; + +/** + * A GraphQLError describes an Error found during the parse, validate, or + * execute phases of performing a GraphQL operation. In addition to a message + * and stack trace, it also includes information about the locations in a + * GraphQL document and/or execution result that correspond to the Error. + */ +declare class GraphQLError extends Error { + constructor( + message: string, + nodes?: $ReadOnlyArray | ASTNode | void, + source?: ?Source, + positions?: ?$ReadOnlyArray, + path?: ?$ReadOnlyArray, + originalError?: ?Error, + extensions?: ?{ [key: string]: mixed }, + ): void; + + /** + * A message describing the Error for debugging purposes. + * + * Enumerable, and appears in the result of JSON.stringify(). + * + * Note: should be treated as readonly, despite invariant usage. + */ + message: string; + + /** + * An array of { line, column } locations within the source GraphQL document + * which correspond to this error. + * + * Errors during validation often contain multiple locations, for example to + * point out two things with the same name. Errors during execution include a + * single location, the field which produced the error. + * + * Enumerable, and appears in the result of JSON.stringify(). + */ + +locations: $ReadOnlyArray | void; + + /** + * An array describing the JSON-path into the execution response which + * corresponds to this error. Only included for errors during execution. + * + * Enumerable, and appears in the result of JSON.stringify(). + */ + +path: $ReadOnlyArray | void; + + /** + * An array of GraphQL AST Nodes corresponding to this error. + */ + +nodes: $ReadOnlyArray | void; + + /** + * The source GraphQL document for the first location of this error. + * + * Note that if this Error represents more than one node, the source may not + * represent nodes after the first node. + */ + +source: Source | void; + + /** + * An array of character offsets within the source GraphQL document + * which correspond to this error. + */ + +positions: $ReadOnlyArray | void; + + /** + * The original error thrown from a field resolver during execution. + */ + +originalError: ?Error; + + /** + * Extension fields to add to the formatted error. + */ + +extensions: { [key: string]: mixed } | void; +} + +export function GraphQLError( // eslint-disable-line no-redeclare + message: string, + nodes?: $ReadOnlyArray | ASTNode | void, + source?: ?Source, + positions?: ?$ReadOnlyArray, + path?: ?$ReadOnlyArray, + originalError?: ?Error, + extensions?: ?{ [key: string]: mixed }, +) { + // Compute list of blame nodes. + const _nodes = Array.isArray(nodes) + ? nodes.length !== 0 + ? nodes + : undefined + : nodes + ? [nodes] + : undefined; + + // Compute locations in the source for the given nodes/positions. + let _source = source; + if (!_source && _nodes) { + const node = _nodes[0]; + _source = node && node.loc && node.loc.source; + } + + let _positions = positions; + if (!_positions && _nodes) { + _positions = _nodes.reduce((list, node) => { + if (node.loc) { + list.push(node.loc.start); + } + return list; + }, []); + } + if (_positions && _positions.length === 0) { + _positions = undefined; + } + + let _locations; + if (positions && source) { + _locations = positions.map(pos => getLocation(source, pos)); + } else if (_nodes) { + _locations = _nodes.reduce((list, node) => { + if (node.loc) { + list.push(getLocation(node.loc.source, node.loc.start)); + } + return list; + }, []); + } + + const _extensions = + extensions || (originalError && (originalError: any).extensions); + + Object.defineProperties(this, { + message: { + value: message, + // By being enumerable, JSON.stringify will include `message` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: true, + writable: true, + }, + locations: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _locations || undefined, + // By being enumerable, JSON.stringify will include `locations` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_locations), + }, + path: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: path || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(path), + }, + nodes: { + value: _nodes || undefined, + }, + source: { + value: _source || undefined, + }, + positions: { + value: _positions || undefined, + }, + originalError: { + value: originalError, + }, + extensions: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _extensions || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_extensions), + }, + }); + + // Include (non-enumerable) stack trace. + if (originalError && originalError.stack) { + Object.defineProperty(this, 'stack', { + value: originalError.stack, + writable: true, + configurable: true, + }); + } else if (Error.captureStackTrace) { + Error.captureStackTrace(this, GraphQLError); + } else { + Object.defineProperty(this, 'stack', { + value: Error().stack, + writable: true, + configurable: true, + }); + } +} + +(GraphQLError: any).prototype = Object.create(Error.prototype, { + constructor: { value: GraphQLError }, + name: { value: 'GraphQLError' }, + toString: { + value: function toString() { + return printError(this); + }, + }, +}); diff --git a/dist/error/GraphQLError.mjs b/dist/error/GraphQLError.mjs new file mode 100644 index 0000000000..ac55ab1aff --- /dev/null +++ b/dist/error/GraphQLError.mjs @@ -0,0 +1,136 @@ +/** + * 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. + * + * strict + */ +import { printError } from './printError'; +import { getLocation } from '../language/location'; +export function GraphQLError( // eslint-disable-line no-redeclare +message, nodes, source, positions, path, originalError, extensions) { + // Compute list of blame nodes. + var _nodes = Array.isArray(nodes) ? nodes.length !== 0 ? nodes : undefined : nodes ? [nodes] : undefined; // Compute locations in the source for the given nodes/positions. + + + var _source = source; + + if (!_source && _nodes) { + var node = _nodes[0]; + _source = node && node.loc && node.loc.source; + } + + var _positions = positions; + + if (!_positions && _nodes) { + _positions = _nodes.reduce(function (list, node) { + if (node.loc) { + list.push(node.loc.start); + } + + return list; + }, []); + } + + if (_positions && _positions.length === 0) { + _positions = undefined; + } + + var _locations; + + if (positions && source) { + _locations = positions.map(function (pos) { + return getLocation(source, pos); + }); + } else if (_nodes) { + _locations = _nodes.reduce(function (list, node) { + if (node.loc) { + list.push(getLocation(node.loc.source, node.loc.start)); + } + + return list; + }, []); + } + + var _extensions = extensions || originalError && originalError.extensions; + + Object.defineProperties(this, { + message: { + value: message, + // By being enumerable, JSON.stringify will include `message` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: true, + writable: true + }, + locations: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _locations || undefined, + // By being enumerable, JSON.stringify will include `locations` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_locations) + }, + path: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: path || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(path) + }, + nodes: { + value: _nodes || undefined + }, + source: { + value: _source || undefined + }, + positions: { + value: _positions || undefined + }, + originalError: { + value: originalError + }, + extensions: { + // Coercing falsey values to undefined ensures they will not be included + // in JSON.stringify() when not provided. + value: _extensions || undefined, + // By being enumerable, JSON.stringify will include `path` in the + // resulting output. This ensures that the simplest possible GraphQL + // service adheres to the spec. + enumerable: Boolean(_extensions) + } + }); // Include (non-enumerable) stack trace. + + if (originalError && originalError.stack) { + Object.defineProperty(this, 'stack', { + value: originalError.stack, + writable: true, + configurable: true + }); + } else if (Error.captureStackTrace) { + Error.captureStackTrace(this, GraphQLError); + } else { + Object.defineProperty(this, 'stack', { + value: Error().stack, + writable: true, + configurable: true + }); + } +} +GraphQLError.prototype = Object.create(Error.prototype, { + constructor: { + value: GraphQLError + }, + name: { + value: 'GraphQLError' + }, + toString: { + value: function toString() { + return printError(this); + } + } +}); \ No newline at end of file diff --git a/dist/error/formatError.js b/dist/error/formatError.js new file mode 100644 index 0000000000..1ccb5ad485 --- /dev/null +++ b/dist/error/formatError.js @@ -0,0 +1,41 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.formatError = formatError; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Given a GraphQLError, format it according to the rules described by the + * Response Format, Errors section of the GraphQL Specification. + */ +function formatError(error) { + !error ? (0, _invariant.default)(0, 'Received null or undefined error.') : void 0; + var message = error.message || 'An unknown error occurred.'; + var locations = error.locations; + var path = error.path; + var extensions = error.extensions; + return extensions ? { + message: message, + locations: locations, + path: path, + extensions: extensions + } : { + message: message, + locations: locations, + path: path + }; +} \ No newline at end of file diff --git a/dist/error/formatError.js.flow b/dist/error/formatError.js.flow new file mode 100644 index 0000000000..a671812961 --- /dev/null +++ b/dist/error/formatError.js.flow @@ -0,0 +1,35 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; +import type { GraphQLError } from './GraphQLError'; +import type { SourceLocation } from '../language/location'; + +/** + * Given a GraphQLError, format it according to the rules described by the + * Response Format, Errors section of the GraphQL Specification. + */ +export function formatError(error: GraphQLError): GraphQLFormattedError { + invariant(error, 'Received null or undefined error.'); + const message = error.message || 'An unknown error occurred.'; + const locations = error.locations; + const path = error.path; + const extensions = error.extensions; + + return extensions + ? { message, locations, path, extensions } + : { message, locations, path }; +} + +export type GraphQLFormattedError = {| + +message: string, + +locations: $ReadOnlyArray | void, + +path: $ReadOnlyArray | void, + +extensions?: { [key: string]: mixed }, +|}; diff --git a/dist/error/formatError.mjs b/dist/error/formatError.mjs new file mode 100644 index 0000000000..a83bea67ec --- /dev/null +++ b/dist/error/formatError.mjs @@ -0,0 +1,31 @@ +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; + +/** + * Given a GraphQLError, format it according to the rules described by the + * Response Format, Errors section of the GraphQL Specification. + */ +export function formatError(error) { + !error ? invariant(0, 'Received null or undefined error.') : void 0; + var message = error.message || 'An unknown error occurred.'; + var locations = error.locations; + var path = error.path; + var extensions = error.extensions; + return extensions ? { + message: message, + locations: locations, + path: path, + extensions: extensions + } : { + message: message, + locations: locations, + path: path + }; +} \ No newline at end of file diff --git a/dist/error/index.js b/dist/error/index.js new file mode 100644 index 0000000000..94939e8760 --- /dev/null +++ b/dist/error/index.js @@ -0,0 +1,45 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "GraphQLError", { + enumerable: true, + get: function get() { + return _GraphQLError.GraphQLError; + } +}); +Object.defineProperty(exports, "syntaxError", { + enumerable: true, + get: function get() { + return _syntaxError.syntaxError; + } +}); +Object.defineProperty(exports, "locatedError", { + enumerable: true, + get: function get() { + return _locatedError.locatedError; + } +}); +Object.defineProperty(exports, "printError", { + enumerable: true, + get: function get() { + return _printError.printError; + } +}); +Object.defineProperty(exports, "formatError", { + enumerable: true, + get: function get() { + return _formatError.formatError; + } +}); + +var _GraphQLError = require("./GraphQLError"); + +var _syntaxError = require("./syntaxError"); + +var _locatedError = require("./locatedError"); + +var _printError = require("./printError"); + +var _formatError = require("./formatError"); \ No newline at end of file diff --git a/dist/error/index.js.flow b/dist/error/index.js.flow new file mode 100644 index 0000000000..3ca6fdb15f --- /dev/null +++ b/dist/error/index.js.flow @@ -0,0 +1,16 @@ +/** + * 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. + * + * @flow strict + */ + +export { GraphQLError } from './GraphQLError'; +export { syntaxError } from './syntaxError'; +export { locatedError } from './locatedError'; +export { printError } from './printError'; +export { formatError } from './formatError'; + +export type { GraphQLFormattedError } from './formatError'; diff --git a/dist/error/index.mjs b/dist/error/index.mjs new file mode 100644 index 0000000000..2a23479fbb --- /dev/null +++ b/dist/error/index.mjs @@ -0,0 +1,13 @@ +/** + * 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. + * + * strict + */ +export { GraphQLError } from './GraphQLError'; +export { syntaxError } from './syntaxError'; +export { locatedError } from './locatedError'; +export { printError } from './printError'; +export { formatError } from './formatError'; \ No newline at end of file diff --git a/dist/error/locatedError.js b/dist/error/locatedError.js new file mode 100644 index 0000000000..07a0b8f623 --- /dev/null +++ b/dist/error/locatedError.js @@ -0,0 +1,32 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.locatedError = locatedError; + +var _GraphQLError = require("./GraphQLError"); + +/** + * 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. + * + * strict + */ + +/** + * Given an arbitrary Error, presumably thrown while attempting to execute a + * GraphQL operation, produce a new GraphQLError aware of the location in the + * document responsible for the original Error. + */ +function locatedError(originalError, nodes, path) { + // Note: this uses a brand-check to support GraphQL errors originating from + // other contexts. + if (originalError && Array.isArray(originalError.path)) { + return originalError; + } + + return new _GraphQLError.GraphQLError(originalError && originalError.message, originalError && originalError.nodes || nodes, originalError && originalError.source, originalError && originalError.positions, path, originalError); +} \ No newline at end of file diff --git a/dist/error/locatedError.js.flow b/dist/error/locatedError.js.flow new file mode 100644 index 0000000000..0f49516f09 --- /dev/null +++ b/dist/error/locatedError.js.flow @@ -0,0 +1,37 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLError } from './GraphQLError'; +import type { ASTNode } from '../language/ast'; + +/** + * Given an arbitrary Error, presumably thrown while attempting to execute a + * GraphQL operation, produce a new GraphQLError aware of the location in the + * document responsible for the original Error. + */ +export function locatedError( + originalError: Error | GraphQLError, + nodes: $ReadOnlyArray, + path: $ReadOnlyArray, +): GraphQLError { + // Note: this uses a brand-check to support GraphQL errors originating from + // other contexts. + if (originalError && Array.isArray(originalError.path)) { + return (originalError: any); + } + + return new GraphQLError( + originalError && originalError.message, + (originalError && (originalError: any).nodes) || nodes, + originalError && (originalError: any).source, + originalError && (originalError: any).positions, + path, + originalError, + ); +} diff --git a/dist/error/locatedError.mjs b/dist/error/locatedError.mjs new file mode 100644 index 0000000000..67a0a3ff02 --- /dev/null +++ b/dist/error/locatedError.mjs @@ -0,0 +1,24 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from './GraphQLError'; + +/** + * Given an arbitrary Error, presumably thrown while attempting to execute a + * GraphQL operation, produce a new GraphQLError aware of the location in the + * document responsible for the original Error. + */ +export function locatedError(originalError, nodes, path) { + // Note: this uses a brand-check to support GraphQL errors originating from + // other contexts. + if (originalError && Array.isArray(originalError.path)) { + return originalError; + } + + return new GraphQLError(originalError && originalError.message, originalError && originalError.nodes || nodes, originalError && originalError.source, originalError && originalError.positions, path, originalError); +} \ No newline at end of file diff --git a/dist/error/printError.js b/dist/error/printError.js new file mode 100644 index 0000000000..d40ea8a763 --- /dev/null +++ b/dist/error/printError.js @@ -0,0 +1,105 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.printError = printError; + +var _location = require("../language/location"); + +/** + * 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. + * + * strict + */ + +/** + * Prints a GraphQLError to a string, representing useful location information + * about the error's position in the source. + */ +function printError(error) { + var printedLocations = []; + + if (error.nodes) { + error.nodes.forEach(function (node) { + if (node.loc) { + printedLocations.push(highlightSourceAtLocation(node.loc.source, (0, _location.getLocation)(node.loc.source, node.loc.start))); + } + }); + } else if (error.source && error.locations) { + var source = error.source; + error.locations.forEach(function (location) { + printedLocations.push(highlightSourceAtLocation(source, location)); + }); + } + + return printedLocations.length === 0 ? error.message : [error.message].concat(printedLocations).join('\n\n') + '\n'; +} +/** + * Render a helpful description of the location of the error in the GraphQL + * Source document. + */ + + +function highlightSourceAtLocation(source, location) { + var firstLineColumnOffset = source.locationOffset.column - 1; + var body = whitespace(firstLineColumnOffset) + source.body; + var lineIndex = location.line - 1; + var lineOffset = source.locationOffset.line - 1; + var lineNum = location.line + lineOffset; + var columnOffset = location.line === 1 ? firstLineColumnOffset : 0; + var columnNum = location.column + columnOffset; + var lines = body.split(/\r\n|[\n\r]/g); + return "".concat(source.name, " (").concat(lineNum, ":").concat(columnNum, ")\n") + printPrefixedLines([// Lines specified like this: ["prefix", "string"], + ["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]); +} + +function printPrefixedLines(lines) { + var existingLines = lines.filter(function (_ref) { + var _ = _ref[0], + line = _ref[1]; + return line !== undefined; + }); + var padLen = 0; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = existingLines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var _ref4 = _step.value; + var prefix = _ref4[0]; + padLen = Math.max(padLen, prefix.length); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return existingLines.map(function (_ref3) { + var prefix = _ref3[0], + line = _ref3[1]; + return lpad(padLen, prefix) + line; + }).join('\n'); +} + +function whitespace(len) { + return Array(len + 1).join(' '); +} + +function lpad(len, str) { + return whitespace(len - str.length) + str; +} \ No newline at end of file diff --git a/dist/error/printError.js.flow b/dist/error/printError.js.flow new file mode 100644 index 0000000000..1741464214 --- /dev/null +++ b/dist/error/printError.js.flow @@ -0,0 +1,93 @@ +/** + * 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. + * + * @flow strict + */ + +import { getLocation } from '../language/location'; +import type { SourceLocation } from '../language/location'; +import type { Source } from '../language/source'; +import type { GraphQLError } from './GraphQLError'; + +/** + * Prints a GraphQLError to a string, representing useful location information + * about the error's position in the source. + */ +export function printError(error: GraphQLError): string { + const printedLocations = []; + if (error.nodes) { + error.nodes.forEach(node => { + if (node.loc) { + printedLocations.push( + highlightSourceAtLocation( + node.loc.source, + getLocation(node.loc.source, node.loc.start), + ), + ); + } + }); + } else if (error.source && error.locations) { + const source = error.source; + error.locations.forEach(location => { + printedLocations.push(highlightSourceAtLocation(source, location)); + }); + } + return printedLocations.length === 0 + ? error.message + : [error.message, ...printedLocations].join('\n\n') + '\n'; +} + +/** + * Render a helpful description of the location of the error in the GraphQL + * Source document. + */ +function highlightSourceAtLocation( + source: Source, + location: SourceLocation, +): string { + const firstLineColumnOffset = source.locationOffset.column - 1; + const body = whitespace(firstLineColumnOffset) + source.body; + + const lineIndex = location.line - 1; + const lineOffset = source.locationOffset.line - 1; + const lineNum = location.line + lineOffset; + + const columnOffset = location.line === 1 ? firstLineColumnOffset : 0; + const columnNum = location.column + columnOffset; + + const lines = body.split(/\r\n|[\n\r]/g); + return ( + `${source.name} (${lineNum}:${columnNum})\n` + + printPrefixedLines([ + // Lines specified like this: ["prefix", "string"], + [`${lineNum - 1}: `, lines[lineIndex - 1]], + [`${lineNum}: `, lines[lineIndex]], + ['', whitespace(columnNum - 1) + '^'], + [`${lineNum + 1}: `, lines[lineIndex + 1]], + ]) + ); +} + +function printPrefixedLines(lines: Array<[string, string]>): string { + const existingLines = lines.filter(([_, line]) => line !== undefined); + + let padLen = 0; + for (const [prefix] of existingLines) { + padLen = Math.max(padLen, prefix.length); + } + + return existingLines + .map(([prefix, line]) => lpad(padLen, prefix) + line) + .join('\n'); +} + +function whitespace(len: number): string { + return Array(len + 1).join(' '); +} + +function lpad(len: number, str: string): string { + return whitespace(len - str.length) + str; +} diff --git a/dist/error/printError.mjs b/dist/error/printError.mjs new file mode 100644 index 0000000000..256666a6ab --- /dev/null +++ b/dist/error/printError.mjs @@ -0,0 +1,96 @@ +/** + * 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. + * + * strict + */ +import { getLocation } from '../language/location'; + +/** + * Prints a GraphQLError to a string, representing useful location information + * about the error's position in the source. + */ +export function printError(error) { + var printedLocations = []; + + if (error.nodes) { + error.nodes.forEach(function (node) { + if (node.loc) { + printedLocations.push(highlightSourceAtLocation(node.loc.source, getLocation(node.loc.source, node.loc.start))); + } + }); + } else if (error.source && error.locations) { + var source = error.source; + error.locations.forEach(function (location) { + printedLocations.push(highlightSourceAtLocation(source, location)); + }); + } + + return printedLocations.length === 0 ? error.message : [error.message].concat(printedLocations).join('\n\n') + '\n'; +} +/** + * Render a helpful description of the location of the error in the GraphQL + * Source document. + */ + +function highlightSourceAtLocation(source, location) { + var firstLineColumnOffset = source.locationOffset.column - 1; + var body = whitespace(firstLineColumnOffset) + source.body; + var lineIndex = location.line - 1; + var lineOffset = source.locationOffset.line - 1; + var lineNum = location.line + lineOffset; + var columnOffset = location.line === 1 ? firstLineColumnOffset : 0; + var columnNum = location.column + columnOffset; + var lines = body.split(/\r\n|[\n\r]/g); + return "".concat(source.name, " (").concat(lineNum, ":").concat(columnNum, ")\n") + printPrefixedLines([// Lines specified like this: ["prefix", "string"], + ["".concat(lineNum - 1, ": "), lines[lineIndex - 1]], ["".concat(lineNum, ": "), lines[lineIndex]], ['', whitespace(columnNum - 1) + '^'], ["".concat(lineNum + 1, ": "), lines[lineIndex + 1]]]); +} + +function printPrefixedLines(lines) { + var existingLines = lines.filter(function (_ref) { + var _ = _ref[0], + line = _ref[1]; + return line !== undefined; + }); + var padLen = 0; + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = existingLines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var _ref4 = _step.value; + var prefix = _ref4[0]; + padLen = Math.max(padLen, prefix.length); + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return existingLines.map(function (_ref3) { + var prefix = _ref3[0], + line = _ref3[1]; + return lpad(padLen, prefix) + line; + }).join('\n'); +} + +function whitespace(len) { + return Array(len + 1).join(' '); +} + +function lpad(len, str) { + return whitespace(len - str.length) + str; +} \ No newline at end of file diff --git a/dist/error/syntaxError.js b/dist/error/syntaxError.js new file mode 100644 index 0000000000..412f6d39e8 --- /dev/null +++ b/dist/error/syntaxError.js @@ -0,0 +1,25 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.syntaxError = syntaxError; + +var _GraphQLError = require("./GraphQLError"); + +/** + * 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. + * + * strict + */ + +/** + * Produces a GraphQLError representing a syntax error, containing useful + * descriptive information about the syntax error's position in the source. + */ +function syntaxError(source, position, description) { + return new _GraphQLError.GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]); +} \ No newline at end of file diff --git a/dist/error/syntaxError.js.flow b/dist/error/syntaxError.js.flow new file mode 100644 index 0000000000..6409f4051b --- /dev/null +++ b/dist/error/syntaxError.js.flow @@ -0,0 +1,25 @@ +/** + * 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. + * + * @flow strict + */ + +import type { Source } from '../language/source'; +import { GraphQLError } from './GraphQLError'; + +/** + * Produces a GraphQLError representing a syntax error, containing useful + * descriptive information about the syntax error's position in the source. + */ +export function syntaxError( + source: Source, + position: number, + description: string, +): GraphQLError { + return new GraphQLError(`Syntax Error: ${description}`, undefined, source, [ + position, + ]); +} diff --git a/dist/error/syntaxError.mjs b/dist/error/syntaxError.mjs new file mode 100644 index 0000000000..c7fe5dc567 --- /dev/null +++ b/dist/error/syntaxError.mjs @@ -0,0 +1,17 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from './GraphQLError'; +/** + * Produces a GraphQLError representing a syntax error, containing useful + * descriptive information about the syntax error's position in the source. + */ + +export function syntaxError(source, position, description) { + return new GraphQLError("Syntax Error: ".concat(description), undefined, source, [position]); +} \ No newline at end of file diff --git a/dist/execution/execute.js b/dist/execution/execute.js new file mode 100644 index 0000000000..af0601e766 --- /dev/null +++ b/dist/execution/execute.js @@ -0,0 +1,829 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.execute = execute; +exports.responsePathAsArray = responsePathAsArray; +exports.addPath = addPath; +exports.assertValidExecutionArguments = assertValidExecutionArguments; +exports.buildExecutionContext = buildExecutionContext; +exports.collectFields = collectFields; +exports.buildResolveInfo = buildResolveInfo; +exports.resolveFieldValueOrError = resolveFieldValueOrError; +exports.getFieldDef = getFieldDef; +exports.defaultFieldResolver = void 0; + +var _iterall = require("iterall"); + +var _error = require("../error"); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _isNullish = _interopRequireDefault(require("../jsutils/isNullish")); + +var _isPromise = _interopRequireDefault(require("../jsutils/isPromise")); + +var _memoize = _interopRequireDefault(require("../jsutils/memoize3")); + +var _promiseForObject = _interopRequireDefault(require("../jsutils/promiseForObject")); + +var _promiseReduce = _interopRequireDefault(require("../jsutils/promiseReduce")); + +var _getOperationRootType = require("../utilities/getOperationRootType"); + +var _typeFromAST = require("../utilities/typeFromAST"); + +var _kinds = require("../language/kinds"); + +var _values = require("./values"); + +var _definition = require("../type/definition"); + +var _schema = require("../type/schema"); + +var _introspection = require("../type/introspection"); + +var _directives = require("../type/directives"); + +var _validate = require("../type/validate"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +function execute(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 ? executeImpl(argsOrSchema.schema, argsOrSchema.document, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : executeImpl(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); +} + +function executeImpl(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // If arguments are missing or incorrect, throw an error. + assertValidExecutionArguments(schema, document, variableValues); // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + + var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); // Return early errors if execution context failed. + + if (Array.isArray(exeContext)) { + return { + errors: exeContext + }; + } // Return a Promise that will eventually resolve to the data described by + // The "Response" section of the GraphQL specification. + // + // If errors are encountered while executing a GraphQL field, only that + // field and its descendants will be omitted, and sibling fields will still + // be executed. An execution which encounters errors will still result in a + // resolved Promise. + + + var data = executeOperation(exeContext, exeContext.operation, rootValue); + return buildResponse(exeContext, data); +} +/** + * Given a completed execution context and data, build the { errors, data } + * response defined by the "Response" section of the GraphQL specification. + */ + + +function buildResponse(exeContext, data) { + if ((0, _isPromise.default)(data)) { + return data.then(function (resolved) { + return buildResponse(exeContext, resolved); + }); + } + + return exeContext.errors.length === 0 ? { + data: data + } : { + errors: exeContext.errors, + data: data + }; +} +/** + * Given a ResponsePath (found in the `path` entry in the information provided + * as the last argument to a field resolver), return an Array of the path keys. + */ + + +function responsePathAsArray(path) { + var flattened = []; + var curr = path; + + while (curr) { + flattened.push(curr.key); + curr = curr.prev; + } + + return flattened.reverse(); +} +/** + * Given a ResponsePath and a key, return a new ResponsePath containing the + * new key. + */ + + +function addPath(prev, key) { + return { + prev: prev, + key: key + }; +} +/** + * Essential assertions before executing to provide developer feedback for + * improper use of the GraphQL library. + */ + + +function assertValidExecutionArguments(schema, document, rawVariableValues) { + !document ? (0, _invariant.default)(0, 'Must provide document') : void 0; // If the schema used for execution is invalid, throw an error. + + (0, _validate.assertValidSchema)(schema); // Variables, if provided, must be an object. + + !(!rawVariableValues || _typeof(rawVariableValues) === 'object') ? (0, _invariant.default)(0, 'Variables must be provided as an Object where each property is a ' + 'variable value. Perhaps look to see if an unparsed JSON string ' + 'was provided.') : void 0; +} +/** + * Constructs a ExecutionContext object from the arguments passed to + * execute, which we will pass throughout the other execution methods. + * + * Throws a GraphQLError if a valid execution context cannot be created. + */ + + +function buildExecutionContext(schema, document, rootValue, contextValue, rawVariableValues, operationName, fieldResolver) { + var errors = []; + var operation; + var hasMultipleAssumedOperations = false; + var fragments = Object.create(null); + + for (var i = 0; i < document.definitions.length; i++) { + var definition = document.definitions[i]; + + switch (definition.kind) { + case _kinds.Kind.OPERATION_DEFINITION: + if (!operationName && operation) { + hasMultipleAssumedOperations = true; + } else if (!operationName || definition.name && definition.name.value === operationName) { + operation = definition; + } + + break; + + case _kinds.Kind.FRAGMENT_DEFINITION: + fragments[definition.name.value] = definition; + break; + } + } + + if (!operation) { + if (operationName) { + errors.push(new _error.GraphQLError("Unknown operation named \"".concat(operationName, "\"."))); + } else { + errors.push(new _error.GraphQLError('Must provide an operation.')); + } + } else if (hasMultipleAssumedOperations) { + errors.push(new _error.GraphQLError('Must provide operation name if query contains ' + 'multiple operations.')); + } + + var variableValues; + + if (operation) { + var coercedVariableValues = (0, _values.getVariableValues)(schema, operation.variableDefinitions || [], rawVariableValues || {}); + + if (coercedVariableValues.errors) { + errors.push.apply(errors, coercedVariableValues.errors); + } else { + variableValues = coercedVariableValues.coerced; + } + } + + if (errors.length !== 0) { + return errors; + } + + !operation ? (0, _invariant.default)(0, 'Has operation if no errors.') : void 0; + !variableValues ? (0, _invariant.default)(0, 'Has variables if no errors.') : void 0; + return { + schema: schema, + fragments: fragments, + rootValue: rootValue, + contextValue: contextValue, + operation: operation, + variableValues: variableValues, + fieldResolver: fieldResolver || defaultFieldResolver, + errors: errors + }; +} +/** + * Implements the "Evaluating operations" section of the spec. + */ + + +function executeOperation(exeContext, operation, rootValue) { + var type = (0, _getOperationRootType.getOperationRootType)(exeContext.schema, operation); + var fields = collectFields(exeContext, type, operation.selectionSet, Object.create(null), Object.create(null)); + var path = undefined; // Errors from sub-fields of a NonNull type may propagate to the top level, + // at which point we still log the error and null the parent field, which + // in this case is the entire response. + // + // Similar to completeValueCatchingError. + + try { + var result = operation.operation === 'mutation' ? executeFieldsSerially(exeContext, type, rootValue, path, fields) : executeFields(exeContext, type, rootValue, path, fields); + + if ((0, _isPromise.default)(result)) { + return result.then(undefined, function (error) { + exeContext.errors.push(error); + return Promise.resolve(null); + }); + } + + return result; + } catch (error) { + exeContext.errors.push(error); + return null; + } +} +/** + * Implements the "Evaluating selection sets" section of the spec + * for "write" mode. + */ + + +function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) { + return (0, _promiseReduce.default)(Object.keys(fields), function (results, responseName) { + var fieldNodes = fields[responseName]; + var fieldPath = addPath(path, responseName); + var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + + if (result === undefined) { + return results; + } + + if ((0, _isPromise.default)(result)) { + return result.then(function (resolvedResult) { + results[responseName] = resolvedResult; + return results; + }); + } + + results[responseName] = result; + return results; + }, Object.create(null)); +} +/** + * Implements the "Evaluating selection sets" section of the spec + * for "read" mode. + */ + + +function executeFields(exeContext, parentType, sourceValue, path, fields) { + var results = Object.create(null); + var containsPromise = false; + + for (var i = 0, keys = Object.keys(fields); i < keys.length; ++i) { + var responseName = keys[i]; + var fieldNodes = fields[responseName]; + var fieldPath = addPath(path, responseName); + var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + + if (result !== undefined) { + results[responseName] = result; + + if (!containsPromise && (0, _isPromise.default)(result)) { + containsPromise = true; + } + } + } // If there are no promises, we can just return the object + + + if (!containsPromise) { + return results; + } // Otherwise, results is a map from field name to the result of resolving that + // field, which is possibly a promise. Return a promise that will return this + // same map, but with any promises replaced with the values they resolved to. + + + return (0, _promiseForObject.default)(results); +} +/** + * Given a selectionSet, adds all of the fields in that selection to + * the passed in map of fields, and returns it at the end. + * + * CollectFields requires the "runtime type" of an object. For a field which + * returns an Interface or Union type, the "runtime type" will be the actual + * Object type returned by that field. + */ + + +function collectFields(exeContext, runtimeType, selectionSet, fields, visitedFragmentNames) { + for (var i = 0; i < selectionSet.selections.length; i++) { + var selection = selectionSet.selections[i]; + + switch (selection.kind) { + case _kinds.Kind.FIELD: + if (!shouldIncludeNode(exeContext, selection)) { + continue; + } + + var name = getFieldEntryKey(selection); + + if (!fields[name]) { + fields[name] = []; + } + + fields[name].push(selection); + break; + + case _kinds.Kind.INLINE_FRAGMENT: + if (!shouldIncludeNode(exeContext, selection) || !doesFragmentConditionMatch(exeContext, selection, runtimeType)) { + continue; + } + + collectFields(exeContext, runtimeType, selection.selectionSet, fields, visitedFragmentNames); + break; + + case _kinds.Kind.FRAGMENT_SPREAD: + var fragName = selection.name.value; + + if (visitedFragmentNames[fragName] || !shouldIncludeNode(exeContext, selection)) { + continue; + } + + visitedFragmentNames[fragName] = true; + var fragment = exeContext.fragments[fragName]; + + if (!fragment || !doesFragmentConditionMatch(exeContext, fragment, runtimeType)) { + continue; + } + + collectFields(exeContext, runtimeType, fragment.selectionSet, fields, visitedFragmentNames); + break; + } + } + + return fields; +} +/** + * Determines if a field should be included based on the @include and @skip + * directives, where @skip has higher precidence than @include. + */ + + +function shouldIncludeNode(exeContext, node) { + var skip = (0, _values.getDirectiveValues)(_directives.GraphQLSkipDirective, node, exeContext.variableValues); + + if (skip && skip.if === true) { + return false; + } + + var include = (0, _values.getDirectiveValues)(_directives.GraphQLIncludeDirective, node, exeContext.variableValues); + + if (include && include.if === false) { + return false; + } + + return true; +} +/** + * Determines if a fragment is applicable to the given type. + */ + + +function doesFragmentConditionMatch(exeContext, fragment, type) { + var typeConditionNode = fragment.typeCondition; + + if (!typeConditionNode) { + return true; + } + + var conditionalType = (0, _typeFromAST.typeFromAST)(exeContext.schema, typeConditionNode); + + if (conditionalType === type) { + return true; + } + + if ((0, _definition.isAbstractType)(conditionalType)) { + return exeContext.schema.isPossibleType(conditionalType, type); + } + + return false; +} +/** + * Implements the logic to compute the key of a given field's entry + */ + + +function getFieldEntryKey(node) { + return node.alias ? node.alias.value : node.name.value; +} +/** + * Resolves the field on the given source object. In particular, this + * figures out the value that the field returns by calling its resolve function, + * then calls completeValue to complete promises, serialize scalars, or execute + * the sub-selection-set for objects. + */ + + +function resolveField(exeContext, parentType, source, fieldNodes, path) { + var fieldNode = fieldNodes[0]; + var fieldName = fieldNode.name.value; + var fieldDef = getFieldDef(exeContext.schema, parentType, fieldName); + + if (!fieldDef) { + return; + } + + var resolveFn = fieldDef.resolve || exeContext.fieldResolver; + var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path); // Get the resolve function, regardless of if its result is normal + // or abrupt (error). + + var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info); + return completeValueCatchingError(exeContext, fieldDef.type, fieldNodes, info, path, result); +} + +function buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path) { + // The resolve function's optional fourth argument is a collection of + // information about the current execution state. + return { + fieldName: fieldDef.name, + fieldNodes: fieldNodes, + returnType: fieldDef.type, + parentType: parentType, + path: path, + schema: exeContext.schema, + fragments: exeContext.fragments, + rootValue: exeContext.rootValue, + operation: exeContext.operation, + variableValues: exeContext.variableValues + }; +} // Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` +// function. Returns the result of resolveFn or the abrupt-return Error object. + + +function resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info) { + try { + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + // TODO: find a way to memoize, in case this field is within a List type. + var args = (0, _values.getArgumentValues)(fieldDef, fieldNodes[0], exeContext.variableValues); // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + + var _contextValue = exeContext.contextValue; + var result = resolveFn(source, args, _contextValue, info); + return (0, _isPromise.default)(result) ? result.then(undefined, asErrorInstance) : result; + } catch (error) { + return asErrorInstance(error); + } +} // Sometimes a non-error is thrown, wrap it as an Error instance to ensure a +// consistent Error interface. + + +function asErrorInstance(error) { + return error instanceof Error ? error : new Error(error || undefined); +} // This is a small wrapper around completeValue which detects and logs errors +// in the execution context. + + +function completeValueCatchingError(exeContext, returnType, fieldNodes, info, path, result) { + try { + var completed; + + if ((0, _isPromise.default)(result)) { + completed = result.then(function (resolved) { + return completeValue(exeContext, returnType, fieldNodes, info, path, resolved); + }); + } else { + completed = completeValue(exeContext, returnType, fieldNodes, info, path, result); + } + + if ((0, _isPromise.default)(completed)) { + // Note: we don't rely on a `catch` method, but we do expect "thenable" + // to take a second callback for the error case. + return completed.then(undefined, function (error) { + return handleFieldError(error, fieldNodes, path, returnType, exeContext); + }); + } + + return completed; + } catch (error) { + return handleFieldError(error, fieldNodes, path, returnType, exeContext); + } +} + +function handleFieldError(rawError, fieldNodes, path, returnType, exeContext) { + var error = (0, _error.locatedError)(asErrorInstance(rawError), fieldNodes, responsePathAsArray(path)); // If the field type is non-nullable, then it is resolved without any + // protection from errors, however it still properly locates the error. + + if ((0, _definition.isNonNullType)(returnType)) { + throw error; + } // Otherwise, error protection is applied, logging the error and resolving + // a null value for this field if one is encountered. + + + exeContext.errors.push(error); + return null; +} +/** + * Implements the instructions for completeValue as defined in the + * "Field entries" section of the spec. + * + * If the field type is Non-Null, then this recursively completes the value + * for the inner type. It throws a field error if that completion returns null, + * as per the "Nullability" section of the spec. + * + * If the field type is a List, then this recursively completes the value + * for the inner type on each item in the list. + * + * If the field type is a Scalar or Enum, ensures the completed value is a legal + * value of the type by calling the `serialize` method of GraphQL type + * definition. + * + * If the field is an abstract type, determine the runtime type of the value + * and then complete based on that type + * + * Otherwise, the field type expects a sub-selection set, and will complete the + * value by evaluating all sub-selections. + */ + + +function completeValue(exeContext, returnType, fieldNodes, info, path, result) { + // If result is an Error, throw a located error. + if (result instanceof Error) { + throw result; + } // If field type is NonNull, complete for inner type, and throw field error + // if result is null. + + + if ((0, _definition.isNonNullType)(returnType)) { + var completed = completeValue(exeContext, returnType.ofType, fieldNodes, info, path, result); + + if (completed === null) { + throw new Error("Cannot return null for non-nullable field ".concat(info.parentType.name, ".").concat(info.fieldName, ".")); + } + + return completed; + } // If result value is null-ish (null, undefined, or NaN) then return null. + + + if ((0, _isNullish.default)(result)) { + return null; + } // If field type is List, complete each item in the list with the inner type + + + if ((0, _definition.isListType)(returnType)) { + return completeListValue(exeContext, returnType, fieldNodes, info, path, result); + } // If field type is a leaf type, Scalar or Enum, serialize to a valid value, + // returning null if serialization is not possible. + + + if ((0, _definition.isLeafType)(returnType)) { + return completeLeafValue(returnType, result); + } // If field type is an abstract type, Interface or Union, determine the + // runtime Object type and complete for that type. + + + if ((0, _definition.isAbstractType)(returnType)) { + return completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result); + } // If field type is Object, execute and complete all sub-selections. + + + if ((0, _definition.isObjectType)(returnType)) { + return completeObjectValue(exeContext, returnType, fieldNodes, info, path, result); + } // Not reachable. All possible output types have been considered. + + /* istanbul ignore next */ + + + throw new Error("Cannot complete value of unexpected type \"".concat(String(returnType), "\".")); +} +/** + * Complete a list value by completing each item in the list with the + * inner type + */ + + +function completeListValue(exeContext, returnType, fieldNodes, info, path, result) { + !(0, _iterall.isCollection)(result) ? (0, _invariant.default)(0, "Expected Iterable, but did not find one for field ".concat(info.parentType.name, ".").concat(info.fieldName, ".")) : void 0; // This is specified as a simple map, however we're optimizing the path + // where the list contains no Promises by avoiding creating another Promise. + + var itemType = returnType.ofType; + var containsPromise = false; + var completedResults = []; + (0, _iterall.forEach)(result, function (item, index) { + // No need to modify the info object containing the path, + // since from here on it is not ever accessed by resolver functions. + var fieldPath = addPath(path, index); + var completedItem = completeValueCatchingError(exeContext, itemType, fieldNodes, info, fieldPath, item); + + if (!containsPromise && (0, _isPromise.default)(completedItem)) { + containsPromise = true; + } + + completedResults.push(completedItem); + }); + return containsPromise ? Promise.all(completedResults) : completedResults; +} +/** + * Complete a Scalar or Enum by serializing to a valid value, returning + * null if serialization is not possible. + */ + + +function completeLeafValue(returnType, result) { + !returnType.serialize ? (0, _invariant.default)(0, 'Missing serialize method on type') : void 0; + var serializedResult = returnType.serialize(result); + + if ((0, _isInvalid.default)(serializedResult)) { + throw new Error("Expected a value of type \"".concat(String(returnType), "\" but ") + "received: ".concat(String(result))); + } + + return serializedResult; +} +/** + * Complete a value of an abstract type by determining the runtime object type + * of that value, then complete the value for that type. + */ + + +function completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result) { + var runtimeType = returnType.resolveType ? returnType.resolveType(result, exeContext.contextValue, info) : defaultResolveTypeFn(result, exeContext.contextValue, info, returnType); + + if ((0, _isPromise.default)(runtimeType)) { + return runtimeType.then(function (resolvedRuntimeType) { + return completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result); + }); + } + + return completeObjectValue(exeContext, ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result); +} + +function ensureValidRuntimeType(runtimeTypeOrName, exeContext, returnType, fieldNodes, info, result) { + var runtimeType = typeof runtimeTypeOrName === 'string' ? exeContext.schema.getType(runtimeTypeOrName) : runtimeTypeOrName; + + if (!(0, _definition.isObjectType)(runtimeType)) { + throw new _error.GraphQLError("Abstract type ".concat(returnType.name, " must resolve to an Object type at ") + "runtime for field ".concat(info.parentType.name, ".").concat(info.fieldName, " with ") + "value \"".concat(String(result), "\", received \"").concat(String(runtimeType), "\". ") + "Either the ".concat(returnType.name, " type should provide a \"resolveType\" ") + 'function or each possible types should provide an ' + '"isTypeOf" function.', fieldNodes); + } + + if (!exeContext.schema.isPossibleType(returnType, runtimeType)) { + throw new _error.GraphQLError("Runtime Object type \"".concat(runtimeType.name, "\" is not a possible type ") + "for \"".concat(returnType.name, "\"."), fieldNodes); + } + + return runtimeType; +} +/** + * Complete an Object value by executing all sub-selections. + */ + + +function completeObjectValue(exeContext, returnType, fieldNodes, info, path, result) { + // If there is an isTypeOf predicate function, call it with the + // current result. If isTypeOf returns false, then raise an error rather + // than continuing execution. + if (returnType.isTypeOf) { + var isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info); + + if ((0, _isPromise.default)(isTypeOf)) { + return isTypeOf.then(function (resolvedIsTypeOf) { + if (!resolvedIsTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + + return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result); + }); + } + + if (!isTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + } + + return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result); +} + +function invalidReturnTypeError(returnType, result, fieldNodes) { + return new _error.GraphQLError("Expected value of type \"".concat(returnType.name, "\" but got: ").concat(String(result), "."), fieldNodes); +} + +function collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result) { + // Collect sub-fields to execute to complete this value. + var subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes); + return executeFields(exeContext, returnType, result, path, subFieldNodes); +} +/** + * A memoized collection of relevant subfields with regard to the return + * type. Memoizing ensures the subfields are not repeatedly calculated, which + * saves overhead when resolving lists of values. + */ + + +var collectSubfields = (0, _memoize.default)(_collectSubfields); + +function _collectSubfields(exeContext, returnType, fieldNodes) { + var subFieldNodes = Object.create(null); + var visitedFragmentNames = Object.create(null); + + for (var i = 0; i < fieldNodes.length; i++) { + var selectionSet = fieldNodes[i].selectionSet; + + if (selectionSet) { + subFieldNodes = collectFields(exeContext, returnType, selectionSet, subFieldNodes, visitedFragmentNames); + } + } + + return subFieldNodes; +} +/** + * If a resolveType function is not given, then a default resolve behavior is + * used which attempts two strategies: + * + * First, See if the provided value has a `__typename` field defined, if so, use + * that value as name of the resolved type. + * + * Otherwise, test each possible type for the abstract type by calling + * isTypeOf for the object being coerced, returning the first type that matches. + */ + + +function defaultResolveTypeFn(value, contextValue, info, abstractType) { + // First, look for `__typename`. + if (value !== null && _typeof(value) === 'object' && typeof value.__typename === 'string') { + return value.__typename; + } // Otherwise, test each possible type. + + + var possibleTypes = info.schema.getPossibleTypes(abstractType); + var promisedIsTypeOfResults = []; + + for (var i = 0; i < possibleTypes.length; i++) { + var type = possibleTypes[i]; + + if (type.isTypeOf) { + var isTypeOfResult = type.isTypeOf(value, contextValue, info); + + if ((0, _isPromise.default)(isTypeOfResult)) { + promisedIsTypeOfResults[i] = isTypeOfResult; + } else if (isTypeOfResult) { + return type; + } + } + } + + if (promisedIsTypeOfResults.length) { + return Promise.all(promisedIsTypeOfResults).then(function (isTypeOfResults) { + for (var _i = 0; _i < isTypeOfResults.length; _i++) { + if (isTypeOfResults[_i]) { + return possibleTypes[_i]; + } + } + }); + } +} +/** + * If a resolve function is not given, then a default resolve behavior is used + * which takes the property of the source object of the same name as the field + * and returns it as the result, or if it's a function, returns the result + * of calling that function while passing along args and context value. + */ + + +var defaultFieldResolver = function defaultFieldResolver(source, args, contextValue, info) { + // ensure source is a value for which property access is acceptable. + if (_typeof(source) === 'object' || typeof source === 'function') { + var property = source[info.fieldName]; + + if (typeof property === 'function') { + return source[info.fieldName](args, contextValue, info); + } + + return property; + } +}; +/** + * This method looks up the field on the given type defintion. + * It has special casing for the two introspection fields, __schema + * and __typename. __typename is special because it can always be + * queried as a field, even in situations where no other fields + * are allowed, like on a Union. __schema could get automatically + * added to the query type, but that would require mutating type + * definitions, which would cause issues. + */ + + +exports.defaultFieldResolver = defaultFieldResolver; + +function getFieldDef(schema, parentType, fieldName) { + if (fieldName === _introspection.SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { + return _introspection.SchemaMetaFieldDef; + } else if (fieldName === _introspection.TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return _introspection.TypeMetaFieldDef; + } else if (fieldName === _introspection.TypeNameMetaFieldDef.name) { + return _introspection.TypeNameMetaFieldDef; + } + + return parentType.getFields()[fieldName]; +} \ No newline at end of file diff --git a/dist/execution/execute.js.flow b/dist/execution/execute.js.flow new file mode 100644 index 0000000000..b272c5754d --- /dev/null +++ b/dist/execution/execute.js.flow @@ -0,0 +1,1268 @@ +/** + * 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. + * + * @flow strict + */ + +import { forEach, isCollection } from 'iterall'; +import { GraphQLError, locatedError } from '../error'; +import invariant from '../jsutils/invariant'; +import isInvalid from '../jsutils/isInvalid'; +import isNullish from '../jsutils/isNullish'; +import isPromise from '../jsutils/isPromise'; +import memoize3 from '../jsutils/memoize3'; +import promiseForObject from '../jsutils/promiseForObject'; +import promiseReduce from '../jsutils/promiseReduce'; +import type { ObjMap } from '../jsutils/ObjMap'; +import type { MaybePromise } from '../jsutils/MaybePromise'; + +import { getOperationRootType } from '../utilities/getOperationRootType'; +import { typeFromAST } from '../utilities/typeFromAST'; +import { Kind } from '../language/kinds'; +import { + getVariableValues, + getArgumentValues, + getDirectiveValues, +} from './values'; +import { + isObjectType, + isAbstractType, + isLeafType, + isListType, + isNonNullType, +} from '../type/definition'; +import type { + GraphQLObjectType, + GraphQLOutputType, + GraphQLLeafType, + GraphQLAbstractType, + GraphQLField, + GraphQLFieldResolver, + GraphQLResolveInfo, + ResponsePath, + GraphQLList, +} from '../type/definition'; +import { GraphQLSchema } from '../type/schema'; +import { + SchemaMetaFieldDef, + TypeMetaFieldDef, + TypeNameMetaFieldDef, +} from '../type/introspection'; +import { + GraphQLIncludeDirective, + GraphQLSkipDirective, +} from '../type/directives'; +import { assertValidSchema } from '../type/validate'; +import type { + DocumentNode, + OperationDefinitionNode, + SelectionSetNode, + FieldNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, +} from '../language/ast'; + +/** + * Terminology + * + * "Definitions" are the generic name for top-level statements in the document. + * Examples of this include: + * 1) Operations (such as a query) + * 2) Fragments + * + * "Operations" are a generic name for requests in the document. + * Examples of this include: + * 1) query, + * 2) mutation + * + * "Selections" are the definitions that can appear legally and at + * single level of the query. These include: + * 1) field references e.g "a" + * 2) fragment "spreads" e.g. "...c" + * 3) inline fragment "spreads" e.g. "...on Type { a }" + */ + +/** + * Data that must be available at all points during query execution. + * + * Namely, schema of the type system that is currently executing, + * and the fragments defined in the query document + */ +export type ExecutionContext = { + schema: GraphQLSchema, + fragments: ObjMap, + rootValue: mixed, + contextValue: mixed, + operation: OperationDefinitionNode, + variableValues: { [variable: string]: mixed }, + fieldResolver: GraphQLFieldResolver, + errors: Array, +}; + +/** + * The result of GraphQL execution. + * + * - `errors` is included when any errors occurred as a non-empty array. + * - `data` is the result of a successful execution of the query. + */ +export type ExecutionResult = { + errors?: $ReadOnlyArray, + data?: ObjMap, +}; + +export type ExecutionArgs = {| + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?{ [variable: string]: mixed }, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +|}; + +/** + * Implements the "Evaluating requests" section of the GraphQL specification. + * + * Returns either a synchronous ExecutionResult (if all encountered resolvers + * are synchronous), or a Promise of an ExecutionResult that will eventually be + * resolved and never rejected. + * + * If the arguments to this function do not result in a legal execution context, + * a GraphQLError will be thrown immediately explaining the invalid input. + * + * Accepts either an object with named arguments, or individual arguments. + */ +declare function execute( + ExecutionArgs, + ..._: [] +): MaybePromise; +/* eslint-disable no-redeclare */ +declare function execute( + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?{ [variable: string]: mixed }, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +): MaybePromise; +export function execute( + argsOrSchema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, +) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 + ? executeImpl( + argsOrSchema.schema, + argsOrSchema.document, + argsOrSchema.rootValue, + argsOrSchema.contextValue, + argsOrSchema.variableValues, + argsOrSchema.operationName, + argsOrSchema.fieldResolver, + ) + : executeImpl( + argsOrSchema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ); +} + +function executeImpl( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, +) { + // If arguments are missing or incorrect, throw an error. + assertValidExecutionArguments(schema, document, variableValues); + + // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + const exeContext = buildExecutionContext( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ); + + // Return early errors if execution context failed. + if (Array.isArray(exeContext)) { + return { errors: exeContext }; + } + + // Return a Promise that will eventually resolve to the data described by + // The "Response" section of the GraphQL specification. + // + // If errors are encountered while executing a GraphQL field, only that + // field and its descendants will be omitted, and sibling fields will still + // be executed. An execution which encounters errors will still result in a + // resolved Promise. + const data = executeOperation(exeContext, exeContext.operation, rootValue); + return buildResponse(exeContext, data); +} + +/** + * Given a completed execution context and data, build the { errors, data } + * response defined by the "Response" section of the GraphQL specification. + */ +function buildResponse( + exeContext: ExecutionContext, + data: MaybePromise | null>, +) { + if (isPromise(data)) { + return data.then(resolved => buildResponse(exeContext, resolved)); + } + return exeContext.errors.length === 0 + ? { data } + : { errors: exeContext.errors, data }; +} + +/** + * Given a ResponsePath (found in the `path` entry in the information provided + * as the last argument to a field resolver), return an Array of the path keys. + */ +export function responsePathAsArray( + path: ResponsePath, +): $ReadOnlyArray { + const flattened = []; + let curr = path; + while (curr) { + flattened.push(curr.key); + curr = curr.prev; + } + return flattened.reverse(); +} + +/** + * Given a ResponsePath and a key, return a new ResponsePath containing the + * new key. + */ +export function addPath(prev: ResponsePath | void, key: string | number) { + return { prev, key }; +} + +/** + * Essential assertions before executing to provide developer feedback for + * improper use of the GraphQL library. + */ +export function assertValidExecutionArguments( + schema: GraphQLSchema, + document: DocumentNode, + rawVariableValues: ?ObjMap, +): void { + invariant(document, 'Must provide document'); + + // If the schema used for execution is invalid, throw an error. + assertValidSchema(schema); + + // Variables, if provided, must be an object. + invariant( + !rawVariableValues || typeof rawVariableValues === 'object', + 'Variables must be provided as an Object where each property is a ' + + 'variable value. Perhaps look to see if an unparsed JSON string ' + + 'was provided.', + ); +} + +/** + * Constructs a ExecutionContext object from the arguments passed to + * execute, which we will pass throughout the other execution methods. + * + * Throws a GraphQLError if a valid execution context cannot be created. + */ +export function buildExecutionContext( + schema: GraphQLSchema, + document: DocumentNode, + rootValue: mixed, + contextValue: mixed, + rawVariableValues: ?ObjMap, + operationName: ?string, + fieldResolver: ?GraphQLFieldResolver, +): $ReadOnlyArray | ExecutionContext { + const errors: Array = []; + let operation: OperationDefinitionNode | void; + let hasMultipleAssumedOperations = false; + const fragments: ObjMap = Object.create(null); + for (let i = 0; i < document.definitions.length; i++) { + const definition = document.definitions[i]; + switch (definition.kind) { + case Kind.OPERATION_DEFINITION: + if (!operationName && operation) { + hasMultipleAssumedOperations = true; + } else if ( + !operationName || + (definition.name && definition.name.value === operationName) + ) { + operation = definition; + } + break; + case Kind.FRAGMENT_DEFINITION: + fragments[definition.name.value] = definition; + break; + } + } + + if (!operation) { + if (operationName) { + errors.push( + new GraphQLError(`Unknown operation named "${operationName}".`), + ); + } else { + errors.push(new GraphQLError('Must provide an operation.')); + } + } else if (hasMultipleAssumedOperations) { + errors.push( + new GraphQLError( + 'Must provide operation name if query contains ' + + 'multiple operations.', + ), + ); + } + + let variableValues; + if (operation) { + const coercedVariableValues = getVariableValues( + schema, + operation.variableDefinitions || [], + rawVariableValues || {}, + ); + + if (coercedVariableValues.errors) { + errors.push(...coercedVariableValues.errors); + } else { + variableValues = coercedVariableValues.coerced; + } + } + + if (errors.length !== 0) { + return errors; + } + + invariant(operation, 'Has operation if no errors.'); + invariant(variableValues, 'Has variables if no errors.'); + + return { + schema, + fragments, + rootValue, + contextValue, + operation, + variableValues, + fieldResolver: fieldResolver || defaultFieldResolver, + errors, + }; +} + +/** + * Implements the "Evaluating operations" section of the spec. + */ +function executeOperation( + exeContext: ExecutionContext, + operation: OperationDefinitionNode, + rootValue: mixed, +): MaybePromise | null> { + const type = getOperationRootType(exeContext.schema, operation); + const fields = collectFields( + exeContext, + type, + operation.selectionSet, + Object.create(null), + Object.create(null), + ); + + const path = undefined; + + // Errors from sub-fields of a NonNull type may propagate to the top level, + // at which point we still log the error and null the parent field, which + // in this case is the entire response. + // + // Similar to completeValueCatchingError. + try { + const result = + operation.operation === 'mutation' + ? executeFieldsSerially(exeContext, type, rootValue, path, fields) + : executeFields(exeContext, type, rootValue, path, fields); + if (isPromise(result)) { + return result.then(undefined, error => { + exeContext.errors.push(error); + return Promise.resolve(null); + }); + } + return result; + } catch (error) { + exeContext.errors.push(error); + return null; + } +} + +/** + * Implements the "Evaluating selection sets" section of the spec + * for "write" mode. + */ +function executeFieldsSerially( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + sourceValue: mixed, + path: ResponsePath | void, + fields: ObjMap>, +): MaybePromise> { + return promiseReduce( + Object.keys(fields), + (results, responseName) => { + const fieldNodes = fields[responseName]; + const fieldPath = addPath(path, responseName); + const result = resolveField( + exeContext, + parentType, + sourceValue, + fieldNodes, + fieldPath, + ); + if (result === undefined) { + return results; + } + if (isPromise(result)) { + return result.then(resolvedResult => { + results[responseName] = resolvedResult; + return results; + }); + } + results[responseName] = result; + return results; + }, + Object.create(null), + ); +} + +/** + * Implements the "Evaluating selection sets" section of the spec + * for "read" mode. + */ +function executeFields( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + sourceValue: mixed, + path: ResponsePath | void, + fields: ObjMap>, +): MaybePromise> { + const results = Object.create(null); + let containsPromise = false; + + for (let i = 0, keys = Object.keys(fields); i < keys.length; ++i) { + const responseName = keys[i]; + const fieldNodes = fields[responseName]; + const fieldPath = addPath(path, responseName); + const result = resolveField( + exeContext, + parentType, + sourceValue, + fieldNodes, + fieldPath, + ); + + if (result !== undefined) { + results[responseName] = result; + if (!containsPromise && isPromise(result)) { + containsPromise = true; + } + } + } + + // If there are no promises, we can just return the object + if (!containsPromise) { + return results; + } + + // Otherwise, results is a map from field name to the result of resolving that + // field, which is possibly a promise. Return a promise that will return this + // same map, but with any promises replaced with the values they resolved to. + return promiseForObject(results); +} + +/** + * Given a selectionSet, adds all of the fields in that selection to + * the passed in map of fields, and returns it at the end. + * + * CollectFields requires the "runtime type" of an object. For a field which + * returns an Interface or Union type, the "runtime type" will be the actual + * Object type returned by that field. + */ +export function collectFields( + exeContext: ExecutionContext, + runtimeType: GraphQLObjectType, + selectionSet: SelectionSetNode, + fields: ObjMap>, + visitedFragmentNames: ObjMap, +): ObjMap> { + for (let i = 0; i < selectionSet.selections.length; i++) { + const selection = selectionSet.selections[i]; + switch (selection.kind) { + case Kind.FIELD: + if (!shouldIncludeNode(exeContext, selection)) { + continue; + } + const name = getFieldEntryKey(selection); + if (!fields[name]) { + fields[name] = []; + } + fields[name].push(selection); + break; + case Kind.INLINE_FRAGMENT: + if ( + !shouldIncludeNode(exeContext, selection) || + !doesFragmentConditionMatch(exeContext, selection, runtimeType) + ) { + continue; + } + collectFields( + exeContext, + runtimeType, + selection.selectionSet, + fields, + visitedFragmentNames, + ); + break; + case Kind.FRAGMENT_SPREAD: + const fragName = selection.name.value; + if ( + visitedFragmentNames[fragName] || + !shouldIncludeNode(exeContext, selection) + ) { + continue; + } + visitedFragmentNames[fragName] = true; + const fragment = exeContext.fragments[fragName]; + if ( + !fragment || + !doesFragmentConditionMatch(exeContext, fragment, runtimeType) + ) { + continue; + } + collectFields( + exeContext, + runtimeType, + fragment.selectionSet, + fields, + visitedFragmentNames, + ); + break; + } + } + return fields; +} + +/** + * Determines if a field should be included based on the @include and @skip + * directives, where @skip has higher precidence than @include. + */ +function shouldIncludeNode( + exeContext: ExecutionContext, + node: FragmentSpreadNode | FieldNode | InlineFragmentNode, +): boolean { + const skip = getDirectiveValues( + GraphQLSkipDirective, + node, + exeContext.variableValues, + ); + if (skip && skip.if === true) { + return false; + } + + const include = getDirectiveValues( + GraphQLIncludeDirective, + node, + exeContext.variableValues, + ); + if (include && include.if === false) { + return false; + } + return true; +} + +/** + * Determines if a fragment is applicable to the given type. + */ +function doesFragmentConditionMatch( + exeContext: ExecutionContext, + fragment: FragmentDefinitionNode | InlineFragmentNode, + type: GraphQLObjectType, +): boolean { + const typeConditionNode = fragment.typeCondition; + if (!typeConditionNode) { + return true; + } + const conditionalType = typeFromAST(exeContext.schema, typeConditionNode); + if (conditionalType === type) { + return true; + } + if (isAbstractType(conditionalType)) { + return exeContext.schema.isPossibleType(conditionalType, type); + } + return false; +} + +/** + * Implements the logic to compute the key of a given field's entry + */ +function getFieldEntryKey(node: FieldNode): string { + return node.alias ? node.alias.value : node.name.value; +} + +/** + * Resolves the field on the given source object. In particular, this + * figures out the value that the field returns by calling its resolve function, + * then calls completeValue to complete promises, serialize scalars, or execute + * the sub-selection-set for objects. + */ +function resolveField( + exeContext: ExecutionContext, + parentType: GraphQLObjectType, + source: mixed, + fieldNodes: $ReadOnlyArray, + path: ResponsePath, +): MaybePromise { + const fieldNode = fieldNodes[0]; + const fieldName = fieldNode.name.value; + + const fieldDef = getFieldDef(exeContext.schema, parentType, fieldName); + if (!fieldDef) { + return; + } + + const resolveFn = fieldDef.resolve || exeContext.fieldResolver; + + const info = buildResolveInfo( + exeContext, + fieldDef, + fieldNodes, + parentType, + path, + ); + + // Get the resolve function, regardless of if its result is normal + // or abrupt (error). + const result = resolveFieldValueOrError( + exeContext, + fieldDef, + fieldNodes, + resolveFn, + source, + info, + ); + + return completeValueCatchingError( + exeContext, + fieldDef.type, + fieldNodes, + info, + path, + result, + ); +} + +export function buildResolveInfo( + exeContext: ExecutionContext, + fieldDef: GraphQLField<*, *>, + fieldNodes: $ReadOnlyArray, + parentType: GraphQLObjectType, + path: ResponsePath, +): GraphQLResolveInfo { + // The resolve function's optional fourth argument is a collection of + // information about the current execution state. + return { + fieldName: fieldDef.name, + fieldNodes, + returnType: fieldDef.type, + parentType, + path, + schema: exeContext.schema, + fragments: exeContext.fragments, + rootValue: exeContext.rootValue, + operation: exeContext.operation, + variableValues: exeContext.variableValues, + }; +} + +// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` +// function. Returns the result of resolveFn or the abrupt-return Error object. +export function resolveFieldValueOrError( + exeContext: ExecutionContext, + fieldDef: GraphQLField, + fieldNodes: $ReadOnlyArray, + resolveFn: GraphQLFieldResolver, + source: TSource, + info: GraphQLResolveInfo, +): Error | mixed { + try { + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + // TODO: find a way to memoize, in case this field is within a List type. + const args = getArgumentValues( + fieldDef, + fieldNodes[0], + exeContext.variableValues, + ); + + // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + const contextValue = exeContext.contextValue; + + const result = resolveFn(source, args, contextValue, info); + return isPromise(result) ? result.then(undefined, asErrorInstance) : result; + } catch (error) { + return asErrorInstance(error); + } +} + +// Sometimes a non-error is thrown, wrap it as an Error instance to ensure a +// consistent Error interface. +function asErrorInstance(error: mixed): Error { + return error instanceof Error ? error : new Error(error || undefined); +} + +// This is a small wrapper around completeValue which detects and logs errors +// in the execution context. +function completeValueCatchingError( + exeContext: ExecutionContext, + returnType: GraphQLOutputType, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + path: ResponsePath, + result: mixed, +): MaybePromise { + try { + let completed; + if (isPromise(result)) { + completed = result.then(resolved => + completeValue(exeContext, returnType, fieldNodes, info, path, resolved), + ); + } else { + completed = completeValue( + exeContext, + returnType, + fieldNodes, + info, + path, + result, + ); + } + + if (isPromise(completed)) { + // Note: we don't rely on a `catch` method, but we do expect "thenable" + // to take a second callback for the error case. + return completed.then(undefined, error => + handleFieldError(error, fieldNodes, path, returnType, exeContext), + ); + } + return completed; + } catch (error) { + return handleFieldError(error, fieldNodes, path, returnType, exeContext); + } +} + +function handleFieldError(rawError, fieldNodes, path, returnType, exeContext) { + const error = locatedError( + asErrorInstance(rawError), + fieldNodes, + responsePathAsArray(path), + ); + + // If the field type is non-nullable, then it is resolved without any + // protection from errors, however it still properly locates the error. + if (isNonNullType(returnType)) { + throw error; + } + + // Otherwise, error protection is applied, logging the error and resolving + // a null value for this field if one is encountered. + exeContext.errors.push(error); + return null; +} + +/** + * Implements the instructions for completeValue as defined in the + * "Field entries" section of the spec. + * + * If the field type is Non-Null, then this recursively completes the value + * for the inner type. It throws a field error if that completion returns null, + * as per the "Nullability" section of the spec. + * + * If the field type is a List, then this recursively completes the value + * for the inner type on each item in the list. + * + * If the field type is a Scalar or Enum, ensures the completed value is a legal + * value of the type by calling the `serialize` method of GraphQL type + * definition. + * + * If the field is an abstract type, determine the runtime type of the value + * and then complete based on that type + * + * Otherwise, the field type expects a sub-selection set, and will complete the + * value by evaluating all sub-selections. + */ +function completeValue( + exeContext: ExecutionContext, + returnType: GraphQLOutputType, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + path: ResponsePath, + result: mixed, +): MaybePromise { + // If result is an Error, throw a located error. + if (result instanceof Error) { + throw result; + } + + // If field type is NonNull, complete for inner type, and throw field error + // if result is null. + if (isNonNullType(returnType)) { + const completed = completeValue( + exeContext, + returnType.ofType, + fieldNodes, + info, + path, + result, + ); + if (completed === null) { + throw new Error( + `Cannot return null for non-nullable field ${info.parentType.name}.${ + info.fieldName + }.`, + ); + } + return completed; + } + + // If result value is null-ish (null, undefined, or NaN) then return null. + if (isNullish(result)) { + return null; + } + + // If field type is List, complete each item in the list with the inner type + if (isListType(returnType)) { + return completeListValue( + exeContext, + returnType, + fieldNodes, + info, + path, + result, + ); + } + + // If field type is a leaf type, Scalar or Enum, serialize to a valid value, + // returning null if serialization is not possible. + if (isLeafType(returnType)) { + return completeLeafValue(returnType, result); + } + + // If field type is an abstract type, Interface or Union, determine the + // runtime Object type and complete for that type. + if (isAbstractType(returnType)) { + return completeAbstractValue( + exeContext, + returnType, + fieldNodes, + info, + path, + result, + ); + } + + // If field type is Object, execute and complete all sub-selections. + if (isObjectType(returnType)) { + return completeObjectValue( + exeContext, + returnType, + fieldNodes, + info, + path, + result, + ); + } + + // Not reachable. All possible output types have been considered. + /* istanbul ignore next */ + throw new Error( + `Cannot complete value of unexpected type "${String( + (returnType: empty), + )}".`, + ); +} + +/** + * Complete a list value by completing each item in the list with the + * inner type + */ +function completeListValue( + exeContext: ExecutionContext, + returnType: GraphQLList, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + path: ResponsePath, + result: mixed, +): MaybePromise<$ReadOnlyArray> { + invariant( + isCollection(result), + `Expected Iterable, but did not find one for field ${ + info.parentType.name + }.${info.fieldName}.`, + ); + + // This is specified as a simple map, however we're optimizing the path + // where the list contains no Promises by avoiding creating another Promise. + const itemType = returnType.ofType; + let containsPromise = false; + const completedResults = []; + forEach((result: any), (item, index) => { + // No need to modify the info object containing the path, + // since from here on it is not ever accessed by resolver functions. + const fieldPath = addPath(path, index); + const completedItem = completeValueCatchingError( + exeContext, + itemType, + fieldNodes, + info, + fieldPath, + item, + ); + + if (!containsPromise && isPromise(completedItem)) { + containsPromise = true; + } + completedResults.push(completedItem); + }); + + return containsPromise ? Promise.all(completedResults) : completedResults; +} + +/** + * Complete a Scalar or Enum by serializing to a valid value, returning + * null if serialization is not possible. + */ +function completeLeafValue(returnType: GraphQLLeafType, result: mixed): mixed { + invariant(returnType.serialize, 'Missing serialize method on type'); + const serializedResult = returnType.serialize(result); + if (isInvalid(serializedResult)) { + throw new Error( + `Expected a value of type "${String(returnType)}" but ` + + `received: ${String(result)}`, + ); + } + return serializedResult; +} + +/** + * Complete a value of an abstract type by determining the runtime object type + * of that value, then complete the value for that type. + */ +function completeAbstractValue( + exeContext: ExecutionContext, + returnType: GraphQLAbstractType, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + path: ResponsePath, + result: mixed, +): MaybePromise> { + const runtimeType = returnType.resolveType + ? returnType.resolveType(result, exeContext.contextValue, info) + : defaultResolveTypeFn(result, exeContext.contextValue, info, returnType); + + if (isPromise(runtimeType)) { + return runtimeType.then(resolvedRuntimeType => + completeObjectValue( + exeContext, + ensureValidRuntimeType( + resolvedRuntimeType, + exeContext, + returnType, + fieldNodes, + info, + result, + ), + fieldNodes, + info, + path, + result, + ), + ); + } + + return completeObjectValue( + exeContext, + ensureValidRuntimeType( + runtimeType, + exeContext, + returnType, + fieldNodes, + info, + result, + ), + fieldNodes, + info, + path, + result, + ); +} + +function ensureValidRuntimeType( + runtimeTypeOrName: ?GraphQLObjectType | string, + exeContext: ExecutionContext, + returnType: GraphQLAbstractType, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + result: mixed, +): GraphQLObjectType { + const runtimeType = + typeof runtimeTypeOrName === 'string' + ? exeContext.schema.getType(runtimeTypeOrName) + : runtimeTypeOrName; + + if (!isObjectType(runtimeType)) { + throw new GraphQLError( + `Abstract type ${returnType.name} must resolve to an Object type at ` + + `runtime for field ${info.parentType.name}.${info.fieldName} with ` + + `value "${String(result)}", received "${String(runtimeType)}". ` + + `Either the ${returnType.name} type should provide a "resolveType" ` + + 'function or each possible types should provide an ' + + '"isTypeOf" function.', + fieldNodes, + ); + } + + if (!exeContext.schema.isPossibleType(returnType, runtimeType)) { + throw new GraphQLError( + `Runtime Object type "${runtimeType.name}" is not a possible type ` + + `for "${returnType.name}".`, + fieldNodes, + ); + } + + return runtimeType; +} + +/** + * Complete an Object value by executing all sub-selections. + */ +function completeObjectValue( + exeContext: ExecutionContext, + returnType: GraphQLObjectType, + fieldNodes: $ReadOnlyArray, + info: GraphQLResolveInfo, + path: ResponsePath, + result: mixed, +): MaybePromise> { + // If there is an isTypeOf predicate function, call it with the + // current result. If isTypeOf returns false, then raise an error rather + // than continuing execution. + if (returnType.isTypeOf) { + const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info); + + if (isPromise(isTypeOf)) { + return isTypeOf.then(resolvedIsTypeOf => { + if (!resolvedIsTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + return collectAndExecuteSubfields( + exeContext, + returnType, + fieldNodes, + path, + result, + ); + }); + } + + if (!isTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + } + + return collectAndExecuteSubfields( + exeContext, + returnType, + fieldNodes, + path, + result, + ); +} + +function invalidReturnTypeError( + returnType: GraphQLObjectType, + result: mixed, + fieldNodes: $ReadOnlyArray, +): GraphQLError { + return new GraphQLError( + `Expected value of type "${returnType.name}" but got: ${String(result)}.`, + fieldNodes, + ); +} + +function collectAndExecuteSubfields( + exeContext: ExecutionContext, + returnType: GraphQLObjectType, + fieldNodes: $ReadOnlyArray, + path: ResponsePath, + result: mixed, +): MaybePromise> { + // Collect sub-fields to execute to complete this value. + const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes); + return executeFields(exeContext, returnType, result, path, subFieldNodes); +} + +/** + * A memoized collection of relevant subfields with regard to the return + * type. Memoizing ensures the subfields are not repeatedly calculated, which + * saves overhead when resolving lists of values. + */ +const collectSubfields = memoize3(_collectSubfields); +function _collectSubfields( + exeContext: ExecutionContext, + returnType: GraphQLObjectType, + fieldNodes: $ReadOnlyArray, +): ObjMap> { + let subFieldNodes = Object.create(null); + const visitedFragmentNames = Object.create(null); + for (let i = 0; i < fieldNodes.length; i++) { + const selectionSet = fieldNodes[i].selectionSet; + if (selectionSet) { + subFieldNodes = collectFields( + exeContext, + returnType, + selectionSet, + subFieldNodes, + visitedFragmentNames, + ); + } + } + return subFieldNodes; +} + +/** + * If a resolveType function is not given, then a default resolve behavior is + * used which attempts two strategies: + * + * First, See if the provided value has a `__typename` field defined, if so, use + * that value as name of the resolved type. + * + * Otherwise, test each possible type for the abstract type by calling + * isTypeOf for the object being coerced, returning the first type that matches. + */ +function defaultResolveTypeFn( + value: mixed, + contextValue: mixed, + info: GraphQLResolveInfo, + abstractType: GraphQLAbstractType, +): MaybePromise { + // First, look for `__typename`. + if ( + value !== null && + typeof value === 'object' && + typeof value.__typename === 'string' + ) { + return value.__typename; + } + + // Otherwise, test each possible type. + const possibleTypes = info.schema.getPossibleTypes(abstractType); + const promisedIsTypeOfResults = []; + + for (let i = 0; i < possibleTypes.length; i++) { + const type = possibleTypes[i]; + + if (type.isTypeOf) { + const isTypeOfResult = type.isTypeOf(value, contextValue, info); + + if (isPromise(isTypeOfResult)) { + promisedIsTypeOfResults[i] = isTypeOfResult; + } else if (isTypeOfResult) { + return type; + } + } + } + + if (promisedIsTypeOfResults.length) { + return Promise.all(promisedIsTypeOfResults).then(isTypeOfResults => { + for (let i = 0; i < isTypeOfResults.length; i++) { + if (isTypeOfResults[i]) { + return possibleTypes[i]; + } + } + }); + } +} + +/** + * If a resolve function is not given, then a default resolve behavior is used + * which takes the property of the source object of the same name as the field + * and returns it as the result, or if it's a function, returns the result + * of calling that function while passing along args and context value. + */ +export const defaultFieldResolver: GraphQLFieldResolver = function( + source, + args, + contextValue, + info, +) { + // ensure source is a value for which property access is acceptable. + if (typeof source === 'object' || typeof source === 'function') { + const property = source[info.fieldName]; + if (typeof property === 'function') { + return source[info.fieldName](args, contextValue, info); + } + return property; + } +}; + +/** + * This method looks up the field on the given type defintion. + * It has special casing for the two introspection fields, __schema + * and __typename. __typename is special because it can always be + * queried as a field, even in situations where no other fields + * are allowed, like on a Union. __schema could get automatically + * added to the query type, but that would require mutating type + * definitions, which would cause issues. + */ +export function getFieldDef( + schema: GraphQLSchema, + parentType: GraphQLObjectType, + fieldName: string, +): ?GraphQLField<*, *> { + if ( + fieldName === SchemaMetaFieldDef.name && + schema.getQueryType() === parentType + ) { + return SchemaMetaFieldDef; + } else if ( + fieldName === TypeMetaFieldDef.name && + schema.getQueryType() === parentType + ) { + return TypeMetaFieldDef; + } else if (fieldName === TypeNameMetaFieldDef.name) { + return TypeNameMetaFieldDef; + } + return parentType.getFields()[fieldName]; +} diff --git a/dist/execution/execute.mjs b/dist/execution/execute.mjs new file mode 100644 index 0000000000..cf7e3ea0d5 --- /dev/null +++ b/dist/execution/execute.mjs @@ -0,0 +1,791 @@ +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +/** + * 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. + * + * strict + */ +import { forEach, isCollection } from 'iterall'; +import { GraphQLError, locatedError } from '../error'; +import invariant from '../jsutils/invariant'; +import isInvalid from '../jsutils/isInvalid'; +import isNullish from '../jsutils/isNullish'; +import isPromise from '../jsutils/isPromise'; +import memoize3 from '../jsutils/memoize3'; +import promiseForObject from '../jsutils/promiseForObject'; +import promiseReduce from '../jsutils/promiseReduce'; +import { getOperationRootType } from '../utilities/getOperationRootType'; +import { typeFromAST } from '../utilities/typeFromAST'; +import { Kind } from '../language/kinds'; +import { getVariableValues, getArgumentValues, getDirectiveValues } from './values'; +import { isObjectType, isAbstractType, isLeafType, isListType, isNonNullType } from '../type/definition'; +import { GraphQLSchema } from '../type/schema'; +import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from '../type/introspection'; +import { GraphQLIncludeDirective, GraphQLSkipDirective } from '../type/directives'; +import { assertValidSchema } from '../type/validate'; +export function execute(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 ? executeImpl(argsOrSchema.schema, argsOrSchema.document, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : executeImpl(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); +} + +function executeImpl(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // If arguments are missing or incorrect, throw an error. + assertValidExecutionArguments(schema, document, variableValues); // If a valid execution context cannot be created due to incorrect arguments, + // a "Response" with only errors is returned. + + var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); // Return early errors if execution context failed. + + if (Array.isArray(exeContext)) { + return { + errors: exeContext + }; + } // Return a Promise that will eventually resolve to the data described by + // The "Response" section of the GraphQL specification. + // + // If errors are encountered while executing a GraphQL field, only that + // field and its descendants will be omitted, and sibling fields will still + // be executed. An execution which encounters errors will still result in a + // resolved Promise. + + + var data = executeOperation(exeContext, exeContext.operation, rootValue); + return buildResponse(exeContext, data); +} +/** + * Given a completed execution context and data, build the { errors, data } + * response defined by the "Response" section of the GraphQL specification. + */ + + +function buildResponse(exeContext, data) { + if (isPromise(data)) { + return data.then(function (resolved) { + return buildResponse(exeContext, resolved); + }); + } + + return exeContext.errors.length === 0 ? { + data: data + } : { + errors: exeContext.errors, + data: data + }; +} +/** + * Given a ResponsePath (found in the `path` entry in the information provided + * as the last argument to a field resolver), return an Array of the path keys. + */ + + +export function responsePathAsArray(path) { + var flattened = []; + var curr = path; + + while (curr) { + flattened.push(curr.key); + curr = curr.prev; + } + + return flattened.reverse(); +} +/** + * Given a ResponsePath and a key, return a new ResponsePath containing the + * new key. + */ + +export function addPath(prev, key) { + return { + prev: prev, + key: key + }; +} +/** + * Essential assertions before executing to provide developer feedback for + * improper use of the GraphQL library. + */ + +export function assertValidExecutionArguments(schema, document, rawVariableValues) { + !document ? invariant(0, 'Must provide document') : void 0; // If the schema used for execution is invalid, throw an error. + + assertValidSchema(schema); // Variables, if provided, must be an object. + + !(!rawVariableValues || _typeof(rawVariableValues) === 'object') ? invariant(0, 'Variables must be provided as an Object where each property is a ' + 'variable value. Perhaps look to see if an unparsed JSON string ' + 'was provided.') : void 0; +} +/** + * Constructs a ExecutionContext object from the arguments passed to + * execute, which we will pass throughout the other execution methods. + * + * Throws a GraphQLError if a valid execution context cannot be created. + */ + +export function buildExecutionContext(schema, document, rootValue, contextValue, rawVariableValues, operationName, fieldResolver) { + var errors = []; + var operation; + var hasMultipleAssumedOperations = false; + var fragments = Object.create(null); + + for (var i = 0; i < document.definitions.length; i++) { + var definition = document.definitions[i]; + + switch (definition.kind) { + case Kind.OPERATION_DEFINITION: + if (!operationName && operation) { + hasMultipleAssumedOperations = true; + } else if (!operationName || definition.name && definition.name.value === operationName) { + operation = definition; + } + + break; + + case Kind.FRAGMENT_DEFINITION: + fragments[definition.name.value] = definition; + break; + } + } + + if (!operation) { + if (operationName) { + errors.push(new GraphQLError("Unknown operation named \"".concat(operationName, "\"."))); + } else { + errors.push(new GraphQLError('Must provide an operation.')); + } + } else if (hasMultipleAssumedOperations) { + errors.push(new GraphQLError('Must provide operation name if query contains ' + 'multiple operations.')); + } + + var variableValues; + + if (operation) { + var coercedVariableValues = getVariableValues(schema, operation.variableDefinitions || [], rawVariableValues || {}); + + if (coercedVariableValues.errors) { + errors.push.apply(errors, coercedVariableValues.errors); + } else { + variableValues = coercedVariableValues.coerced; + } + } + + if (errors.length !== 0) { + return errors; + } + + !operation ? invariant(0, 'Has operation if no errors.') : void 0; + !variableValues ? invariant(0, 'Has variables if no errors.') : void 0; + return { + schema: schema, + fragments: fragments, + rootValue: rootValue, + contextValue: contextValue, + operation: operation, + variableValues: variableValues, + fieldResolver: fieldResolver || defaultFieldResolver, + errors: errors + }; +} +/** + * Implements the "Evaluating operations" section of the spec. + */ + +function executeOperation(exeContext, operation, rootValue) { + var type = getOperationRootType(exeContext.schema, operation); + var fields = collectFields(exeContext, type, operation.selectionSet, Object.create(null), Object.create(null)); + var path = undefined; // Errors from sub-fields of a NonNull type may propagate to the top level, + // at which point we still log the error and null the parent field, which + // in this case is the entire response. + // + // Similar to completeValueCatchingError. + + try { + var result = operation.operation === 'mutation' ? executeFieldsSerially(exeContext, type, rootValue, path, fields) : executeFields(exeContext, type, rootValue, path, fields); + + if (isPromise(result)) { + return result.then(undefined, function (error) { + exeContext.errors.push(error); + return Promise.resolve(null); + }); + } + + return result; + } catch (error) { + exeContext.errors.push(error); + return null; + } +} +/** + * Implements the "Evaluating selection sets" section of the spec + * for "write" mode. + */ + + +function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) { + return promiseReduce(Object.keys(fields), function (results, responseName) { + var fieldNodes = fields[responseName]; + var fieldPath = addPath(path, responseName); + var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + + if (result === undefined) { + return results; + } + + if (isPromise(result)) { + return result.then(function (resolvedResult) { + results[responseName] = resolvedResult; + return results; + }); + } + + results[responseName] = result; + return results; + }, Object.create(null)); +} +/** + * Implements the "Evaluating selection sets" section of the spec + * for "read" mode. + */ + + +function executeFields(exeContext, parentType, sourceValue, path, fields) { + var results = Object.create(null); + var containsPromise = false; + + for (var i = 0, keys = Object.keys(fields); i < keys.length; ++i) { + var responseName = keys[i]; + var fieldNodes = fields[responseName]; + var fieldPath = addPath(path, responseName); + var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); + + if (result !== undefined) { + results[responseName] = result; + + if (!containsPromise && isPromise(result)) { + containsPromise = true; + } + } + } // If there are no promises, we can just return the object + + + if (!containsPromise) { + return results; + } // Otherwise, results is a map from field name to the result of resolving that + // field, which is possibly a promise. Return a promise that will return this + // same map, but with any promises replaced with the values they resolved to. + + + return promiseForObject(results); +} +/** + * Given a selectionSet, adds all of the fields in that selection to + * the passed in map of fields, and returns it at the end. + * + * CollectFields requires the "runtime type" of an object. For a field which + * returns an Interface or Union type, the "runtime type" will be the actual + * Object type returned by that field. + */ + + +export function collectFields(exeContext, runtimeType, selectionSet, fields, visitedFragmentNames) { + for (var i = 0; i < selectionSet.selections.length; i++) { + var selection = selectionSet.selections[i]; + + switch (selection.kind) { + case Kind.FIELD: + if (!shouldIncludeNode(exeContext, selection)) { + continue; + } + + var name = getFieldEntryKey(selection); + + if (!fields[name]) { + fields[name] = []; + } + + fields[name].push(selection); + break; + + case Kind.INLINE_FRAGMENT: + if (!shouldIncludeNode(exeContext, selection) || !doesFragmentConditionMatch(exeContext, selection, runtimeType)) { + continue; + } + + collectFields(exeContext, runtimeType, selection.selectionSet, fields, visitedFragmentNames); + break; + + case Kind.FRAGMENT_SPREAD: + var fragName = selection.name.value; + + if (visitedFragmentNames[fragName] || !shouldIncludeNode(exeContext, selection)) { + continue; + } + + visitedFragmentNames[fragName] = true; + var fragment = exeContext.fragments[fragName]; + + if (!fragment || !doesFragmentConditionMatch(exeContext, fragment, runtimeType)) { + continue; + } + + collectFields(exeContext, runtimeType, fragment.selectionSet, fields, visitedFragmentNames); + break; + } + } + + return fields; +} +/** + * Determines if a field should be included based on the @include and @skip + * directives, where @skip has higher precidence than @include. + */ + +function shouldIncludeNode(exeContext, node) { + var skip = getDirectiveValues(GraphQLSkipDirective, node, exeContext.variableValues); + + if (skip && skip.if === true) { + return false; + } + + var include = getDirectiveValues(GraphQLIncludeDirective, node, exeContext.variableValues); + + if (include && include.if === false) { + return false; + } + + return true; +} +/** + * Determines if a fragment is applicable to the given type. + */ + + +function doesFragmentConditionMatch(exeContext, fragment, type) { + var typeConditionNode = fragment.typeCondition; + + if (!typeConditionNode) { + return true; + } + + var conditionalType = typeFromAST(exeContext.schema, typeConditionNode); + + if (conditionalType === type) { + return true; + } + + if (isAbstractType(conditionalType)) { + return exeContext.schema.isPossibleType(conditionalType, type); + } + + return false; +} +/** + * Implements the logic to compute the key of a given field's entry + */ + + +function getFieldEntryKey(node) { + return node.alias ? node.alias.value : node.name.value; +} +/** + * Resolves the field on the given source object. In particular, this + * figures out the value that the field returns by calling its resolve function, + * then calls completeValue to complete promises, serialize scalars, or execute + * the sub-selection-set for objects. + */ + + +function resolveField(exeContext, parentType, source, fieldNodes, path) { + var fieldNode = fieldNodes[0]; + var fieldName = fieldNode.name.value; + var fieldDef = getFieldDef(exeContext.schema, parentType, fieldName); + + if (!fieldDef) { + return; + } + + var resolveFn = fieldDef.resolve || exeContext.fieldResolver; + var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path); // Get the resolve function, regardless of if its result is normal + // or abrupt (error). + + var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info); + return completeValueCatchingError(exeContext, fieldDef.type, fieldNodes, info, path, result); +} + +export function buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path) { + // The resolve function's optional fourth argument is a collection of + // information about the current execution state. + return { + fieldName: fieldDef.name, + fieldNodes: fieldNodes, + returnType: fieldDef.type, + parentType: parentType, + path: path, + schema: exeContext.schema, + fragments: exeContext.fragments, + rootValue: exeContext.rootValue, + operation: exeContext.operation, + variableValues: exeContext.variableValues + }; +} // Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField` +// function. Returns the result of resolveFn or the abrupt-return Error object. + +export function resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info) { + try { + // Build a JS object of arguments from the field.arguments AST, using the + // variables scope to fulfill any variable references. + // TODO: find a way to memoize, in case this field is within a List type. + var args = getArgumentValues(fieldDef, fieldNodes[0], exeContext.variableValues); // The resolve function's optional third argument is a context value that + // is provided to every resolve function within an execution. It is commonly + // used to represent an authenticated user, or request-specific caches. + + var _contextValue = exeContext.contextValue; + var result = resolveFn(source, args, _contextValue, info); + return isPromise(result) ? result.then(undefined, asErrorInstance) : result; + } catch (error) { + return asErrorInstance(error); + } +} // Sometimes a non-error is thrown, wrap it as an Error instance to ensure a +// consistent Error interface. + +function asErrorInstance(error) { + return error instanceof Error ? error : new Error(error || undefined); +} // This is a small wrapper around completeValue which detects and logs errors +// in the execution context. + + +function completeValueCatchingError(exeContext, returnType, fieldNodes, info, path, result) { + try { + var completed; + + if (isPromise(result)) { + completed = result.then(function (resolved) { + return completeValue(exeContext, returnType, fieldNodes, info, path, resolved); + }); + } else { + completed = completeValue(exeContext, returnType, fieldNodes, info, path, result); + } + + if (isPromise(completed)) { + // Note: we don't rely on a `catch` method, but we do expect "thenable" + // to take a second callback for the error case. + return completed.then(undefined, function (error) { + return handleFieldError(error, fieldNodes, path, returnType, exeContext); + }); + } + + return completed; + } catch (error) { + return handleFieldError(error, fieldNodes, path, returnType, exeContext); + } +} + +function handleFieldError(rawError, fieldNodes, path, returnType, exeContext) { + var error = locatedError(asErrorInstance(rawError), fieldNodes, responsePathAsArray(path)); // If the field type is non-nullable, then it is resolved without any + // protection from errors, however it still properly locates the error. + + if (isNonNullType(returnType)) { + throw error; + } // Otherwise, error protection is applied, logging the error and resolving + // a null value for this field if one is encountered. + + + exeContext.errors.push(error); + return null; +} +/** + * Implements the instructions for completeValue as defined in the + * "Field entries" section of the spec. + * + * If the field type is Non-Null, then this recursively completes the value + * for the inner type. It throws a field error if that completion returns null, + * as per the "Nullability" section of the spec. + * + * If the field type is a List, then this recursively completes the value + * for the inner type on each item in the list. + * + * If the field type is a Scalar or Enum, ensures the completed value is a legal + * value of the type by calling the `serialize` method of GraphQL type + * definition. + * + * If the field is an abstract type, determine the runtime type of the value + * and then complete based on that type + * + * Otherwise, the field type expects a sub-selection set, and will complete the + * value by evaluating all sub-selections. + */ + + +function completeValue(exeContext, returnType, fieldNodes, info, path, result) { + // If result is an Error, throw a located error. + if (result instanceof Error) { + throw result; + } // If field type is NonNull, complete for inner type, and throw field error + // if result is null. + + + if (isNonNullType(returnType)) { + var completed = completeValue(exeContext, returnType.ofType, fieldNodes, info, path, result); + + if (completed === null) { + throw new Error("Cannot return null for non-nullable field ".concat(info.parentType.name, ".").concat(info.fieldName, ".")); + } + + return completed; + } // If result value is null-ish (null, undefined, or NaN) then return null. + + + if (isNullish(result)) { + return null; + } // If field type is List, complete each item in the list with the inner type + + + if (isListType(returnType)) { + return completeListValue(exeContext, returnType, fieldNodes, info, path, result); + } // If field type is a leaf type, Scalar or Enum, serialize to a valid value, + // returning null if serialization is not possible. + + + if (isLeafType(returnType)) { + return completeLeafValue(returnType, result); + } // If field type is an abstract type, Interface or Union, determine the + // runtime Object type and complete for that type. + + + if (isAbstractType(returnType)) { + return completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result); + } // If field type is Object, execute and complete all sub-selections. + + + if (isObjectType(returnType)) { + return completeObjectValue(exeContext, returnType, fieldNodes, info, path, result); + } // Not reachable. All possible output types have been considered. + + /* istanbul ignore next */ + + + throw new Error("Cannot complete value of unexpected type \"".concat(String(returnType), "\".")); +} +/** + * Complete a list value by completing each item in the list with the + * inner type + */ + + +function completeListValue(exeContext, returnType, fieldNodes, info, path, result) { + !isCollection(result) ? invariant(0, "Expected Iterable, but did not find one for field ".concat(info.parentType.name, ".").concat(info.fieldName, ".")) : void 0; // This is specified as a simple map, however we're optimizing the path + // where the list contains no Promises by avoiding creating another Promise. + + var itemType = returnType.ofType; + var containsPromise = false; + var completedResults = []; + forEach(result, function (item, index) { + // No need to modify the info object containing the path, + // since from here on it is not ever accessed by resolver functions. + var fieldPath = addPath(path, index); + var completedItem = completeValueCatchingError(exeContext, itemType, fieldNodes, info, fieldPath, item); + + if (!containsPromise && isPromise(completedItem)) { + containsPromise = true; + } + + completedResults.push(completedItem); + }); + return containsPromise ? Promise.all(completedResults) : completedResults; +} +/** + * Complete a Scalar or Enum by serializing to a valid value, returning + * null if serialization is not possible. + */ + + +function completeLeafValue(returnType, result) { + !returnType.serialize ? invariant(0, 'Missing serialize method on type') : void 0; + var serializedResult = returnType.serialize(result); + + if (isInvalid(serializedResult)) { + throw new Error("Expected a value of type \"".concat(String(returnType), "\" but ") + "received: ".concat(String(result))); + } + + return serializedResult; +} +/** + * Complete a value of an abstract type by determining the runtime object type + * of that value, then complete the value for that type. + */ + + +function completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result) { + var runtimeType = returnType.resolveType ? returnType.resolveType(result, exeContext.contextValue, info) : defaultResolveTypeFn(result, exeContext.contextValue, info, returnType); + + if (isPromise(runtimeType)) { + return runtimeType.then(function (resolvedRuntimeType) { + return completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result); + }); + } + + return completeObjectValue(exeContext, ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result); +} + +function ensureValidRuntimeType(runtimeTypeOrName, exeContext, returnType, fieldNodes, info, result) { + var runtimeType = typeof runtimeTypeOrName === 'string' ? exeContext.schema.getType(runtimeTypeOrName) : runtimeTypeOrName; + + if (!isObjectType(runtimeType)) { + throw new GraphQLError("Abstract type ".concat(returnType.name, " must resolve to an Object type at ") + "runtime for field ".concat(info.parentType.name, ".").concat(info.fieldName, " with ") + "value \"".concat(String(result), "\", received \"").concat(String(runtimeType), "\". ") + "Either the ".concat(returnType.name, " type should provide a \"resolveType\" ") + 'function or each possible types should provide an ' + '"isTypeOf" function.', fieldNodes); + } + + if (!exeContext.schema.isPossibleType(returnType, runtimeType)) { + throw new GraphQLError("Runtime Object type \"".concat(runtimeType.name, "\" is not a possible type ") + "for \"".concat(returnType.name, "\"."), fieldNodes); + } + + return runtimeType; +} +/** + * Complete an Object value by executing all sub-selections. + */ + + +function completeObjectValue(exeContext, returnType, fieldNodes, info, path, result) { + // If there is an isTypeOf predicate function, call it with the + // current result. If isTypeOf returns false, then raise an error rather + // than continuing execution. + if (returnType.isTypeOf) { + var isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info); + + if (isPromise(isTypeOf)) { + return isTypeOf.then(function (resolvedIsTypeOf) { + if (!resolvedIsTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + + return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result); + }); + } + + if (!isTypeOf) { + throw invalidReturnTypeError(returnType, result, fieldNodes); + } + } + + return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result); +} + +function invalidReturnTypeError(returnType, result, fieldNodes) { + return new GraphQLError("Expected value of type \"".concat(returnType.name, "\" but got: ").concat(String(result), "."), fieldNodes); +} + +function collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result) { + // Collect sub-fields to execute to complete this value. + var subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes); + return executeFields(exeContext, returnType, result, path, subFieldNodes); +} +/** + * A memoized collection of relevant subfields with regard to the return + * type. Memoizing ensures the subfields are not repeatedly calculated, which + * saves overhead when resolving lists of values. + */ + + +var collectSubfields = memoize3(_collectSubfields); + +function _collectSubfields(exeContext, returnType, fieldNodes) { + var subFieldNodes = Object.create(null); + var visitedFragmentNames = Object.create(null); + + for (var i = 0; i < fieldNodes.length; i++) { + var selectionSet = fieldNodes[i].selectionSet; + + if (selectionSet) { + subFieldNodes = collectFields(exeContext, returnType, selectionSet, subFieldNodes, visitedFragmentNames); + } + } + + return subFieldNodes; +} +/** + * If a resolveType function is not given, then a default resolve behavior is + * used which attempts two strategies: + * + * First, See if the provided value has a `__typename` field defined, if so, use + * that value as name of the resolved type. + * + * Otherwise, test each possible type for the abstract type by calling + * isTypeOf for the object being coerced, returning the first type that matches. + */ + + +function defaultResolveTypeFn(value, contextValue, info, abstractType) { + // First, look for `__typename`. + if (value !== null && _typeof(value) === 'object' && typeof value.__typename === 'string') { + return value.__typename; + } // Otherwise, test each possible type. + + + var possibleTypes = info.schema.getPossibleTypes(abstractType); + var promisedIsTypeOfResults = []; + + for (var i = 0; i < possibleTypes.length; i++) { + var type = possibleTypes[i]; + + if (type.isTypeOf) { + var isTypeOfResult = type.isTypeOf(value, contextValue, info); + + if (isPromise(isTypeOfResult)) { + promisedIsTypeOfResults[i] = isTypeOfResult; + } else if (isTypeOfResult) { + return type; + } + } + } + + if (promisedIsTypeOfResults.length) { + return Promise.all(promisedIsTypeOfResults).then(function (isTypeOfResults) { + for (var _i = 0; _i < isTypeOfResults.length; _i++) { + if (isTypeOfResults[_i]) { + return possibleTypes[_i]; + } + } + }); + } +} +/** + * If a resolve function is not given, then a default resolve behavior is used + * which takes the property of the source object of the same name as the field + * and returns it as the result, or if it's a function, returns the result + * of calling that function while passing along args and context value. + */ + + +export var defaultFieldResolver = function defaultFieldResolver(source, args, contextValue, info) { + // ensure source is a value for which property access is acceptable. + if (_typeof(source) === 'object' || typeof source === 'function') { + var property = source[info.fieldName]; + + if (typeof property === 'function') { + return source[info.fieldName](args, contextValue, info); + } + + return property; + } +}; +/** + * This method looks up the field on the given type defintion. + * It has special casing for the two introspection fields, __schema + * and __typename. __typename is special because it can always be + * queried as a field, even in situations where no other fields + * are allowed, like on a Union. __schema could get automatically + * added to the query type, but that would require mutating type + * definitions, which would cause issues. + */ + +export function getFieldDef(schema, parentType, fieldName) { + if (fieldName === SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { + return SchemaMetaFieldDef; + } else if (fieldName === TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return TypeMetaFieldDef; + } else if (fieldName === TypeNameMetaFieldDef.name) { + return TypeNameMetaFieldDef; + } + + return parentType.getFields()[fieldName]; +} \ No newline at end of file diff --git a/dist/execution/index.js b/dist/execution/index.js new file mode 100644 index 0000000000..77f12743ed --- /dev/null +++ b/dist/execution/index.js @@ -0,0 +1,33 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "execute", { + enumerable: true, + get: function get() { + return _execute.execute; + } +}); +Object.defineProperty(exports, "defaultFieldResolver", { + enumerable: true, + get: function get() { + return _execute.defaultFieldResolver; + } +}); +Object.defineProperty(exports, "responsePathAsArray", { + enumerable: true, + get: function get() { + return _execute.responsePathAsArray; + } +}); +Object.defineProperty(exports, "getDirectiveValues", { + enumerable: true, + get: function get() { + return _values.getDirectiveValues; + } +}); + +var _execute = require("./execute"); + +var _values = require("./values"); \ No newline at end of file diff --git a/dist/execution/index.js.flow b/dist/execution/index.js.flow new file mode 100644 index 0000000000..d793e2613a --- /dev/null +++ b/dist/execution/index.js.flow @@ -0,0 +1,13 @@ +/** + * 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. + * + * @flow strict + */ + +export { execute, defaultFieldResolver, responsePathAsArray } from './execute'; +export { getDirectiveValues } from './values'; + +export type { ExecutionArgs, ExecutionResult } from './execute'; diff --git a/dist/execution/index.mjs b/dist/execution/index.mjs new file mode 100644 index 0000000000..66db93af53 --- /dev/null +++ b/dist/execution/index.mjs @@ -0,0 +1,10 @@ +/** + * 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. + * + * strict + */ +export { execute, defaultFieldResolver, responsePathAsArray } from './execute'; +export { getDirectiveValues } from './values'; \ No newline at end of file diff --git a/dist/execution/values.js b/dist/execution/values.js new file mode 100644 index 0000000000..81d0e87940 --- /dev/null +++ b/dist/execution/values.js @@ -0,0 +1,222 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getVariableValues = getVariableValues; +exports.getArgumentValues = getArgumentValues; +exports.getDirectiveValues = getDirectiveValues; + +var _error = require("../error"); + +var _find = _interopRequireDefault(require("../jsutils/find")); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _coerceValue = require("../utilities/coerceValue"); + +var _typeFromAST = require("../utilities/typeFromAST"); + +var _valueFromAST = require("../utilities/valueFromAST"); + +var _kinds = require("../language/kinds"); + +var _printer = require("../language/printer"); + +var _definition = require("../type/definition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Prepares an object map of variableValues of the correct type based on the + * provided variable definitions and arbitrary input. If the input cannot be + * parsed to match the variable definitions, a GraphQLError will be thrown. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +function getVariableValues(schema, varDefNodes, inputs) { + var errors = []; + var coercedValues = {}; + + var _loop = function _loop(i) { + var varDefNode = varDefNodes[i]; + var varName = varDefNode.variable.name.value; + var varType = (0, _typeFromAST.typeFromAST)(schema, varDefNode.type); + + if (!(0, _definition.isInputType)(varType)) { + // Must use input types for variables. This should be caught during + // validation, however is checked again here for safety. + errors.push(new _error.GraphQLError("Variable \"$".concat(varName, "\" expected value of type ") + "\"".concat((0, _printer.print)(varDefNode.type), "\" which cannot be used as an input type."), [varDefNode.type])); + } else { + var hasValue = hasOwnProperty(inputs, varName); + var value = hasValue ? inputs[varName] : undefined; + + if (!hasValue && varDefNode.defaultValue) { + // If no value was provided to a variable with a default value, + // use the default value. + coercedValues[varName] = (0, _valueFromAST.valueFromAST)(varDefNode.defaultValue, varType); + } else if ((!hasValue || value === null) && (0, _definition.isNonNullType)(varType)) { + // If no value or a nullish value was provided to a variable with a + // non-null type (required), produce an error. + errors.push(new _error.GraphQLError(hasValue ? "Variable \"$".concat(varName, "\" of non-null type ") + "\"".concat(String(varType), "\" must not be null.") : "Variable \"$".concat(varName, "\" of required type ") + "\"".concat(String(varType), "\" was not provided."), [varDefNode])); + } else if (hasValue) { + if (value === null) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[varName] = null; + } else { + // Otherwise, a non-null value was provided, coerce it to the expected + // type or report an error if coercion fails. + var _coerced = (0, _coerceValue.coerceValue)(value, varType, varDefNode); + + var coercionErrors = _coerced.errors; + + if (coercionErrors) { + coercionErrors.forEach(function (error) { + error.message = "Variable \"$".concat(varName, "\" got invalid ") + "value ".concat(JSON.stringify(value), "; ").concat(error.message); + }); + errors.push.apply(errors, coercionErrors); + } else { + coercedValues[varName] = _coerced.value; + } + } + } + } + }; + + for (var i = 0; i < varDefNodes.length; i++) { + _loop(i); + } + + return errors.length === 0 ? { + errors: undefined, + coerced: coercedValues + } : { + errors: errors, + coerced: undefined + }; +} +/** + * Prepares an object map of argument values given a list of argument + * definitions and list of argument AST nodes. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ + + +function getArgumentValues(def, node, variableValues) { + var coercedValues = {}; + var argDefs = def.args; + var argNodes = node.arguments; + + if (!argDefs || !argNodes) { + return coercedValues; + } + + var argNodeMap = (0, _keyMap.default)(argNodes, function (arg) { + return arg.name.value; + }); + + for (var i = 0; i < argDefs.length; i++) { + var argDef = argDefs[i]; + var name = argDef.name; + var argType = argDef.type; + var argumentNode = argNodeMap[name]; + var hasValue = void 0; + var isNull = void 0; + + if (argumentNode && argumentNode.value.kind === _kinds.Kind.VARIABLE) { + var variableName = argumentNode.value.name.value; + hasValue = variableValues && hasOwnProperty(variableValues, variableName); + isNull = variableValues && variableValues[variableName] === null; + } else { + hasValue = argumentNode != null; + isNull = argumentNode && argumentNode.value.kind === _kinds.Kind.NULL; + } + + if (!hasValue && argDef.defaultValue !== undefined) { + // If no argument was provided where the definition has a default value, + // use the default value. + coercedValues[name] = argDef.defaultValue; + } else if ((!hasValue || isNull) && (0, _definition.isNonNullType)(argType)) { + // If no argument or a null value was provided to an argument with a + // non-null type (required), produce a field error. + if (isNull) { + throw new _error.GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat(String(argType), "\" ") + 'must not be null.', [argumentNode.value]); + } else if (argumentNode && argumentNode.value.kind === _kinds.Kind.VARIABLE) { + var _variableName = argumentNode.value.name.value; + throw new _error.GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(String(argType), "\" ") + "was provided the variable \"$".concat(_variableName, "\" ") + 'which was not provided a runtime value.', [argumentNode.value]); + } else { + throw new _error.GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(String(argType), "\" ") + 'was not provided.', [node]); + } + } else if (hasValue) { + if (argumentNode.value.kind === _kinds.Kind.NULL) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[name] = null; + } else if (argumentNode.value.kind === _kinds.Kind.VARIABLE) { + var _variableName2 = argumentNode.value.name.value; + !variableValues ? (0, _invariant.default)(0, 'Must exist for hasValue to be true.') : void 0; // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + + coercedValues[name] = variableValues[_variableName2]; + } else { + var valueNode = argumentNode.value; + var coercedValue = (0, _valueFromAST.valueFromAST)(valueNode, argType, variableValues); + + if (coercedValue === undefined) { + // Note: ValuesOfCorrectType validation should catch this before + // execution. This is a runtime check to ensure execution does not + // continue with an invalid argument value. + throw new _error.GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat((0, _printer.print)(valueNode), "."), [argumentNode.value]); + } + + coercedValues[name] = coercedValue; + } + } + } + + return coercedValues; +} +/** + * Prepares an object map of argument values given a directive definition + * and a AST node which may contain directives. Optionally also accepts a map + * of variable values. + * + * If the directive does not exist on the node, returns undefined. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ + + +function getDirectiveValues(directiveDef, node, variableValues) { + var directiveNode = node.directives && (0, _find.default)(node.directives, function (directive) { + return directive.name.value === directiveDef.name; + }); + + if (directiveNode) { + return getArgumentValues(directiveDef, directiveNode, variableValues); + } +} + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} \ No newline at end of file diff --git a/dist/execution/values.js.flow b/dist/execution/values.js.flow new file mode 100644 index 0000000000..5690bbe366 --- /dev/null +++ b/dist/execution/values.js.flow @@ -0,0 +1,241 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLError } from '../error'; +import find from '../jsutils/find'; +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import { coerceValue } from '../utilities/coerceValue'; +import { typeFromAST } from '../utilities/typeFromAST'; +import { valueFromAST } from '../utilities/valueFromAST'; +import { Kind } from '../language/kinds'; +import { print } from '../language/printer'; +import { isInputType, isNonNullType } from '../type/definition'; +import type { ObjMap } from '../jsutils/ObjMap'; +import type { GraphQLField } from '../type/definition'; +import type { GraphQLDirective } from '../type/directives'; +import type { GraphQLSchema } from '../type/schema'; +import type { + FieldNode, + DirectiveNode, + VariableDefinitionNode, +} from '../language/ast'; + +type CoercedVariableValues = {| + errors: $ReadOnlyArray | void, + coerced: { [variable: string]: mixed } | void, +|}; + +/** + * Prepares an object map of variableValues of the correct type based on the + * provided variable definitions and arbitrary input. If the input cannot be + * parsed to match the variable definitions, a GraphQLError will be thrown. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getVariableValues( + schema: GraphQLSchema, + varDefNodes: Array, + inputs: ObjMap, +): CoercedVariableValues { + const errors = []; + const coercedValues = {}; + for (let i = 0; i < varDefNodes.length; i++) { + const varDefNode = varDefNodes[i]; + const varName = varDefNode.variable.name.value; + const varType = typeFromAST(schema, varDefNode.type); + if (!isInputType(varType)) { + // Must use input types for variables. This should be caught during + // validation, however is checked again here for safety. + errors.push( + new GraphQLError( + `Variable "$${varName}" expected value of type ` + + `"${print( + varDefNode.type, + )}" which cannot be used as an input type.`, + [varDefNode.type], + ), + ); + } else { + const hasValue = hasOwnProperty(inputs, varName); + const value = hasValue ? inputs[varName] : undefined; + if (!hasValue && varDefNode.defaultValue) { + // If no value was provided to a variable with a default value, + // use the default value. + coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType); + } else if ((!hasValue || value === null) && isNonNullType(varType)) { + // If no value or a nullish value was provided to a variable with a + // non-null type (required), produce an error. + errors.push( + new GraphQLError( + hasValue + ? `Variable "$${varName}" of non-null type ` + + `"${String(varType)}" must not be null.` + : `Variable "$${varName}" of required type ` + + `"${String(varType)}" was not provided.`, + [varDefNode], + ), + ); + } else if (hasValue) { + if (value === null) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[varName] = null; + } else { + // Otherwise, a non-null value was provided, coerce it to the expected + // type or report an error if coercion fails. + const coerced = coerceValue(value, varType, varDefNode); + const coercionErrors = coerced.errors; + if (coercionErrors) { + coercionErrors.forEach(error => { + error.message = + `Variable "$${varName}" got invalid ` + + `value ${JSON.stringify(value)}; ${error.message}`; + }); + errors.push(...coercionErrors); + } else { + coercedValues[varName] = coerced.value; + } + } + } + } + } + return errors.length === 0 + ? { errors: undefined, coerced: coercedValues } + : { errors, coerced: undefined }; +} + +/** + * Prepares an object map of argument values given a list of argument + * definitions and list of argument AST nodes. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getArgumentValues( + def: GraphQLField<*, *> | GraphQLDirective, + node: FieldNode | DirectiveNode, + variableValues?: ?ObjMap, +): { [argument: string]: mixed } { + const coercedValues = {}; + const argDefs = def.args; + const argNodes = node.arguments; + if (!argDefs || !argNodes) { + return coercedValues; + } + const argNodeMap = keyMap(argNodes, arg => arg.name.value); + for (let i = 0; i < argDefs.length; i++) { + const argDef = argDefs[i]; + const name = argDef.name; + const argType = argDef.type; + const argumentNode = argNodeMap[name]; + let hasValue; + let isNull; + if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) { + const variableName = argumentNode.value.name.value; + hasValue = variableValues && hasOwnProperty(variableValues, variableName); + isNull = variableValues && variableValues[variableName] === null; + } else { + hasValue = argumentNode != null; + isNull = argumentNode && argumentNode.value.kind === Kind.NULL; + } + + if (!hasValue && argDef.defaultValue !== undefined) { + // If no argument was provided where the definition has a default value, + // use the default value. + coercedValues[name] = argDef.defaultValue; + } else if ((!hasValue || isNull) && isNonNullType(argType)) { + // If no argument or a null value was provided to an argument with a + // non-null type (required), produce a field error. + if (isNull) { + throw new GraphQLError( + `Argument "${name}" of non-null type "${String(argType)}" ` + + 'must not be null.', + [argumentNode.value], + ); + } else if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) { + const variableName = argumentNode.value.name.value; + throw new GraphQLError( + `Argument "${name}" of required type "${String(argType)}" ` + + `was provided the variable "$${variableName}" ` + + 'which was not provided a runtime value.', + [argumentNode.value], + ); + } else { + throw new GraphQLError( + `Argument "${name}" of required type "${String(argType)}" ` + + 'was not provided.', + [node], + ); + } + } else if (hasValue) { + if (argumentNode.value.kind === Kind.NULL) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[name] = null; + } else if (argumentNode.value.kind === Kind.VARIABLE) { + const variableName = argumentNode.value.name.value; + invariant(variableValues, 'Must exist for hasValue to be true.'); + // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + coercedValues[name] = variableValues[variableName]; + } else { + const valueNode = argumentNode.value; + const coercedValue = valueFromAST(valueNode, argType, variableValues); + if (coercedValue === undefined) { + // Note: ValuesOfCorrectType validation should catch this before + // execution. This is a runtime check to ensure execution does not + // continue with an invalid argument value. + throw new GraphQLError( + `Argument "${name}" has invalid value ${print(valueNode)}.`, + [argumentNode.value], + ); + } + coercedValues[name] = coercedValue; + } + } + } + return coercedValues; +} + +/** + * Prepares an object map of argument values given a directive definition + * and a AST node which may contain directives. Optionally also accepts a map + * of variable values. + * + * If the directive does not exist on the node, returns undefined. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getDirectiveValues( + directiveDef: GraphQLDirective, + node: { +directives?: $ReadOnlyArray }, + variableValues?: ?ObjMap, +): void | { [argument: string]: mixed } { + const directiveNode = + node.directives && + find( + node.directives, + directive => directive.name.value === directiveDef.name, + ); + + if (directiveNode) { + return getArgumentValues(directiveDef, directiveNode, variableValues); + } +} + +function hasOwnProperty(obj: mixed, prop: string): boolean { + return Object.prototype.hasOwnProperty.call(obj, prop); +} diff --git a/dist/execution/values.mjs b/dist/execution/values.mjs new file mode 100644 index 0000000000..ea3d4a641d --- /dev/null +++ b/dist/execution/values.mjs @@ -0,0 +1,199 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../error'; +import find from '../jsutils/find'; +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import { coerceValue } from '../utilities/coerceValue'; +import { typeFromAST } from '../utilities/typeFromAST'; +import { valueFromAST } from '../utilities/valueFromAST'; +import { Kind } from '../language/kinds'; +import { print } from '../language/printer'; +import { isInputType, isNonNullType } from '../type/definition'; + +/** + * Prepares an object map of variableValues of the correct type based on the + * provided variable definitions and arbitrary input. If the input cannot be + * parsed to match the variable definitions, a GraphQLError will be thrown. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ +export function getVariableValues(schema, varDefNodes, inputs) { + var errors = []; + var coercedValues = {}; + + var _loop = function _loop(i) { + var varDefNode = varDefNodes[i]; + var varName = varDefNode.variable.name.value; + var varType = typeFromAST(schema, varDefNode.type); + + if (!isInputType(varType)) { + // Must use input types for variables. This should be caught during + // validation, however is checked again here for safety. + errors.push(new GraphQLError("Variable \"$".concat(varName, "\" expected value of type ") + "\"".concat(print(varDefNode.type), "\" which cannot be used as an input type."), [varDefNode.type])); + } else { + var hasValue = hasOwnProperty(inputs, varName); + var value = hasValue ? inputs[varName] : undefined; + + if (!hasValue && varDefNode.defaultValue) { + // If no value was provided to a variable with a default value, + // use the default value. + coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType); + } else if ((!hasValue || value === null) && isNonNullType(varType)) { + // If no value or a nullish value was provided to a variable with a + // non-null type (required), produce an error. + errors.push(new GraphQLError(hasValue ? "Variable \"$".concat(varName, "\" of non-null type ") + "\"".concat(String(varType), "\" must not be null.") : "Variable \"$".concat(varName, "\" of required type ") + "\"".concat(String(varType), "\" was not provided."), [varDefNode])); + } else if (hasValue) { + if (value === null) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[varName] = null; + } else { + // Otherwise, a non-null value was provided, coerce it to the expected + // type or report an error if coercion fails. + var _coerced = coerceValue(value, varType, varDefNode); + + var coercionErrors = _coerced.errors; + + if (coercionErrors) { + coercionErrors.forEach(function (error) { + error.message = "Variable \"$".concat(varName, "\" got invalid ") + "value ".concat(JSON.stringify(value), "; ").concat(error.message); + }); + errors.push.apply(errors, coercionErrors); + } else { + coercedValues[varName] = _coerced.value; + } + } + } + } + }; + + for (var i = 0; i < varDefNodes.length; i++) { + _loop(i); + } + + return errors.length === 0 ? { + errors: undefined, + coerced: coercedValues + } : { + errors: errors, + coerced: undefined + }; +} +/** + * Prepares an object map of argument values given a list of argument + * definitions and list of argument AST nodes. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ + +export function getArgumentValues(def, node, variableValues) { + var coercedValues = {}; + var argDefs = def.args; + var argNodes = node.arguments; + + if (!argDefs || !argNodes) { + return coercedValues; + } + + var argNodeMap = keyMap(argNodes, function (arg) { + return arg.name.value; + }); + + for (var i = 0; i < argDefs.length; i++) { + var argDef = argDefs[i]; + var name = argDef.name; + var argType = argDef.type; + var argumentNode = argNodeMap[name]; + var hasValue = void 0; + var isNull = void 0; + + if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) { + var variableName = argumentNode.value.name.value; + hasValue = variableValues && hasOwnProperty(variableValues, variableName); + isNull = variableValues && variableValues[variableName] === null; + } else { + hasValue = argumentNode != null; + isNull = argumentNode && argumentNode.value.kind === Kind.NULL; + } + + if (!hasValue && argDef.defaultValue !== undefined) { + // If no argument was provided where the definition has a default value, + // use the default value. + coercedValues[name] = argDef.defaultValue; + } else if ((!hasValue || isNull) && isNonNullType(argType)) { + // If no argument or a null value was provided to an argument with a + // non-null type (required), produce a field error. + if (isNull) { + throw new GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat(String(argType), "\" ") + 'must not be null.', [argumentNode.value]); + } else if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) { + var _variableName = argumentNode.value.name.value; + throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(String(argType), "\" ") + "was provided the variable \"$".concat(_variableName, "\" ") + 'which was not provided a runtime value.', [argumentNode.value]); + } else { + throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(String(argType), "\" ") + 'was not provided.', [node]); + } + } else if (hasValue) { + if (argumentNode.value.kind === Kind.NULL) { + // If the explicit value `null` was provided, an entry in the coerced + // values must exist as the value `null`. + coercedValues[name] = null; + } else if (argumentNode.value.kind === Kind.VARIABLE) { + var _variableName2 = argumentNode.value.name.value; + !variableValues ? invariant(0, 'Must exist for hasValue to be true.') : void 0; // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + + coercedValues[name] = variableValues[_variableName2]; + } else { + var valueNode = argumentNode.value; + var coercedValue = valueFromAST(valueNode, argType, variableValues); + + if (coercedValue === undefined) { + // Note: ValuesOfCorrectType validation should catch this before + // execution. This is a runtime check to ensure execution does not + // continue with an invalid argument value. + throw new GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat(print(valueNode), "."), [argumentNode.value]); + } + + coercedValues[name] = coercedValue; + } + } + } + + return coercedValues; +} +/** + * Prepares an object map of argument values given a directive definition + * and a AST node which may contain directives. Optionally also accepts a map + * of variable values. + * + * If the directive does not exist on the node, returns undefined. + * + * Note: The returned value is a plain Object with a prototype, since it is + * exposed to user code. Care should be taken to not pull values from the + * Object prototype. + */ + +export function getDirectiveValues(directiveDef, node, variableValues) { + var directiveNode = node.directives && find(node.directives, function (directive) { + return directive.name.value === directiveDef.name; + }); + + if (directiveNode) { + return getArgumentValues(directiveDef, directiveNode, variableValues); + } +} + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} \ No newline at end of file diff --git a/dist/graphql.js b/dist/graphql.js new file mode 100644 index 0000000000..1c2ed25d23 --- /dev/null +++ b/dist/graphql.js @@ -0,0 +1,86 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.graphql = graphql; +exports.graphqlSync = graphqlSync; + +var _validate = require("./type/validate"); + +var _parser = require("./language/parser"); + +var _validate2 = require("./validation/validate"); + +var _execute = require("./execution/execute"); + +/** + * 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. + * + * strict + */ +function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + var _arguments = arguments; + + /* eslint-enable no-redeclare */ + // Always return a Promise for a consistent API. + return new Promise(function (resolve) { + return resolve( // Extract arguments from object args if provided. + _arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver)); + }); +} +/** + * The graphqlSync function also fulfills GraphQL operations by parsing, + * validating, and executing a GraphQL document along side a GraphQL schema. + * However, it guarantees to complete synchronously (or throw an error) assuming + * that all field resolvers are also synchronous. + */ + + +function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // Extract arguments from object args if provided. + var result = arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver); // Assert that the execution was synchronous. + + if (result.then) { + throw new Error('GraphQL execution failed to complete synchronously.'); + } + + return result; +} + +function graphqlImpl(schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // Validate Schema + var schemaValidationErrors = (0, _validate.validateSchema)(schema); + + if (schemaValidationErrors.length > 0) { + return { + errors: schemaValidationErrors + }; + } // Parse + + + var document; + + try { + document = (0, _parser.parse)(source); + } catch (syntaxError) { + return { + errors: [syntaxError] + }; + } // Validate + + + var validationErrors = (0, _validate2.validate)(schema, document); + + if (validationErrors.length > 0) { + return { + errors: validationErrors + }; + } // Execute + + + return (0, _execute.execute)(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); +} \ No newline at end of file diff --git a/dist/graphql.js.flow b/dist/graphql.js.flow new file mode 100644 index 0000000000..8b0fe061f2 --- /dev/null +++ b/dist/graphql.js.flow @@ -0,0 +1,203 @@ +/** + * 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. + * + * @flow strict + */ + +import { validateSchema } from './type/validate'; +import { parse } from './language/parser'; +import { validate } from './validation/validate'; +import { execute } from './execution/execute'; +import type { ObjMap } from './jsutils/ObjMap'; +import type { Source } from './language/source'; +import type { GraphQLFieldResolver } from './type/definition'; +import type { GraphQLSchema } from './type/schema'; +import type { ExecutionResult } from './execution/execute'; +import type { MaybePromise } from './jsutils/MaybePromise'; + +/** + * This is the primary entry point function for fulfilling GraphQL operations + * by parsing, validating, and executing a GraphQL document along side a + * GraphQL schema. + * + * More sophisticated GraphQL servers, such as those which persist queries, + * may wish to separate the validation and execution phases to a static time + * tooling step, and a server runtime step. + * + * Accepts either an object with named arguments, or individual arguments: + * + * schema: + * The GraphQL type system to use when validating and executing a query. + * source: + * A GraphQL language formatted string representing the requested operation. + * rootValue: + * The value provided as the first argument to resolver functions on the top + * level type (e.g. the query object type). + * variableValues: + * A mapping of variable name to runtime value to use for all variables + * defined in the requestString. + * operationName: + * The name of the operation to use if requestString contains multiple + * possible operations. Can be omitted if requestString contains only + * one operation. + * fieldResolver: + * A resolver function to use when one is not provided by the schema. + * If not provided, the default field resolver is used (which looks for a + * value or method on the source value with the field's name). + */ +export type GraphQLArgs = {| + schema: GraphQLSchema, + source: string | Source, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +|}; +declare function graphql(GraphQLArgs, ..._: []): Promise; +/* eslint-disable no-redeclare */ +declare function graphql( + schema: GraphQLSchema, + source: Source | string, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +): Promise; +export function graphql( + argsOrSchema, + source, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, +) { + /* eslint-enable no-redeclare */ + // Always return a Promise for a consistent API. + return new Promise(resolve => + resolve( + // Extract arguments from object args if provided. + arguments.length === 1 + ? graphqlImpl( + argsOrSchema.schema, + argsOrSchema.source, + argsOrSchema.rootValue, + argsOrSchema.contextValue, + argsOrSchema.variableValues, + argsOrSchema.operationName, + argsOrSchema.fieldResolver, + ) + : graphqlImpl( + argsOrSchema, + source, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ), + ), + ); +} + +/** + * The graphqlSync function also fulfills GraphQL operations by parsing, + * validating, and executing a GraphQL document along side a GraphQL schema. + * However, it guarantees to complete synchronously (or throw an error) assuming + * that all field resolvers are also synchronous. + */ +declare function graphqlSync(GraphQLArgs, ..._: []): ExecutionResult; +/* eslint-disable no-redeclare */ +declare function graphqlSync( + schema: GraphQLSchema, + source: Source | string, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +): ExecutionResult; +export function graphqlSync( + argsOrSchema, + source, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, +) { + // Extract arguments from object args if provided. + const result = + arguments.length === 1 + ? graphqlImpl( + argsOrSchema.schema, + argsOrSchema.source, + argsOrSchema.rootValue, + argsOrSchema.contextValue, + argsOrSchema.variableValues, + argsOrSchema.operationName, + argsOrSchema.fieldResolver, + ) + : graphqlImpl( + argsOrSchema, + source, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ); + + // Assert that the execution was synchronous. + if (result.then) { + throw new Error('GraphQL execution failed to complete synchronously.'); + } + + return result; +} + +function graphqlImpl( + schema, + source, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, +): MaybePromise { + // Validate Schema + const schemaValidationErrors = validateSchema(schema); + if (schemaValidationErrors.length > 0) { + return { errors: schemaValidationErrors }; + } + + // Parse + let document; + try { + document = parse(source); + } catch (syntaxError) { + return { errors: [syntaxError] }; + } + + // Validate + const validationErrors = validate(schema, document); + if (validationErrors.length > 0) { + return { errors: validationErrors }; + } + + // Execute + return execute( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ); +} diff --git a/dist/graphql.mjs b/dist/graphql.mjs new file mode 100644 index 0000000000..0e75166691 --- /dev/null +++ b/dist/graphql.mjs @@ -0,0 +1,73 @@ +/** + * 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. + * + * strict + */ +import { validateSchema } from './type/validate'; +import { parse } from './language/parser'; +import { validate } from './validation/validate'; +import { execute } from './execution/execute'; +export function graphql(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + var _arguments = arguments; + + /* eslint-enable no-redeclare */ + // Always return a Promise for a consistent API. + return new Promise(function (resolve) { + return resolve( // Extract arguments from object args if provided. + _arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver)); + }); +} +/** + * The graphqlSync function also fulfills GraphQL operations by parsing, + * validating, and executing a GraphQL document along side a GraphQL schema. + * However, it guarantees to complete synchronously (or throw an error) assuming + * that all field resolvers are also synchronous. + */ + +export function graphqlSync(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // Extract arguments from object args if provided. + var result = arguments.length === 1 ? graphqlImpl(argsOrSchema.schema, argsOrSchema.source, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver) : graphqlImpl(argsOrSchema, source, rootValue, contextValue, variableValues, operationName, fieldResolver); // Assert that the execution was synchronous. + + if (result.then) { + throw new Error('GraphQL execution failed to complete synchronously.'); + } + + return result; +} + +function graphqlImpl(schema, source, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // Validate Schema + var schemaValidationErrors = validateSchema(schema); + + if (schemaValidationErrors.length > 0) { + return { + errors: schemaValidationErrors + }; + } // Parse + + + var document; + + try { + document = parse(source); + } catch (syntaxError) { + return { + errors: [syntaxError] + }; + } // Validate + + + var validationErrors = validate(schema, document); + + if (validationErrors.length > 0) { + return { + errors: validationErrors + }; + } // Execute + + + return execute(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); +} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000000..94a30f3adb --- /dev/null +++ b/dist/index.js @@ -0,0 +1,1005 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "graphql", { + enumerable: true, + get: function get() { + return _graphql.graphql; + } +}); +Object.defineProperty(exports, "graphqlSync", { + enumerable: true, + get: function get() { + return _graphql.graphqlSync; + } +}); +Object.defineProperty(exports, "GraphQLSchema", { + enumerable: true, + get: function get() { + return _type.GraphQLSchema; + } +}); +Object.defineProperty(exports, "GraphQLScalarType", { + enumerable: true, + get: function get() { + return _type.GraphQLScalarType; + } +}); +Object.defineProperty(exports, "GraphQLObjectType", { + enumerable: true, + get: function get() { + return _type.GraphQLObjectType; + } +}); +Object.defineProperty(exports, "GraphQLInterfaceType", { + enumerable: true, + get: function get() { + return _type.GraphQLInterfaceType; + } +}); +Object.defineProperty(exports, "GraphQLUnionType", { + enumerable: true, + get: function get() { + return _type.GraphQLUnionType; + } +}); +Object.defineProperty(exports, "GraphQLEnumType", { + enumerable: true, + get: function get() { + return _type.GraphQLEnumType; + } +}); +Object.defineProperty(exports, "GraphQLInputObjectType", { + enumerable: true, + get: function get() { + return _type.GraphQLInputObjectType; + } +}); +Object.defineProperty(exports, "GraphQLList", { + enumerable: true, + get: function get() { + return _type.GraphQLList; + } +}); +Object.defineProperty(exports, "GraphQLNonNull", { + enumerable: true, + get: function get() { + return _type.GraphQLNonNull; + } +}); +Object.defineProperty(exports, "GraphQLDirective", { + enumerable: true, + get: function get() { + return _type.GraphQLDirective; + } +}); +Object.defineProperty(exports, "TypeKind", { + enumerable: true, + get: function get() { + return _type.TypeKind; + } +}); +Object.defineProperty(exports, "specifiedScalarTypes", { + enumerable: true, + get: function get() { + return _type.specifiedScalarTypes; + } +}); +Object.defineProperty(exports, "GraphQLInt", { + enumerable: true, + get: function get() { + return _type.GraphQLInt; + } +}); +Object.defineProperty(exports, "GraphQLFloat", { + enumerable: true, + get: function get() { + return _type.GraphQLFloat; + } +}); +Object.defineProperty(exports, "GraphQLString", { + enumerable: true, + get: function get() { + return _type.GraphQLString; + } +}); +Object.defineProperty(exports, "GraphQLBoolean", { + enumerable: true, + get: function get() { + return _type.GraphQLBoolean; + } +}); +Object.defineProperty(exports, "GraphQLID", { + enumerable: true, + get: function get() { + return _type.GraphQLID; + } +}); +Object.defineProperty(exports, "specifiedDirectives", { + enumerable: true, + get: function get() { + return _type.specifiedDirectives; + } +}); +Object.defineProperty(exports, "GraphQLIncludeDirective", { + enumerable: true, + get: function get() { + return _type.GraphQLIncludeDirective; + } +}); +Object.defineProperty(exports, "GraphQLSkipDirective", { + enumerable: true, + get: function get() { + return _type.GraphQLSkipDirective; + } +}); +Object.defineProperty(exports, "GraphQLDeprecatedDirective", { + enumerable: true, + get: function get() { + return _type.GraphQLDeprecatedDirective; + } +}); +Object.defineProperty(exports, "DEFAULT_DEPRECATION_REASON", { + enumerable: true, + get: function get() { + return _type.DEFAULT_DEPRECATION_REASON; + } +}); +Object.defineProperty(exports, "SchemaMetaFieldDef", { + enumerable: true, + get: function get() { + return _type.SchemaMetaFieldDef; + } +}); +Object.defineProperty(exports, "TypeMetaFieldDef", { + enumerable: true, + get: function get() { + return _type.TypeMetaFieldDef; + } +}); +Object.defineProperty(exports, "TypeNameMetaFieldDef", { + enumerable: true, + get: function get() { + return _type.TypeNameMetaFieldDef; + } +}); +Object.defineProperty(exports, "introspectionTypes", { + enumerable: true, + get: function get() { + return _type.introspectionTypes; + } +}); +Object.defineProperty(exports, "__Schema", { + enumerable: true, + get: function get() { + return _type.__Schema; + } +}); +Object.defineProperty(exports, "__Directive", { + enumerable: true, + get: function get() { + return _type.__Directive; + } +}); +Object.defineProperty(exports, "__DirectiveLocation", { + enumerable: true, + get: function get() { + return _type.__DirectiveLocation; + } +}); +Object.defineProperty(exports, "__Type", { + enumerable: true, + get: function get() { + return _type.__Type; + } +}); +Object.defineProperty(exports, "__Field", { + enumerable: true, + get: function get() { + return _type.__Field; + } +}); +Object.defineProperty(exports, "__InputValue", { + enumerable: true, + get: function get() { + return _type.__InputValue; + } +}); +Object.defineProperty(exports, "__EnumValue", { + enumerable: true, + get: function get() { + return _type.__EnumValue; + } +}); +Object.defineProperty(exports, "__TypeKind", { + enumerable: true, + get: function get() { + return _type.__TypeKind; + } +}); +Object.defineProperty(exports, "isSchema", { + enumerable: true, + get: function get() { + return _type.isSchema; + } +}); +Object.defineProperty(exports, "isDirective", { + enumerable: true, + get: function get() { + return _type.isDirective; + } +}); +Object.defineProperty(exports, "isType", { + enumerable: true, + get: function get() { + return _type.isType; + } +}); +Object.defineProperty(exports, "isScalarType", { + enumerable: true, + get: function get() { + return _type.isScalarType; + } +}); +Object.defineProperty(exports, "isObjectType", { + enumerable: true, + get: function get() { + return _type.isObjectType; + } +}); +Object.defineProperty(exports, "isInterfaceType", { + enumerable: true, + get: function get() { + return _type.isInterfaceType; + } +}); +Object.defineProperty(exports, "isUnionType", { + enumerable: true, + get: function get() { + return _type.isUnionType; + } +}); +Object.defineProperty(exports, "isEnumType", { + enumerable: true, + get: function get() { + return _type.isEnumType; + } +}); +Object.defineProperty(exports, "isInputObjectType", { + enumerable: true, + get: function get() { + return _type.isInputObjectType; + } +}); +Object.defineProperty(exports, "isListType", { + enumerable: true, + get: function get() { + return _type.isListType; + } +}); +Object.defineProperty(exports, "isNonNullType", { + enumerable: true, + get: function get() { + return _type.isNonNullType; + } +}); +Object.defineProperty(exports, "isInputType", { + enumerable: true, + get: function get() { + return _type.isInputType; + } +}); +Object.defineProperty(exports, "isOutputType", { + enumerable: true, + get: function get() { + return _type.isOutputType; + } +}); +Object.defineProperty(exports, "isLeafType", { + enumerable: true, + get: function get() { + return _type.isLeafType; + } +}); +Object.defineProperty(exports, "isCompositeType", { + enumerable: true, + get: function get() { + return _type.isCompositeType; + } +}); +Object.defineProperty(exports, "isAbstractType", { + enumerable: true, + get: function get() { + return _type.isAbstractType; + } +}); +Object.defineProperty(exports, "isWrappingType", { + enumerable: true, + get: function get() { + return _type.isWrappingType; + } +}); +Object.defineProperty(exports, "isNullableType", { + enumerable: true, + get: function get() { + return _type.isNullableType; + } +}); +Object.defineProperty(exports, "isNamedType", { + enumerable: true, + get: function get() { + return _type.isNamedType; + } +}); +Object.defineProperty(exports, "isSpecifiedScalarType", { + enumerable: true, + get: function get() { + return _type.isSpecifiedScalarType; + } +}); +Object.defineProperty(exports, "isIntrospectionType", { + enumerable: true, + get: function get() { + return _type.isIntrospectionType; + } +}); +Object.defineProperty(exports, "isSpecifiedDirective", { + enumerable: true, + get: function get() { + return _type.isSpecifiedDirective; + } +}); +Object.defineProperty(exports, "assertType", { + enumerable: true, + get: function get() { + return _type.assertType; + } +}); +Object.defineProperty(exports, "assertScalarType", { + enumerable: true, + get: function get() { + return _type.assertScalarType; + } +}); +Object.defineProperty(exports, "assertObjectType", { + enumerable: true, + get: function get() { + return _type.assertObjectType; + } +}); +Object.defineProperty(exports, "assertInterfaceType", { + enumerable: true, + get: function get() { + return _type.assertInterfaceType; + } +}); +Object.defineProperty(exports, "assertUnionType", { + enumerable: true, + get: function get() { + return _type.assertUnionType; + } +}); +Object.defineProperty(exports, "assertEnumType", { + enumerable: true, + get: function get() { + return _type.assertEnumType; + } +}); +Object.defineProperty(exports, "assertInputObjectType", { + enumerable: true, + get: function get() { + return _type.assertInputObjectType; + } +}); +Object.defineProperty(exports, "assertListType", { + enumerable: true, + get: function get() { + return _type.assertListType; + } +}); +Object.defineProperty(exports, "assertNonNullType", { + enumerable: true, + get: function get() { + return _type.assertNonNullType; + } +}); +Object.defineProperty(exports, "assertInputType", { + enumerable: true, + get: function get() { + return _type.assertInputType; + } +}); +Object.defineProperty(exports, "assertOutputType", { + enumerable: true, + get: function get() { + return _type.assertOutputType; + } +}); +Object.defineProperty(exports, "assertLeafType", { + enumerable: true, + get: function get() { + return _type.assertLeafType; + } +}); +Object.defineProperty(exports, "assertCompositeType", { + enumerable: true, + get: function get() { + return _type.assertCompositeType; + } +}); +Object.defineProperty(exports, "assertAbstractType", { + enumerable: true, + get: function get() { + return _type.assertAbstractType; + } +}); +Object.defineProperty(exports, "assertWrappingType", { + enumerable: true, + get: function get() { + return _type.assertWrappingType; + } +}); +Object.defineProperty(exports, "assertNullableType", { + enumerable: true, + get: function get() { + return _type.assertNullableType; + } +}); +Object.defineProperty(exports, "assertNamedType", { + enumerable: true, + get: function get() { + return _type.assertNamedType; + } +}); +Object.defineProperty(exports, "getNullableType", { + enumerable: true, + get: function get() { + return _type.getNullableType; + } +}); +Object.defineProperty(exports, "getNamedType", { + enumerable: true, + get: function get() { + return _type.getNamedType; + } +}); +Object.defineProperty(exports, "validateSchema", { + enumerable: true, + get: function get() { + return _type.validateSchema; + } +}); +Object.defineProperty(exports, "assertValidSchema", { + enumerable: true, + get: function get() { + return _type.assertValidSchema; + } +}); +Object.defineProperty(exports, "Source", { + enumerable: true, + get: function get() { + return _language.Source; + } +}); +Object.defineProperty(exports, "getLocation", { + enumerable: true, + get: function get() { + return _language.getLocation; + } +}); +Object.defineProperty(exports, "parse", { + enumerable: true, + get: function get() { + return _language.parse; + } +}); +Object.defineProperty(exports, "parseValue", { + enumerable: true, + get: function get() { + return _language.parseValue; + } +}); +Object.defineProperty(exports, "parseType", { + enumerable: true, + get: function get() { + return _language.parseType; + } +}); +Object.defineProperty(exports, "print", { + enumerable: true, + get: function get() { + return _language.print; + } +}); +Object.defineProperty(exports, "visit", { + enumerable: true, + get: function get() { + return _language.visit; + } +}); +Object.defineProperty(exports, "visitInParallel", { + enumerable: true, + get: function get() { + return _language.visitInParallel; + } +}); +Object.defineProperty(exports, "visitWithTypeInfo", { + enumerable: true, + get: function get() { + return _language.visitWithTypeInfo; + } +}); +Object.defineProperty(exports, "getVisitFn", { + enumerable: true, + get: function get() { + return _language.getVisitFn; + } +}); +Object.defineProperty(exports, "Kind", { + enumerable: true, + get: function get() { + return _language.Kind; + } +}); +Object.defineProperty(exports, "TokenKind", { + enumerable: true, + get: function get() { + return _language.TokenKind; + } +}); +Object.defineProperty(exports, "DirectiveLocation", { + enumerable: true, + get: function get() { + return _language.DirectiveLocation; + } +}); +Object.defineProperty(exports, "BREAK", { + enumerable: true, + get: function get() { + return _language.BREAK; + } +}); +Object.defineProperty(exports, "execute", { + enumerable: true, + get: function get() { + return _execution.execute; + } +}); +Object.defineProperty(exports, "defaultFieldResolver", { + enumerable: true, + get: function get() { + return _execution.defaultFieldResolver; + } +}); +Object.defineProperty(exports, "responsePathAsArray", { + enumerable: true, + get: function get() { + return _execution.responsePathAsArray; + } +}); +Object.defineProperty(exports, "getDirectiveValues", { + enumerable: true, + get: function get() { + return _execution.getDirectiveValues; + } +}); +Object.defineProperty(exports, "subscribe", { + enumerable: true, + get: function get() { + return _subscription.subscribe; + } +}); +Object.defineProperty(exports, "createSourceEventStream", { + enumerable: true, + get: function get() { + return _subscription.createSourceEventStream; + } +}); +Object.defineProperty(exports, "validate", { + enumerable: true, + get: function get() { + return _validation.validate; + } +}); +Object.defineProperty(exports, "ValidationContext", { + enumerable: true, + get: function get() { + return _validation.ValidationContext; + } +}); +Object.defineProperty(exports, "specifiedRules", { + enumerable: true, + get: function get() { + return _validation.specifiedRules; + } +}); +Object.defineProperty(exports, "FieldsOnCorrectTypeRule", { + enumerable: true, + get: function get() { + return _validation.FieldsOnCorrectTypeRule; + } +}); +Object.defineProperty(exports, "FragmentsOnCompositeTypesRule", { + enumerable: true, + get: function get() { + return _validation.FragmentsOnCompositeTypesRule; + } +}); +Object.defineProperty(exports, "KnownArgumentNamesRule", { + enumerable: true, + get: function get() { + return _validation.KnownArgumentNamesRule; + } +}); +Object.defineProperty(exports, "KnownDirectivesRule", { + enumerable: true, + get: function get() { + return _validation.KnownDirectivesRule; + } +}); +Object.defineProperty(exports, "KnownFragmentNamesRule", { + enumerable: true, + get: function get() { + return _validation.KnownFragmentNamesRule; + } +}); +Object.defineProperty(exports, "KnownTypeNamesRule", { + enumerable: true, + get: function get() { + return _validation.KnownTypeNamesRule; + } +}); +Object.defineProperty(exports, "LoneAnonymousOperationRule", { + enumerable: true, + get: function get() { + return _validation.LoneAnonymousOperationRule; + } +}); +Object.defineProperty(exports, "NoFragmentCyclesRule", { + enumerable: true, + get: function get() { + return _validation.NoFragmentCyclesRule; + } +}); +Object.defineProperty(exports, "NoUndefinedVariablesRule", { + enumerable: true, + get: function get() { + return _validation.NoUndefinedVariablesRule; + } +}); +Object.defineProperty(exports, "NoUnusedFragmentsRule", { + enumerable: true, + get: function get() { + return _validation.NoUnusedFragmentsRule; + } +}); +Object.defineProperty(exports, "NoUnusedVariablesRule", { + enumerable: true, + get: function get() { + return _validation.NoUnusedVariablesRule; + } +}); +Object.defineProperty(exports, "OverlappingFieldsCanBeMergedRule", { + enumerable: true, + get: function get() { + return _validation.OverlappingFieldsCanBeMergedRule; + } +}); +Object.defineProperty(exports, "PossibleFragmentSpreadsRule", { + enumerable: true, + get: function get() { + return _validation.PossibleFragmentSpreadsRule; + } +}); +Object.defineProperty(exports, "ProvidedRequiredArgumentsRule", { + enumerable: true, + get: function get() { + return _validation.ProvidedRequiredArgumentsRule; + } +}); +Object.defineProperty(exports, "ScalarLeafsRule", { + enumerable: true, + get: function get() { + return _validation.ScalarLeafsRule; + } +}); +Object.defineProperty(exports, "SingleFieldSubscriptionsRule", { + enumerable: true, + get: function get() { + return _validation.SingleFieldSubscriptionsRule; + } +}); +Object.defineProperty(exports, "UniqueArgumentNamesRule", { + enumerable: true, + get: function get() { + return _validation.UniqueArgumentNamesRule; + } +}); +Object.defineProperty(exports, "UniqueDirectivesPerLocationRule", { + enumerable: true, + get: function get() { + return _validation.UniqueDirectivesPerLocationRule; + } +}); +Object.defineProperty(exports, "UniqueFragmentNamesRule", { + enumerable: true, + get: function get() { + return _validation.UniqueFragmentNamesRule; + } +}); +Object.defineProperty(exports, "UniqueInputFieldNamesRule", { + enumerable: true, + get: function get() { + return _validation.UniqueInputFieldNamesRule; + } +}); +Object.defineProperty(exports, "UniqueOperationNamesRule", { + enumerable: true, + get: function get() { + return _validation.UniqueOperationNamesRule; + } +}); +Object.defineProperty(exports, "UniqueVariableNamesRule", { + enumerable: true, + get: function get() { + return _validation.UniqueVariableNamesRule; + } +}); +Object.defineProperty(exports, "ValuesOfCorrectTypeRule", { + enumerable: true, + get: function get() { + return _validation.ValuesOfCorrectTypeRule; + } +}); +Object.defineProperty(exports, "VariablesAreInputTypesRule", { + enumerable: true, + get: function get() { + return _validation.VariablesAreInputTypesRule; + } +}); +Object.defineProperty(exports, "VariablesInAllowedPositionRule", { + enumerable: true, + get: function get() { + return _validation.VariablesInAllowedPositionRule; + } +}); +Object.defineProperty(exports, "GraphQLError", { + enumerable: true, + get: function get() { + return _error.GraphQLError; + } +}); +Object.defineProperty(exports, "formatError", { + enumerable: true, + get: function get() { + return _error.formatError; + } +}); +Object.defineProperty(exports, "printError", { + enumerable: true, + get: function get() { + return _error.printError; + } +}); +Object.defineProperty(exports, "getIntrospectionQuery", { + enumerable: true, + get: function get() { + return _utilities.getIntrospectionQuery; + } +}); +Object.defineProperty(exports, "introspectionQuery", { + enumerable: true, + get: function get() { + return _utilities.introspectionQuery; + } +}); +Object.defineProperty(exports, "getOperationAST", { + enumerable: true, + get: function get() { + return _utilities.getOperationAST; + } +}); +Object.defineProperty(exports, "getOperationRootType", { + enumerable: true, + get: function get() { + return _utilities.getOperationRootType; + } +}); +Object.defineProperty(exports, "introspectionFromSchema", { + enumerable: true, + get: function get() { + return _utilities.introspectionFromSchema; + } +}); +Object.defineProperty(exports, "buildClientSchema", { + enumerable: true, + get: function get() { + return _utilities.buildClientSchema; + } +}); +Object.defineProperty(exports, "buildASTSchema", { + enumerable: true, + get: function get() { + return _utilities.buildASTSchema; + } +}); +Object.defineProperty(exports, "buildSchema", { + enumerable: true, + get: function get() { + return _utilities.buildSchema; + } +}); +Object.defineProperty(exports, "getDescription", { + enumerable: true, + get: function get() { + return _utilities.getDescription; + } +}); +Object.defineProperty(exports, "extendSchema", { + enumerable: true, + get: function get() { + return _utilities.extendSchema; + } +}); +Object.defineProperty(exports, "lexicographicSortSchema", { + enumerable: true, + get: function get() { + return _utilities.lexicographicSortSchema; + } +}); +Object.defineProperty(exports, "printSchema", { + enumerable: true, + get: function get() { + return _utilities.printSchema; + } +}); +Object.defineProperty(exports, "printIntrospectionSchema", { + enumerable: true, + get: function get() { + return _utilities.printIntrospectionSchema; + } +}); +Object.defineProperty(exports, "printType", { + enumerable: true, + get: function get() { + return _utilities.printType; + } +}); +Object.defineProperty(exports, "typeFromAST", { + enumerable: true, + get: function get() { + return _utilities.typeFromAST; + } +}); +Object.defineProperty(exports, "valueFromAST", { + enumerable: true, + get: function get() { + return _utilities.valueFromAST; + } +}); +Object.defineProperty(exports, "valueFromASTUntyped", { + enumerable: true, + get: function get() { + return _utilities.valueFromASTUntyped; + } +}); +Object.defineProperty(exports, "astFromValue", { + enumerable: true, + get: function get() { + return _utilities.astFromValue; + } +}); +Object.defineProperty(exports, "TypeInfo", { + enumerable: true, + get: function get() { + return _utilities.TypeInfo; + } +}); +Object.defineProperty(exports, "coerceValue", { + enumerable: true, + get: function get() { + return _utilities.coerceValue; + } +}); +Object.defineProperty(exports, "isValidJSValue", { + enumerable: true, + get: function get() { + return _utilities.isValidJSValue; + } +}); +Object.defineProperty(exports, "isValidLiteralValue", { + enumerable: true, + get: function get() { + return _utilities.isValidLiteralValue; + } +}); +Object.defineProperty(exports, "concatAST", { + enumerable: true, + get: function get() { + return _utilities.concatAST; + } +}); +Object.defineProperty(exports, "separateOperations", { + enumerable: true, + get: function get() { + return _utilities.separateOperations; + } +}); +Object.defineProperty(exports, "isEqualType", { + enumerable: true, + get: function get() { + return _utilities.isEqualType; + } +}); +Object.defineProperty(exports, "isTypeSubTypeOf", { + enumerable: true, + get: function get() { + return _utilities.isTypeSubTypeOf; + } +}); +Object.defineProperty(exports, "doTypesOverlap", { + enumerable: true, + get: function get() { + return _utilities.doTypesOverlap; + } +}); +Object.defineProperty(exports, "assertValidName", { + enumerable: true, + get: function get() { + return _utilities.assertValidName; + } +}); +Object.defineProperty(exports, "isValidNameError", { + enumerable: true, + get: function get() { + return _utilities.isValidNameError; + } +}); +Object.defineProperty(exports, "findBreakingChanges", { + enumerable: true, + get: function get() { + return _utilities.findBreakingChanges; + } +}); +Object.defineProperty(exports, "findDangerousChanges", { + enumerable: true, + get: function get() { + return _utilities.findDangerousChanges; + } +}); +Object.defineProperty(exports, "BreakingChangeType", { + enumerable: true, + get: function get() { + return _utilities.BreakingChangeType; + } +}); +Object.defineProperty(exports, "DangerousChangeType", { + enumerable: true, + get: function get() { + return _utilities.DangerousChangeType; + } +}); +Object.defineProperty(exports, "findDeprecatedUsages", { + enumerable: true, + get: function get() { + return _utilities.findDeprecatedUsages; + } +}); + +var _graphql = require("./graphql"); + +var _type = require("./type"); + +var _language = require("./language"); + +var _execution = require("./execution"); + +var _subscription = require("./subscription"); + +var _validation = require("./validation"); + +var _error = require("./error"); + +var _utilities = require("./utilities"); \ No newline at end of file diff --git a/dist/index.js.flow b/dist/index.js.flow new file mode 100644 index 0000000000..697734cf47 --- /dev/null +++ b/dist/index.js.flow @@ -0,0 +1,411 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * GraphQL.js provides a reference implementation for the GraphQL specification + * but is also a useful utility for operating on GraphQL files and building + * sophisticated tools. + * + * This primary module exports a general purpose function for fulfilling all + * steps of the GraphQL specification in a single operation, but also includes + * utilities for every part of the GraphQL specification: + * + * - Parsing the GraphQL language. + * - Building a GraphQL type schema. + * - Validating a GraphQL request against a type schema. + * - Executing a GraphQL request against a type schema. + * + * This also includes utility functions for operating on GraphQL types and + * GraphQL documents to facilitate building tools. + * + * You may also import from each sub-directory directly. For example, the + * following two import statements are equivalent: + * + * import { parse } from 'graphql'; + * import { parse } from 'graphql/language'; + */ + +// The primary entry point into fulfilling a GraphQL request. +export type { GraphQLArgs } from './graphql'; +export { graphql, graphqlSync } from './graphql'; + +// Create and operate on GraphQL type definitions and schema. +export { + GraphQLSchema, + // Definitions + GraphQLScalarType, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + GraphQLDirective, + // "Enum" of Type Kinds + TypeKind, + // Scalars + specifiedScalarTypes, + GraphQLInt, + GraphQLFloat, + GraphQLString, + GraphQLBoolean, + GraphQLID, + // Built-in Directives defined by the Spec + specifiedDirectives, + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, + // Constant Deprecation Reason + DEFAULT_DEPRECATION_REASON, + // Meta-field definitions. + SchemaMetaFieldDef, + TypeMetaFieldDef, + TypeNameMetaFieldDef, + // GraphQL Types for introspection. + introspectionTypes, + __Schema, + __Directive, + __DirectiveLocation, + __Type, + __Field, + __InputValue, + __EnumValue, + __TypeKind, + // Predicates + isSchema, + isDirective, + isType, + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, + isInputType, + isOutputType, + isLeafType, + isCompositeType, + isAbstractType, + isWrappingType, + isNullableType, + isNamedType, + isSpecifiedScalarType, + isIntrospectionType, + isSpecifiedDirective, + // Assertions + assertType, + assertScalarType, + assertObjectType, + assertInterfaceType, + assertUnionType, + assertEnumType, + assertInputObjectType, + assertListType, + assertNonNullType, + assertInputType, + assertOutputType, + assertLeafType, + assertCompositeType, + assertAbstractType, + assertWrappingType, + assertNullableType, + assertNamedType, + // Un-modifiers + getNullableType, + getNamedType, + // Validate GraphQL schema. + validateSchema, + assertValidSchema, +} from './type'; + +export type { + GraphQLType, + GraphQLInputType, + GraphQLOutputType, + GraphQLLeafType, + GraphQLCompositeType, + GraphQLAbstractType, + GraphQLWrappingType, + GraphQLNullableType, + GraphQLNamedType, + Thunk, + GraphQLSchemaConfig, + GraphQLArgument, + GraphQLArgumentConfig, + GraphQLEnumTypeConfig, + GraphQLEnumValue, + GraphQLEnumValueConfig, + GraphQLEnumValueConfigMap, + GraphQLField, + GraphQLFieldConfig, + GraphQLFieldConfigArgumentMap, + GraphQLFieldConfigMap, + GraphQLFieldMap, + GraphQLFieldResolver, + GraphQLInputField, + GraphQLInputFieldConfig, + GraphQLInputFieldConfigMap, + GraphQLInputFieldMap, + GraphQLInputObjectTypeConfig, + GraphQLInterfaceTypeConfig, + GraphQLIsTypeOfFn, + GraphQLObjectTypeConfig, + GraphQLResolveInfo, + ResponsePath, + GraphQLScalarTypeConfig, + GraphQLTypeResolver, + GraphQLUnionTypeConfig, + GraphQLDirectiveConfig, +} from './type'; + +// Parse and operate on GraphQL language source files. +export { + Source, + getLocation, + // Parse + parse, + parseValue, + parseType, + // Print + print, + // Visit + visit, + visitInParallel, + visitWithTypeInfo, + getVisitFn, + Kind, + TokenKind, + DirectiveLocation, + BREAK, +} from './language'; + +export type { + Lexer, + ParseOptions, + SourceLocation, + // Visitor utilities + ASTVisitor, + Visitor, + VisitFn, + VisitorKeyMap, + // AST nodes + Location, + Token, + ASTNode, + ASTKindToNode, + NameNode, + DocumentNode, + DefinitionNode, + ExecutableDefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + VariableNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + ValueNode, + IntValueNode, + FloatValueNode, + StringValueNode, + BooleanValueNode, + NullValueNode, + EnumValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + TypeSystemDefinitionNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, + TypeSystemExtensionNode, + SchemaExtensionNode, + TypeExtensionNode, + ScalarTypeExtensionNode, + ObjectTypeExtensionNode, + InterfaceTypeExtensionNode, + UnionTypeExtensionNode, + EnumTypeExtensionNode, + InputObjectTypeExtensionNode, + KindEnum, + TokenKindEnum, + DirectiveLocationEnum, +} from './language'; + +// Execute GraphQL queries. +export { + execute, + defaultFieldResolver, + responsePathAsArray, + getDirectiveValues, +} from './execution'; + +export type { ExecutionArgs, ExecutionResult } from './execution'; + +export { subscribe, createSourceEventStream } from './subscription'; + +// Validate GraphQL queries. +export { + validate, + ValidationContext, + // All validation rules in the GraphQL Specification. + specifiedRules, + // Individual validation rules. + FieldsOnCorrectTypeRule, + FragmentsOnCompositeTypesRule, + KnownArgumentNamesRule, + KnownDirectivesRule, + KnownFragmentNamesRule, + KnownTypeNamesRule, + LoneAnonymousOperationRule, + NoFragmentCyclesRule, + NoUndefinedVariablesRule, + NoUnusedFragmentsRule, + NoUnusedVariablesRule, + OverlappingFieldsCanBeMergedRule, + PossibleFragmentSpreadsRule, + ProvidedRequiredArgumentsRule, + ScalarLeafsRule, + SingleFieldSubscriptionsRule, + UniqueArgumentNamesRule, + UniqueDirectivesPerLocationRule, + UniqueFragmentNamesRule, + UniqueInputFieldNamesRule, + UniqueOperationNamesRule, + UniqueVariableNamesRule, + ValuesOfCorrectTypeRule, + VariablesAreInputTypesRule, + VariablesInAllowedPositionRule, +} from './validation'; + +// Create, format, and print GraphQL errors. +export { GraphQLError, formatError, printError } from './error'; + +export type { GraphQLFormattedError } from './error'; + +// Utilities for operating on GraphQL type schema and parsed sources. +export { + // Produce the GraphQL query recommended for a full schema introspection. + // Accepts optional IntrospectionOptions. + getIntrospectionQuery, + // Deprecated: use getIntrospectionQuery + introspectionQuery, + // Gets the target Operation from a Document + getOperationAST, + // Gets the Type for the target Operation AST. + getOperationRootType, + // Convert a GraphQLSchema to an IntrospectionQuery + introspectionFromSchema, + // Build a GraphQLSchema from an introspection result. + buildClientSchema, + // Build a GraphQLSchema from a parsed GraphQL Schema language AST. + buildASTSchema, + // Build a GraphQLSchema from a GraphQL schema language document. + buildSchema, + // Get the description from a schema AST node. + getDescription, + // Extends an existing GraphQLSchema from a parsed GraphQL Schema + // language AST. + extendSchema, + // Sort a GraphQLSchema. + lexicographicSortSchema, + // Print a GraphQLSchema to GraphQL Schema language. + printSchema, + // Prints the built-in introspection schema in the Schema Language + // format. + printIntrospectionSchema, + // Print a GraphQLType to GraphQL Schema language. + printType, + // Create a GraphQLType from a GraphQL language AST. + typeFromAST, + // Create a JavaScript value from a GraphQL language AST with a Type. + valueFromAST, + // Create a JavaScript value from a GraphQL language AST without a Type. + valueFromASTUntyped, + // Create a GraphQL language AST from a JavaScript value. + astFromValue, + // A helper to use within recursive-descent visitors which need to be aware of + // the GraphQL type system. + TypeInfo, + // Coerces a JavaScript value to a GraphQL type, or produces errors. + coerceValue, + // @deprecated use coerceValue + isValidJSValue, + // Determine if AST values adhere to a GraphQL type. + isValidLiteralValue, + // Concatenates multiple AST together. + concatAST, + // Separates an AST into an AST per Operation. + separateOperations, + // Comparators for types + isEqualType, + isTypeSubTypeOf, + doTypesOverlap, + // Asserts a string is a valid GraphQL name. + assertValidName, + // Determine if a string is a valid GraphQL name. + isValidNameError, + // Compares two GraphQLSchemas and detects breaking changes. + findBreakingChanges, + findDangerousChanges, + BreakingChangeType, + DangerousChangeType, + // Report all deprecated usage within a GraphQL document. + findDeprecatedUsages, +} from './utilities'; + +export type { + BuildSchemaOptions, + BreakingChange, + DangerousChange, + IntrospectionOptions, + IntrospectionDirective, + IntrospectionEnumType, + IntrospectionEnumValue, + IntrospectionField, + IntrospectionInputObjectType, + IntrospectionInputType, + IntrospectionInputTypeRef, + IntrospectionInputValue, + IntrospectionInterfaceType, + IntrospectionListTypeRef, + IntrospectionNamedTypeRef, + IntrospectionNonNullTypeRef, + IntrospectionObjectType, + IntrospectionOutputType, + IntrospectionOutputTypeRef, + IntrospectionQuery, + IntrospectionScalarType, + IntrospectionSchema, + IntrospectionType, + IntrospectionTypeRef, + IntrospectionUnionType, +} from './utilities'; diff --git a/dist/index.mjs b/dist/index.mjs new file mode 100644 index 0000000000..9088204f24 --- /dev/null +++ b/dist/index.mjs @@ -0,0 +1,96 @@ +/** + * 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. + * + * strict + */ + +/** + * GraphQL.js provides a reference implementation for the GraphQL specification + * but is also a useful utility for operating on GraphQL files and building + * sophisticated tools. + * + * This primary module exports a general purpose function for fulfilling all + * steps of the GraphQL specification in a single operation, but also includes + * utilities for every part of the GraphQL specification: + * + * - Parsing the GraphQL language. + * - Building a GraphQL type schema. + * - Validating a GraphQL request against a type schema. + * - Executing a GraphQL request against a type schema. + * + * This also includes utility functions for operating on GraphQL types and + * GraphQL documents to facilitate building tools. + * + * You may also import from each sub-directory directly. For example, the + * following two import statements are equivalent: + * + * import { parse } from 'graphql'; + * import { parse } from 'graphql/language'; + */ +// The primary entry point into fulfilling a GraphQL request. +export { graphql, graphqlSync } from './graphql'; // Create and operate on GraphQL type definitions and schema. + +export { GraphQLSchema, // Definitions +GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, GraphQLDirective, // "Enum" of Type Kinds +TypeKind, // Scalars +specifiedScalarTypes, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean, GraphQLID, // Built-in Directives defined by the Spec +specifiedDirectives, GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective, // Constant Deprecation Reason +DEFAULT_DEPRECATION_REASON, // Meta-field definitions. +SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, // GraphQL Types for introspection. +introspectionTypes, __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue, __EnumValue, __TypeKind, // Predicates +isSchema, isDirective, isType, isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isListType, isNonNullType, isInputType, isOutputType, isLeafType, isCompositeType, isAbstractType, isWrappingType, isNullableType, isNamedType, isSpecifiedScalarType, isIntrospectionType, isSpecifiedDirective, // Assertions +assertType, assertScalarType, assertObjectType, assertInterfaceType, assertUnionType, assertEnumType, assertInputObjectType, assertListType, assertNonNullType, assertInputType, assertOutputType, assertLeafType, assertCompositeType, assertAbstractType, assertWrappingType, assertNullableType, assertNamedType, // Un-modifiers +getNullableType, getNamedType, // Validate GraphQL schema. +validateSchema, assertValidSchema } from './type'; +// Parse and operate on GraphQL language source files. +export { Source, getLocation, // Parse +parse, parseValue, parseType, // Print +print, // Visit +visit, visitInParallel, visitWithTypeInfo, getVisitFn, Kind, TokenKind, DirectiveLocation, BREAK } from './language'; +// Execute GraphQL queries. +export { execute, defaultFieldResolver, responsePathAsArray, getDirectiveValues } from './execution'; +export { subscribe, createSourceEventStream } from './subscription'; // Validate GraphQL queries. + +export { validate, ValidationContext, // All validation rules in the GraphQL Specification. +specifiedRules, // Individual validation rules. +FieldsOnCorrectTypeRule, FragmentsOnCompositeTypesRule, KnownArgumentNamesRule, KnownDirectivesRule, KnownFragmentNamesRule, KnownTypeNamesRule, LoneAnonymousOperationRule, NoFragmentCyclesRule, NoUndefinedVariablesRule, NoUnusedFragmentsRule, NoUnusedVariablesRule, OverlappingFieldsCanBeMergedRule, PossibleFragmentSpreadsRule, ProvidedRequiredArgumentsRule, ScalarLeafsRule, SingleFieldSubscriptionsRule, UniqueArgumentNamesRule, UniqueDirectivesPerLocationRule, UniqueFragmentNamesRule, UniqueInputFieldNamesRule, UniqueOperationNamesRule, UniqueVariableNamesRule, ValuesOfCorrectTypeRule, VariablesAreInputTypesRule, VariablesInAllowedPositionRule } from './validation'; // Create, format, and print GraphQL errors. + +export { GraphQLError, formatError, printError } from './error'; +// Utilities for operating on GraphQL type schema and parsed sources. +export { // Produce the GraphQL query recommended for a full schema introspection. +// Accepts optional IntrospectionOptions. +getIntrospectionQuery, // Deprecated: use getIntrospectionQuery +introspectionQuery, // Gets the target Operation from a Document +getOperationAST, // Gets the Type for the target Operation AST. +getOperationRootType, // Convert a GraphQLSchema to an IntrospectionQuery +introspectionFromSchema, // Build a GraphQLSchema from an introspection result. +buildClientSchema, // Build a GraphQLSchema from a parsed GraphQL Schema language AST. +buildASTSchema, // Build a GraphQLSchema from a GraphQL schema language document. +buildSchema, // Get the description from a schema AST node. +getDescription, // Extends an existing GraphQLSchema from a parsed GraphQL Schema +// language AST. +extendSchema, // Sort a GraphQLSchema. +lexicographicSortSchema, // Print a GraphQLSchema to GraphQL Schema language. +printSchema, // Prints the built-in introspection schema in the Schema Language +// format. +printIntrospectionSchema, // Print a GraphQLType to GraphQL Schema language. +printType, // Create a GraphQLType from a GraphQL language AST. +typeFromAST, // Create a JavaScript value from a GraphQL language AST with a Type. +valueFromAST, // Create a JavaScript value from a GraphQL language AST without a Type. +valueFromASTUntyped, // Create a GraphQL language AST from a JavaScript value. +astFromValue, // A helper to use within recursive-descent visitors which need to be aware of +// the GraphQL type system. +TypeInfo, // Coerces a JavaScript value to a GraphQL type, or produces errors. +coerceValue, // @deprecated use coerceValue +isValidJSValue, // Determine if AST values adhere to a GraphQL type. +isValidLiteralValue, // Concatenates multiple AST together. +concatAST, // Separates an AST into an AST per Operation. +separateOperations, // Comparators for types +isEqualType, isTypeSubTypeOf, doTypesOverlap, // Asserts a string is a valid GraphQL name. +assertValidName, // Determine if a string is a valid GraphQL name. +isValidNameError, // Compares two GraphQLSchemas and detects breaking changes. +findBreakingChanges, findDangerousChanges, BreakingChangeType, DangerousChangeType, // Report all deprecated usage within a GraphQL document. +findDeprecatedUsages } from './utilities'; \ No newline at end of file diff --git a/dist/jsutils/MaybePromise.js b/dist/jsutils/MaybePromise.js new file mode 100644 index 0000000000..9a390c31f7 --- /dev/null +++ b/dist/jsutils/MaybePromise.js @@ -0,0 +1 @@ +"use strict"; \ No newline at end of file diff --git a/dist/jsutils/MaybePromise.js.flow b/dist/jsutils/MaybePromise.js.flow new file mode 100644 index 0000000000..c4b6b6e252 --- /dev/null +++ b/dist/jsutils/MaybePromise.js.flow @@ -0,0 +1,10 @@ +/** + * 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. + * + * @flow strict + */ + +export type MaybePromise<+T> = Promise | T; diff --git a/dist/jsutils/MaybePromise.mjs b/dist/jsutils/MaybePromise.mjs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dist/jsutils/ObjMap.js b/dist/jsutils/ObjMap.js new file mode 100644 index 0000000000..9a390c31f7 --- /dev/null +++ b/dist/jsutils/ObjMap.js @@ -0,0 +1 @@ +"use strict"; \ No newline at end of file diff --git a/dist/jsutils/ObjMap.js.flow b/dist/jsutils/ObjMap.js.flow new file mode 100644 index 0000000000..140e60fec7 --- /dev/null +++ b/dist/jsutils/ObjMap.js.flow @@ -0,0 +1,10 @@ +/** + * 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. + * + * @flow strict + */ + +export type ObjMap = { [key: string]: T, __proto__: null }; diff --git a/dist/jsutils/ObjMap.mjs b/dist/jsutils/ObjMap.mjs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dist/jsutils/dedent.js b/dist/jsutils/dedent.js new file mode 100644 index 0000000000..6dfa875f1f --- /dev/null +++ b/dist/jsutils/dedent.js @@ -0,0 +1,60 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = dedent; + +/** + * Copyright (c) 2017-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. + * + * strict + */ + +/** + * fixes indentation by removing leading spaces and tabs from each line + */ +function fixIndent(str) { + var trimmedStr = str.replace(/^\n*/m, '') // remove leading newline + .replace(/[ \t]*$/, ''); // remove trailing spaces and tabs + + var indent = /^[ \t]*/.exec(trimmedStr)[0]; // figure out indent + + return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent +} +/** + * An ES6 string tag that fixes indentation. Also removes leading newlines + * and trailing spaces and tabs, but keeps trailing newlines. + * + * Example usage: + * const str = dedent` + * { + * test + * } + * ` + * str === "{\n test\n}\n"; + */ + + +function dedent(strings) { + for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + values[_key - 1] = arguments[_key]; + } + + // when used as an ordinary function, allow passing a singleton string + var strArray = typeof strings === 'string' ? [strings] : strings; + var numValues = values.length; + var str = strArray.reduce(function (prev, cur, index) { + var next = prev + cur; + + if (index < numValues) { + next += values[index]; // interpolation + } + + return next; + }, ''); + return fixIndent(str); +} \ No newline at end of file diff --git a/dist/jsutils/dedent.js.flow b/dist/jsutils/dedent.js.flow new file mode 100644 index 0000000000..0239266e94 --- /dev/null +++ b/dist/jsutils/dedent.js.flow @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +/** + * fixes indentation by removing leading spaces and tabs from each line + */ +function fixIndent(str: string): string { + const trimmedStr = str + .replace(/^\n*/m, '') // remove leading newline + .replace(/[ \t]*$/, ''); // remove trailing spaces and tabs + const indent = /^[ \t]*/.exec(trimmedStr)[0]; // figure out indent + return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent +} + +/** + * An ES6 string tag that fixes indentation. Also removes leading newlines + * and trailing spaces and tabs, but keeps trailing newlines. + * + * Example usage: + * const str = dedent` + * { + * test + * } + * ` + * str === "{\n test\n}\n"; + */ +export default function dedent( + strings: string | Array, + ...values: Array +): string { + // when used as an ordinary function, allow passing a singleton string + const strArray = typeof strings === 'string' ? [strings] : strings; + const numValues = values.length; + + const str = strArray.reduce((prev, cur, index) => { + let next = prev + cur; + if (index < numValues) { + next += values[index]; // interpolation + } + return next; + }, ''); + + return fixIndent(str); +} diff --git a/dist/jsutils/dedent.mjs b/dist/jsutils/dedent.mjs new file mode 100644 index 0000000000..ff12dfac7e --- /dev/null +++ b/dist/jsutils/dedent.mjs @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2017-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. + * + * strict + */ + +/** + * fixes indentation by removing leading spaces and tabs from each line + */ +function fixIndent(str) { + var trimmedStr = str.replace(/^\n*/m, '') // remove leading newline + .replace(/[ \t]*$/, ''); // remove trailing spaces and tabs + + var indent = /^[ \t]*/.exec(trimmedStr)[0]; // figure out indent + + return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent +} +/** + * An ES6 string tag that fixes indentation. Also removes leading newlines + * and trailing spaces and tabs, but keeps trailing newlines. + * + * Example usage: + * const str = dedent` + * { + * test + * } + * ` + * str === "{\n test\n}\n"; + */ + + +export default function dedent(strings) { + for (var _len = arguments.length, values = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { + values[_key - 1] = arguments[_key]; + } + + // when used as an ordinary function, allow passing a singleton string + var strArray = typeof strings === 'string' ? [strings] : strings; + var numValues = values.length; + var str = strArray.reduce(function (prev, cur, index) { + var next = prev + cur; + + if (index < numValues) { + next += values[index]; // interpolation + } + + return next; + }, ''); + return fixIndent(str); +} \ No newline at end of file diff --git a/dist/jsutils/find.js b/dist/jsutils/find.js new file mode 100644 index 0000000000..78b08776c3 --- /dev/null +++ b/dist/jsutils/find.js @@ -0,0 +1,22 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = find; + +/** + * 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. + * + * strict + */ +function find(list, predicate) { + for (var i = 0; i < list.length; i++) { + if (predicate(list[i])) { + return list[i]; + } + } +} \ No newline at end of file diff --git a/dist/jsutils/find.js.flow b/dist/jsutils/find.js.flow new file mode 100644 index 0000000000..ba608d90b3 --- /dev/null +++ b/dist/jsutils/find.js.flow @@ -0,0 +1,19 @@ +/** + * 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. + * + * @flow strict + */ + +export default function find( + list: $ReadOnlyArray, + predicate: (item: T) => boolean, +): ?T { + for (let i = 0; i < list.length; i++) { + if (predicate(list[i])) { + return list[i]; + } + } +} diff --git a/dist/jsutils/find.mjs b/dist/jsutils/find.mjs new file mode 100644 index 0000000000..965b53f24d --- /dev/null +++ b/dist/jsutils/find.mjs @@ -0,0 +1,15 @@ +/** + * 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. + * + * strict + */ +export default function find(list, predicate) { + for (var i = 0; i < list.length; i++) { + if (predicate(list[i])) { + return list[i]; + } + } +} \ No newline at end of file diff --git a/dist/jsutils/instanceOf.js b/dist/jsutils/instanceOf.js new file mode 100644 index 0000000000..2b7410d1a4 --- /dev/null +++ b/dist/jsutils/instanceOf.js @@ -0,0 +1,44 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +/** + * 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. + * + * strict + */ + +/** + * A replacement for instanceof which includes an error warning when multi-realm + * constructors are detected. + */ +// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production +// See: https://webpack.js.org/guides/production/ +var _default = process.env.NODE_ENV === 'production' ? // eslint-disable-next-line no-shadow +function instanceOf(value, constructor) { + return value instanceof constructor; +} : // eslint-disable-next-line no-shadow +function instanceOf(value, constructor) { + if (value instanceof constructor) { + return true; + } + + if (value) { + var valueClass = value.constructor; + var className = constructor.name; + + if (valueClass && valueClass.name === className) { + throw new Error("Cannot use ".concat(className, " \"").concat(value, "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results.")); + } + } + + return false; +}; + +exports.default = _default; \ No newline at end of file diff --git a/dist/jsutils/instanceOf.js.flow b/dist/jsutils/instanceOf.js.flow new file mode 100644 index 0000000000..6fc1db5f2c --- /dev/null +++ b/dist/jsutils/instanceOf.js.flow @@ -0,0 +1,52 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * A replacement for instanceof which includes an error warning when multi-realm + * constructors are detected. + */ +declare function instanceOf( + value: mixed, + constructor: mixed, +): boolean %checks(value instanceof constructor); + +// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production +// See: https://webpack.js.org/guides/production/ +export default (process.env.NODE_ENV === 'production' + ? // eslint-disable-next-line no-shadow + function instanceOf(value: any, constructor: any) { + return value instanceof constructor; + } + : // eslint-disable-next-line no-shadow + function instanceOf(value: any, constructor: any) { + if (value instanceof constructor) { + return true; + } + if (value) { + const valueClass = value.constructor; + const className = constructor.name; + if (valueClass && valueClass.name === className) { + throw new Error( + `Cannot use ${className} "${value}" from another module or realm. + +Ensure that there is only one instance of "graphql" in the node_modules +directory. If different versions of "graphql" are the dependencies of other +relied on modules, use "resolutions" to ensure only one version is installed. + +https://yarnpkg.com/en/docs/selective-version-resolutions + +Duplicate "graphql" modules cannot be used at the same time since different +versions may have different capabilities and behavior. The data from one +version used in the function from another could produce confusing and +spurious results.`, + ); + } + } + return false; + }); diff --git a/dist/jsutils/instanceOf.mjs b/dist/jsutils/instanceOf.mjs new file mode 100644 index 0000000000..caae1d6423 --- /dev/null +++ b/dist/jsutils/instanceOf.mjs @@ -0,0 +1,35 @@ +/** + * 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. + * + * strict + */ + +/** + * A replacement for instanceof which includes an error warning when multi-realm + * constructors are detected. + */ +// See: https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production +// See: https://webpack.js.org/guides/production/ +export default process.env.NODE_ENV === 'production' ? // eslint-disable-next-line no-shadow +function instanceOf(value, constructor) { + return value instanceof constructor; +} : // eslint-disable-next-line no-shadow +function instanceOf(value, constructor) { + if (value instanceof constructor) { + return true; + } + + if (value) { + var valueClass = value.constructor; + var className = constructor.name; + + if (valueClass && valueClass.name === className) { + throw new Error("Cannot use ".concat(className, " \"").concat(value, "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results.")); + } + } + + return false; +}; \ No newline at end of file diff --git a/dist/jsutils/invariant.js b/dist/jsutils/invariant.js new file mode 100644 index 0000000000..f62b28eb2b --- /dev/null +++ b/dist/jsutils/invariant.js @@ -0,0 +1,21 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = invariant; + +/** + * 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. + * + * strict + */ +function invariant(condition, message) { + /* istanbul ignore else */ + if (!condition) { + throw new Error(message); + } +} \ No newline at end of file diff --git a/dist/jsutils/invariant.js.flow b/dist/jsutils/invariant.js.flow new file mode 100644 index 0000000000..f4a51efb54 --- /dev/null +++ b/dist/jsutils/invariant.js.flow @@ -0,0 +1,15 @@ +/** + * 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. + * + * @flow strict + */ + +export default function invariant(condition: mixed, message: string) { + /* istanbul ignore else */ + if (!condition) { + throw new Error(message); + } +} diff --git a/dist/jsutils/invariant.mjs b/dist/jsutils/invariant.mjs new file mode 100644 index 0000000000..a5b5ace0e6 --- /dev/null +++ b/dist/jsutils/invariant.mjs @@ -0,0 +1,14 @@ +/** + * 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. + * + * strict + */ +export default function invariant(condition, message) { + /* istanbul ignore else */ + if (!condition) { + throw new Error(message); + } +} \ No newline at end of file diff --git a/dist/jsutils/isInvalid.js b/dist/jsutils/isInvalid.js new file mode 100644 index 0000000000..0025c6a2c8 --- /dev/null +++ b/dist/jsutils/isInvalid.js @@ -0,0 +1,22 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = isInvalid; + +/** + * 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. + * + * strict + */ + +/** + * Returns true if a value is undefined, or NaN. + */ +function isInvalid(value) { + return value === undefined || value !== value; +} \ No newline at end of file diff --git a/dist/jsutils/isInvalid.js.flow b/dist/jsutils/isInvalid.js.flow new file mode 100644 index 0000000000..9ddbfed512 --- /dev/null +++ b/dist/jsutils/isInvalid.js.flow @@ -0,0 +1,15 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * Returns true if a value is undefined, or NaN. + */ +export default function isInvalid(value: mixed): boolean %checks { + return value === undefined || value !== value; +} diff --git a/dist/jsutils/isInvalid.mjs b/dist/jsutils/isInvalid.mjs new file mode 100644 index 0000000000..af36e077f2 --- /dev/null +++ b/dist/jsutils/isInvalid.mjs @@ -0,0 +1,15 @@ +/** + * 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. + * + * strict + */ + +/** + * Returns true if a value is undefined, or NaN. + */ +export default function isInvalid(value) { + return value === undefined || value !== value; +} \ No newline at end of file diff --git a/dist/jsutils/isNullish.js b/dist/jsutils/isNullish.js new file mode 100644 index 0000000000..d7cf030087 --- /dev/null +++ b/dist/jsutils/isNullish.js @@ -0,0 +1,22 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = isNullish; + +/** + * 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. + * + * strict + */ + +/** + * Returns true if a value is null, undefined, or NaN. + */ +function isNullish(value) { + return value === null || value === undefined || value !== value; +} \ No newline at end of file diff --git a/dist/jsutils/isNullish.js.flow b/dist/jsutils/isNullish.js.flow new file mode 100644 index 0000000000..aedd6c86dc --- /dev/null +++ b/dist/jsutils/isNullish.js.flow @@ -0,0 +1,15 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * Returns true if a value is null, undefined, or NaN. + */ +export default function isNullish(value: mixed): boolean %checks { + return value === null || value === undefined || value !== value; +} diff --git a/dist/jsutils/isNullish.mjs b/dist/jsutils/isNullish.mjs new file mode 100644 index 0000000000..7a2e92da06 --- /dev/null +++ b/dist/jsutils/isNullish.mjs @@ -0,0 +1,15 @@ +/** + * 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. + * + * strict + */ + +/** + * Returns true if a value is null, undefined, or NaN. + */ +export default function isNullish(value) { + return value === null || value === undefined || value !== value; +} \ No newline at end of file diff --git a/dist/jsutils/isPromise.js b/dist/jsutils/isPromise.js new file mode 100644 index 0000000000..0feb12bb5d --- /dev/null +++ b/dist/jsutils/isPromise.js @@ -0,0 +1,24 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = isPromise; + +/** + * 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. + * + * strict + */ + +/** + * Returns true if the value acts like a Promise, i.e. has a "then" function, + * otherwise returns false. + */ +// eslint-disable-next-line no-redeclare +function isPromise(value) { + return Boolean(value && typeof value.then === 'function'); +} \ No newline at end of file diff --git a/dist/jsutils/isPromise.js.flow b/dist/jsutils/isPromise.js.flow new file mode 100644 index 0000000000..d53c9eea6b --- /dev/null +++ b/dist/jsutils/isPromise.js.flow @@ -0,0 +1,20 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * Returns true if the value acts like a Promise, i.e. has a "then" function, + * otherwise returns false. + */ +declare function isPromise(value: mixed): boolean %checks(value instanceof + Promise); + +// eslint-disable-next-line no-redeclare +export default function isPromise(value) { + return Boolean(value && typeof value.then === 'function'); +} diff --git a/dist/jsutils/isPromise.mjs b/dist/jsutils/isPromise.mjs new file mode 100644 index 0000000000..9705fcb418 --- /dev/null +++ b/dist/jsutils/isPromise.mjs @@ -0,0 +1,17 @@ +/** + * 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. + * + * strict + */ + +/** + * Returns true if the value acts like a Promise, i.e. has a "then" function, + * otherwise returns false. + */ +// eslint-disable-next-line no-redeclare +export default function isPromise(value) { + return Boolean(value && typeof value.then === 'function'); +} \ No newline at end of file diff --git a/dist/jsutils/keyMap.js b/dist/jsutils/keyMap.js new file mode 100644 index 0000000000..6d4250f670 --- /dev/null +++ b/dist/jsutils/keyMap.js @@ -0,0 +1,44 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = keyMap; + +/** + * 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. + * + * strict + */ + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * for each value in the array. + * + * This provides a convenient lookup for the array items if the key function + * produces unique results. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: { name: 'Jon', num: '555-1234' }, + * // Jenny: { name: 'Jenny', num: '867-5309' } } + * const entriesByName = keyMap( + * phoneBook, + * entry => entry.name + * ) + * + * // { name: 'Jenny', num: '857-6309' } + * const jennyEntry = entriesByName['Jenny'] + * + */ +function keyMap(list, keyFn) { + return list.reduce(function (map, item) { + return map[keyFn(item)] = item, map; + }, Object.create(null)); +} \ No newline at end of file diff --git a/dist/jsutils/keyMap.js.flow b/dist/jsutils/keyMap.js.flow new file mode 100644 index 0000000000..58fce2f7ac --- /dev/null +++ b/dist/jsutils/keyMap.js.flow @@ -0,0 +1,43 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from './ObjMap'; + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * for each value in the array. + * + * This provides a convenient lookup for the array items if the key function + * produces unique results. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: { name: 'Jon', num: '555-1234' }, + * // Jenny: { name: 'Jenny', num: '867-5309' } } + * const entriesByName = keyMap( + * phoneBook, + * entry => entry.name + * ) + * + * // { name: 'Jenny', num: '857-6309' } + * const jennyEntry = entriesByName['Jenny'] + * + */ +export default function keyMap( + list: $ReadOnlyArray, + keyFn: (item: T) => string, +): ObjMap { + return list.reduce( + (map, item) => ((map[keyFn(item)] = item), map), + Object.create(null), + ); +} diff --git a/dist/jsutils/keyMap.mjs b/dist/jsutils/keyMap.mjs new file mode 100644 index 0000000000..9df8eb09b5 --- /dev/null +++ b/dist/jsutils/keyMap.mjs @@ -0,0 +1,37 @@ +/** + * 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. + * + * strict + */ + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * for each value in the array. + * + * This provides a convenient lookup for the array items if the key function + * produces unique results. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: { name: 'Jon', num: '555-1234' }, + * // Jenny: { name: 'Jenny', num: '867-5309' } } + * const entriesByName = keyMap( + * phoneBook, + * entry => entry.name + * ) + * + * // { name: 'Jenny', num: '857-6309' } + * const jennyEntry = entriesByName['Jenny'] + * + */ +export default function keyMap(list, keyFn) { + return list.reduce(function (map, item) { + return map[keyFn(item)] = item, map; + }, Object.create(null)); +} \ No newline at end of file diff --git a/dist/jsutils/keyValMap.js b/dist/jsutils/keyValMap.js new file mode 100644 index 0000000000..d12801d25a --- /dev/null +++ b/dist/jsutils/keyValMap.js @@ -0,0 +1,38 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = keyValMap; + +/** + * 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. + * + * strict + */ + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * and a function to produce the values from each item in the array. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: '555-1234', Jenny: '867-5309' } + * const phonesByName = keyValMap( + * phoneBook, + * entry => entry.name, + * entry => entry.num + * ) + * + */ +function keyValMap(list, keyFn, valFn) { + return list.reduce(function (map, item) { + return map[keyFn(item)] = valFn(item), map; + }, Object.create(null)); +} \ No newline at end of file diff --git a/dist/jsutils/keyValMap.js.flow b/dist/jsutils/keyValMap.js.flow new file mode 100644 index 0000000000..7917f3ccce --- /dev/null +++ b/dist/jsutils/keyValMap.js.flow @@ -0,0 +1,38 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from './ObjMap'; + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * and a function to produce the values from each item in the array. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: '555-1234', Jenny: '867-5309' } + * const phonesByName = keyValMap( + * phoneBook, + * entry => entry.name, + * entry => entry.num + * ) + * + */ +export default function keyValMap( + list: $ReadOnlyArray, + keyFn: (item: T) => string, + valFn: (item: T) => V, +): ObjMap { + return list.reduce( + (map, item) => ((map[keyFn(item)] = valFn(item)), map), + Object.create(null), + ); +} diff --git a/dist/jsutils/keyValMap.mjs b/dist/jsutils/keyValMap.mjs new file mode 100644 index 0000000000..eae73e8976 --- /dev/null +++ b/dist/jsutils/keyValMap.mjs @@ -0,0 +1,31 @@ +/** + * 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. + * + * strict + */ + +/** + * Creates a keyed JS object from an array, given a function to produce the keys + * and a function to produce the values from each item in the array. + * + * const phoneBook = [ + * { name: 'Jon', num: '555-1234' }, + * { name: 'Jenny', num: '867-5309' } + * ] + * + * // { Jon: '555-1234', Jenny: '867-5309' } + * const phonesByName = keyValMap( + * phoneBook, + * entry => entry.name, + * entry => entry.num + * ) + * + */ +export default function keyValMap(list, keyFn, valFn) { + return list.reduce(function (map, item) { + return map[keyFn(item)] = valFn(item), map; + }, Object.create(null)); +} \ No newline at end of file diff --git a/dist/jsutils/memoize3.js b/dist/jsutils/memoize3.js new file mode 100644 index 0000000000..e079629fe9 --- /dev/null +++ b/dist/jsutils/memoize3.js @@ -0,0 +1,57 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = memoize3; + +/** + * Copyright (c) 2017-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. + * + * strict + */ + +/** + * Memoizes the provided three-argument function. + */ +function memoize3(fn) { + var cache0; + + function memoized(a1, a2, a3) { + if (!cache0) { + cache0 = new WeakMap(); + } + + var cache1 = cache0.get(a1); + var cache2; + + if (cache1) { + cache2 = cache1.get(a2); + + if (cache2) { + var cachedValue = cache2.get(a3); + + if (cachedValue !== undefined) { + return cachedValue; + } + } + } else { + cache1 = new WeakMap(); + cache0.set(a1, cache1); + } + + if (!cache2) { + cache2 = new WeakMap(); + cache1.set(a2, cache2); + } + + var newValue = fn.apply(this, arguments); + cache2.set(a3, newValue); + return newValue; + } + + return memoized; +} \ No newline at end of file diff --git a/dist/jsutils/memoize3.js.flow b/dist/jsutils/memoize3.js.flow new file mode 100644 index 0000000000..37e3951d58 --- /dev/null +++ b/dist/jsutils/memoize3.js.flow @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +/** + * Memoizes the provided three-argument function. + */ +export default function memoize3 any>( + fn: T, +): T { + let cache0; + function memoized(a1, a2, a3) { + if (!cache0) { + cache0 = new WeakMap(); + } + let cache1 = cache0.get(a1); + let cache2; + if (cache1) { + cache2 = cache1.get(a2); + if (cache2) { + const cachedValue = cache2.get(a3); + if (cachedValue !== undefined) { + return cachedValue; + } + } + } else { + cache1 = new WeakMap(); + cache0.set(a1, cache1); + } + if (!cache2) { + cache2 = new WeakMap(); + cache1.set(a2, cache2); + } + const newValue = fn.apply(this, arguments); + cache2.set(a3, newValue); + return newValue; + } + return (memoized: any); +} diff --git a/dist/jsutils/memoize3.mjs b/dist/jsutils/memoize3.mjs new file mode 100644 index 0000000000..c82432be7a --- /dev/null +++ b/dist/jsutils/memoize3.mjs @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2017-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. + * + * strict + */ + +/** + * Memoizes the provided three-argument function. + */ +export default function memoize3(fn) { + var cache0; + + function memoized(a1, a2, a3) { + if (!cache0) { + cache0 = new WeakMap(); + } + + var cache1 = cache0.get(a1); + var cache2; + + if (cache1) { + cache2 = cache1.get(a2); + + if (cache2) { + var cachedValue = cache2.get(a3); + + if (cachedValue !== undefined) { + return cachedValue; + } + } + } else { + cache1 = new WeakMap(); + cache0.set(a1, cache1); + } + + if (!cache2) { + cache2 = new WeakMap(); + cache1.set(a2, cache2); + } + + var newValue = fn.apply(this, arguments); + cache2.set(a3, newValue); + return newValue; + } + + return memoized; +} \ No newline at end of file diff --git a/dist/jsutils/objectValues.js b/dist/jsutils/objectValues.js new file mode 100644 index 0000000000..c43e3893d9 --- /dev/null +++ b/dist/jsutils/objectValues.js @@ -0,0 +1,26 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +/** + * 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. + * + * strict + */ + +/* eslint-disable no-redeclare */ +// $FlowFixMe workaround for: https://github.com/facebook/flow/issues/2221 +var objectValues = Object.values || function (obj) { + return Object.keys(obj).map(function (key) { + return obj[key]; + }); +}; + +var _default = objectValues; +exports.default = _default; \ No newline at end of file diff --git a/dist/jsutils/objectValues.js.flow b/dist/jsutils/objectValues.js.flow new file mode 100644 index 0000000000..d6bab482e2 --- /dev/null +++ b/dist/jsutils/objectValues.js.flow @@ -0,0 +1,18 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from './ObjMap'; + +declare function objectValues(obj: ObjMap): Array; + +/* eslint-disable no-redeclare */ +// $FlowFixMe workaround for: https://github.com/facebook/flow/issues/2221 +const objectValues = + Object.values || (obj => Object.keys(obj).map(key => obj[key])); +export default objectValues; diff --git a/dist/jsutils/objectValues.mjs b/dist/jsutils/objectValues.mjs new file mode 100644 index 0000000000..8a08ca3c0e --- /dev/null +++ b/dist/jsutils/objectValues.mjs @@ -0,0 +1,18 @@ +/** + * 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. + * + * strict + */ + +/* eslint-disable no-redeclare */ +// $FlowFixMe workaround for: https://github.com/facebook/flow/issues/2221 +var objectValues = Object.values || function (obj) { + return Object.keys(obj).map(function (key) { + return obj[key]; + }); +}; + +export default objectValues; \ No newline at end of file diff --git a/dist/jsutils/orList.js b/dist/jsutils/orList.js new file mode 100644 index 0000000000..d52df380c4 --- /dev/null +++ b/dist/jsutils/orList.js @@ -0,0 +1,26 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = orList; + +/** + * 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. + * + * strict + */ +var MAX_LENGTH = 5; +/** + * Given [ A, B, C ] return 'A, B, or C'. + */ + +function orList(items) { + var selected = items.slice(0, MAX_LENGTH); + return selected.reduce(function (list, quoted, index) { + return list + (selected.length > 2 ? ', ' : ' ') + (index === selected.length - 1 ? 'or ' : '') + quoted; + }); +} \ No newline at end of file diff --git a/dist/jsutils/orList.js.flow b/dist/jsutils/orList.js.flow new file mode 100644 index 0000000000..dc63e0db3c --- /dev/null +++ b/dist/jsutils/orList.js.flow @@ -0,0 +1,24 @@ +/** + * 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. + * + * @flow strict + */ + +const MAX_LENGTH = 5; + +/** + * Given [ A, B, C ] return 'A, B, or C'. + */ +export default function orList(items: $ReadOnlyArray): string { + const selected = items.slice(0, MAX_LENGTH); + return selected.reduce( + (list, quoted, index) => + list + + (selected.length > 2 ? ', ' : ' ') + + (index === selected.length - 1 ? 'or ' : '') + + quoted, + ); +} diff --git a/dist/jsutils/orList.mjs b/dist/jsutils/orList.mjs new file mode 100644 index 0000000000..3c68746b4a --- /dev/null +++ b/dist/jsutils/orList.mjs @@ -0,0 +1,19 @@ +/** + * 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. + * + * strict + */ +var MAX_LENGTH = 5; +/** + * Given [ A, B, C ] return 'A, B, or C'. + */ + +export default function orList(items) { + var selected = items.slice(0, MAX_LENGTH); + return selected.reduce(function (list, quoted, index) { + return list + (selected.length > 2 ? ', ' : ' ') + (index === selected.length - 1 ? 'or ' : '') + quoted; + }); +} \ No newline at end of file diff --git a/dist/jsutils/promiseForObject.js b/dist/jsutils/promiseForObject.js new file mode 100644 index 0000000000..55328b2739 --- /dev/null +++ b/dist/jsutils/promiseForObject.js @@ -0,0 +1,35 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = promiseForObject; + +/** + * 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. + * + * strict + */ + +/** + * This function transforms a JS object `ObjMap>` into + * a `Promise>` + * + * This is akin to bluebird's `Promise.props`, but implemented only using + * `Promise.all` so it will work with any implementation of ES6 promises. + */ +function promiseForObject(object) { + var keys = Object.keys(object); + var valuesAndPromises = keys.map(function (name) { + return object[name]; + }); + return Promise.all(valuesAndPromises).then(function (values) { + return values.reduce(function (resolvedObject, value, i) { + resolvedObject[keys[i]] = value; + return resolvedObject; + }, Object.create(null)); + }); +} \ No newline at end of file diff --git a/dist/jsutils/promiseForObject.js.flow b/dist/jsutils/promiseForObject.js.flow new file mode 100644 index 0000000000..74d178760f --- /dev/null +++ b/dist/jsutils/promiseForObject.js.flow @@ -0,0 +1,30 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from './ObjMap'; + +/** + * This function transforms a JS object `ObjMap>` into + * a `Promise>` + * + * This is akin to bluebird's `Promise.props`, but implemented only using + * `Promise.all` so it will work with any implementation of ES6 promises. + */ +export default function promiseForObject( + object: ObjMap>, +): Promise> { + const keys = Object.keys(object); + const valuesAndPromises = keys.map(name => object[name]); + return Promise.all(valuesAndPromises).then(values => + values.reduce((resolvedObject, value, i) => { + resolvedObject[keys[i]] = value; + return resolvedObject; + }, Object.create(null)), + ); +} diff --git a/dist/jsutils/promiseForObject.mjs b/dist/jsutils/promiseForObject.mjs new file mode 100644 index 0000000000..356ede356f --- /dev/null +++ b/dist/jsutils/promiseForObject.mjs @@ -0,0 +1,28 @@ +/** + * 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. + * + * strict + */ + +/** + * This function transforms a JS object `ObjMap>` into + * a `Promise>` + * + * This is akin to bluebird's `Promise.props`, but implemented only using + * `Promise.all` so it will work with any implementation of ES6 promises. + */ +export default function promiseForObject(object) { + var keys = Object.keys(object); + var valuesAndPromises = keys.map(function (name) { + return object[name]; + }); + return Promise.all(valuesAndPromises).then(function (values) { + return values.reduce(function (resolvedObject, value, i) { + resolvedObject[keys[i]] = value; + return resolvedObject; + }, Object.create(null)); + }); +} \ No newline at end of file diff --git a/dist/jsutils/promiseReduce.js b/dist/jsutils/promiseReduce.js new file mode 100644 index 0000000000..9ed206af9c --- /dev/null +++ b/dist/jsutils/promiseReduce.js @@ -0,0 +1,34 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = promiseReduce; + +var _isPromise = _interopRequireDefault(require("./isPromise")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Similar to Array.prototype.reduce(), however the reducing callback may return + * a Promise, in which case reduction will continue after each promise resolves. + * + * If the callback does not return a Promise, then this function will also not + * return a Promise. + */ +function promiseReduce(values, callback, initialValue) { + return values.reduce(function (previous, value) { + return (0, _isPromise.default)(previous) ? previous.then(function (resolved) { + return callback(resolved, value); + }) : callback(previous, value); + }, initialValue); +} \ No newline at end of file diff --git a/dist/jsutils/promiseReduce.js.flow b/dist/jsutils/promiseReduce.js.flow new file mode 100644 index 0000000000..a09837b8ac --- /dev/null +++ b/dist/jsutils/promiseReduce.js.flow @@ -0,0 +1,32 @@ +/** + * 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. + * + * @flow strict + */ + +import isPromise from './isPromise'; +import type { MaybePromise } from './MaybePromise'; + +/** + * Similar to Array.prototype.reduce(), however the reducing callback may return + * a Promise, in which case reduction will continue after each promise resolves. + * + * If the callback does not return a Promise, then this function will also not + * return a Promise. + */ +export default function promiseReduce( + values: $ReadOnlyArray, + callback: (U, T) => MaybePromise, + initialValue: MaybePromise, +): MaybePromise { + return values.reduce( + (previous, value) => + isPromise(previous) + ? previous.then(resolved => callback(resolved, value)) + : callback(previous, value), + initialValue, + ); +} diff --git a/dist/jsutils/promiseReduce.mjs b/dist/jsutils/promiseReduce.mjs new file mode 100644 index 0000000000..8fb791913a --- /dev/null +++ b/dist/jsutils/promiseReduce.mjs @@ -0,0 +1,24 @@ +/** + * 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. + * + * strict + */ +import isPromise from './isPromise'; + +/** + * Similar to Array.prototype.reduce(), however the reducing callback may return + * a Promise, in which case reduction will continue after each promise resolves. + * + * If the callback does not return a Promise, then this function will also not + * return a Promise. + */ +export default function promiseReduce(values, callback, initialValue) { + return values.reduce(function (previous, value) { + return isPromise(previous) ? previous.then(function (resolved) { + return callback(resolved, value); + }) : callback(previous, value); + }, initialValue); +} \ No newline at end of file diff --git a/dist/jsutils/quotedOrList.js b/dist/jsutils/quotedOrList.js new file mode 100644 index 0000000000..826fc02e96 --- /dev/null +++ b/dist/jsutils/quotedOrList.js @@ -0,0 +1,28 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = quotedOrList; + +var _orList = _interopRequireDefault(require("./orList")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Given [ A, B, C ] return '"A", "B", or "C"'. + */ +function quotedOrList(items) { + return (0, _orList.default)(items.map(function (item) { + return "\"".concat(item, "\""); + })); +} \ No newline at end of file diff --git a/dist/jsutils/quotedOrList.js.flow b/dist/jsutils/quotedOrList.js.flow new file mode 100644 index 0000000000..f5670388cd --- /dev/null +++ b/dist/jsutils/quotedOrList.js.flow @@ -0,0 +1,17 @@ +/** + * 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. + * + * @flow strict + */ + +import orList from './orList'; + +/** + * Given [ A, B, C ] return '"A", "B", or "C"'. + */ +export default function quotedOrList(items: $ReadOnlyArray): string { + return orList(items.map(item => `"${item}"`)); +} diff --git a/dist/jsutils/quotedOrList.mjs b/dist/jsutils/quotedOrList.mjs new file mode 100644 index 0000000000..6cff7805f0 --- /dev/null +++ b/dist/jsutils/quotedOrList.mjs @@ -0,0 +1,18 @@ +/** + * 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. + * + * strict + */ +import orList from './orList'; +/** + * Given [ A, B, C ] return '"A", "B", or "C"'. + */ + +export default function quotedOrList(items) { + return orList(items.map(function (item) { + return "\"".concat(item, "\""); + })); +} \ No newline at end of file diff --git a/dist/jsutils/suggestionList.js b/dist/jsutils/suggestionList.js new file mode 100644 index 0000000000..42072b3904 --- /dev/null +++ b/dist/jsutils/suggestionList.js @@ -0,0 +1,96 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = suggestionList; + +/** + * 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. + * + * strict + */ + +/** + * Given an invalid input string and a list of valid options, returns a filtered + * list of valid options sorted based on their similarity with the input. + */ +function suggestionList(input, options) { + var optionsByDistance = Object.create(null); + var oLength = options.length; + var inputThreshold = input.length / 2; + + for (var i = 0; i < oLength; i++) { + var distance = lexicalDistance(input, options[i]); + var threshold = Math.max(inputThreshold, options[i].length / 2, 1); + + if (distance <= threshold) { + optionsByDistance[options[i]] = distance; + } + } + + return Object.keys(optionsByDistance).sort(function (a, b) { + return optionsByDistance[a] - optionsByDistance[b]; + }); +} +/** + * Computes the lexical distance between strings A and B. + * + * The "distance" between two strings is given by counting the minimum number + * of edits needed to transform string A into string B. An edit can be an + * insertion, deletion, or substitution of a single character, or a swap of two + * adjacent characters. + * + * Includes a custom alteration from Damerau-Levenshtein to treat case changes + * as a single edit which helps identify mis-cased values with an edit distance + * of 1. + * + * This distance can be useful for detecting typos in input or sorting + * + * @param {string} a + * @param {string} b + * @return {int} distance in number of edits + */ + + +function lexicalDistance(aStr, bStr) { + if (aStr === bStr) { + return 0; + } + + var i; + var j; + var d = []; + var a = aStr.toLowerCase(); + var b = bStr.toLowerCase(); + var aLength = a.length; + var bLength = b.length; // Any case change counts as a single edit + + if (a === b) { + return 1; + } + + for (i = 0; i <= aLength; i++) { + d[i] = [i]; + } + + for (j = 1; j <= bLength; j++) { + d[0][j] = j; + } + + for (i = 1; i <= aLength; i++) { + for (j = 1; j <= bLength; j++) { + var cost = a[i - 1] === b[j - 1] ? 0 : 1; + d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); + + if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) { + d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost); + } + } + } + + return d[aLength][bLength]; +} \ No newline at end of file diff --git a/dist/jsutils/suggestionList.js.flow b/dist/jsutils/suggestionList.js.flow new file mode 100644 index 0000000000..e800d03a8c --- /dev/null +++ b/dist/jsutils/suggestionList.js.flow @@ -0,0 +1,94 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * Given an invalid input string and a list of valid options, returns a filtered + * list of valid options sorted based on their similarity with the input. + */ +export default function suggestionList( + input: string, + options: $ReadOnlyArray, +): Array { + const optionsByDistance = Object.create(null); + const oLength = options.length; + const inputThreshold = input.length / 2; + for (let i = 0; i < oLength; i++) { + const distance = lexicalDistance(input, options[i]); + const threshold = Math.max(inputThreshold, options[i].length / 2, 1); + if (distance <= threshold) { + optionsByDistance[options[i]] = distance; + } + } + return Object.keys(optionsByDistance).sort( + (a, b) => optionsByDistance[a] - optionsByDistance[b], + ); +} + +/** + * Computes the lexical distance between strings A and B. + * + * The "distance" between two strings is given by counting the minimum number + * of edits needed to transform string A into string B. An edit can be an + * insertion, deletion, or substitution of a single character, or a swap of two + * adjacent characters. + * + * Includes a custom alteration from Damerau-Levenshtein to treat case changes + * as a single edit which helps identify mis-cased values with an edit distance + * of 1. + * + * This distance can be useful for detecting typos in input or sorting + * + * @param {string} a + * @param {string} b + * @return {int} distance in number of edits + */ +function lexicalDistance(aStr, bStr) { + if (aStr === bStr) { + return 0; + } + + let i; + let j; + const d = []; + const a = aStr.toLowerCase(); + const b = bStr.toLowerCase(); + const aLength = a.length; + const bLength = b.length; + + // Any case change counts as a single edit + if (a === b) { + return 1; + } + + for (i = 0; i <= aLength; i++) { + d[i] = [i]; + } + + for (j = 1; j <= bLength; j++) { + d[0][j] = j; + } + + for (i = 1; i <= aLength; i++) { + for (j = 1; j <= bLength; j++) { + const cost = a[i - 1] === b[j - 1] ? 0 : 1; + + d[i][j] = Math.min( + d[i - 1][j] + 1, + d[i][j - 1] + 1, + d[i - 1][j - 1] + cost, + ); + + if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) { + d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost); + } + } + } + + return d[aLength][bLength]; +} diff --git a/dist/jsutils/suggestionList.mjs b/dist/jsutils/suggestionList.mjs new file mode 100644 index 0000000000..229d5578fb --- /dev/null +++ b/dist/jsutils/suggestionList.mjs @@ -0,0 +1,88 @@ +/** + * 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. + * + * strict + */ + +/** + * Given an invalid input string and a list of valid options, returns a filtered + * list of valid options sorted based on their similarity with the input. + */ +export default function suggestionList(input, options) { + var optionsByDistance = Object.create(null); + var oLength = options.length; + var inputThreshold = input.length / 2; + + for (var i = 0; i < oLength; i++) { + var distance = lexicalDistance(input, options[i]); + var threshold = Math.max(inputThreshold, options[i].length / 2, 1); + + if (distance <= threshold) { + optionsByDistance[options[i]] = distance; + } + } + + return Object.keys(optionsByDistance).sort(function (a, b) { + return optionsByDistance[a] - optionsByDistance[b]; + }); +} +/** + * Computes the lexical distance between strings A and B. + * + * The "distance" between two strings is given by counting the minimum number + * of edits needed to transform string A into string B. An edit can be an + * insertion, deletion, or substitution of a single character, or a swap of two + * adjacent characters. + * + * Includes a custom alteration from Damerau-Levenshtein to treat case changes + * as a single edit which helps identify mis-cased values with an edit distance + * of 1. + * + * This distance can be useful for detecting typos in input or sorting + * + * @param {string} a + * @param {string} b + * @return {int} distance in number of edits + */ + +function lexicalDistance(aStr, bStr) { + if (aStr === bStr) { + return 0; + } + + var i; + var j; + var d = []; + var a = aStr.toLowerCase(); + var b = bStr.toLowerCase(); + var aLength = a.length; + var bLength = b.length; // Any case change counts as a single edit + + if (a === b) { + return 1; + } + + for (i = 0; i <= aLength; i++) { + d[i] = [i]; + } + + for (j = 1; j <= bLength; j++) { + d[0][j] = j; + } + + for (i = 1; i <= aLength; i++) { + for (j = 1; j <= bLength; j++) { + var cost = a[i - 1] === b[j - 1] ? 0 : 1; + d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost); + + if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) { + d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost); + } + } + } + + return d[aLength][bLength]; +} \ No newline at end of file diff --git a/dist/language/ast.js b/dist/language/ast.js new file mode 100644 index 0000000000..9a390c31f7 --- /dev/null +++ b/dist/language/ast.js @@ -0,0 +1 @@ +"use strict"; \ No newline at end of file diff --git a/dist/language/ast.js.flow b/dist/language/ast.js.flow new file mode 100644 index 0000000000..000bc3bdad --- /dev/null +++ b/dist/language/ast.js.flow @@ -0,0 +1,580 @@ +/** + * 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. + * + * @flow strict + */ + +import type { Source } from './source'; +import type { TokenKindEnum } from './lexer'; + +/** + * Contains a range of UTF-8 character offsets and token references that + * identify the region of the source from which the AST derived. + */ +export type Location = { + /** + * The character offset at which this Node begins. + */ + +start: number, + + /** + * The character offset at which this Node ends. + */ + +end: number, + + /** + * The Token at which this Node begins. + */ + +startToken: Token, + + /** + * The Token at which this Node ends. + */ + +endToken: Token, + + /** + * The Source document the AST represents. + */ + +source: Source, +}; + +/** + * Represents a range of characters represented by a lexical token + * within a Source. + */ +export type Token = { + /** + * The kind of Token. + */ + +kind: TokenKindEnum, + + /** + * The character offset at which this Node begins. + */ + +start: number, + + /** + * The character offset at which this Node ends. + */ + +end: number, + + /** + * The 1-indexed line number on which this Token appears. + */ + +line: number, + + /** + * The 1-indexed column number at which this Token begins. + */ + +column: number, + + /** + * For non-punctuation tokens, represents the interpreted value of the token. + */ + +value: string | void, + + /** + * Tokens exist as nodes in a double-linked-list amongst all tokens + * including ignored tokens. is always the first node and + * the last. + */ + +prev: Token | null, + +next: Token | null, +}; + +/** + * The list of all possible AST node types. + */ +export type ASTNode = + | NameNode + | DocumentNode + | OperationDefinitionNode + | VariableDefinitionNode + | VariableNode + | SelectionSetNode + | FieldNode + | ArgumentNode + | FragmentSpreadNode + | InlineFragmentNode + | FragmentDefinitionNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode + | ObjectFieldNode + | DirectiveNode + | NamedTypeNode + | ListTypeNode + | NonNullTypeNode + | SchemaDefinitionNode + | OperationTypeDefinitionNode + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | FieldDefinitionNode + | InputValueDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | EnumValueDefinitionNode + | InputObjectTypeDefinitionNode + | DirectiveDefinitionNode + | SchemaExtensionNode + | ScalarTypeExtensionNode + | ObjectTypeExtensionNode + | InterfaceTypeExtensionNode + | UnionTypeExtensionNode + | EnumTypeExtensionNode + | InputObjectTypeExtensionNode; + +/** + * Utility type listing all nodes indexed by their kind. + */ +export type ASTKindToNode = { + Name: NameNode, + Document: DocumentNode, + OperationDefinition: OperationDefinitionNode, + VariableDefinition: VariableDefinitionNode, + Variable: VariableNode, + SelectionSet: SelectionSetNode, + Field: FieldNode, + Argument: ArgumentNode, + FragmentSpread: FragmentSpreadNode, + InlineFragment: InlineFragmentNode, + FragmentDefinition: FragmentDefinitionNode, + IntValue: IntValueNode, + FloatValue: FloatValueNode, + StringValue: StringValueNode, + BooleanValue: BooleanValueNode, + NullValue: NullValueNode, + EnumValue: EnumValueNode, + ListValue: ListValueNode, + ObjectValue: ObjectValueNode, + ObjectField: ObjectFieldNode, + Directive: DirectiveNode, + NamedType: NamedTypeNode, + ListType: ListTypeNode, + NonNullType: NonNullTypeNode, + SchemaDefinition: SchemaDefinitionNode, + OperationTypeDefinition: OperationTypeDefinitionNode, + ScalarTypeDefinition: ScalarTypeDefinitionNode, + ObjectTypeDefinition: ObjectTypeDefinitionNode, + FieldDefinition: FieldDefinitionNode, + InputValueDefinition: InputValueDefinitionNode, + InterfaceTypeDefinition: InterfaceTypeDefinitionNode, + UnionTypeDefinition: UnionTypeDefinitionNode, + EnumTypeDefinition: EnumTypeDefinitionNode, + EnumValueDefinition: EnumValueDefinitionNode, + InputObjectTypeDefinition: InputObjectTypeDefinitionNode, + DirectiveDefinition: DirectiveDefinitionNode, + SchemaExtension: SchemaExtensionNode, + ScalarTypeExtension: ScalarTypeExtensionNode, + ObjectTypeExtension: ObjectTypeExtensionNode, + InterfaceTypeExtension: InterfaceTypeExtensionNode, + UnionTypeExtension: UnionTypeExtensionNode, + EnumTypeExtension: EnumTypeExtensionNode, + InputObjectTypeExtension: InputObjectTypeExtensionNode, +}; + +// Name + +export type NameNode = { + +kind: 'Name', + +loc?: Location, + +value: string, +}; + +// Document + +export type DocumentNode = { + +kind: 'Document', + +loc?: Location, + +definitions: $ReadOnlyArray, +}; + +export type DefinitionNode = + | ExecutableDefinitionNode + | TypeSystemDefinitionNode + | TypeSystemExtensionNode; + +export type ExecutableDefinitionNode = + | OperationDefinitionNode + | FragmentDefinitionNode; + +export type OperationDefinitionNode = { + +kind: 'OperationDefinition', + +loc?: Location, + +operation: OperationTypeNode, + +name?: NameNode, + +variableDefinitions?: $ReadOnlyArray, + +directives?: $ReadOnlyArray, + +selectionSet: SelectionSetNode, +}; + +export type OperationTypeNode = 'query' | 'mutation' | 'subscription'; + +export type VariableDefinitionNode = { + +kind: 'VariableDefinition', + +loc?: Location, + +variable: VariableNode, + +type: TypeNode, + +defaultValue?: ValueNode, +}; + +export type VariableNode = { + +kind: 'Variable', + +loc?: Location, + +name: NameNode, +}; + +export type SelectionSetNode = { + kind: 'SelectionSet', + loc?: Location, + selections: $ReadOnlyArray, +}; + +export type SelectionNode = FieldNode | FragmentSpreadNode | InlineFragmentNode; + +export type FieldNode = { + +kind: 'Field', + +loc?: Location, + +alias?: NameNode, + +name: NameNode, + +arguments?: $ReadOnlyArray, + +directives?: $ReadOnlyArray, + +selectionSet?: SelectionSetNode, +}; + +export type ArgumentNode = { + +kind: 'Argument', + +loc?: Location, + +name: NameNode, + +value: ValueNode, +}; + +// Fragments + +export type FragmentSpreadNode = { + +kind: 'FragmentSpread', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, +}; + +export type InlineFragmentNode = { + +kind: 'InlineFragment', + +loc?: Location, + +typeCondition?: NamedTypeNode, + +directives?: $ReadOnlyArray, + +selectionSet: SelectionSetNode, +}; + +export type FragmentDefinitionNode = { + +kind: 'FragmentDefinition', + +loc?: Location, + +name: NameNode, + // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + +variableDefinitions?: $ReadOnlyArray, + +typeCondition: NamedTypeNode, + +directives?: $ReadOnlyArray, + +selectionSet: SelectionSetNode, +}; + +// Values + +export type ValueNode = + | VariableNode + | IntValueNode + | FloatValueNode + | StringValueNode + | BooleanValueNode + | NullValueNode + | EnumValueNode + | ListValueNode + | ObjectValueNode; + +export type IntValueNode = { + +kind: 'IntValue', + +loc?: Location, + +value: string, +}; + +export type FloatValueNode = { + +kind: 'FloatValue', + +loc?: Location, + +value: string, +}; + +export type StringValueNode = { + +kind: 'StringValue', + +loc?: Location, + +value: string, + +block?: boolean, +}; + +export type BooleanValueNode = { + +kind: 'BooleanValue', + +loc?: Location, + +value: boolean, +}; + +export type NullValueNode = { + +kind: 'NullValue', + +loc?: Location, +}; + +export type EnumValueNode = { + +kind: 'EnumValue', + +loc?: Location, + +value: string, +}; + +export type ListValueNode = { + +kind: 'ListValue', + +loc?: Location, + +values: $ReadOnlyArray, +}; + +export type ObjectValueNode = { + +kind: 'ObjectValue', + +loc?: Location, + +fields: $ReadOnlyArray, +}; + +export type ObjectFieldNode = { + +kind: 'ObjectField', + +loc?: Location, + +name: NameNode, + +value: ValueNode, +}; + +// Directives + +export type DirectiveNode = { + +kind: 'Directive', + +loc?: Location, + +name: NameNode, + +arguments?: $ReadOnlyArray, +}; + +// Type Reference + +export type TypeNode = NamedTypeNode | ListTypeNode | NonNullTypeNode; + +export type NamedTypeNode = { + +kind: 'NamedType', + +loc?: Location, + +name: NameNode, +}; + +export type ListTypeNode = { + +kind: 'ListType', + +loc?: Location, + +type: TypeNode, +}; + +export type NonNullTypeNode = { + +kind: 'NonNullType', + +loc?: Location, + +type: NamedTypeNode | ListTypeNode, +}; + +// Type System Definition + +export type TypeSystemDefinitionNode = + | SchemaDefinitionNode + | TypeDefinitionNode + | DirectiveDefinitionNode; + +export type SchemaDefinitionNode = { + +kind: 'SchemaDefinition', + +loc?: Location, + +directives?: $ReadOnlyArray, + +operationTypes: $ReadOnlyArray, +}; + +export type OperationTypeDefinitionNode = { + +kind: 'OperationTypeDefinition', + +loc?: Location, + +operation: OperationTypeNode, + +type: NamedTypeNode, +}; + +// Type Definition + +export type TypeDefinitionNode = + | ScalarTypeDefinitionNode + | ObjectTypeDefinitionNode + | InterfaceTypeDefinitionNode + | UnionTypeDefinitionNode + | EnumTypeDefinitionNode + | InputObjectTypeDefinitionNode; + +export type ScalarTypeDefinitionNode = { + +kind: 'ScalarTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, +}; + +export type ObjectTypeDefinitionNode = { + +kind: 'ObjectTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +interfaces?: $ReadOnlyArray, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; + +export type FieldDefinitionNode = { + +kind: 'FieldDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +arguments?: $ReadOnlyArray, + +type: TypeNode, + +directives?: $ReadOnlyArray, +}; + +export type InputValueDefinitionNode = { + +kind: 'InputValueDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +type: TypeNode, + +defaultValue?: ValueNode, + +directives?: $ReadOnlyArray, +}; + +export type InterfaceTypeDefinitionNode = { + +kind: 'InterfaceTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; + +export type UnionTypeDefinitionNode = { + +kind: 'UnionTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, + +types?: $ReadOnlyArray, +}; + +export type EnumTypeDefinitionNode = { + +kind: 'EnumTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, + +values?: $ReadOnlyArray, +}; + +export type EnumValueDefinitionNode = { + +kind: 'EnumValueDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, +}; + +export type InputObjectTypeDefinitionNode = { + +kind: 'InputObjectTypeDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; + +// Directive Definitions + +export type DirectiveDefinitionNode = { + +kind: 'DirectiveDefinition', + +loc?: Location, + +description?: StringValueNode, + +name: NameNode, + +arguments?: $ReadOnlyArray, + +locations: $ReadOnlyArray, +}; + +// Type System Extensions + +export type TypeSystemExtensionNode = SchemaExtensionNode | TypeExtensionNode; + +export type SchemaExtensionNode = { + +kind: 'SchemaExtension', + +loc?: Location, + +directives?: $ReadOnlyArray, + +operationTypes?: $ReadOnlyArray, +}; + +// Type Extensions + +export type TypeExtensionNode = + | ScalarTypeExtensionNode + | ObjectTypeExtensionNode + | InterfaceTypeExtensionNode + | UnionTypeExtensionNode + | EnumTypeExtensionNode + | InputObjectTypeExtensionNode; + +export type ScalarTypeExtensionNode = { + +kind: 'ScalarTypeExtension', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, +}; + +export type ObjectTypeExtensionNode = { + +kind: 'ObjectTypeExtension', + +loc?: Location, + +name: NameNode, + +interfaces?: $ReadOnlyArray, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; + +export type InterfaceTypeExtensionNode = { + +kind: 'InterfaceTypeExtension', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; + +export type UnionTypeExtensionNode = { + +kind: 'UnionTypeExtension', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, + +types?: $ReadOnlyArray, +}; + +export type EnumTypeExtensionNode = { + +kind: 'EnumTypeExtension', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, + +values?: $ReadOnlyArray, +}; + +export type InputObjectTypeExtensionNode = { + +kind: 'InputObjectTypeExtension', + +loc?: Location, + +name: NameNode, + +directives?: $ReadOnlyArray, + +fields?: $ReadOnlyArray, +}; diff --git a/dist/language/ast.mjs b/dist/language/ast.mjs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dist/language/blockStringValue.js b/dist/language/blockStringValue.js new file mode 100644 index 0000000000..d959639ceb --- /dev/null +++ b/dist/language/blockStringValue.js @@ -0,0 +1,73 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = blockStringValue; + +/** + * 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. + * + * strict + */ + +/** + * Produces the value of a block string from its parsed raw value, similar to + * Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc. + * + * This implements the GraphQL spec's BlockStringValue() static algorithm. + */ +function blockStringValue(rawString) { + // Expand a block string's raw value into independent lines. + var lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first. + + var commonIndent = null; + + for (var i = 1; i < lines.length; i++) { + var line = lines[i]; + var indent = leadingWhitespace(line); + + if (indent < line.length && (commonIndent === null || indent < commonIndent)) { + commonIndent = indent; + + if (commonIndent === 0) { + break; + } + } + } + + if (commonIndent) { + for (var _i = 1; _i < lines.length; _i++) { + lines[_i] = lines[_i].slice(commonIndent); + } + } // Remove leading and trailing blank lines. + + + while (lines.length > 0 && isBlank(lines[0])) { + lines.shift(); + } + + while (lines.length > 0 && isBlank(lines[lines.length - 1])) { + lines.pop(); + } // Return a string of the lines joined with U+000A. + + + return lines.join('\n'); +} + +function leadingWhitespace(str) { + var i = 0; + + while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { + i++; + } + + return i; +} + +function isBlank(str) { + return leadingWhitespace(str) === str.length; +} \ No newline at end of file diff --git a/dist/language/blockStringValue.js.flow b/dist/language/blockStringValue.js.flow new file mode 100644 index 0000000000..ec2449ffeb --- /dev/null +++ b/dist/language/blockStringValue.js.flow @@ -0,0 +1,64 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * Produces the value of a block string from its parsed raw value, similar to + * Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc. + * + * This implements the GraphQL spec's BlockStringValue() static algorithm. + */ +export default function blockStringValue(rawString: string): string { + // Expand a block string's raw value into independent lines. + const lines = rawString.split(/\r\n|[\n\r]/g); + + // Remove common indentation from all lines but first. + let commonIndent = null; + for (let i = 1; i < lines.length; i++) { + const line = lines[i]; + const indent = leadingWhitespace(line); + if ( + indent < line.length && + (commonIndent === null || indent < commonIndent) + ) { + commonIndent = indent; + if (commonIndent === 0) { + break; + } + } + } + + if (commonIndent) { + for (let i = 1; i < lines.length; i++) { + lines[i] = lines[i].slice(commonIndent); + } + } + + // Remove leading and trailing blank lines. + while (lines.length > 0 && isBlank(lines[0])) { + lines.shift(); + } + while (lines.length > 0 && isBlank(lines[lines.length - 1])) { + lines.pop(); + } + + // Return a string of the lines joined with U+000A. + return lines.join('\n'); +} + +function leadingWhitespace(str) { + let i = 0; + while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { + i++; + } + return i; +} + +function isBlank(str) { + return leadingWhitespace(str) === str.length; +} diff --git a/dist/language/blockStringValue.mjs b/dist/language/blockStringValue.mjs new file mode 100644 index 0000000000..83e7a895f3 --- /dev/null +++ b/dist/language/blockStringValue.mjs @@ -0,0 +1,66 @@ +/** + * 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. + * + * strict + */ + +/** + * Produces the value of a block string from its parsed raw value, similar to + * Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc. + * + * This implements the GraphQL spec's BlockStringValue() static algorithm. + */ +export default function blockStringValue(rawString) { + // Expand a block string's raw value into independent lines. + var lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first. + + var commonIndent = null; + + for (var i = 1; i < lines.length; i++) { + var line = lines[i]; + var indent = leadingWhitespace(line); + + if (indent < line.length && (commonIndent === null || indent < commonIndent)) { + commonIndent = indent; + + if (commonIndent === 0) { + break; + } + } + } + + if (commonIndent) { + for (var _i = 1; _i < lines.length; _i++) { + lines[_i] = lines[_i].slice(commonIndent); + } + } // Remove leading and trailing blank lines. + + + while (lines.length > 0 && isBlank(lines[0])) { + lines.shift(); + } + + while (lines.length > 0 && isBlank(lines[lines.length - 1])) { + lines.pop(); + } // Return a string of the lines joined with U+000A. + + + return lines.join('\n'); +} + +function leadingWhitespace(str) { + var i = 0; + + while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { + i++; + } + + return i; +} + +function isBlank(str) { + return leadingWhitespace(str) === str.length; +} \ No newline at end of file diff --git a/dist/language/directiveLocation.js b/dist/language/directiveLocation.js new file mode 100644 index 0000000000..61010ade92 --- /dev/null +++ b/dist/language/directiveLocation.js @@ -0,0 +1,46 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.DirectiveLocation = void 0; + +/** + * 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. + * + * strict + */ + +/** + * The set of allowed directive location values. + */ +var DirectiveLocation = Object.freeze({ + // Request Definitions + QUERY: 'QUERY', + MUTATION: 'MUTATION', + SUBSCRIPTION: 'SUBSCRIPTION', + FIELD: 'FIELD', + FRAGMENT_DEFINITION: 'FRAGMENT_DEFINITION', + FRAGMENT_SPREAD: 'FRAGMENT_SPREAD', + INLINE_FRAGMENT: 'INLINE_FRAGMENT', + // Type System Definitions + SCHEMA: 'SCHEMA', + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + FIELD_DEFINITION: 'FIELD_DEFINITION', + ARGUMENT_DEFINITION: 'ARGUMENT_DEFINITION', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + ENUM_VALUE: 'ENUM_VALUE', + INPUT_OBJECT: 'INPUT_OBJECT', + INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION' +}); +/** + * The enum type representing the directive location values. + */ + +exports.DirectiveLocation = DirectiveLocation; \ No newline at end of file diff --git a/dist/language/directiveLocation.js.flow b/dist/language/directiveLocation.js.flow new file mode 100644 index 0000000000..0a87001c36 --- /dev/null +++ b/dist/language/directiveLocation.js.flow @@ -0,0 +1,39 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * The set of allowed directive location values. + */ +export const DirectiveLocation = Object.freeze({ + // Request Definitions + QUERY: 'QUERY', + MUTATION: 'MUTATION', + SUBSCRIPTION: 'SUBSCRIPTION', + FIELD: 'FIELD', + FRAGMENT_DEFINITION: 'FRAGMENT_DEFINITION', + FRAGMENT_SPREAD: 'FRAGMENT_SPREAD', + INLINE_FRAGMENT: 'INLINE_FRAGMENT', + // Type System Definitions + SCHEMA: 'SCHEMA', + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + FIELD_DEFINITION: 'FIELD_DEFINITION', + ARGUMENT_DEFINITION: 'ARGUMENT_DEFINITION', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + ENUM_VALUE: 'ENUM_VALUE', + INPUT_OBJECT: 'INPUT_OBJECT', + INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION', +}); + +/** + * The enum type representing the directive location values. + */ +export type DirectiveLocationEnum = $Values; diff --git a/dist/language/directiveLocation.mjs b/dist/language/directiveLocation.mjs new file mode 100644 index 0000000000..7b59a6efc7 --- /dev/null +++ b/dist/language/directiveLocation.mjs @@ -0,0 +1,37 @@ +/** + * 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. + * + * strict + */ + +/** + * The set of allowed directive location values. + */ +export var DirectiveLocation = Object.freeze({ + // Request Definitions + QUERY: 'QUERY', + MUTATION: 'MUTATION', + SUBSCRIPTION: 'SUBSCRIPTION', + FIELD: 'FIELD', + FRAGMENT_DEFINITION: 'FRAGMENT_DEFINITION', + FRAGMENT_SPREAD: 'FRAGMENT_SPREAD', + INLINE_FRAGMENT: 'INLINE_FRAGMENT', + // Type System Definitions + SCHEMA: 'SCHEMA', + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + FIELD_DEFINITION: 'FIELD_DEFINITION', + ARGUMENT_DEFINITION: 'ARGUMENT_DEFINITION', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + ENUM_VALUE: 'ENUM_VALUE', + INPUT_OBJECT: 'INPUT_OBJECT', + INPUT_FIELD_DEFINITION: 'INPUT_FIELD_DEFINITION' +}); +/** + * The enum type representing the directive location values. + */ \ No newline at end of file diff --git a/dist/language/index.js b/dist/language/index.js new file mode 100644 index 0000000000..f8a337225d --- /dev/null +++ b/dist/language/index.js @@ -0,0 +1,111 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "getLocation", { + enumerable: true, + get: function get() { + return _location.getLocation; + } +}); +Object.defineProperty(exports, "Kind", { + enumerable: true, + get: function get() { + return _kinds.Kind; + } +}); +Object.defineProperty(exports, "createLexer", { + enumerable: true, + get: function get() { + return _lexer.createLexer; + } +}); +Object.defineProperty(exports, "TokenKind", { + enumerable: true, + get: function get() { + return _lexer.TokenKind; + } +}); +Object.defineProperty(exports, "parse", { + enumerable: true, + get: function get() { + return _parser.parse; + } +}); +Object.defineProperty(exports, "parseValue", { + enumerable: true, + get: function get() { + return _parser.parseValue; + } +}); +Object.defineProperty(exports, "parseType", { + enumerable: true, + get: function get() { + return _parser.parseType; + } +}); +Object.defineProperty(exports, "print", { + enumerable: true, + get: function get() { + return _printer.print; + } +}); +Object.defineProperty(exports, "Source", { + enumerable: true, + get: function get() { + return _source.Source; + } +}); +Object.defineProperty(exports, "visit", { + enumerable: true, + get: function get() { + return _visitor.visit; + } +}); +Object.defineProperty(exports, "visitInParallel", { + enumerable: true, + get: function get() { + return _visitor.visitInParallel; + } +}); +Object.defineProperty(exports, "visitWithTypeInfo", { + enumerable: true, + get: function get() { + return _visitor.visitWithTypeInfo; + } +}); +Object.defineProperty(exports, "getVisitFn", { + enumerable: true, + get: function get() { + return _visitor.getVisitFn; + } +}); +Object.defineProperty(exports, "BREAK", { + enumerable: true, + get: function get() { + return _visitor.BREAK; + } +}); +Object.defineProperty(exports, "DirectiveLocation", { + enumerable: true, + get: function get() { + return _directiveLocation.DirectiveLocation; + } +}); + +var _location = require("./location"); + +var _kinds = require("./kinds"); + +var _lexer = require("./lexer"); + +var _parser = require("./parser"); + +var _printer = require("./printer"); + +var _source = require("./source"); + +var _visitor = require("./visitor"); + +var _directiveLocation = require("./directiveLocation"); \ No newline at end of file diff --git a/dist/language/index.js.flow b/dist/language/index.js.flow new file mode 100644 index 0000000000..e49e8917d1 --- /dev/null +++ b/dist/language/index.js.flow @@ -0,0 +1,92 @@ +/** + * 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. + * + * @flow strict + */ + +export { getLocation } from './location'; +export type { SourceLocation } from './location'; +export { Kind } from './kinds'; +export type { KindEnum } from './kinds'; +export { createLexer, TokenKind } from './lexer'; +export { parse, parseValue, parseType } from './parser'; +export { print } from './printer'; +export { Source } from './source'; +export { + visit, + visitInParallel, + visitWithTypeInfo, + getVisitFn, + BREAK, +} from './visitor'; +export type { ASTVisitor, Visitor, VisitFn, VisitorKeyMap } from './visitor'; + +export type { Lexer, TokenKindEnum } from './lexer'; +export type { ParseOptions } from './parser'; + +export type { + Location, + Token, + ASTNode, + ASTKindToNode, + // Each kind of AST node + NameNode, + DocumentNode, + DefinitionNode, + ExecutableDefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + VariableNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + ValueNode, + IntValueNode, + FloatValueNode, + StringValueNode, + BooleanValueNode, + NullValueNode, + EnumValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + TypeSystemDefinitionNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, + TypeSystemExtensionNode, + SchemaExtensionNode, + TypeExtensionNode, + ScalarTypeExtensionNode, + ObjectTypeExtensionNode, + InterfaceTypeExtensionNode, + UnionTypeExtensionNode, + EnumTypeExtensionNode, + InputObjectTypeExtensionNode, +} from './ast'; + +export { DirectiveLocation } from './directiveLocation'; +export type { DirectiveLocationEnum } from './directiveLocation'; diff --git a/dist/language/index.mjs b/dist/language/index.mjs new file mode 100644 index 0000000000..a3220b82ec --- /dev/null +++ b/dist/language/index.mjs @@ -0,0 +1,16 @@ +/** + * 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. + * + * strict + */ +export { getLocation } from './location'; +export { Kind } from './kinds'; +export { createLexer, TokenKind } from './lexer'; +export { parse, parseValue, parseType } from './parser'; +export { print } from './printer'; +export { Source } from './source'; +export { visit, visitInParallel, visitWithTypeInfo, getVisitFn, BREAK } from './visitor'; +export { DirectiveLocation } from './directiveLocation'; \ No newline at end of file diff --git a/dist/language/kinds.js b/dist/language/kinds.js new file mode 100644 index 0000000000..bf79c9c44d --- /dev/null +++ b/dist/language/kinds.js @@ -0,0 +1,80 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Kind = void 0; + +/** + * 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. + * + * strict + */ + +/** + * The set of allowed kind values for AST nodes. + */ +var Kind = Object.freeze({ + // Name + NAME: 'Name', + // Document + DOCUMENT: 'Document', + OPERATION_DEFINITION: 'OperationDefinition', + VARIABLE_DEFINITION: 'VariableDefinition', + VARIABLE: 'Variable', + SELECTION_SET: 'SelectionSet', + FIELD: 'Field', + ARGUMENT: 'Argument', + // Fragments + FRAGMENT_SPREAD: 'FragmentSpread', + INLINE_FRAGMENT: 'InlineFragment', + FRAGMENT_DEFINITION: 'FragmentDefinition', + // Values + INT: 'IntValue', + FLOAT: 'FloatValue', + STRING: 'StringValue', + BOOLEAN: 'BooleanValue', + NULL: 'NullValue', + ENUM: 'EnumValue', + LIST: 'ListValue', + OBJECT: 'ObjectValue', + OBJECT_FIELD: 'ObjectField', + // Directives + DIRECTIVE: 'Directive', + // Types + NAMED_TYPE: 'NamedType', + LIST_TYPE: 'ListType', + NON_NULL_TYPE: 'NonNullType', + // Type System Definitions + SCHEMA_DEFINITION: 'SchemaDefinition', + OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', + // Type Definitions + SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', + OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', + FIELD_DEFINITION: 'FieldDefinition', + INPUT_VALUE_DEFINITION: 'InputValueDefinition', + INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', + UNION_TYPE_DEFINITION: 'UnionTypeDefinition', + ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', + ENUM_VALUE_DEFINITION: 'EnumValueDefinition', + INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', + // Directive Definitions + DIRECTIVE_DEFINITION: 'DirectiveDefinition', + // Type System Extensions + SCHEMA_EXTENSION: 'SchemaExtension', + // Type Extensions + SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', + OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', + INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', + UNION_TYPE_EXTENSION: 'UnionTypeExtension', + ENUM_TYPE_EXTENSION: 'EnumTypeExtension', + INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension' +}); +/** + * The enum type representing the possible kind values of AST nodes. + */ + +exports.Kind = Kind; \ No newline at end of file diff --git a/dist/language/kinds.js.flow b/dist/language/kinds.js.flow new file mode 100644 index 0000000000..c6f1587ee4 --- /dev/null +++ b/dist/language/kinds.js.flow @@ -0,0 +1,83 @@ +/** + * 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. + * + * @flow strict + */ + +/** + * The set of allowed kind values for AST nodes. + */ +export const Kind = Object.freeze({ + // Name + NAME: 'Name', + + // Document + DOCUMENT: 'Document', + OPERATION_DEFINITION: 'OperationDefinition', + VARIABLE_DEFINITION: 'VariableDefinition', + VARIABLE: 'Variable', + SELECTION_SET: 'SelectionSet', + FIELD: 'Field', + ARGUMENT: 'Argument', + + // Fragments + FRAGMENT_SPREAD: 'FragmentSpread', + INLINE_FRAGMENT: 'InlineFragment', + FRAGMENT_DEFINITION: 'FragmentDefinition', + + // Values + INT: 'IntValue', + FLOAT: 'FloatValue', + STRING: 'StringValue', + BOOLEAN: 'BooleanValue', + NULL: 'NullValue', + ENUM: 'EnumValue', + LIST: 'ListValue', + OBJECT: 'ObjectValue', + OBJECT_FIELD: 'ObjectField', + + // Directives + DIRECTIVE: 'Directive', + + // Types + NAMED_TYPE: 'NamedType', + LIST_TYPE: 'ListType', + NON_NULL_TYPE: 'NonNullType', + + // Type System Definitions + SCHEMA_DEFINITION: 'SchemaDefinition', + OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', + + // Type Definitions + SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', + OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', + FIELD_DEFINITION: 'FieldDefinition', + INPUT_VALUE_DEFINITION: 'InputValueDefinition', + INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', + UNION_TYPE_DEFINITION: 'UnionTypeDefinition', + ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', + ENUM_VALUE_DEFINITION: 'EnumValueDefinition', + INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', + + // Directive Definitions + DIRECTIVE_DEFINITION: 'DirectiveDefinition', + + // Type System Extensions + SCHEMA_EXTENSION: 'SchemaExtension', + + // Type Extensions + SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', + OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', + INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', + UNION_TYPE_EXTENSION: 'UnionTypeExtension', + ENUM_TYPE_EXTENSION: 'EnumTypeExtension', + INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension', +}); + +/** + * The enum type representing the possible kind values of AST nodes. + */ +export type KindEnum = $Values; diff --git a/dist/language/kinds.mjs b/dist/language/kinds.mjs new file mode 100644 index 0000000000..2c84dfdc19 --- /dev/null +++ b/dist/language/kinds.mjs @@ -0,0 +1,71 @@ +/** + * 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. + * + * strict + */ + +/** + * The set of allowed kind values for AST nodes. + */ +export var Kind = Object.freeze({ + // Name + NAME: 'Name', + // Document + DOCUMENT: 'Document', + OPERATION_DEFINITION: 'OperationDefinition', + VARIABLE_DEFINITION: 'VariableDefinition', + VARIABLE: 'Variable', + SELECTION_SET: 'SelectionSet', + FIELD: 'Field', + ARGUMENT: 'Argument', + // Fragments + FRAGMENT_SPREAD: 'FragmentSpread', + INLINE_FRAGMENT: 'InlineFragment', + FRAGMENT_DEFINITION: 'FragmentDefinition', + // Values + INT: 'IntValue', + FLOAT: 'FloatValue', + STRING: 'StringValue', + BOOLEAN: 'BooleanValue', + NULL: 'NullValue', + ENUM: 'EnumValue', + LIST: 'ListValue', + OBJECT: 'ObjectValue', + OBJECT_FIELD: 'ObjectField', + // Directives + DIRECTIVE: 'Directive', + // Types + NAMED_TYPE: 'NamedType', + LIST_TYPE: 'ListType', + NON_NULL_TYPE: 'NonNullType', + // Type System Definitions + SCHEMA_DEFINITION: 'SchemaDefinition', + OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', + // Type Definitions + SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', + OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', + FIELD_DEFINITION: 'FieldDefinition', + INPUT_VALUE_DEFINITION: 'InputValueDefinition', + INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', + UNION_TYPE_DEFINITION: 'UnionTypeDefinition', + ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', + ENUM_VALUE_DEFINITION: 'EnumValueDefinition', + INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', + // Directive Definitions + DIRECTIVE_DEFINITION: 'DirectiveDefinition', + // Type System Extensions + SCHEMA_EXTENSION: 'SchemaExtension', + // Type Extensions + SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', + OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', + INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', + UNION_TYPE_EXTENSION: 'UnionTypeExtension', + ENUM_TYPE_EXTENSION: 'EnumTypeExtension', + INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension' +}); +/** + * The enum type representing the possible kind values of AST nodes. + */ \ No newline at end of file diff --git a/dist/language/lexer.js b/dist/language/lexer.js new file mode 100644 index 0000000000..29c2ceb1e8 --- /dev/null +++ b/dist/language/lexer.js @@ -0,0 +1,648 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.createLexer = createLexer; +exports.getTokenDesc = getTokenDesc; +exports.TokenKind = void 0; + +var _error = require("../error"); + +var _blockStringValue = _interopRequireDefault(require("./blockStringValue")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Given a Source object, this returns a Lexer for that source. + * A Lexer is a stateful stream generator in that every time + * it is advanced, it returns the next token in the Source. Assuming the + * source lexes, the final Token emitted by the lexer will be of kind + * EOF, after which the lexer will repeatedly return the same EOF token + * whenever called. + */ +function createLexer(source, options) { + var startOfFileToken = new Tok(TokenKind.SOF, 0, 0, 0, 0, null); + var lexer = { + source: source, + options: options, + lastToken: startOfFileToken, + token: startOfFileToken, + line: 1, + lineStart: 0, + advance: advanceLexer, + lookahead: lookahead + }; + return lexer; +} + +function advanceLexer() { + this.lastToken = this.token; + var token = this.token = this.lookahead(); + return token; +} + +function lookahead() { + var token = this.token; + + if (token.kind !== TokenKind.EOF) { + do { + // Note: next is only mutable during parsing, so we cast to allow this. + token = token.next || (token.next = readToken(this, token)); + } while (token.kind === TokenKind.COMMENT); + } + + return token; +} +/** + * The return type of createLexer. + */ + + +/** + * An exported enum describing the different kinds of tokens that the + * lexer emits. + */ +var TokenKind = Object.freeze({ + SOF: '', + EOF: '', + BANG: '!', + DOLLAR: '$', + AMP: '&', + PAREN_L: '(', + PAREN_R: ')', + SPREAD: '...', + COLON: ':', + EQUALS: '=', + AT: '@', + BRACKET_L: '[', + BRACKET_R: ']', + BRACE_L: '{', + PIPE: '|', + BRACE_R: '}', + NAME: 'Name', + INT: 'Int', + FLOAT: 'Float', + STRING: 'String', + BLOCK_STRING: 'BlockString', + COMMENT: 'Comment' +}); +/** + * The enum type representing the token kinds values. + */ + +exports.TokenKind = TokenKind; + +/** + * A helper function to describe a token as a string for debugging + */ +function getTokenDesc(token) { + var value = token.value; + return value ? "".concat(token.kind, " \"").concat(value, "\"") : token.kind; +} + +var charCodeAt = String.prototype.charCodeAt; +var slice = String.prototype.slice; +/** + * Helper function for constructing the Token object. + */ + +function Tok(kind, start, end, line, column, prev, value) { + this.kind = kind; + this.start = start; + this.end = end; + this.line = line; + this.column = column; + this.value = value; + this.prev = prev; + this.next = null; +} // Print a simplified form when appearing in JSON/util.inspect. + + +Tok.prototype.toJSON = Tok.prototype.inspect = function toJSON() { + return { + kind: this.kind, + value: this.value, + line: this.line, + column: this.column + }; +}; + +function printCharCode(code) { + return (// NaN/undefined represents access beyond the end of the file. + isNaN(code) ? TokenKind.EOF : // Trust JSON for ASCII. + code < 0x007f ? JSON.stringify(String.fromCharCode(code)) : // Otherwise print the escaped form. + "\"\\u".concat(('00' + code.toString(16).toUpperCase()).slice(-4), "\"") + ); +} +/** + * Gets the next token from the source starting at the given position. + * + * This skips over whitespace and comments until it finds the next lexable + * token, then lexes punctuators immediately or calls the appropriate helper + * function for more complicated tokens. + */ + + +function readToken(lexer, prev) { + var source = lexer.source; + var body = source.body; + var bodyLength = body.length; + var pos = positionAfterWhitespace(body, prev.end, lexer); + var line = lexer.line; + var col = 1 + pos - lexer.lineStart; + + if (pos >= bodyLength) { + return new Tok(TokenKind.EOF, bodyLength, bodyLength, line, col, prev); + } + + var code = charCodeAt.call(body, pos); // SourceCharacter + + switch (code) { + // ! + case 33: + return new Tok(TokenKind.BANG, pos, pos + 1, line, col, prev); + // # + + case 35: + return readComment(source, pos, line, col, prev); + // $ + + case 36: + return new Tok(TokenKind.DOLLAR, pos, pos + 1, line, col, prev); + // & + + case 38: + return new Tok(TokenKind.AMP, pos, pos + 1, line, col, prev); + // ( + + case 40: + return new Tok(TokenKind.PAREN_L, pos, pos + 1, line, col, prev); + // ) + + case 41: + return new Tok(TokenKind.PAREN_R, pos, pos + 1, line, col, prev); + // . + + case 46: + if (charCodeAt.call(body, pos + 1) === 46 && charCodeAt.call(body, pos + 2) === 46) { + return new Tok(TokenKind.SPREAD, pos, pos + 3, line, col, prev); + } + + break; + // : + + case 58: + return new Tok(TokenKind.COLON, pos, pos + 1, line, col, prev); + // = + + case 61: + return new Tok(TokenKind.EQUALS, pos, pos + 1, line, col, prev); + // @ + + case 64: + return new Tok(TokenKind.AT, pos, pos + 1, line, col, prev); + // [ + + case 91: + return new Tok(TokenKind.BRACKET_L, pos, pos + 1, line, col, prev); + // ] + + case 93: + return new Tok(TokenKind.BRACKET_R, pos, pos + 1, line, col, prev); + // { + + case 123: + return new Tok(TokenKind.BRACE_L, pos, pos + 1, line, col, prev); + // | + + case 124: + return new Tok(TokenKind.PIPE, pos, pos + 1, line, col, prev); + // } + + case 125: + return new Tok(TokenKind.BRACE_R, pos, pos + 1, line, col, prev); + // A-Z _ a-z + + case 65: + case 66: + case 67: + case 68: + case 69: + case 70: + case 71: + case 72: + case 73: + case 74: + case 75: + case 76: + case 77: + case 78: + case 79: + case 80: + case 81: + case 82: + case 83: + case 84: + case 85: + case 86: + case 87: + case 88: + case 89: + case 90: + case 95: + case 97: + case 98: + case 99: + case 100: + case 101: + case 102: + case 103: + case 104: + case 105: + case 106: + case 107: + case 108: + case 109: + case 110: + case 111: + case 112: + case 113: + case 114: + case 115: + case 116: + case 117: + case 118: + case 119: + case 120: + case 121: + case 122: + return readName(source, pos, line, col, prev); + // - 0-9 + + case 45: + case 48: + case 49: + case 50: + case 51: + case 52: + case 53: + case 54: + case 55: + case 56: + case 57: + return readNumber(source, pos, code, line, col, prev); + // " + + case 34: + if (charCodeAt.call(body, pos + 1) === 34 && charCodeAt.call(body, pos + 2) === 34) { + return readBlockString(source, pos, line, col, prev); + } + + return readString(source, pos, line, col, prev); + } + + throw (0, _error.syntaxError)(source, pos, unexpectedCharacterMessage(code)); +} +/** + * Report a message that an unexpected character was encountered. + */ + + +function unexpectedCharacterMessage(code) { + if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) { + return "Cannot contain the invalid character ".concat(printCharCode(code), "."); + } + + if (code === 39) { + // ' + return "Unexpected single quote character ('), did you mean to use " + 'a double quote (")?'; + } + + return "Cannot parse the unexpected character ".concat(printCharCode(code), "."); +} +/** + * Reads from body starting at startPosition until it finds a non-whitespace + * or commented character, then returns the position of that character for + * lexing. + */ + + +function positionAfterWhitespace(body, startPosition, lexer) { + var bodyLength = body.length; + var position = startPosition; + + while (position < bodyLength) { + var code = charCodeAt.call(body, position); // tab | space | comma | BOM + + if (code === 9 || code === 32 || code === 44 || code === 0xfeff) { + ++position; + } else if (code === 10) { + // new line + ++position; + ++lexer.line; + lexer.lineStart = position; + } else if (code === 13) { + // carriage return + if (charCodeAt.call(body, position + 1) === 10) { + position += 2; + } else { + ++position; + } + + ++lexer.line; + lexer.lineStart = position; + } else { + break; + } + } + + return position; +} +/** + * Reads a comment token from the source file. + * + * #[\u0009\u0020-\uFFFF]* + */ + + +function readComment(source, start, line, col, prev) { + var body = source.body; + var code; + var position = start; + + do { + code = charCodeAt.call(body, ++position); + } while (code !== null && ( // SourceCharacter but not LineTerminator + code > 0x001f || code === 0x0009)); + + return new Tok(TokenKind.COMMENT, start, position, line, col, prev, slice.call(body, start + 1, position)); +} +/** + * Reads a number token from the source file, either a float + * or an int depending on whether a decimal point appears. + * + * Int: -?(0|[1-9][0-9]*) + * Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)? + */ + + +function readNumber(source, start, firstCode, line, col, prev) { + var body = source.body; + var code = firstCode; + var position = start; + var isFloat = false; + + if (code === 45) { + // - + code = charCodeAt.call(body, ++position); + } + + if (code === 48) { + // 0 + code = charCodeAt.call(body, ++position); + + if (code >= 48 && code <= 57) { + throw (0, _error.syntaxError)(source, position, "Invalid number, unexpected digit after 0: ".concat(printCharCode(code), ".")); + } + } else { + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 46) { + // . + isFloat = true; + code = charCodeAt.call(body, ++position); + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 69 || code === 101) { + // E e + isFloat = true; + code = charCodeAt.call(body, ++position); + + if (code === 43 || code === 45) { + // + - + code = charCodeAt.call(body, ++position); + } + + position = readDigits(source, position, code); + } + + return new Tok(isFloat ? TokenKind.FLOAT : TokenKind.INT, start, position, line, col, prev, slice.call(body, start, position)); +} +/** + * Returns the new position in the source after reading digits. + */ + + +function readDigits(source, start, firstCode) { + var body = source.body; + var position = start; + var code = firstCode; + + if (code >= 48 && code <= 57) { + // 0 - 9 + do { + code = charCodeAt.call(body, ++position); + } while (code >= 48 && code <= 57); // 0 - 9 + + + return position; + } + + throw (0, _error.syntaxError)(source, position, "Invalid number, expected digit but got: ".concat(printCharCode(code), ".")); +} +/** + * Reads a string token from the source file. + * + * "([^"\\\u000A\u000D]|(\\(u[0-9a-fA-F]{4}|["\\/bfnrt])))*" + */ + + +function readString(source, start, line, col, prev) { + var body = source.body; + var position = start + 1; + var chunkStart = position; + var code = 0; + var value = ''; + + while (position < body.length && (code = charCodeAt.call(body, position)) !== null && // not LineTerminator + code !== 0x000a && code !== 0x000d) { + // Closing Quote (") + if (code === 34) { + value += slice.call(body, chunkStart, position); + return new Tok(TokenKind.STRING, start, position + 1, line, col, prev, value); + } // SourceCharacter + + + if (code < 0x0020 && code !== 0x0009) { + throw (0, _error.syntaxError)(source, position, "Invalid character within String: ".concat(printCharCode(code), ".")); + } + + ++position; + + if (code === 92) { + // \ + value += slice.call(body, chunkStart, position - 1); + code = charCodeAt.call(body, position); + + switch (code) { + case 34: + value += '"'; + break; + + case 47: + value += '/'; + break; + + case 92: + value += '\\'; + break; + + case 98: + value += '\b'; + break; + + case 102: + value += '\f'; + break; + + case 110: + value += '\n'; + break; + + case 114: + value += '\r'; + break; + + case 116: + value += '\t'; + break; + + case 117: + // u + var charCode = uniCharCode(charCodeAt.call(body, position + 1), charCodeAt.call(body, position + 2), charCodeAt.call(body, position + 3), charCodeAt.call(body, position + 4)); + + if (charCode < 0) { + throw (0, _error.syntaxError)(source, position, 'Invalid character escape sequence: ' + "\\u".concat(body.slice(position + 1, position + 5), ".")); + } + + value += String.fromCharCode(charCode); + position += 4; + break; + + default: + throw (0, _error.syntaxError)(source, position, "Invalid character escape sequence: \\".concat(String.fromCharCode(code), ".")); + } + + ++position; + chunkStart = position; + } + } + + throw (0, _error.syntaxError)(source, position, 'Unterminated string.'); +} +/** + * Reads a block string token from the source file. + * + * """("?"?(\\"""|\\(?!=""")|[^"\\]))*""" + */ + + +function readBlockString(source, start, line, col, prev) { + var body = source.body; + var position = start + 3; + var chunkStart = position; + var code = 0; + var rawValue = ''; + + while (position < body.length && (code = charCodeAt.call(body, position)) !== null) { + // Closing Triple-Quote (""") + if (code === 34 && charCodeAt.call(body, position + 1) === 34 && charCodeAt.call(body, position + 2) === 34) { + rawValue += slice.call(body, chunkStart, position); + return new Tok(TokenKind.BLOCK_STRING, start, position + 3, line, col, prev, (0, _blockStringValue.default)(rawValue)); + } // SourceCharacter + + + if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) { + throw (0, _error.syntaxError)(source, position, "Invalid character within String: ".concat(printCharCode(code), ".")); + } // Escape Triple-Quote (\""") + + + if (code === 92 && charCodeAt.call(body, position + 1) === 34 && charCodeAt.call(body, position + 2) === 34 && charCodeAt.call(body, position + 3) === 34) { + rawValue += slice.call(body, chunkStart, position) + '"""'; + position += 4; + chunkStart = position; + } else { + ++position; + } + } + + throw (0, _error.syntaxError)(source, position, 'Unterminated string.'); +} +/** + * Converts four hexidecimal chars to the integer that the + * string represents. For example, uniCharCode('0','0','0','f') + * will return 15, and uniCharCode('0','0','f','f') returns 255. + * + * Returns a negative number on error, if a char was invalid. + * + * This is implemented by noting that char2hex() returns -1 on error, + * which means the result of ORing the char2hex() will also be negative. + */ + + +function uniCharCode(a, b, c, d) { + return char2hex(a) << 12 | char2hex(b) << 8 | char2hex(c) << 4 | char2hex(d); +} +/** + * Converts a hex character to its integer value. + * '0' becomes 0, '9' becomes 9 + * 'A' becomes 10, 'F' becomes 15 + * 'a' becomes 10, 'f' becomes 15 + * + * Returns -1 on error. + */ + + +function char2hex(a) { + return a >= 48 && a <= 57 ? a - 48 // 0-9 + : a >= 65 && a <= 70 ? a - 55 // A-F + : a >= 97 && a <= 102 ? a - 87 // a-f + : -1; +} +/** + * Reads an alphanumeric + underscore name from the source. + * + * [_A-Za-z][_0-9A-Za-z]* + */ + + +function readName(source, start, line, col, prev) { + var body = source.body; + var bodyLength = body.length; + var position = start + 1; + var code = 0; + + while (position !== bodyLength && (code = charCodeAt.call(body, position)) !== null && (code === 95 || // _ + code >= 48 && code <= 57 || // 0-9 + code >= 65 && code <= 90 || // A-Z + code >= 97 && code <= 122) // a-z + ) { + ++position; + } + + return new Tok(TokenKind.NAME, start, position, line, col, prev, slice.call(body, start, position)); +} \ No newline at end of file diff --git a/dist/language/lexer.js.flow b/dist/language/lexer.js.flow new file mode 100644 index 0000000000..36d6d82cb3 --- /dev/null +++ b/dist/language/lexer.js.flow @@ -0,0 +1,752 @@ +/** + * 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. + * + * @flow strict + */ + +import type { Token } from './ast'; +import type { Source } from './source'; +import { syntaxError } from '../error'; +import blockStringValue from './blockStringValue'; + +/** + * Given a Source object, this returns a Lexer for that source. + * A Lexer is a stateful stream generator in that every time + * it is advanced, it returns the next token in the Source. Assuming the + * source lexes, the final Token emitted by the lexer will be of kind + * EOF, after which the lexer will repeatedly return the same EOF token + * whenever called. + */ +export function createLexer( + source: Source, + options: TOptions, +): Lexer { + const startOfFileToken = new Tok(TokenKind.SOF, 0, 0, 0, 0, null); + const lexer: Lexer = { + source, + options, + lastToken: startOfFileToken, + token: startOfFileToken, + line: 1, + lineStart: 0, + advance: advanceLexer, + lookahead, + }; + return lexer; +} + +function advanceLexer() { + this.lastToken = this.token; + const token = (this.token = this.lookahead()); + return token; +} + +function lookahead() { + let token = this.token; + if (token.kind !== TokenKind.EOF) { + do { + // Note: next is only mutable during parsing, so we cast to allow this. + token = token.next || ((token: any).next = readToken(this, token)); + } while (token.kind === TokenKind.COMMENT); + } + return token; +} + +/** + * The return type of createLexer. + */ +export type Lexer = { + source: Source, + options: TOptions, + + /** + * The previously focused non-ignored token. + */ + lastToken: Token, + + /** + * The currently focused non-ignored token. + */ + token: Token, + + /** + * The (1-indexed) line containing the current token. + */ + line: number, + + /** + * The character offset at which the current line begins. + */ + lineStart: number, + + /** + * Advances the token stream to the next non-ignored token. + */ + advance(): Token, + + /** + * Looks ahead and returns the next non-ignored token, but does not change + * the Lexer's state. + */ + lookahead(): Token, +}; + +/** + * An exported enum describing the different kinds of tokens that the + * lexer emits. + */ +export const TokenKind = Object.freeze({ + SOF: '', + EOF: '', + BANG: '!', + DOLLAR: '$', + AMP: '&', + PAREN_L: '(', + PAREN_R: ')', + SPREAD: '...', + COLON: ':', + EQUALS: '=', + AT: '@', + BRACKET_L: '[', + BRACKET_R: ']', + BRACE_L: '{', + PIPE: '|', + BRACE_R: '}', + NAME: 'Name', + INT: 'Int', + FLOAT: 'Float', + STRING: 'String', + BLOCK_STRING: 'BlockString', + COMMENT: 'Comment', +}); + +/** + * The enum type representing the token kinds values. + */ +export type TokenKindEnum = $Values; + +/** + * A helper function to describe a token as a string for debugging + */ +export function getTokenDesc(token: Token): string { + const value = token.value; + return value ? `${token.kind} "${value}"` : token.kind; +} + +const charCodeAt = String.prototype.charCodeAt; +const slice = String.prototype.slice; + +/** + * Helper function for constructing the Token object. + */ +function Tok( + kind: TokenKindEnum, + start: number, + end: number, + line: number, + column: number, + prev: Token | null, + value?: string, +) { + this.kind = kind; + this.start = start; + this.end = end; + this.line = line; + this.column = column; + this.value = value; + this.prev = prev; + this.next = null; +} + +// Print a simplified form when appearing in JSON/util.inspect. +Tok.prototype.toJSON = Tok.prototype.inspect = function toJSON() { + return { + kind: this.kind, + value: this.value, + line: this.line, + column: this.column, + }; +}; + +function printCharCode(code) { + return ( + // NaN/undefined represents access beyond the end of the file. + isNaN(code) + ? TokenKind.EOF + : // Trust JSON for ASCII. + code < 0x007f + ? JSON.stringify(String.fromCharCode(code)) + : // Otherwise print the escaped form. + `"\\u${('00' + code.toString(16).toUpperCase()).slice(-4)}"` + ); +} + +/** + * Gets the next token from the source starting at the given position. + * + * This skips over whitespace and comments until it finds the next lexable + * token, then lexes punctuators immediately or calls the appropriate helper + * function for more complicated tokens. + */ +function readToken(lexer: Lexer<*>, prev: Token): Token { + const source = lexer.source; + const body = source.body; + const bodyLength = body.length; + + const pos = positionAfterWhitespace(body, prev.end, lexer); + const line = lexer.line; + const col = 1 + pos - lexer.lineStart; + + if (pos >= bodyLength) { + return new Tok(TokenKind.EOF, bodyLength, bodyLength, line, col, prev); + } + + const code = charCodeAt.call(body, pos); + + // SourceCharacter + switch (code) { + // ! + case 33: + return new Tok(TokenKind.BANG, pos, pos + 1, line, col, prev); + // # + case 35: + return readComment(source, pos, line, col, prev); + // $ + case 36: + return new Tok(TokenKind.DOLLAR, pos, pos + 1, line, col, prev); + // & + case 38: + return new Tok(TokenKind.AMP, pos, pos + 1, line, col, prev); + // ( + case 40: + return new Tok(TokenKind.PAREN_L, pos, pos + 1, line, col, prev); + // ) + case 41: + return new Tok(TokenKind.PAREN_R, pos, pos + 1, line, col, prev); + // . + case 46: + if ( + charCodeAt.call(body, pos + 1) === 46 && + charCodeAt.call(body, pos + 2) === 46 + ) { + return new Tok(TokenKind.SPREAD, pos, pos + 3, line, col, prev); + } + break; + // : + case 58: + return new Tok(TokenKind.COLON, pos, pos + 1, line, col, prev); + // = + case 61: + return new Tok(TokenKind.EQUALS, pos, pos + 1, line, col, prev); + // @ + case 64: + return new Tok(TokenKind.AT, pos, pos + 1, line, col, prev); + // [ + case 91: + return new Tok(TokenKind.BRACKET_L, pos, pos + 1, line, col, prev); + // ] + case 93: + return new Tok(TokenKind.BRACKET_R, pos, pos + 1, line, col, prev); + // { + case 123: + return new Tok(TokenKind.BRACE_L, pos, pos + 1, line, col, prev); + // | + case 124: + return new Tok(TokenKind.PIPE, pos, pos + 1, line, col, prev); + // } + case 125: + return new Tok(TokenKind.BRACE_R, pos, pos + 1, line, col, prev); + // A-Z _ a-z + case 65: + case 66: + case 67: + case 68: + case 69: + case 70: + case 71: + case 72: + case 73: + case 74: + case 75: + case 76: + case 77: + case 78: + case 79: + case 80: + case 81: + case 82: + case 83: + case 84: + case 85: + case 86: + case 87: + case 88: + case 89: + case 90: + case 95: + case 97: + case 98: + case 99: + case 100: + case 101: + case 102: + case 103: + case 104: + case 105: + case 106: + case 107: + case 108: + case 109: + case 110: + case 111: + case 112: + case 113: + case 114: + case 115: + case 116: + case 117: + case 118: + case 119: + case 120: + case 121: + case 122: + return readName(source, pos, line, col, prev); + // - 0-9 + case 45: + case 48: + case 49: + case 50: + case 51: + case 52: + case 53: + case 54: + case 55: + case 56: + case 57: + return readNumber(source, pos, code, line, col, prev); + // " + case 34: + if ( + charCodeAt.call(body, pos + 1) === 34 && + charCodeAt.call(body, pos + 2) === 34 + ) { + return readBlockString(source, pos, line, col, prev); + } + return readString(source, pos, line, col, prev); + } + + throw syntaxError(source, pos, unexpectedCharacterMessage(code)); +} + +/** + * Report a message that an unexpected character was encountered. + */ +function unexpectedCharacterMessage(code) { + if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) { + return `Cannot contain the invalid character ${printCharCode(code)}.`; + } + + if (code === 39) { + // ' + return ( + "Unexpected single quote character ('), did you mean to use " + + 'a double quote (")?' + ); + } + + return `Cannot parse the unexpected character ${printCharCode(code)}.`; +} + +/** + * Reads from body starting at startPosition until it finds a non-whitespace + * or commented character, then returns the position of that character for + * lexing. + */ +function positionAfterWhitespace( + body: string, + startPosition: number, + lexer: Lexer<*>, +): number { + const bodyLength = body.length; + let position = startPosition; + while (position < bodyLength) { + const code = charCodeAt.call(body, position); + // tab | space | comma | BOM + if (code === 9 || code === 32 || code === 44 || code === 0xfeff) { + ++position; + } else if (code === 10) { + // new line + ++position; + ++lexer.line; + lexer.lineStart = position; + } else if (code === 13) { + // carriage return + if (charCodeAt.call(body, position + 1) === 10) { + position += 2; + } else { + ++position; + } + ++lexer.line; + lexer.lineStart = position; + } else { + break; + } + } + return position; +} + +/** + * Reads a comment token from the source file. + * + * #[\u0009\u0020-\uFFFF]* + */ +function readComment(source, start, line, col, prev): Token { + const body = source.body; + let code; + let position = start; + + do { + code = charCodeAt.call(body, ++position); + } while ( + code !== null && + // SourceCharacter but not LineTerminator + (code > 0x001f || code === 0x0009) + ); + + return new Tok( + TokenKind.COMMENT, + start, + position, + line, + col, + prev, + slice.call(body, start + 1, position), + ); +} + +/** + * Reads a number token from the source file, either a float + * or an int depending on whether a decimal point appears. + * + * Int: -?(0|[1-9][0-9]*) + * Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)? + */ +function readNumber(source, start, firstCode, line, col, prev): Token { + const body = source.body; + let code = firstCode; + let position = start; + let isFloat = false; + + if (code === 45) { + // - + code = charCodeAt.call(body, ++position); + } + + if (code === 48) { + // 0 + code = charCodeAt.call(body, ++position); + if (code >= 48 && code <= 57) { + throw syntaxError( + source, + position, + `Invalid number, unexpected digit after 0: ${printCharCode(code)}.`, + ); + } + } else { + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 46) { + // . + isFloat = true; + + code = charCodeAt.call(body, ++position); + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 69 || code === 101) { + // E e + isFloat = true; + + code = charCodeAt.call(body, ++position); + if (code === 43 || code === 45) { + // + - + code = charCodeAt.call(body, ++position); + } + position = readDigits(source, position, code); + } + + return new Tok( + isFloat ? TokenKind.FLOAT : TokenKind.INT, + start, + position, + line, + col, + prev, + slice.call(body, start, position), + ); +} + +/** + * Returns the new position in the source after reading digits. + */ +function readDigits(source, start, firstCode) { + const body = source.body; + let position = start; + let code = firstCode; + if (code >= 48 && code <= 57) { + // 0 - 9 + do { + code = charCodeAt.call(body, ++position); + } while (code >= 48 && code <= 57); // 0 - 9 + return position; + } + throw syntaxError( + source, + position, + `Invalid number, expected digit but got: ${printCharCode(code)}.`, + ); +} + +/** + * Reads a string token from the source file. + * + * "([^"\\\u000A\u000D]|(\\(u[0-9a-fA-F]{4}|["\\/bfnrt])))*" + */ +function readString(source, start, line, col, prev): Token { + const body = source.body; + let position = start + 1; + let chunkStart = position; + let code = 0; + let value = ''; + + while ( + position < body.length && + (code = charCodeAt.call(body, position)) !== null && + // not LineTerminator + code !== 0x000a && + code !== 0x000d + ) { + // Closing Quote (") + if (code === 34) { + value += slice.call(body, chunkStart, position); + return new Tok( + TokenKind.STRING, + start, + position + 1, + line, + col, + prev, + value, + ); + } + + // SourceCharacter + if (code < 0x0020 && code !== 0x0009) { + throw syntaxError( + source, + position, + `Invalid character within String: ${printCharCode(code)}.`, + ); + } + + ++position; + if (code === 92) { + // \ + value += slice.call(body, chunkStart, position - 1); + code = charCodeAt.call(body, position); + switch (code) { + case 34: + value += '"'; + break; + case 47: + value += '/'; + break; + case 92: + value += '\\'; + break; + case 98: + value += '\b'; + break; + case 102: + value += '\f'; + break; + case 110: + value += '\n'; + break; + case 114: + value += '\r'; + break; + case 116: + value += '\t'; + break; + case 117: // u + const charCode = uniCharCode( + charCodeAt.call(body, position + 1), + charCodeAt.call(body, position + 2), + charCodeAt.call(body, position + 3), + charCodeAt.call(body, position + 4), + ); + if (charCode < 0) { + throw syntaxError( + source, + position, + 'Invalid character escape sequence: ' + + `\\u${body.slice(position + 1, position + 5)}.`, + ); + } + value += String.fromCharCode(charCode); + position += 4; + break; + default: + throw syntaxError( + source, + position, + `Invalid character escape sequence: \\${String.fromCharCode( + code, + )}.`, + ); + } + ++position; + chunkStart = position; + } + } + + throw syntaxError(source, position, 'Unterminated string.'); +} + +/** + * Reads a block string token from the source file. + * + * """("?"?(\\"""|\\(?!=""")|[^"\\]))*""" + */ +function readBlockString(source, start, line, col, prev): Token { + const body = source.body; + let position = start + 3; + let chunkStart = position; + let code = 0; + let rawValue = ''; + + while ( + position < body.length && + (code = charCodeAt.call(body, position)) !== null + ) { + // Closing Triple-Quote (""") + if ( + code === 34 && + charCodeAt.call(body, position + 1) === 34 && + charCodeAt.call(body, position + 2) === 34 + ) { + rawValue += slice.call(body, chunkStart, position); + return new Tok( + TokenKind.BLOCK_STRING, + start, + position + 3, + line, + col, + prev, + blockStringValue(rawValue), + ); + } + + // SourceCharacter + if ( + code < 0x0020 && + code !== 0x0009 && + code !== 0x000a && + code !== 0x000d + ) { + throw syntaxError( + source, + position, + `Invalid character within String: ${printCharCode(code)}.`, + ); + } + + // Escape Triple-Quote (\""") + if ( + code === 92 && + charCodeAt.call(body, position + 1) === 34 && + charCodeAt.call(body, position + 2) === 34 && + charCodeAt.call(body, position + 3) === 34 + ) { + rawValue += slice.call(body, chunkStart, position) + '"""'; + position += 4; + chunkStart = position; + } else { + ++position; + } + } + + throw syntaxError(source, position, 'Unterminated string.'); +} + +/** + * Converts four hexidecimal chars to the integer that the + * string represents. For example, uniCharCode('0','0','0','f') + * will return 15, and uniCharCode('0','0','f','f') returns 255. + * + * Returns a negative number on error, if a char was invalid. + * + * This is implemented by noting that char2hex() returns -1 on error, + * which means the result of ORing the char2hex() will also be negative. + */ +function uniCharCode(a, b, c, d) { + return ( + (char2hex(a) << 12) | (char2hex(b) << 8) | (char2hex(c) << 4) | char2hex(d) + ); +} + +/** + * Converts a hex character to its integer value. + * '0' becomes 0, '9' becomes 9 + * 'A' becomes 10, 'F' becomes 15 + * 'a' becomes 10, 'f' becomes 15 + * + * Returns -1 on error. + */ +function char2hex(a) { + return a >= 48 && a <= 57 + ? a - 48 // 0-9 + : a >= 65 && a <= 70 + ? a - 55 // A-F + : a >= 97 && a <= 102 + ? a - 87 // a-f + : -1; +} + +/** + * Reads an alphanumeric + underscore name from the source. + * + * [_A-Za-z][_0-9A-Za-z]* + */ +function readName(source, start, line, col, prev): Token { + const body = source.body; + const bodyLength = body.length; + let position = start + 1; + let code = 0; + while ( + position !== bodyLength && + (code = charCodeAt.call(body, position)) !== null && + (code === 95 || // _ + (code >= 48 && code <= 57) || // 0-9 + (code >= 65 && code <= 90) || // A-Z + (code >= 97 && code <= 122)) // a-z + ) { + ++position; + } + return new Tok( + TokenKind.NAME, + start, + position, + line, + col, + prev, + slice.call(body, start, position), + ); +} diff --git a/dist/language/lexer.mjs b/dist/language/lexer.mjs new file mode 100644 index 0000000000..78d8ef727f --- /dev/null +++ b/dist/language/lexer.mjs @@ -0,0 +1,632 @@ +/** + * 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. + * + * strict + */ +import { syntaxError } from '../error'; +import blockStringValue from './blockStringValue'; +/** + * Given a Source object, this returns a Lexer for that source. + * A Lexer is a stateful stream generator in that every time + * it is advanced, it returns the next token in the Source. Assuming the + * source lexes, the final Token emitted by the lexer will be of kind + * EOF, after which the lexer will repeatedly return the same EOF token + * whenever called. + */ + +export function createLexer(source, options) { + var startOfFileToken = new Tok(TokenKind.SOF, 0, 0, 0, 0, null); + var lexer = { + source: source, + options: options, + lastToken: startOfFileToken, + token: startOfFileToken, + line: 1, + lineStart: 0, + advance: advanceLexer, + lookahead: lookahead + }; + return lexer; +} + +function advanceLexer() { + this.lastToken = this.token; + var token = this.token = this.lookahead(); + return token; +} + +function lookahead() { + var token = this.token; + + if (token.kind !== TokenKind.EOF) { + do { + // Note: next is only mutable during parsing, so we cast to allow this. + token = token.next || (token.next = readToken(this, token)); + } while (token.kind === TokenKind.COMMENT); + } + + return token; +} +/** + * The return type of createLexer. + */ + + +/** + * An exported enum describing the different kinds of tokens that the + * lexer emits. + */ +export var TokenKind = Object.freeze({ + SOF: '', + EOF: '', + BANG: '!', + DOLLAR: '$', + AMP: '&', + PAREN_L: '(', + PAREN_R: ')', + SPREAD: '...', + COLON: ':', + EQUALS: '=', + AT: '@', + BRACKET_L: '[', + BRACKET_R: ']', + BRACE_L: '{', + PIPE: '|', + BRACE_R: '}', + NAME: 'Name', + INT: 'Int', + FLOAT: 'Float', + STRING: 'String', + BLOCK_STRING: 'BlockString', + COMMENT: 'Comment' +}); +/** + * The enum type representing the token kinds values. + */ + +/** + * A helper function to describe a token as a string for debugging + */ +export function getTokenDesc(token) { + var value = token.value; + return value ? "".concat(token.kind, " \"").concat(value, "\"") : token.kind; +} +var charCodeAt = String.prototype.charCodeAt; +var slice = String.prototype.slice; +/** + * Helper function for constructing the Token object. + */ + +function Tok(kind, start, end, line, column, prev, value) { + this.kind = kind; + this.start = start; + this.end = end; + this.line = line; + this.column = column; + this.value = value; + this.prev = prev; + this.next = null; +} // Print a simplified form when appearing in JSON/util.inspect. + + +Tok.prototype.toJSON = Tok.prototype.inspect = function toJSON() { + return { + kind: this.kind, + value: this.value, + line: this.line, + column: this.column + }; +}; + +function printCharCode(code) { + return (// NaN/undefined represents access beyond the end of the file. + isNaN(code) ? TokenKind.EOF : // Trust JSON for ASCII. + code < 0x007f ? JSON.stringify(String.fromCharCode(code)) : // Otherwise print the escaped form. + "\"\\u".concat(('00' + code.toString(16).toUpperCase()).slice(-4), "\"") + ); +} +/** + * Gets the next token from the source starting at the given position. + * + * This skips over whitespace and comments until it finds the next lexable + * token, then lexes punctuators immediately or calls the appropriate helper + * function for more complicated tokens. + */ + + +function readToken(lexer, prev) { + var source = lexer.source; + var body = source.body; + var bodyLength = body.length; + var pos = positionAfterWhitespace(body, prev.end, lexer); + var line = lexer.line; + var col = 1 + pos - lexer.lineStart; + + if (pos >= bodyLength) { + return new Tok(TokenKind.EOF, bodyLength, bodyLength, line, col, prev); + } + + var code = charCodeAt.call(body, pos); // SourceCharacter + + switch (code) { + // ! + case 33: + return new Tok(TokenKind.BANG, pos, pos + 1, line, col, prev); + // # + + case 35: + return readComment(source, pos, line, col, prev); + // $ + + case 36: + return new Tok(TokenKind.DOLLAR, pos, pos + 1, line, col, prev); + // & + + case 38: + return new Tok(TokenKind.AMP, pos, pos + 1, line, col, prev); + // ( + + case 40: + return new Tok(TokenKind.PAREN_L, pos, pos + 1, line, col, prev); + // ) + + case 41: + return new Tok(TokenKind.PAREN_R, pos, pos + 1, line, col, prev); + // . + + case 46: + if (charCodeAt.call(body, pos + 1) === 46 && charCodeAt.call(body, pos + 2) === 46) { + return new Tok(TokenKind.SPREAD, pos, pos + 3, line, col, prev); + } + + break; + // : + + case 58: + return new Tok(TokenKind.COLON, pos, pos + 1, line, col, prev); + // = + + case 61: + return new Tok(TokenKind.EQUALS, pos, pos + 1, line, col, prev); + // @ + + case 64: + return new Tok(TokenKind.AT, pos, pos + 1, line, col, prev); + // [ + + case 91: + return new Tok(TokenKind.BRACKET_L, pos, pos + 1, line, col, prev); + // ] + + case 93: + return new Tok(TokenKind.BRACKET_R, pos, pos + 1, line, col, prev); + // { + + case 123: + return new Tok(TokenKind.BRACE_L, pos, pos + 1, line, col, prev); + // | + + case 124: + return new Tok(TokenKind.PIPE, pos, pos + 1, line, col, prev); + // } + + case 125: + return new Tok(TokenKind.BRACE_R, pos, pos + 1, line, col, prev); + // A-Z _ a-z + + case 65: + case 66: + case 67: + case 68: + case 69: + case 70: + case 71: + case 72: + case 73: + case 74: + case 75: + case 76: + case 77: + case 78: + case 79: + case 80: + case 81: + case 82: + case 83: + case 84: + case 85: + case 86: + case 87: + case 88: + case 89: + case 90: + case 95: + case 97: + case 98: + case 99: + case 100: + case 101: + case 102: + case 103: + case 104: + case 105: + case 106: + case 107: + case 108: + case 109: + case 110: + case 111: + case 112: + case 113: + case 114: + case 115: + case 116: + case 117: + case 118: + case 119: + case 120: + case 121: + case 122: + return readName(source, pos, line, col, prev); + // - 0-9 + + case 45: + case 48: + case 49: + case 50: + case 51: + case 52: + case 53: + case 54: + case 55: + case 56: + case 57: + return readNumber(source, pos, code, line, col, prev); + // " + + case 34: + if (charCodeAt.call(body, pos + 1) === 34 && charCodeAt.call(body, pos + 2) === 34) { + return readBlockString(source, pos, line, col, prev); + } + + return readString(source, pos, line, col, prev); + } + + throw syntaxError(source, pos, unexpectedCharacterMessage(code)); +} +/** + * Report a message that an unexpected character was encountered. + */ + + +function unexpectedCharacterMessage(code) { + if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) { + return "Cannot contain the invalid character ".concat(printCharCode(code), "."); + } + + if (code === 39) { + // ' + return "Unexpected single quote character ('), did you mean to use " + 'a double quote (")?'; + } + + return "Cannot parse the unexpected character ".concat(printCharCode(code), "."); +} +/** + * Reads from body starting at startPosition until it finds a non-whitespace + * or commented character, then returns the position of that character for + * lexing. + */ + + +function positionAfterWhitespace(body, startPosition, lexer) { + var bodyLength = body.length; + var position = startPosition; + + while (position < bodyLength) { + var code = charCodeAt.call(body, position); // tab | space | comma | BOM + + if (code === 9 || code === 32 || code === 44 || code === 0xfeff) { + ++position; + } else if (code === 10) { + // new line + ++position; + ++lexer.line; + lexer.lineStart = position; + } else if (code === 13) { + // carriage return + if (charCodeAt.call(body, position + 1) === 10) { + position += 2; + } else { + ++position; + } + + ++lexer.line; + lexer.lineStart = position; + } else { + break; + } + } + + return position; +} +/** + * Reads a comment token from the source file. + * + * #[\u0009\u0020-\uFFFF]* + */ + + +function readComment(source, start, line, col, prev) { + var body = source.body; + var code; + var position = start; + + do { + code = charCodeAt.call(body, ++position); + } while (code !== null && ( // SourceCharacter but not LineTerminator + code > 0x001f || code === 0x0009)); + + return new Tok(TokenKind.COMMENT, start, position, line, col, prev, slice.call(body, start + 1, position)); +} +/** + * Reads a number token from the source file, either a float + * or an int depending on whether a decimal point appears. + * + * Int: -?(0|[1-9][0-9]*) + * Float: -?(0|[1-9][0-9]*)(\.[0-9]+)?((E|e)(+|-)?[0-9]+)? + */ + + +function readNumber(source, start, firstCode, line, col, prev) { + var body = source.body; + var code = firstCode; + var position = start; + var isFloat = false; + + if (code === 45) { + // - + code = charCodeAt.call(body, ++position); + } + + if (code === 48) { + // 0 + code = charCodeAt.call(body, ++position); + + if (code >= 48 && code <= 57) { + throw syntaxError(source, position, "Invalid number, unexpected digit after 0: ".concat(printCharCode(code), ".")); + } + } else { + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 46) { + // . + isFloat = true; + code = charCodeAt.call(body, ++position); + position = readDigits(source, position, code); + code = charCodeAt.call(body, position); + } + + if (code === 69 || code === 101) { + // E e + isFloat = true; + code = charCodeAt.call(body, ++position); + + if (code === 43 || code === 45) { + // + - + code = charCodeAt.call(body, ++position); + } + + position = readDigits(source, position, code); + } + + return new Tok(isFloat ? TokenKind.FLOAT : TokenKind.INT, start, position, line, col, prev, slice.call(body, start, position)); +} +/** + * Returns the new position in the source after reading digits. + */ + + +function readDigits(source, start, firstCode) { + var body = source.body; + var position = start; + var code = firstCode; + + if (code >= 48 && code <= 57) { + // 0 - 9 + do { + code = charCodeAt.call(body, ++position); + } while (code >= 48 && code <= 57); // 0 - 9 + + + return position; + } + + throw syntaxError(source, position, "Invalid number, expected digit but got: ".concat(printCharCode(code), ".")); +} +/** + * Reads a string token from the source file. + * + * "([^"\\\u000A\u000D]|(\\(u[0-9a-fA-F]{4}|["\\/bfnrt])))*" + */ + + +function readString(source, start, line, col, prev) { + var body = source.body; + var position = start + 1; + var chunkStart = position; + var code = 0; + var value = ''; + + while (position < body.length && (code = charCodeAt.call(body, position)) !== null && // not LineTerminator + code !== 0x000a && code !== 0x000d) { + // Closing Quote (") + if (code === 34) { + value += slice.call(body, chunkStart, position); + return new Tok(TokenKind.STRING, start, position + 1, line, col, prev, value); + } // SourceCharacter + + + if (code < 0x0020 && code !== 0x0009) { + throw syntaxError(source, position, "Invalid character within String: ".concat(printCharCode(code), ".")); + } + + ++position; + + if (code === 92) { + // \ + value += slice.call(body, chunkStart, position - 1); + code = charCodeAt.call(body, position); + + switch (code) { + case 34: + value += '"'; + break; + + case 47: + value += '/'; + break; + + case 92: + value += '\\'; + break; + + case 98: + value += '\b'; + break; + + case 102: + value += '\f'; + break; + + case 110: + value += '\n'; + break; + + case 114: + value += '\r'; + break; + + case 116: + value += '\t'; + break; + + case 117: + // u + var charCode = uniCharCode(charCodeAt.call(body, position + 1), charCodeAt.call(body, position + 2), charCodeAt.call(body, position + 3), charCodeAt.call(body, position + 4)); + + if (charCode < 0) { + throw syntaxError(source, position, 'Invalid character escape sequence: ' + "\\u".concat(body.slice(position + 1, position + 5), ".")); + } + + value += String.fromCharCode(charCode); + position += 4; + break; + + default: + throw syntaxError(source, position, "Invalid character escape sequence: \\".concat(String.fromCharCode(code), ".")); + } + + ++position; + chunkStart = position; + } + } + + throw syntaxError(source, position, 'Unterminated string.'); +} +/** + * Reads a block string token from the source file. + * + * """("?"?(\\"""|\\(?!=""")|[^"\\]))*""" + */ + + +function readBlockString(source, start, line, col, prev) { + var body = source.body; + var position = start + 3; + var chunkStart = position; + var code = 0; + var rawValue = ''; + + while (position < body.length && (code = charCodeAt.call(body, position)) !== null) { + // Closing Triple-Quote (""") + if (code === 34 && charCodeAt.call(body, position + 1) === 34 && charCodeAt.call(body, position + 2) === 34) { + rawValue += slice.call(body, chunkStart, position); + return new Tok(TokenKind.BLOCK_STRING, start, position + 3, line, col, prev, blockStringValue(rawValue)); + } // SourceCharacter + + + if (code < 0x0020 && code !== 0x0009 && code !== 0x000a && code !== 0x000d) { + throw syntaxError(source, position, "Invalid character within String: ".concat(printCharCode(code), ".")); + } // Escape Triple-Quote (\""") + + + if (code === 92 && charCodeAt.call(body, position + 1) === 34 && charCodeAt.call(body, position + 2) === 34 && charCodeAt.call(body, position + 3) === 34) { + rawValue += slice.call(body, chunkStart, position) + '"""'; + position += 4; + chunkStart = position; + } else { + ++position; + } + } + + throw syntaxError(source, position, 'Unterminated string.'); +} +/** + * Converts four hexidecimal chars to the integer that the + * string represents. For example, uniCharCode('0','0','0','f') + * will return 15, and uniCharCode('0','0','f','f') returns 255. + * + * Returns a negative number on error, if a char was invalid. + * + * This is implemented by noting that char2hex() returns -1 on error, + * which means the result of ORing the char2hex() will also be negative. + */ + + +function uniCharCode(a, b, c, d) { + return char2hex(a) << 12 | char2hex(b) << 8 | char2hex(c) << 4 | char2hex(d); +} +/** + * Converts a hex character to its integer value. + * '0' becomes 0, '9' becomes 9 + * 'A' becomes 10, 'F' becomes 15 + * 'a' becomes 10, 'f' becomes 15 + * + * Returns -1 on error. + */ + + +function char2hex(a) { + return a >= 48 && a <= 57 ? a - 48 // 0-9 + : a >= 65 && a <= 70 ? a - 55 // A-F + : a >= 97 && a <= 102 ? a - 87 // a-f + : -1; +} +/** + * Reads an alphanumeric + underscore name from the source. + * + * [_A-Za-z][_0-9A-Za-z]* + */ + + +function readName(source, start, line, col, prev) { + var body = source.body; + var bodyLength = body.length; + var position = start + 1; + var code = 0; + + while (position !== bodyLength && (code = charCodeAt.call(body, position)) !== null && (code === 95 || // _ + code >= 48 && code <= 57 || // 0-9 + code >= 65 && code <= 90 || // A-Z + code >= 97 && code <= 122) // a-z + ) { + ++position; + } + + return new Tok(TokenKind.NAME, start, position, line, col, prev, slice.call(body, start, position)); +} \ No newline at end of file diff --git a/dist/language/location.js b/dist/language/location.js new file mode 100644 index 0000000000..1257b2fa4c --- /dev/null +++ b/dist/language/location.js @@ -0,0 +1,40 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getLocation = getLocation; + +/** + * 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. + * + * strict + */ + +/** + * Represents a location in a Source. + */ + +/** + * Takes a Source and a UTF-8 character offset, and returns the corresponding + * line and column as a SourceLocation. + */ +function getLocation(source, position) { + var lineRegexp = /\r\n|[\n\r]/g; + var line = 1; + var column = position + 1; + var match; + + while ((match = lineRegexp.exec(source.body)) && match.index < position) { + line += 1; + column = position + 1 - (match.index + match[0].length); + } + + return { + line: line, + column: column + }; +} \ No newline at end of file diff --git a/dist/language/location.js.flow b/dist/language/location.js.flow new file mode 100644 index 0000000000..8029d5b821 --- /dev/null +++ b/dist/language/location.js.flow @@ -0,0 +1,34 @@ +/** + * 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. + * + * @flow strict + */ + +import type { Source } from './source'; + +/** + * Represents a location in a Source. + */ +export type SourceLocation = {| + +line: number, + +column: number, +|}; + +/** + * Takes a Source and a UTF-8 character offset, and returns the corresponding + * line and column as a SourceLocation. + */ +export function getLocation(source: Source, position: number): SourceLocation { + const lineRegexp = /\r\n|[\n\r]/g; + let line = 1; + let column = position + 1; + let match; + while ((match = lineRegexp.exec(source.body)) && match.index < position) { + line += 1; + column = position + 1 - (match.index + match[0].length); + } + return { line, column }; +} diff --git a/dist/language/location.mjs b/dist/language/location.mjs new file mode 100644 index 0000000000..023d5dcc2d --- /dev/null +++ b/dist/language/location.mjs @@ -0,0 +1,33 @@ +/** + * 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. + * + * strict + */ + +/** + * Represents a location in a Source. + */ + +/** + * Takes a Source and a UTF-8 character offset, and returns the corresponding + * line and column as a SourceLocation. + */ +export function getLocation(source, position) { + var lineRegexp = /\r\n|[\n\r]/g; + var line = 1; + var column = position + 1; + var match; + + while ((match = lineRegexp.exec(source.body)) && match.index < position) { + line += 1; + column = position + 1 - (match.index + match[0].length); + } + + return { + line: line, + column: column + }; +} \ No newline at end of file diff --git a/dist/language/parser.js b/dist/language/parser.js new file mode 100644 index 0000000000..a0c9ebc70a --- /dev/null +++ b/dist/language/parser.js @@ -0,0 +1,1518 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.parse = parse; +exports.parseValue = parseValue; +exports.parseType = parseType; +exports.parseConstValue = parseConstValue; +exports.parseTypeReference = parseTypeReference; +exports.parseNamedType = parseNamedType; + +var _source = require("./source"); + +var _error = require("../error"); + +var _lexer = require("./lexer"); + +var _kinds = require("./kinds"); + +var _directiveLocation = require("./directiveLocation"); + +/** + * 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. + * + * strict + */ + +/** + * Given a GraphQL source, parses it into a Document. + * Throws GraphQLError if a syntax error is encountered. + */ +function parse(source, options) { + var sourceObj = typeof source === 'string' ? new _source.Source(source) : source; + + if (!(sourceObj instanceof _source.Source)) { + throw new TypeError('Must provide Source. Received: ' + String(sourceObj)); + } + + var lexer = (0, _lexer.createLexer)(sourceObj, options || {}); + return parseDocument(lexer); +} +/** + * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for + * that value. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Values directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: valueFromAST(). + */ + + +function parseValue(source, options) { + var sourceObj = typeof source === 'string' ? new _source.Source(source) : source; + var lexer = (0, _lexer.createLexer)(sourceObj, options || {}); + expect(lexer, _lexer.TokenKind.SOF); + var value = parseValueLiteral(lexer, false); + expect(lexer, _lexer.TokenKind.EOF); + return value; +} +/** + * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for + * that type. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Types directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: typeFromAST(). + */ + + +function parseType(source, options) { + var sourceObj = typeof source === 'string' ? new _source.Source(source) : source; + var lexer = (0, _lexer.createLexer)(sourceObj, options || {}); + expect(lexer, _lexer.TokenKind.SOF); + var type = parseTypeReference(lexer); + expect(lexer, _lexer.TokenKind.EOF); + return type; +} +/** + * Converts a name lex token into a name parse node. + */ + + +function parseName(lexer) { + var token = expect(lexer, _lexer.TokenKind.NAME); + return { + kind: _kinds.Kind.NAME, + value: token.value, + loc: loc(lexer, token) + }; +} // Implements the parsing rules in the Document section. + +/** + * Document : Definition+ + */ + + +function parseDocument(lexer) { + var start = lexer.token; + expect(lexer, _lexer.TokenKind.SOF); + var definitions = []; + + do { + definitions.push(parseDefinition(lexer)); + } while (!skip(lexer, _lexer.TokenKind.EOF)); + + return { + kind: _kinds.Kind.DOCUMENT, + definitions: definitions, + loc: loc(lexer, start) + }; +} +/** + * Definition : + * - ExecutableDefinition + * - TypeSystemDefinition + * - TypeSystemExtension + */ + + +function parseDefinition(lexer) { + if (peek(lexer, _lexer.TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + case 'fragment': + return parseExecutableDefinition(lexer); + + case 'schema': + case 'scalar': + case 'type': + case 'interface': + case 'union': + case 'enum': + case 'input': + case 'directive': + return parseTypeSystemDefinition(lexer); + + case 'extend': + return parseTypeSystemExtension(lexer); + } + } else if (peek(lexer, _lexer.TokenKind.BRACE_L)) { + return parseExecutableDefinition(lexer); + } else if (peekDescription(lexer)) { + return parseTypeSystemDefinition(lexer); + } + + throw unexpected(lexer); +} +/** + * ExecutableDefinition : + * - OperationDefinition + * - FragmentDefinition + */ + + +function parseExecutableDefinition(lexer) { + if (peek(lexer, _lexer.TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + return parseOperationDefinition(lexer); + + case 'fragment': + return parseFragmentDefinition(lexer); + } + } else if (peek(lexer, _lexer.TokenKind.BRACE_L)) { + return parseOperationDefinition(lexer); + } + + throw unexpected(lexer); +} // Implements the parsing rules in the Operations section. + +/** + * OperationDefinition : + * - SelectionSet + * - OperationType Name? VariableDefinitions? Directives? SelectionSet + */ + + +function parseOperationDefinition(lexer) { + var start = lexer.token; + + if (peek(lexer, _lexer.TokenKind.BRACE_L)) { + return { + kind: _kinds.Kind.OPERATION_DEFINITION, + operation: 'query', + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; + } + + var operation = parseOperationType(lexer); + var name; + + if (peek(lexer, _lexer.TokenKind.NAME)) { + name = parseName(lexer); + } + + return { + kind: _kinds.Kind.OPERATION_DEFINITION, + operation: operation, + name: name, + variableDefinitions: parseVariableDefinitions(lexer), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * OperationType : one of query mutation subscription + */ + + +function parseOperationType(lexer) { + var operationToken = expect(lexer, _lexer.TokenKind.NAME); + + switch (operationToken.value) { + case 'query': + return 'query'; + + case 'mutation': + return 'mutation'; + + case 'subscription': + return 'subscription'; + } + + throw unexpected(lexer, operationToken); +} +/** + * VariableDefinitions : ( VariableDefinition+ ) + */ + + +function parseVariableDefinitions(lexer) { + return peek(lexer, _lexer.TokenKind.PAREN_L) ? many(lexer, _lexer.TokenKind.PAREN_L, parseVariableDefinition, _lexer.TokenKind.PAREN_R) : []; +} +/** + * VariableDefinition : Variable : Type DefaultValue? + */ + + +function parseVariableDefinition(lexer) { + var start = lexer.token; + return { + kind: _kinds.Kind.VARIABLE_DEFINITION, + variable: parseVariable(lexer), + type: (expect(lexer, _lexer.TokenKind.COLON), parseTypeReference(lexer)), + defaultValue: skip(lexer, _lexer.TokenKind.EQUALS) ? parseValueLiteral(lexer, true) : undefined, + loc: loc(lexer, start) + }; +} +/** + * Variable : $ Name + */ + + +function parseVariable(lexer) { + var start = lexer.token; + expect(lexer, _lexer.TokenKind.DOLLAR); + return { + kind: _kinds.Kind.VARIABLE, + name: parseName(lexer), + loc: loc(lexer, start) + }; +} +/** + * SelectionSet : { Selection+ } + */ + + +function parseSelectionSet(lexer) { + var start = lexer.token; + return { + kind: _kinds.Kind.SELECTION_SET, + selections: many(lexer, _lexer.TokenKind.BRACE_L, parseSelection, _lexer.TokenKind.BRACE_R), + loc: loc(lexer, start) + }; +} +/** + * Selection : + * - Field + * - FragmentSpread + * - InlineFragment + */ + + +function parseSelection(lexer) { + return peek(lexer, _lexer.TokenKind.SPREAD) ? parseFragment(lexer) : parseField(lexer); +} +/** + * Field : Alias? Name Arguments? Directives? SelectionSet? + * + * Alias : Name : + */ + + +function parseField(lexer) { + var start = lexer.token; + var nameOrAlias = parseName(lexer); + var alias; + var name; + + if (skip(lexer, _lexer.TokenKind.COLON)) { + alias = nameOrAlias; + name = parseName(lexer); + } else { + name = nameOrAlias; + } + + return { + kind: _kinds.Kind.FIELD, + alias: alias, + name: name, + arguments: parseArguments(lexer, false), + directives: parseDirectives(lexer, false), + selectionSet: peek(lexer, _lexer.TokenKind.BRACE_L) ? parseSelectionSet(lexer) : undefined, + loc: loc(lexer, start) + }; +} +/** + * Arguments[Const] : ( Argument[?Const]+ ) + */ + + +function parseArguments(lexer, isConst) { + var item = isConst ? parseConstArgument : parseArgument; + return peek(lexer, _lexer.TokenKind.PAREN_L) ? many(lexer, _lexer.TokenKind.PAREN_L, item, _lexer.TokenKind.PAREN_R) : []; +} +/** + * Argument[Const] : Name : Value[?Const] + */ + + +function parseArgument(lexer) { + var start = lexer.token; + return { + kind: _kinds.Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, _lexer.TokenKind.COLON), parseValueLiteral(lexer, false)), + loc: loc(lexer, start) + }; +} + +function parseConstArgument(lexer) { + var start = lexer.token; + return { + kind: _kinds.Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, _lexer.TokenKind.COLON), parseConstValue(lexer)), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Fragments section. + +/** + * Corresponds to both FragmentSpread and InlineFragment in the spec. + * + * FragmentSpread : ... FragmentName Directives? + * + * InlineFragment : ... TypeCondition? Directives? SelectionSet + */ + + +function parseFragment(lexer) { + var start = lexer.token; + expect(lexer, _lexer.TokenKind.SPREAD); + + if (peek(lexer, _lexer.TokenKind.NAME) && lexer.token.value !== 'on') { + return { + kind: _kinds.Kind.FRAGMENT_SPREAD, + name: parseFragmentName(lexer), + directives: parseDirectives(lexer, false), + loc: loc(lexer, start) + }; + } + + var typeCondition; + + if (lexer.token.value === 'on') { + lexer.advance(); + typeCondition = parseNamedType(lexer); + } + + return { + kind: _kinds.Kind.INLINE_FRAGMENT, + typeCondition: typeCondition, + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * FragmentDefinition : + * - fragment FragmentName on TypeCondition Directives? SelectionSet + * + * TypeCondition : NamedType + */ + + +function parseFragmentDefinition(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'fragment'); // Experimental support for defining variables within fragments changes + // the grammar of FragmentDefinition: + // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet + + if (lexer.options.experimentalFragmentVariables) { + return { + kind: _kinds.Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + variableDefinitions: parseVariableDefinitions(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; + } + + return { + kind: _kinds.Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * FragmentName : Name but not `on` + */ + + +function parseFragmentName(lexer) { + if (lexer.token.value === 'on') { + throw unexpected(lexer); + } + + return parseName(lexer); +} // Implements the parsing rules in the Values section. + +/** + * Value[Const] : + * - [~Const] Variable + * - IntValue + * - FloatValue + * - StringValue + * - BooleanValue + * - NullValue + * - EnumValue + * - ListValue[?Const] + * - ObjectValue[?Const] + * + * BooleanValue : one of `true` `false` + * + * NullValue : `null` + * + * EnumValue : Name but not `true`, `false` or `null` + */ + + +function parseValueLiteral(lexer, isConst) { + var token = lexer.token; + + switch (token.kind) { + case _lexer.TokenKind.BRACKET_L: + return parseList(lexer, isConst); + + case _lexer.TokenKind.BRACE_L: + return parseObject(lexer, isConst); + + case _lexer.TokenKind.INT: + lexer.advance(); + return { + kind: _kinds.Kind.INT, + value: token.value, + loc: loc(lexer, token) + }; + + case _lexer.TokenKind.FLOAT: + lexer.advance(); + return { + kind: _kinds.Kind.FLOAT, + value: token.value, + loc: loc(lexer, token) + }; + + case _lexer.TokenKind.STRING: + case _lexer.TokenKind.BLOCK_STRING: + return parseStringLiteral(lexer); + + case _lexer.TokenKind.NAME: + if (token.value === 'true' || token.value === 'false') { + lexer.advance(); + return { + kind: _kinds.Kind.BOOLEAN, + value: token.value === 'true', + loc: loc(lexer, token) + }; + } else if (token.value === 'null') { + lexer.advance(); + return { + kind: _kinds.Kind.NULL, + loc: loc(lexer, token) + }; + } + + lexer.advance(); + return { + kind: _kinds.Kind.ENUM, + value: token.value, + loc: loc(lexer, token) + }; + + case _lexer.TokenKind.DOLLAR: + if (!isConst) { + return parseVariable(lexer); + } + + break; + } + + throw unexpected(lexer); +} + +function parseStringLiteral(lexer) { + var token = lexer.token; + lexer.advance(); + return { + kind: _kinds.Kind.STRING, + value: token.value, + block: token.kind === _lexer.TokenKind.BLOCK_STRING, + loc: loc(lexer, token) + }; +} + +function parseConstValue(lexer) { + return parseValueLiteral(lexer, true); +} + +function parseValueValue(lexer) { + return parseValueLiteral(lexer, false); +} +/** + * ListValue[Const] : + * - [ ] + * - [ Value[?Const]+ ] + */ + + +function parseList(lexer, isConst) { + var start = lexer.token; + var item = isConst ? parseConstValue : parseValueValue; + return { + kind: _kinds.Kind.LIST, + values: any(lexer, _lexer.TokenKind.BRACKET_L, item, _lexer.TokenKind.BRACKET_R), + loc: loc(lexer, start) + }; +} +/** + * ObjectValue[Const] : + * - { } + * - { ObjectField[?Const]+ } + */ + + +function parseObject(lexer, isConst) { + var start = lexer.token; + expect(lexer, _lexer.TokenKind.BRACE_L); + var fields = []; + + while (!skip(lexer, _lexer.TokenKind.BRACE_R)) { + fields.push(parseObjectField(lexer, isConst)); + } + + return { + kind: _kinds.Kind.OBJECT, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * ObjectField[Const] : Name : Value[?Const] + */ + + +function parseObjectField(lexer, isConst) { + var start = lexer.token; + return { + kind: _kinds.Kind.OBJECT_FIELD, + name: parseName(lexer), + value: (expect(lexer, _lexer.TokenKind.COLON), parseValueLiteral(lexer, isConst)), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Directives section. + +/** + * Directives[Const] : Directive[?Const]+ + */ + + +function parseDirectives(lexer, isConst) { + var directives = []; + + while (peek(lexer, _lexer.TokenKind.AT)) { + directives.push(parseDirective(lexer, isConst)); + } + + return directives; +} +/** + * Directive[Const] : @ Name Arguments[?Const]? + */ + + +function parseDirective(lexer, isConst) { + var start = lexer.token; + expect(lexer, _lexer.TokenKind.AT); + return { + kind: _kinds.Kind.DIRECTIVE, + name: parseName(lexer), + arguments: parseArguments(lexer, isConst), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Types section. + +/** + * Type : + * - NamedType + * - ListType + * - NonNullType + */ + + +function parseTypeReference(lexer) { + var start = lexer.token; + var type; + + if (skip(lexer, _lexer.TokenKind.BRACKET_L)) { + type = parseTypeReference(lexer); + expect(lexer, _lexer.TokenKind.BRACKET_R); + type = { + kind: _kinds.Kind.LIST_TYPE, + type: type, + loc: loc(lexer, start) + }; + } else { + type = parseNamedType(lexer); + } + + if (skip(lexer, _lexer.TokenKind.BANG)) { + return { + kind: _kinds.Kind.NON_NULL_TYPE, + type: type, + loc: loc(lexer, start) + }; + } + + return type; +} +/** + * NamedType : Name + */ + + +function parseNamedType(lexer) { + var start = lexer.token; + return { + kind: _kinds.Kind.NAMED_TYPE, + name: parseName(lexer), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Type Definition section. + +/** + * TypeSystemDefinition : + * - SchemaDefinition + * - TypeDefinition + * - DirectiveDefinition + * + * TypeDefinition : + * - ScalarTypeDefinition + * - ObjectTypeDefinition + * - InterfaceTypeDefinition + * - UnionTypeDefinition + * - EnumTypeDefinition + * - InputObjectTypeDefinition + */ + + +function parseTypeSystemDefinition(lexer) { + // Many definitions begin with a description and require a lookahead. + var keywordToken = peekDescription(lexer) ? lexer.lookahead() : lexer.token; + + if (keywordToken.kind === _lexer.TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaDefinition(lexer); + + case 'scalar': + return parseScalarTypeDefinition(lexer); + + case 'type': + return parseObjectTypeDefinition(lexer); + + case 'interface': + return parseInterfaceTypeDefinition(lexer); + + case 'union': + return parseUnionTypeDefinition(lexer); + + case 'enum': + return parseEnumTypeDefinition(lexer); + + case 'input': + return parseInputObjectTypeDefinition(lexer); + + case 'directive': + return parseDirectiveDefinition(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} + +function peekDescription(lexer) { + return peek(lexer, _lexer.TokenKind.STRING) || peek(lexer, _lexer.TokenKind.BLOCK_STRING); +} +/** + * Description : StringValue + */ + + +function parseDescription(lexer) { + if (peekDescription(lexer)) { + return parseStringLiteral(lexer); + } +} +/** + * SchemaDefinition : schema Directives[Const]? { OperationTypeDefinition+ } + */ + + +function parseSchemaDefinition(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'schema'); + var directives = parseDirectives(lexer, true); + var operationTypes = many(lexer, _lexer.TokenKind.BRACE_L, parseOperationTypeDefinition, _lexer.TokenKind.BRACE_R); + return { + kind: _kinds.Kind.SCHEMA_DEFINITION, + directives: directives, + operationTypes: operationTypes, + loc: loc(lexer, start) + }; +} +/** + * OperationTypeDefinition : OperationType : NamedType + */ + + +function parseOperationTypeDefinition(lexer) { + var start = lexer.token; + var operation = parseOperationType(lexer); + expect(lexer, _lexer.TokenKind.COLON); + var type = parseNamedType(lexer); + return { + kind: _kinds.Kind.OPERATION_TYPE_DEFINITION, + operation: operation, + type: type, + loc: loc(lexer, start) + }; +} +/** + * ScalarTypeDefinition : Description? scalar Name Directives[Const]? + */ + + +function parseScalarTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'scalar'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: _kinds.Kind.SCALAR_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ObjectTypeDefinition : + * Description? + * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition? + */ + + +function parseObjectTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'type'); + var name = parseName(lexer); + var interfaces = parseImplementsInterfaces(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + return { + kind: _kinds.Kind.OBJECT_TYPE_DEFINITION, + description: description, + name: name, + interfaces: interfaces, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * ImplementsInterfaces : + * - implements `&`? NamedType + * - ImplementsInterfaces & NamedType + */ + + +function parseImplementsInterfaces(lexer) { + var types = []; + + if (lexer.token.value === 'implements') { + lexer.advance(); // Optional leading ampersand + + skip(lexer, _lexer.TokenKind.AMP); + + do { + types.push(parseNamedType(lexer)); + } while (skip(lexer, _lexer.TokenKind.AMP) || // Legacy support for the SDL? + lexer.options.allowLegacySDLImplementsInterfaces && peek(lexer, _lexer.TokenKind.NAME)); + } + + return types; +} +/** + * FieldsDefinition : { FieldDefinition+ } + */ + + +function parseFieldsDefinition(lexer) { + // Legacy support for the SDL? + if (lexer.options.allowLegacySDLEmptyFields && peek(lexer, _lexer.TokenKind.BRACE_L) && lexer.lookahead().kind === _lexer.TokenKind.BRACE_R) { + lexer.advance(); + lexer.advance(); + return []; + } + + return peek(lexer, _lexer.TokenKind.BRACE_L) ? many(lexer, _lexer.TokenKind.BRACE_L, parseFieldDefinition, _lexer.TokenKind.BRACE_R) : []; +} +/** + * FieldDefinition : + * - Description? Name ArgumentsDefinition? : Type Directives[Const]? + */ + + +function parseFieldDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + var args = parseArgumentDefs(lexer); + expect(lexer, _lexer.TokenKind.COLON); + var type = parseTypeReference(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: _kinds.Kind.FIELD_DEFINITION, + description: description, + name: name, + arguments: args, + type: type, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ArgumentsDefinition : ( InputValueDefinition+ ) + */ + + +function parseArgumentDefs(lexer) { + if (!peek(lexer, _lexer.TokenKind.PAREN_L)) { + return []; + } + + return many(lexer, _lexer.TokenKind.PAREN_L, parseInputValueDef, _lexer.TokenKind.PAREN_R); +} +/** + * InputValueDefinition : + * - Description? Name : Type DefaultValue? Directives[Const]? + */ + + +function parseInputValueDef(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + expect(lexer, _lexer.TokenKind.COLON); + var type = parseTypeReference(lexer); + var defaultValue; + + if (skip(lexer, _lexer.TokenKind.EQUALS)) { + defaultValue = parseConstValue(lexer); + } + + var directives = parseDirectives(lexer, true); + return { + kind: _kinds.Kind.INPUT_VALUE_DEFINITION, + description: description, + name: name, + type: type, + defaultValue: defaultValue, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * InterfaceTypeDefinition : + * - Description? interface Name Directives[Const]? FieldsDefinition? + */ + + +function parseInterfaceTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'interface'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + return { + kind: _kinds.Kind.INTERFACE_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * UnionTypeDefinition : + * - Description? union Name Directives[Const]? UnionMemberTypes? + */ + + +function parseUnionTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'union'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var types = parseUnionMemberTypes(lexer); + return { + kind: _kinds.Kind.UNION_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + types: types, + loc: loc(lexer, start) + }; +} +/** + * UnionMemberTypes : + * - = `|`? NamedType + * - UnionMemberTypes | NamedType + */ + + +function parseUnionMemberTypes(lexer) { + var types = []; + + if (skip(lexer, _lexer.TokenKind.EQUALS)) { + // Optional leading pipe + skip(lexer, _lexer.TokenKind.PIPE); + + do { + types.push(parseNamedType(lexer)); + } while (skip(lexer, _lexer.TokenKind.PIPE)); + } + + return types; +} +/** + * EnumTypeDefinition : + * - Description? enum Name Directives[Const]? EnumValuesDefinition? + */ + + +function parseEnumTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'enum'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var values = parseEnumValuesDefinition(lexer); + return { + kind: _kinds.Kind.ENUM_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + values: values, + loc: loc(lexer, start) + }; +} +/** + * EnumValuesDefinition : { EnumValueDefinition+ } + */ + + +function parseEnumValuesDefinition(lexer) { + return peek(lexer, _lexer.TokenKind.BRACE_L) ? many(lexer, _lexer.TokenKind.BRACE_L, parseEnumValueDefinition, _lexer.TokenKind.BRACE_R) : []; +} +/** + * EnumValueDefinition : Description? EnumValue Directives[Const]? + * + * EnumValue : Name + */ + + +function parseEnumValueDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: _kinds.Kind.ENUM_VALUE_DEFINITION, + description: description, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * InputObjectTypeDefinition : + * - Description? input Name Directives[Const]? InputFieldsDefinition? + */ + + +function parseInputObjectTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'input'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseInputFieldsDefinition(lexer); + return { + kind: _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * InputFieldsDefinition : { InputValueDefinition+ } + */ + + +function parseInputFieldsDefinition(lexer) { + return peek(lexer, _lexer.TokenKind.BRACE_L) ? many(lexer, _lexer.TokenKind.BRACE_L, parseInputValueDef, _lexer.TokenKind.BRACE_R) : []; +} +/** + * TypeSystemExtension : + * - SchemaExtension + * - TypeExtension + * + * TypeExtension : + * - ScalarTypeExtension + * - ObjectTypeExtension + * - InterfaceTypeExtension + * - UnionTypeExtension + * - EnumTypeExtension + * - InputObjectTypeDefinition + */ + + +function parseTypeSystemExtension(lexer) { + var keywordToken = lexer.lookahead(); + + if (keywordToken.kind === _lexer.TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaExtension(lexer); + + case 'scalar': + return parseScalarTypeExtension(lexer); + + case 'type': + return parseObjectTypeExtension(lexer); + + case 'interface': + return parseInterfaceTypeExtension(lexer); + + case 'union': + return parseUnionTypeExtension(lexer); + + case 'enum': + return parseEnumTypeExtension(lexer); + + case 'input': + return parseInputObjectTypeExtension(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} +/** + * SchemaExtension : + * - extend schema Directives[Const]? { OperationTypeDefinition+ } + * - extend schema Directives[Const] + */ + + +function parseSchemaExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'schema'); + var directives = parseDirectives(lexer, true); + var operationTypes = peek(lexer, _lexer.TokenKind.BRACE_L) ? many(lexer, _lexer.TokenKind.BRACE_L, parseOperationTypeDefinition, _lexer.TokenKind.BRACE_R) : []; + + if (directives.length === 0 && operationTypes.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.SCHEMA_EXTENSION, + directives: directives, + operationTypes: operationTypes, + loc: loc(lexer, start) + }; +} +/** + * ScalarTypeExtension : + * - extend scalar Name Directives[Const] + */ + + +function parseScalarTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'scalar'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + + if (directives.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.SCALAR_TYPE_EXTENSION, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ObjectTypeExtension : + * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition + * - extend type Name ImplementsInterfaces? Directives[Const] + * - extend type Name ImplementsInterfaces + */ + + +function parseObjectTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'type'); + var name = parseName(lexer); + var interfaces = parseImplementsInterfaces(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + + if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.OBJECT_TYPE_EXTENSION, + name: name, + interfaces: interfaces, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * InterfaceTypeExtension : + * - extend interface Name Directives[Const]? FieldsDefinition + * - extend interface Name Directives[Const] + */ + + +function parseInterfaceTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'interface'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.INTERFACE_TYPE_EXTENSION, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * UnionTypeExtension : + * - extend union Name Directives[Const]? UnionMemberTypes + * - extend union Name Directives[Const] + */ + + +function parseUnionTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'union'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var types = parseUnionMemberTypes(lexer); + + if (directives.length === 0 && types.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.UNION_TYPE_EXTENSION, + name: name, + directives: directives, + types: types, + loc: loc(lexer, start) + }; +} +/** + * EnumTypeExtension : + * - extend enum Name Directives[Const]? EnumValuesDefinition + * - extend enum Name Directives[Const] + */ + + +function parseEnumTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'enum'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var values = parseEnumValuesDefinition(lexer); + + if (directives.length === 0 && values.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.ENUM_TYPE_EXTENSION, + name: name, + directives: directives, + values: values, + loc: loc(lexer, start) + }; +} +/** + * InputObjectTypeExtension : + * - extend input Name Directives[Const]? InputFieldsDefinition + * - extend input Name Directives[Const] + */ + + +function parseInputObjectTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'input'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseInputFieldsDefinition(lexer); + + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * DirectiveDefinition : + * - Description? directive @ Name ArgumentsDefinition? on DirectiveLocations + */ + + +function parseDirectiveDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'directive'); + expect(lexer, _lexer.TokenKind.AT); + var name = parseName(lexer); + var args = parseArgumentDefs(lexer); + expectKeyword(lexer, 'on'); + var locations = parseDirectiveLocations(lexer); + return { + kind: _kinds.Kind.DIRECTIVE_DEFINITION, + description: description, + name: name, + arguments: args, + locations: locations, + loc: loc(lexer, start) + }; +} +/** + * DirectiveLocations : + * - `|`? DirectiveLocation + * - DirectiveLocations | DirectiveLocation + */ + + +function parseDirectiveLocations(lexer) { + // Optional leading pipe + skip(lexer, _lexer.TokenKind.PIPE); + var locations = []; + + do { + locations.push(parseDirectiveLocation(lexer)); + } while (skip(lexer, _lexer.TokenKind.PIPE)); + + return locations; +} +/* + * DirectiveLocation : + * - ExecutableDirectiveLocation + * - TypeSystemDirectiveLocation + * + * ExecutableDirectiveLocation : one of + * `QUERY` + * `MUTATION` + * `SUBSCRIPTION` + * `FIELD` + * `FRAGMENT_DEFINITION` + * `FRAGMENT_SPREAD` + * `INLINE_FRAGMENT` + * + * TypeSystemDirectiveLocation : one of + * `SCHEMA` + * `SCALAR` + * `OBJECT` + * `FIELD_DEFINITION` + * `ARGUMENT_DEFINITION` + * `INTERFACE` + * `UNION` + * `ENUM` + * `ENUM_VALUE` + * `INPUT_OBJECT` + * `INPUT_FIELD_DEFINITION` + */ + + +function parseDirectiveLocation(lexer) { + var start = lexer.token; + var name = parseName(lexer); + + if (_directiveLocation.DirectiveLocation.hasOwnProperty(name.value)) { + return name; + } + + throw unexpected(lexer, start); +} // Core parsing utility functions + +/** + * Returns a location object, used to identify the place in + * the source that created a given parsed object. + */ + + +function loc(lexer, startToken) { + if (!lexer.options.noLocation) { + return new Loc(startToken, lexer.lastToken, lexer.source); + } +} + +function Loc(startToken, endToken, source) { + this.start = startToken.start; + this.end = endToken.end; + this.startToken = startToken; + this.endToken = endToken; + this.source = source; +} // Print a simplified form when appearing in JSON/util.inspect. + + +Loc.prototype.toJSON = Loc.prototype.inspect = function toJSON() { + return { + start: this.start, + end: this.end + }; +}; +/** + * Determines if the next token is of a given kind + */ + + +function peek(lexer, kind) { + return lexer.token.kind === kind; +} +/** + * If the next token is of the given kind, return true after advancing + * the lexer. Otherwise, do not change the parser state and return false. + */ + + +function skip(lexer, kind) { + var match = lexer.token.kind === kind; + + if (match) { + lexer.advance(); + } + + return match; +} +/** + * If the next token is of the given kind, return that token after advancing + * the lexer. Otherwise, do not change the parser state and throw an error. + */ + + +function expect(lexer, kind) { + var token = lexer.token; + + if (token.kind === kind) { + lexer.advance(); + return token; + } + + throw (0, _error.syntaxError)(lexer.source, token.start, "Expected ".concat(kind, ", found ").concat((0, _lexer.getTokenDesc)(token))); +} +/** + * If the next token is a keyword with the given value, return that token after + * advancing the lexer. Otherwise, do not change the parser state and return + * false. + */ + + +function expectKeyword(lexer, value) { + var token = lexer.token; + + if (token.kind === _lexer.TokenKind.NAME && token.value === value) { + lexer.advance(); + return token; + } + + throw (0, _error.syntaxError)(lexer.source, token.start, "Expected \"".concat(value, "\", found ").concat((0, _lexer.getTokenDesc)(token))); +} +/** + * Helper function for creating an error when an unexpected lexed token + * is encountered. + */ + + +function unexpected(lexer, atToken) { + var token = atToken || lexer.token; + return (0, _error.syntaxError)(lexer.source, token.start, "Unexpected ".concat((0, _lexer.getTokenDesc)(token))); +} +/** + * Returns a possibly empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ + + +function any(lexer, openKind, parseFn, closeKind) { + expect(lexer, openKind); + var nodes = []; + + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + + return nodes; +} +/** + * Returns a non-empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ + + +function many(lexer, openKind, parseFn, closeKind) { + expect(lexer, openKind); + var nodes = [parseFn(lexer)]; + + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + + return nodes; +} \ No newline at end of file diff --git a/dist/language/parser.js.flow b/dist/language/parser.js.flow new file mode 100644 index 0000000000..6fe84fe9da --- /dev/null +++ b/dist/language/parser.js.flow @@ -0,0 +1,1564 @@ +/** + * 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. + * + * @flow strict + */ + +import { Source } from './source'; +import { syntaxError } from '../error'; +import type { GraphQLError } from '../error'; +import { createLexer, TokenKind, getTokenDesc } from './lexer'; +import type { Lexer, TokenKindEnum } from './lexer'; +import type { + Location, + Token, + NameNode, + VariableNode, + DocumentNode, + DefinitionNode, + ExecutableDefinitionNode, + OperationDefinitionNode, + OperationTypeNode, + VariableDefinitionNode, + SelectionSetNode, + SelectionNode, + FieldNode, + ArgumentNode, + FragmentSpreadNode, + InlineFragmentNode, + FragmentDefinitionNode, + ValueNode, + StringValueNode, + ListValueNode, + ObjectValueNode, + ObjectFieldNode, + DirectiveNode, + TypeNode, + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, + TypeSystemDefinitionNode, + SchemaDefinitionNode, + OperationTypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, + TypeSystemExtensionNode, + SchemaExtensionNode, + ScalarTypeExtensionNode, + ObjectTypeExtensionNode, + InterfaceTypeExtensionNode, + UnionTypeExtensionNode, + EnumTypeExtensionNode, + InputObjectTypeExtensionNode, +} from './ast'; + +import { Kind } from './kinds'; +import { DirectiveLocation } from './directiveLocation'; + +/** + * Configuration options to control parser behavior + */ +export type ParseOptions = { + /** + * By default, the parser creates AST nodes that know the location + * in the source that they correspond to. This configuration flag + * disables that behavior for performance or testing. + */ + noLocation?: boolean, + + /** + * If enabled, the parser will parse empty fields sets in the Schema + * Definition Language. Otherwise, the parser will follow the current + * specification. + * + * This option is provided to ease adoption of the final SDL specification + * and will be removed in a future major release. + */ + allowLegacySDLEmptyFields?: boolean, + + /** + * If enabled, the parser will parse implemented interfaces with no `&` + * character between each interface. Otherwise, the parser will follow the + * current specification. + * + * This option is provided to ease adoption of the final SDL specification + * and will be removed in a future major release. + */ + allowLegacySDLImplementsInterfaces?: boolean, + + /** + * EXPERIMENTAL: + * + * If enabled, the parser will understand and parse variable definitions + * contained in a fragment definition. They'll be represented in the + * `variableDefinitions` field of the FragmentDefinitionNode. + * + * The syntax is identical to normal, query-defined variables. For example: + * + * fragment A($var: Boolean = false) on T { + * ... + * } + * + * Note: this feature is experimental and may change or be removed in the + * future. + */ + experimentalFragmentVariables?: boolean, +}; + +/** + * Given a GraphQL source, parses it into a Document. + * Throws GraphQLError if a syntax error is encountered. + */ +export function parse( + source: string | Source, + options?: ParseOptions, +): DocumentNode { + const sourceObj = typeof source === 'string' ? new Source(source) : source; + if (!(sourceObj instanceof Source)) { + throw new TypeError('Must provide Source. Received: ' + String(sourceObj)); + } + const lexer = createLexer(sourceObj, options || {}); + return parseDocument(lexer); +} + +/** + * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for + * that value. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Values directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: valueFromAST(). + */ +export function parseValue( + source: string | Source, + options?: ParseOptions, +): ValueNode { + const sourceObj = typeof source === 'string' ? new Source(source) : source; + const lexer = createLexer(sourceObj, options || {}); + expect(lexer, TokenKind.SOF); + const value = parseValueLiteral(lexer, false); + expect(lexer, TokenKind.EOF); + return value; +} + +/** + * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for + * that type. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Types directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: typeFromAST(). + */ +export function parseType( + source: string | Source, + options?: ParseOptions, +): TypeNode { + const sourceObj = typeof source === 'string' ? new Source(source) : source; + const lexer = createLexer(sourceObj, options || {}); + expect(lexer, TokenKind.SOF); + const type = parseTypeReference(lexer); + expect(lexer, TokenKind.EOF); + return type; +} + +/** + * Converts a name lex token into a name parse node. + */ +function parseName(lexer: Lexer<*>): NameNode { + const token = expect(lexer, TokenKind.NAME); + return { + kind: Kind.NAME, + value: ((token.value: any): string), + loc: loc(lexer, token), + }; +} + +// Implements the parsing rules in the Document section. + +/** + * Document : Definition+ + */ +function parseDocument(lexer: Lexer<*>): DocumentNode { + const start = lexer.token; + expect(lexer, TokenKind.SOF); + const definitions = []; + do { + definitions.push(parseDefinition(lexer)); + } while (!skip(lexer, TokenKind.EOF)); + + return { + kind: Kind.DOCUMENT, + definitions, + loc: loc(lexer, start), + }; +} + +/** + * Definition : + * - ExecutableDefinition + * - TypeSystemDefinition + * - TypeSystemExtension + */ +function parseDefinition(lexer: Lexer<*>): DefinitionNode { + if (peek(lexer, TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + case 'fragment': + return parseExecutableDefinition(lexer); + case 'schema': + case 'scalar': + case 'type': + case 'interface': + case 'union': + case 'enum': + case 'input': + case 'directive': + return parseTypeSystemDefinition(lexer); + case 'extend': + return parseTypeSystemExtension(lexer); + } + } else if (peek(lexer, TokenKind.BRACE_L)) { + return parseExecutableDefinition(lexer); + } else if (peekDescription(lexer)) { + return parseTypeSystemDefinition(lexer); + } + + throw unexpected(lexer); +} + +/** + * ExecutableDefinition : + * - OperationDefinition + * - FragmentDefinition + */ +function parseExecutableDefinition(lexer: Lexer<*>): ExecutableDefinitionNode { + if (peek(lexer, TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + return parseOperationDefinition(lexer); + + case 'fragment': + return parseFragmentDefinition(lexer); + } + } else if (peek(lexer, TokenKind.BRACE_L)) { + return parseOperationDefinition(lexer); + } + + throw unexpected(lexer); +} + +// Implements the parsing rules in the Operations section. + +/** + * OperationDefinition : + * - SelectionSet + * - OperationType Name? VariableDefinitions? Directives? SelectionSet + */ +function parseOperationDefinition(lexer: Lexer<*>): OperationDefinitionNode { + const start = lexer.token; + if (peek(lexer, TokenKind.BRACE_L)) { + return { + kind: Kind.OPERATION_DEFINITION, + operation: 'query', + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start), + }; + } + const operation = parseOperationType(lexer); + let name; + if (peek(lexer, TokenKind.NAME)) { + name = parseName(lexer); + } + return { + kind: Kind.OPERATION_DEFINITION, + operation, + name, + variableDefinitions: parseVariableDefinitions(lexer), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start), + }; +} + +/** + * OperationType : one of query mutation subscription + */ +function parseOperationType(lexer: Lexer<*>): OperationTypeNode { + const operationToken = expect(lexer, TokenKind.NAME); + switch (operationToken.value) { + case 'query': + return 'query'; + case 'mutation': + return 'mutation'; + case 'subscription': + return 'subscription'; + } + + throw unexpected(lexer, operationToken); +} + +/** + * VariableDefinitions : ( VariableDefinition+ ) + */ +function parseVariableDefinitions( + lexer: Lexer<*>, +): Array { + return peek(lexer, TokenKind.PAREN_L) + ? many(lexer, TokenKind.PAREN_L, parseVariableDefinition, TokenKind.PAREN_R) + : []; +} + +/** + * VariableDefinition : Variable : Type DefaultValue? + */ +function parseVariableDefinition(lexer: Lexer<*>): VariableDefinitionNode { + const start = lexer.token; + return { + kind: Kind.VARIABLE_DEFINITION, + variable: parseVariable(lexer), + type: (expect(lexer, TokenKind.COLON), parseTypeReference(lexer)), + defaultValue: skip(lexer, TokenKind.EQUALS) + ? parseValueLiteral(lexer, true) + : undefined, + loc: loc(lexer, start), + }; +} + +/** + * Variable : $ Name + */ +function parseVariable(lexer: Lexer<*>): VariableNode { + const start = lexer.token; + expect(lexer, TokenKind.DOLLAR); + return { + kind: Kind.VARIABLE, + name: parseName(lexer), + loc: loc(lexer, start), + }; +} + +/** + * SelectionSet : { Selection+ } + */ +function parseSelectionSet(lexer: Lexer<*>): SelectionSetNode { + const start = lexer.token; + return { + kind: Kind.SELECTION_SET, + selections: many( + lexer, + TokenKind.BRACE_L, + parseSelection, + TokenKind.BRACE_R, + ), + loc: loc(lexer, start), + }; +} + +/** + * Selection : + * - Field + * - FragmentSpread + * - InlineFragment + */ +function parseSelection(lexer: Lexer<*>): SelectionNode { + return peek(lexer, TokenKind.SPREAD) + ? parseFragment(lexer) + : parseField(lexer); +} + +/** + * Field : Alias? Name Arguments? Directives? SelectionSet? + * + * Alias : Name : + */ +function parseField(lexer: Lexer<*>): FieldNode { + const start = lexer.token; + + const nameOrAlias = parseName(lexer); + let alias; + let name; + if (skip(lexer, TokenKind.COLON)) { + alias = nameOrAlias; + name = parseName(lexer); + } else { + name = nameOrAlias; + } + + return { + kind: Kind.FIELD, + alias, + name, + arguments: parseArguments(lexer, false), + directives: parseDirectives(lexer, false), + selectionSet: peek(lexer, TokenKind.BRACE_L) + ? parseSelectionSet(lexer) + : undefined, + loc: loc(lexer, start), + }; +} + +/** + * Arguments[Const] : ( Argument[?Const]+ ) + */ +function parseArguments( + lexer: Lexer<*>, + isConst: boolean, +): Array { + const item = isConst ? parseConstArgument : parseArgument; + return peek(lexer, TokenKind.PAREN_L) + ? many(lexer, TokenKind.PAREN_L, item, TokenKind.PAREN_R) + : []; +} + +/** + * Argument[Const] : Name : Value[?Const] + */ +function parseArgument(lexer: Lexer<*>): ArgumentNode { + const start = lexer.token; + return { + kind: Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseValueLiteral(lexer, false)), + loc: loc(lexer, start), + }; +} + +function parseConstArgument(lexer: Lexer<*>): ArgumentNode { + const start = lexer.token; + return { + kind: Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseConstValue(lexer)), + loc: loc(lexer, start), + }; +} + +// Implements the parsing rules in the Fragments section. + +/** + * Corresponds to both FragmentSpread and InlineFragment in the spec. + * + * FragmentSpread : ... FragmentName Directives? + * + * InlineFragment : ... TypeCondition? Directives? SelectionSet + */ +function parseFragment( + lexer: Lexer<*>, +): FragmentSpreadNode | InlineFragmentNode { + const start = lexer.token; + expect(lexer, TokenKind.SPREAD); + if (peek(lexer, TokenKind.NAME) && lexer.token.value !== 'on') { + return { + kind: Kind.FRAGMENT_SPREAD, + name: parseFragmentName(lexer), + directives: parseDirectives(lexer, false), + loc: loc(lexer, start), + }; + } + let typeCondition; + if (lexer.token.value === 'on') { + lexer.advance(); + typeCondition = parseNamedType(lexer); + } + return { + kind: Kind.INLINE_FRAGMENT, + typeCondition, + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start), + }; +} + +/** + * FragmentDefinition : + * - fragment FragmentName on TypeCondition Directives? SelectionSet + * + * TypeCondition : NamedType + */ +function parseFragmentDefinition(lexer: Lexer<*>): FragmentDefinitionNode { + const start = lexer.token; + expectKeyword(lexer, 'fragment'); + // Experimental support for defining variables within fragments changes + // the grammar of FragmentDefinition: + // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet + if (lexer.options.experimentalFragmentVariables) { + return { + kind: Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + variableDefinitions: parseVariableDefinitions(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start), + }; + } + return { + kind: Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start), + }; +} + +/** + * FragmentName : Name but not `on` + */ +function parseFragmentName(lexer: Lexer<*>): NameNode { + if (lexer.token.value === 'on') { + throw unexpected(lexer); + } + return parseName(lexer); +} + +// Implements the parsing rules in the Values section. + +/** + * Value[Const] : + * - [~Const] Variable + * - IntValue + * - FloatValue + * - StringValue + * - BooleanValue + * - NullValue + * - EnumValue + * - ListValue[?Const] + * - ObjectValue[?Const] + * + * BooleanValue : one of `true` `false` + * + * NullValue : `null` + * + * EnumValue : Name but not `true`, `false` or `null` + */ +function parseValueLiteral(lexer: Lexer<*>, isConst: boolean): ValueNode { + const token = lexer.token; + switch (token.kind) { + case TokenKind.BRACKET_L: + return parseList(lexer, isConst); + case TokenKind.BRACE_L: + return parseObject(lexer, isConst); + case TokenKind.INT: + lexer.advance(); + return { + kind: Kind.INT, + value: ((token.value: any): string), + loc: loc(lexer, token), + }; + case TokenKind.FLOAT: + lexer.advance(); + return { + kind: Kind.FLOAT, + value: ((token.value: any): string), + loc: loc(lexer, token), + }; + case TokenKind.STRING: + case TokenKind.BLOCK_STRING: + return parseStringLiteral(lexer); + case TokenKind.NAME: + if (token.value === 'true' || token.value === 'false') { + lexer.advance(); + return { + kind: Kind.BOOLEAN, + value: token.value === 'true', + loc: loc(lexer, token), + }; + } else if (token.value === 'null') { + lexer.advance(); + return { + kind: Kind.NULL, + loc: loc(lexer, token), + }; + } + lexer.advance(); + return { + kind: Kind.ENUM, + value: ((token.value: any): string), + loc: loc(lexer, token), + }; + case TokenKind.DOLLAR: + if (!isConst) { + return parseVariable(lexer); + } + break; + } + throw unexpected(lexer); +} + +function parseStringLiteral(lexer: Lexer<*>): StringValueNode { + const token = lexer.token; + lexer.advance(); + return { + kind: Kind.STRING, + value: ((token.value: any): string), + block: token.kind === TokenKind.BLOCK_STRING, + loc: loc(lexer, token), + }; +} + +export function parseConstValue(lexer: Lexer<*>): ValueNode { + return parseValueLiteral(lexer, true); +} + +function parseValueValue(lexer: Lexer<*>): ValueNode { + return parseValueLiteral(lexer, false); +} + +/** + * ListValue[Const] : + * - [ ] + * - [ Value[?Const]+ ] + */ +function parseList(lexer: Lexer<*>, isConst: boolean): ListValueNode { + const start = lexer.token; + const item = isConst ? parseConstValue : parseValueValue; + return { + kind: Kind.LIST, + values: any(lexer, TokenKind.BRACKET_L, item, TokenKind.BRACKET_R), + loc: loc(lexer, start), + }; +} + +/** + * ObjectValue[Const] : + * - { } + * - { ObjectField[?Const]+ } + */ +function parseObject(lexer: Lexer<*>, isConst: boolean): ObjectValueNode { + const start = lexer.token; + expect(lexer, TokenKind.BRACE_L); + const fields = []; + while (!skip(lexer, TokenKind.BRACE_R)) { + fields.push(parseObjectField(lexer, isConst)); + } + return { + kind: Kind.OBJECT, + fields, + loc: loc(lexer, start), + }; +} + +/** + * ObjectField[Const] : Name : Value[?Const] + */ +function parseObjectField(lexer: Lexer<*>, isConst: boolean): ObjectFieldNode { + const start = lexer.token; + return { + kind: Kind.OBJECT_FIELD, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseValueLiteral(lexer, isConst)), + loc: loc(lexer, start), + }; +} + +// Implements the parsing rules in the Directives section. + +/** + * Directives[Const] : Directive[?Const]+ + */ +function parseDirectives( + lexer: Lexer<*>, + isConst: boolean, +): Array { + const directives = []; + while (peek(lexer, TokenKind.AT)) { + directives.push(parseDirective(lexer, isConst)); + } + return directives; +} + +/** + * Directive[Const] : @ Name Arguments[?Const]? + */ +function parseDirective(lexer: Lexer<*>, isConst: boolean): DirectiveNode { + const start = lexer.token; + expect(lexer, TokenKind.AT); + return { + kind: Kind.DIRECTIVE, + name: parseName(lexer), + arguments: parseArguments(lexer, isConst), + loc: loc(lexer, start), + }; +} + +// Implements the parsing rules in the Types section. + +/** + * Type : + * - NamedType + * - ListType + * - NonNullType + */ +export function parseTypeReference(lexer: Lexer<*>): TypeNode { + const start = lexer.token; + let type; + if (skip(lexer, TokenKind.BRACKET_L)) { + type = parseTypeReference(lexer); + expect(lexer, TokenKind.BRACKET_R); + type = ({ + kind: Kind.LIST_TYPE, + type, + loc: loc(lexer, start), + }: ListTypeNode); + } else { + type = parseNamedType(lexer); + } + if (skip(lexer, TokenKind.BANG)) { + return ({ + kind: Kind.NON_NULL_TYPE, + type, + loc: loc(lexer, start), + }: NonNullTypeNode); + } + return type; +} + +/** + * NamedType : Name + */ +export function parseNamedType(lexer: Lexer<*>): NamedTypeNode { + const start = lexer.token; + return { + kind: Kind.NAMED_TYPE, + name: parseName(lexer), + loc: loc(lexer, start), + }; +} + +// Implements the parsing rules in the Type Definition section. + +/** + * TypeSystemDefinition : + * - SchemaDefinition + * - TypeDefinition + * - DirectiveDefinition + * + * TypeDefinition : + * - ScalarTypeDefinition + * - ObjectTypeDefinition + * - InterfaceTypeDefinition + * - UnionTypeDefinition + * - EnumTypeDefinition + * - InputObjectTypeDefinition + */ +function parseTypeSystemDefinition(lexer: Lexer<*>): TypeSystemDefinitionNode { + // Many definitions begin with a description and require a lookahead. + const keywordToken = peekDescription(lexer) ? lexer.lookahead() : lexer.token; + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaDefinition(lexer); + case 'scalar': + return parseScalarTypeDefinition(lexer); + case 'type': + return parseObjectTypeDefinition(lexer); + case 'interface': + return parseInterfaceTypeDefinition(lexer); + case 'union': + return parseUnionTypeDefinition(lexer); + case 'enum': + return parseEnumTypeDefinition(lexer); + case 'input': + return parseInputObjectTypeDefinition(lexer); + case 'directive': + return parseDirectiveDefinition(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} + +function peekDescription(lexer: Lexer<*>): boolean { + return peek(lexer, TokenKind.STRING) || peek(lexer, TokenKind.BLOCK_STRING); +} + +/** + * Description : StringValue + */ +function parseDescription(lexer: Lexer<*>): void | StringValueNode { + if (peekDescription(lexer)) { + return parseStringLiteral(lexer); + } +} + +/** + * SchemaDefinition : schema Directives[Const]? { OperationTypeDefinition+ } + */ +function parseSchemaDefinition(lexer: Lexer<*>): SchemaDefinitionNode { + const start = lexer.token; + expectKeyword(lexer, 'schema'); + const directives = parseDirectives(lexer, true); + const operationTypes = many( + lexer, + TokenKind.BRACE_L, + parseOperationTypeDefinition, + TokenKind.BRACE_R, + ); + return { + kind: Kind.SCHEMA_DEFINITION, + directives, + operationTypes, + loc: loc(lexer, start), + }; +} + +/** + * OperationTypeDefinition : OperationType : NamedType + */ +function parseOperationTypeDefinition( + lexer: Lexer<*>, +): OperationTypeDefinitionNode { + const start = lexer.token; + const operation = parseOperationType(lexer); + expect(lexer, TokenKind.COLON); + const type = parseNamedType(lexer); + return { + kind: Kind.OPERATION_TYPE_DEFINITION, + operation, + type, + loc: loc(lexer, start), + }; +} + +/** + * ScalarTypeDefinition : Description? scalar Name Directives[Const]? + */ +function parseScalarTypeDefinition(lexer: Lexer<*>): ScalarTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'scalar'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + return { + kind: Kind.SCALAR_TYPE_DEFINITION, + description, + name, + directives, + loc: loc(lexer, start), + }; +} + +/** + * ObjectTypeDefinition : + * Description? + * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition? + */ +function parseObjectTypeDefinition(lexer: Lexer<*>): ObjectTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'type'); + const name = parseName(lexer); + const interfaces = parseImplementsInterfaces(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseFieldsDefinition(lexer); + return { + kind: Kind.OBJECT_TYPE_DEFINITION, + description, + name, + interfaces, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * ImplementsInterfaces : + * - implements `&`? NamedType + * - ImplementsInterfaces & NamedType + */ +function parseImplementsInterfaces(lexer: Lexer<*>): Array { + const types = []; + if (lexer.token.value === 'implements') { + lexer.advance(); + // Optional leading ampersand + skip(lexer, TokenKind.AMP); + do { + types.push(parseNamedType(lexer)); + } while ( + skip(lexer, TokenKind.AMP) || + // Legacy support for the SDL? + (lexer.options.allowLegacySDLImplementsInterfaces && + peek(lexer, TokenKind.NAME)) + ); + } + return types; +} + +/** + * FieldsDefinition : { FieldDefinition+ } + */ +function parseFieldsDefinition(lexer: Lexer<*>): Array { + // Legacy support for the SDL? + if ( + lexer.options.allowLegacySDLEmptyFields && + peek(lexer, TokenKind.BRACE_L) && + lexer.lookahead().kind === TokenKind.BRACE_R + ) { + lexer.advance(); + lexer.advance(); + return []; + } + return peek(lexer, TokenKind.BRACE_L) + ? many(lexer, TokenKind.BRACE_L, parseFieldDefinition, TokenKind.BRACE_R) + : []; +} + +/** + * FieldDefinition : + * - Description? Name ArgumentsDefinition? : Type Directives[Const]? + */ +function parseFieldDefinition(lexer: Lexer<*>): FieldDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + const name = parseName(lexer); + const args = parseArgumentDefs(lexer); + expect(lexer, TokenKind.COLON); + const type = parseTypeReference(lexer); + const directives = parseDirectives(lexer, true); + return { + kind: Kind.FIELD_DEFINITION, + description, + name, + arguments: args, + type, + directives, + loc: loc(lexer, start), + }; +} + +/** + * ArgumentsDefinition : ( InputValueDefinition+ ) + */ +function parseArgumentDefs(lexer: Lexer<*>): Array { + if (!peek(lexer, TokenKind.PAREN_L)) { + return []; + } + return many(lexer, TokenKind.PAREN_L, parseInputValueDef, TokenKind.PAREN_R); +} + +/** + * InputValueDefinition : + * - Description? Name : Type DefaultValue? Directives[Const]? + */ +function parseInputValueDef(lexer: Lexer<*>): InputValueDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + const name = parseName(lexer); + expect(lexer, TokenKind.COLON); + const type = parseTypeReference(lexer); + let defaultValue; + if (skip(lexer, TokenKind.EQUALS)) { + defaultValue = parseConstValue(lexer); + } + const directives = parseDirectives(lexer, true); + return { + kind: Kind.INPUT_VALUE_DEFINITION, + description, + name, + type, + defaultValue, + directives, + loc: loc(lexer, start), + }; +} + +/** + * InterfaceTypeDefinition : + * - Description? interface Name Directives[Const]? FieldsDefinition? + */ +function parseInterfaceTypeDefinition( + lexer: Lexer<*>, +): InterfaceTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'interface'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseFieldsDefinition(lexer); + return { + kind: Kind.INTERFACE_TYPE_DEFINITION, + description, + name, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * UnionTypeDefinition : + * - Description? union Name Directives[Const]? UnionMemberTypes? + */ +function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'union'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const types = parseUnionMemberTypes(lexer); + return { + kind: Kind.UNION_TYPE_DEFINITION, + description, + name, + directives, + types, + loc: loc(lexer, start), + }; +} + +/** + * UnionMemberTypes : + * - = `|`? NamedType + * - UnionMemberTypes | NamedType + */ +function parseUnionMemberTypes(lexer: Lexer<*>): Array { + const types = []; + if (skip(lexer, TokenKind.EQUALS)) { + // Optional leading pipe + skip(lexer, TokenKind.PIPE); + do { + types.push(parseNamedType(lexer)); + } while (skip(lexer, TokenKind.PIPE)); + } + return types; +} + +/** + * EnumTypeDefinition : + * - Description? enum Name Directives[Const]? EnumValuesDefinition? + */ +function parseEnumTypeDefinition(lexer: Lexer<*>): EnumTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'enum'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const values = parseEnumValuesDefinition(lexer); + return { + kind: Kind.ENUM_TYPE_DEFINITION, + description, + name, + directives, + values, + loc: loc(lexer, start), + }; +} + +/** + * EnumValuesDefinition : { EnumValueDefinition+ } + */ +function parseEnumValuesDefinition( + lexer: Lexer<*>, +): Array { + return peek(lexer, TokenKind.BRACE_L) + ? many( + lexer, + TokenKind.BRACE_L, + parseEnumValueDefinition, + TokenKind.BRACE_R, + ) + : []; +} + +/** + * EnumValueDefinition : Description? EnumValue Directives[Const]? + * + * EnumValue : Name + */ +function parseEnumValueDefinition(lexer: Lexer<*>): EnumValueDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + return { + kind: Kind.ENUM_VALUE_DEFINITION, + description, + name, + directives, + loc: loc(lexer, start), + }; +} + +/** + * InputObjectTypeDefinition : + * - Description? input Name Directives[Const]? InputFieldsDefinition? + */ +function parseInputObjectTypeDefinition( + lexer: Lexer<*>, +): InputObjectTypeDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'input'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseInputFieldsDefinition(lexer); + return { + kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, + description, + name, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * InputFieldsDefinition : { InputValueDefinition+ } + */ +function parseInputFieldsDefinition( + lexer: Lexer<*>, +): Array { + return peek(lexer, TokenKind.BRACE_L) + ? many(lexer, TokenKind.BRACE_L, parseInputValueDef, TokenKind.BRACE_R) + : []; +} + +/** + * TypeSystemExtension : + * - SchemaExtension + * - TypeExtension + * + * TypeExtension : + * - ScalarTypeExtension + * - ObjectTypeExtension + * - InterfaceTypeExtension + * - UnionTypeExtension + * - EnumTypeExtension + * - InputObjectTypeDefinition + */ +function parseTypeSystemExtension(lexer: Lexer<*>): TypeSystemExtensionNode { + const keywordToken = lexer.lookahead(); + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaExtension(lexer); + case 'scalar': + return parseScalarTypeExtension(lexer); + case 'type': + return parseObjectTypeExtension(lexer); + case 'interface': + return parseInterfaceTypeExtension(lexer); + case 'union': + return parseUnionTypeExtension(lexer); + case 'enum': + return parseEnumTypeExtension(lexer); + case 'input': + return parseInputObjectTypeExtension(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} + +/** + * SchemaExtension : + * - extend schema Directives[Const]? { OperationTypeDefinition+ } + * - extend schema Directives[Const] + */ +function parseSchemaExtension(lexer: Lexer<*>): SchemaExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'schema'); + const directives = parseDirectives(lexer, true); + const operationTypes = peek(lexer, TokenKind.BRACE_L) + ? many( + lexer, + TokenKind.BRACE_L, + parseOperationTypeDefinition, + TokenKind.BRACE_R, + ) + : []; + if (directives.length === 0 && operationTypes.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.SCHEMA_EXTENSION, + directives, + operationTypes, + loc: loc(lexer, start), + }; +} + +/** + * ScalarTypeExtension : + * - extend scalar Name Directives[Const] + */ +function parseScalarTypeExtension(lexer: Lexer<*>): ScalarTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'scalar'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + if (directives.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.SCALAR_TYPE_EXTENSION, + name, + directives, + loc: loc(lexer, start), + }; +} + +/** + * ObjectTypeExtension : + * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition + * - extend type Name ImplementsInterfaces? Directives[Const] + * - extend type Name ImplementsInterfaces + */ +function parseObjectTypeExtension(lexer: Lexer<*>): ObjectTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'type'); + const name = parseName(lexer); + const interfaces = parseImplementsInterfaces(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseFieldsDefinition(lexer); + if ( + interfaces.length === 0 && + directives.length === 0 && + fields.length === 0 + ) { + throw unexpected(lexer); + } + return { + kind: Kind.OBJECT_TYPE_EXTENSION, + name, + interfaces, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * InterfaceTypeExtension : + * - extend interface Name Directives[Const]? FieldsDefinition + * - extend interface Name Directives[Const] + */ +function parseInterfaceTypeExtension( + lexer: Lexer<*>, +): InterfaceTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'interface'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseFieldsDefinition(lexer); + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.INTERFACE_TYPE_EXTENSION, + name, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * UnionTypeExtension : + * - extend union Name Directives[Const]? UnionMemberTypes + * - extend union Name Directives[Const] + */ +function parseUnionTypeExtension(lexer: Lexer<*>): UnionTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'union'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const types = parseUnionMemberTypes(lexer); + if (directives.length === 0 && types.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.UNION_TYPE_EXTENSION, + name, + directives, + types, + loc: loc(lexer, start), + }; +} + +/** + * EnumTypeExtension : + * - extend enum Name Directives[Const]? EnumValuesDefinition + * - extend enum Name Directives[Const] + */ +function parseEnumTypeExtension(lexer: Lexer<*>): EnumTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'enum'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const values = parseEnumValuesDefinition(lexer); + if (directives.length === 0 && values.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.ENUM_TYPE_EXTENSION, + name, + directives, + values, + loc: loc(lexer, start), + }; +} + +/** + * InputObjectTypeExtension : + * - extend input Name Directives[Const]? InputFieldsDefinition + * - extend input Name Directives[Const] + */ +function parseInputObjectTypeExtension( + lexer: Lexer<*>, +): InputObjectTypeExtensionNode { + const start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'input'); + const name = parseName(lexer); + const directives = parseDirectives(lexer, true); + const fields = parseInputFieldsDefinition(lexer); + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + return { + kind: Kind.INPUT_OBJECT_TYPE_EXTENSION, + name, + directives, + fields, + loc: loc(lexer, start), + }; +} + +/** + * DirectiveDefinition : + * - Description? directive @ Name ArgumentsDefinition? on DirectiveLocations + */ +function parseDirectiveDefinition(lexer: Lexer<*>): DirectiveDefinitionNode { + const start = lexer.token; + const description = parseDescription(lexer); + expectKeyword(lexer, 'directive'); + expect(lexer, TokenKind.AT); + const name = parseName(lexer); + const args = parseArgumentDefs(lexer); + expectKeyword(lexer, 'on'); + const locations = parseDirectiveLocations(lexer); + return { + kind: Kind.DIRECTIVE_DEFINITION, + description, + name, + arguments: args, + locations, + loc: loc(lexer, start), + }; +} + +/** + * DirectiveLocations : + * - `|`? DirectiveLocation + * - DirectiveLocations | DirectiveLocation + */ +function parseDirectiveLocations(lexer: Lexer<*>): Array { + // Optional leading pipe + skip(lexer, TokenKind.PIPE); + const locations = []; + do { + locations.push(parseDirectiveLocation(lexer)); + } while (skip(lexer, TokenKind.PIPE)); + return locations; +} + +/* + * DirectiveLocation : + * - ExecutableDirectiveLocation + * - TypeSystemDirectiveLocation + * + * ExecutableDirectiveLocation : one of + * `QUERY` + * `MUTATION` + * `SUBSCRIPTION` + * `FIELD` + * `FRAGMENT_DEFINITION` + * `FRAGMENT_SPREAD` + * `INLINE_FRAGMENT` + * + * TypeSystemDirectiveLocation : one of + * `SCHEMA` + * `SCALAR` + * `OBJECT` + * `FIELD_DEFINITION` + * `ARGUMENT_DEFINITION` + * `INTERFACE` + * `UNION` + * `ENUM` + * `ENUM_VALUE` + * `INPUT_OBJECT` + * `INPUT_FIELD_DEFINITION` + */ +function parseDirectiveLocation(lexer: Lexer<*>): NameNode { + const start = lexer.token; + const name = parseName(lexer); + if (DirectiveLocation.hasOwnProperty(name.value)) { + return name; + } + throw unexpected(lexer, start); +} + +// Core parsing utility functions + +/** + * Returns a location object, used to identify the place in + * the source that created a given parsed object. + */ +function loc(lexer: Lexer<*>, startToken: Token): Location | void { + if (!lexer.options.noLocation) { + return new Loc(startToken, lexer.lastToken, lexer.source); + } +} + +function Loc(startToken: Token, endToken: Token, source: Source) { + this.start = startToken.start; + this.end = endToken.end; + this.startToken = startToken; + this.endToken = endToken; + this.source = source; +} + +// Print a simplified form when appearing in JSON/util.inspect. +Loc.prototype.toJSON = Loc.prototype.inspect = function toJSON() { + return { start: this.start, end: this.end }; +}; + +/** + * Determines if the next token is of a given kind + */ +function peek(lexer: Lexer<*>, kind: TokenKindEnum): boolean { + return lexer.token.kind === kind; +} + +/** + * If the next token is of the given kind, return true after advancing + * the lexer. Otherwise, do not change the parser state and return false. + */ +function skip(lexer: Lexer<*>, kind: TokenKindEnum): boolean { + const match = lexer.token.kind === kind; + if (match) { + lexer.advance(); + } + return match; +} + +/** + * If the next token is of the given kind, return that token after advancing + * the lexer. Otherwise, do not change the parser state and throw an error. + */ +function expect(lexer: Lexer<*>, kind: TokenKindEnum): Token { + const token = lexer.token; + if (token.kind === kind) { + lexer.advance(); + return token; + } + throw syntaxError( + lexer.source, + token.start, + `Expected ${kind}, found ${getTokenDesc(token)}`, + ); +} + +/** + * If the next token is a keyword with the given value, return that token after + * advancing the lexer. Otherwise, do not change the parser state and return + * false. + */ +function expectKeyword(lexer: Lexer<*>, value: string): Token { + const token = lexer.token; + if (token.kind === TokenKind.NAME && token.value === value) { + lexer.advance(); + return token; + } + throw syntaxError( + lexer.source, + token.start, + `Expected "${value}", found ${getTokenDesc(token)}`, + ); +} + +/** + * Helper function for creating an error when an unexpected lexed token + * is encountered. + */ +function unexpected(lexer: Lexer<*>, atToken?: ?Token): GraphQLError { + const token = atToken || lexer.token; + return syntaxError( + lexer.source, + token.start, + `Unexpected ${getTokenDesc(token)}`, + ); +} + +/** + * Returns a possibly empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ +function any( + lexer: Lexer<*>, + openKind: TokenKindEnum, + parseFn: (lexer: Lexer<*>) => T, + closeKind: TokenKindEnum, +): Array { + expect(lexer, openKind); + const nodes = []; + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + return nodes; +} + +/** + * Returns a non-empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ +function many( + lexer: Lexer<*>, + openKind: TokenKindEnum, + parseFn: (lexer: Lexer<*>) => T, + closeKind: TokenKindEnum, +): Array { + expect(lexer, openKind); + const nodes = [parseFn(lexer)]; + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + return nodes; +} diff --git a/dist/language/parser.mjs b/dist/language/parser.mjs new file mode 100644 index 0000000000..34895346ec --- /dev/null +++ b/dist/language/parser.mjs @@ -0,0 +1,1499 @@ +/** + * 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. + * + * strict + */ +import { Source } from './source'; +import { syntaxError } from '../error'; +import { createLexer, TokenKind, getTokenDesc } from './lexer'; +import { Kind } from './kinds'; +import { DirectiveLocation } from './directiveLocation'; +/** + * Configuration options to control parser behavior + */ + +/** + * Given a GraphQL source, parses it into a Document. + * Throws GraphQLError if a syntax error is encountered. + */ +export function parse(source, options) { + var sourceObj = typeof source === 'string' ? new Source(source) : source; + + if (!(sourceObj instanceof Source)) { + throw new TypeError('Must provide Source. Received: ' + String(sourceObj)); + } + + var lexer = createLexer(sourceObj, options || {}); + return parseDocument(lexer); +} +/** + * Given a string containing a GraphQL value (ex. `[42]`), parse the AST for + * that value. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Values directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: valueFromAST(). + */ + +export function parseValue(source, options) { + var sourceObj = typeof source === 'string' ? new Source(source) : source; + var lexer = createLexer(sourceObj, options || {}); + expect(lexer, TokenKind.SOF); + var value = parseValueLiteral(lexer, false); + expect(lexer, TokenKind.EOF); + return value; +} +/** + * Given a string containing a GraphQL Type (ex. `[Int!]`), parse the AST for + * that type. + * Throws GraphQLError if a syntax error is encountered. + * + * This is useful within tools that operate upon GraphQL Types directly and + * in isolation of complete GraphQL documents. + * + * Consider providing the results to the utility function: typeFromAST(). + */ + +export function parseType(source, options) { + var sourceObj = typeof source === 'string' ? new Source(source) : source; + var lexer = createLexer(sourceObj, options || {}); + expect(lexer, TokenKind.SOF); + var type = parseTypeReference(lexer); + expect(lexer, TokenKind.EOF); + return type; +} +/** + * Converts a name lex token into a name parse node. + */ + +function parseName(lexer) { + var token = expect(lexer, TokenKind.NAME); + return { + kind: Kind.NAME, + value: token.value, + loc: loc(lexer, token) + }; +} // Implements the parsing rules in the Document section. + +/** + * Document : Definition+ + */ + + +function parseDocument(lexer) { + var start = lexer.token; + expect(lexer, TokenKind.SOF); + var definitions = []; + + do { + definitions.push(parseDefinition(lexer)); + } while (!skip(lexer, TokenKind.EOF)); + + return { + kind: Kind.DOCUMENT, + definitions: definitions, + loc: loc(lexer, start) + }; +} +/** + * Definition : + * - ExecutableDefinition + * - TypeSystemDefinition + * - TypeSystemExtension + */ + + +function parseDefinition(lexer) { + if (peek(lexer, TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + case 'fragment': + return parseExecutableDefinition(lexer); + + case 'schema': + case 'scalar': + case 'type': + case 'interface': + case 'union': + case 'enum': + case 'input': + case 'directive': + return parseTypeSystemDefinition(lexer); + + case 'extend': + return parseTypeSystemExtension(lexer); + } + } else if (peek(lexer, TokenKind.BRACE_L)) { + return parseExecutableDefinition(lexer); + } else if (peekDescription(lexer)) { + return parseTypeSystemDefinition(lexer); + } + + throw unexpected(lexer); +} +/** + * ExecutableDefinition : + * - OperationDefinition + * - FragmentDefinition + */ + + +function parseExecutableDefinition(lexer) { + if (peek(lexer, TokenKind.NAME)) { + switch (lexer.token.value) { + case 'query': + case 'mutation': + case 'subscription': + return parseOperationDefinition(lexer); + + case 'fragment': + return parseFragmentDefinition(lexer); + } + } else if (peek(lexer, TokenKind.BRACE_L)) { + return parseOperationDefinition(lexer); + } + + throw unexpected(lexer); +} // Implements the parsing rules in the Operations section. + +/** + * OperationDefinition : + * - SelectionSet + * - OperationType Name? VariableDefinitions? Directives? SelectionSet + */ + + +function parseOperationDefinition(lexer) { + var start = lexer.token; + + if (peek(lexer, TokenKind.BRACE_L)) { + return { + kind: Kind.OPERATION_DEFINITION, + operation: 'query', + name: undefined, + variableDefinitions: [], + directives: [], + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; + } + + var operation = parseOperationType(lexer); + var name; + + if (peek(lexer, TokenKind.NAME)) { + name = parseName(lexer); + } + + return { + kind: Kind.OPERATION_DEFINITION, + operation: operation, + name: name, + variableDefinitions: parseVariableDefinitions(lexer), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * OperationType : one of query mutation subscription + */ + + +function parseOperationType(lexer) { + var operationToken = expect(lexer, TokenKind.NAME); + + switch (operationToken.value) { + case 'query': + return 'query'; + + case 'mutation': + return 'mutation'; + + case 'subscription': + return 'subscription'; + } + + throw unexpected(lexer, operationToken); +} +/** + * VariableDefinitions : ( VariableDefinition+ ) + */ + + +function parseVariableDefinitions(lexer) { + return peek(lexer, TokenKind.PAREN_L) ? many(lexer, TokenKind.PAREN_L, parseVariableDefinition, TokenKind.PAREN_R) : []; +} +/** + * VariableDefinition : Variable : Type DefaultValue? + */ + + +function parseVariableDefinition(lexer) { + var start = lexer.token; + return { + kind: Kind.VARIABLE_DEFINITION, + variable: parseVariable(lexer), + type: (expect(lexer, TokenKind.COLON), parseTypeReference(lexer)), + defaultValue: skip(lexer, TokenKind.EQUALS) ? parseValueLiteral(lexer, true) : undefined, + loc: loc(lexer, start) + }; +} +/** + * Variable : $ Name + */ + + +function parseVariable(lexer) { + var start = lexer.token; + expect(lexer, TokenKind.DOLLAR); + return { + kind: Kind.VARIABLE, + name: parseName(lexer), + loc: loc(lexer, start) + }; +} +/** + * SelectionSet : { Selection+ } + */ + + +function parseSelectionSet(lexer) { + var start = lexer.token; + return { + kind: Kind.SELECTION_SET, + selections: many(lexer, TokenKind.BRACE_L, parseSelection, TokenKind.BRACE_R), + loc: loc(lexer, start) + }; +} +/** + * Selection : + * - Field + * - FragmentSpread + * - InlineFragment + */ + + +function parseSelection(lexer) { + return peek(lexer, TokenKind.SPREAD) ? parseFragment(lexer) : parseField(lexer); +} +/** + * Field : Alias? Name Arguments? Directives? SelectionSet? + * + * Alias : Name : + */ + + +function parseField(lexer) { + var start = lexer.token; + var nameOrAlias = parseName(lexer); + var alias; + var name; + + if (skip(lexer, TokenKind.COLON)) { + alias = nameOrAlias; + name = parseName(lexer); + } else { + name = nameOrAlias; + } + + return { + kind: Kind.FIELD, + alias: alias, + name: name, + arguments: parseArguments(lexer, false), + directives: parseDirectives(lexer, false), + selectionSet: peek(lexer, TokenKind.BRACE_L) ? parseSelectionSet(lexer) : undefined, + loc: loc(lexer, start) + }; +} +/** + * Arguments[Const] : ( Argument[?Const]+ ) + */ + + +function parseArguments(lexer, isConst) { + var item = isConst ? parseConstArgument : parseArgument; + return peek(lexer, TokenKind.PAREN_L) ? many(lexer, TokenKind.PAREN_L, item, TokenKind.PAREN_R) : []; +} +/** + * Argument[Const] : Name : Value[?Const] + */ + + +function parseArgument(lexer) { + var start = lexer.token; + return { + kind: Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseValueLiteral(lexer, false)), + loc: loc(lexer, start) + }; +} + +function parseConstArgument(lexer) { + var start = lexer.token; + return { + kind: Kind.ARGUMENT, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseConstValue(lexer)), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Fragments section. + +/** + * Corresponds to both FragmentSpread and InlineFragment in the spec. + * + * FragmentSpread : ... FragmentName Directives? + * + * InlineFragment : ... TypeCondition? Directives? SelectionSet + */ + + +function parseFragment(lexer) { + var start = lexer.token; + expect(lexer, TokenKind.SPREAD); + + if (peek(lexer, TokenKind.NAME) && lexer.token.value !== 'on') { + return { + kind: Kind.FRAGMENT_SPREAD, + name: parseFragmentName(lexer), + directives: parseDirectives(lexer, false), + loc: loc(lexer, start) + }; + } + + var typeCondition; + + if (lexer.token.value === 'on') { + lexer.advance(); + typeCondition = parseNamedType(lexer); + } + + return { + kind: Kind.INLINE_FRAGMENT, + typeCondition: typeCondition, + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * FragmentDefinition : + * - fragment FragmentName on TypeCondition Directives? SelectionSet + * + * TypeCondition : NamedType + */ + + +function parseFragmentDefinition(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'fragment'); // Experimental support for defining variables within fragments changes + // the grammar of FragmentDefinition: + // - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet + + if (lexer.options.experimentalFragmentVariables) { + return { + kind: Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + variableDefinitions: parseVariableDefinitions(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; + } + + return { + kind: Kind.FRAGMENT_DEFINITION, + name: parseFragmentName(lexer), + typeCondition: (expectKeyword(lexer, 'on'), parseNamedType(lexer)), + directives: parseDirectives(lexer, false), + selectionSet: parseSelectionSet(lexer), + loc: loc(lexer, start) + }; +} +/** + * FragmentName : Name but not `on` + */ + + +function parseFragmentName(lexer) { + if (lexer.token.value === 'on') { + throw unexpected(lexer); + } + + return parseName(lexer); +} // Implements the parsing rules in the Values section. + +/** + * Value[Const] : + * - [~Const] Variable + * - IntValue + * - FloatValue + * - StringValue + * - BooleanValue + * - NullValue + * - EnumValue + * - ListValue[?Const] + * - ObjectValue[?Const] + * + * BooleanValue : one of `true` `false` + * + * NullValue : `null` + * + * EnumValue : Name but not `true`, `false` or `null` + */ + + +function parseValueLiteral(lexer, isConst) { + var token = lexer.token; + + switch (token.kind) { + case TokenKind.BRACKET_L: + return parseList(lexer, isConst); + + case TokenKind.BRACE_L: + return parseObject(lexer, isConst); + + case TokenKind.INT: + lexer.advance(); + return { + kind: Kind.INT, + value: token.value, + loc: loc(lexer, token) + }; + + case TokenKind.FLOAT: + lexer.advance(); + return { + kind: Kind.FLOAT, + value: token.value, + loc: loc(lexer, token) + }; + + case TokenKind.STRING: + case TokenKind.BLOCK_STRING: + return parseStringLiteral(lexer); + + case TokenKind.NAME: + if (token.value === 'true' || token.value === 'false') { + lexer.advance(); + return { + kind: Kind.BOOLEAN, + value: token.value === 'true', + loc: loc(lexer, token) + }; + } else if (token.value === 'null') { + lexer.advance(); + return { + kind: Kind.NULL, + loc: loc(lexer, token) + }; + } + + lexer.advance(); + return { + kind: Kind.ENUM, + value: token.value, + loc: loc(lexer, token) + }; + + case TokenKind.DOLLAR: + if (!isConst) { + return parseVariable(lexer); + } + + break; + } + + throw unexpected(lexer); +} + +function parseStringLiteral(lexer) { + var token = lexer.token; + lexer.advance(); + return { + kind: Kind.STRING, + value: token.value, + block: token.kind === TokenKind.BLOCK_STRING, + loc: loc(lexer, token) + }; +} + +export function parseConstValue(lexer) { + return parseValueLiteral(lexer, true); +} + +function parseValueValue(lexer) { + return parseValueLiteral(lexer, false); +} +/** + * ListValue[Const] : + * - [ ] + * - [ Value[?Const]+ ] + */ + + +function parseList(lexer, isConst) { + var start = lexer.token; + var item = isConst ? parseConstValue : parseValueValue; + return { + kind: Kind.LIST, + values: any(lexer, TokenKind.BRACKET_L, item, TokenKind.BRACKET_R), + loc: loc(lexer, start) + }; +} +/** + * ObjectValue[Const] : + * - { } + * - { ObjectField[?Const]+ } + */ + + +function parseObject(lexer, isConst) { + var start = lexer.token; + expect(lexer, TokenKind.BRACE_L); + var fields = []; + + while (!skip(lexer, TokenKind.BRACE_R)) { + fields.push(parseObjectField(lexer, isConst)); + } + + return { + kind: Kind.OBJECT, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * ObjectField[Const] : Name : Value[?Const] + */ + + +function parseObjectField(lexer, isConst) { + var start = lexer.token; + return { + kind: Kind.OBJECT_FIELD, + name: parseName(lexer), + value: (expect(lexer, TokenKind.COLON), parseValueLiteral(lexer, isConst)), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Directives section. + +/** + * Directives[Const] : Directive[?Const]+ + */ + + +function parseDirectives(lexer, isConst) { + var directives = []; + + while (peek(lexer, TokenKind.AT)) { + directives.push(parseDirective(lexer, isConst)); + } + + return directives; +} +/** + * Directive[Const] : @ Name Arguments[?Const]? + */ + + +function parseDirective(lexer, isConst) { + var start = lexer.token; + expect(lexer, TokenKind.AT); + return { + kind: Kind.DIRECTIVE, + name: parseName(lexer), + arguments: parseArguments(lexer, isConst), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Types section. + +/** + * Type : + * - NamedType + * - ListType + * - NonNullType + */ + + +export function parseTypeReference(lexer) { + var start = lexer.token; + var type; + + if (skip(lexer, TokenKind.BRACKET_L)) { + type = parseTypeReference(lexer); + expect(lexer, TokenKind.BRACKET_R); + type = { + kind: Kind.LIST_TYPE, + type: type, + loc: loc(lexer, start) + }; + } else { + type = parseNamedType(lexer); + } + + if (skip(lexer, TokenKind.BANG)) { + return { + kind: Kind.NON_NULL_TYPE, + type: type, + loc: loc(lexer, start) + }; + } + + return type; +} +/** + * NamedType : Name + */ + +export function parseNamedType(lexer) { + var start = lexer.token; + return { + kind: Kind.NAMED_TYPE, + name: parseName(lexer), + loc: loc(lexer, start) + }; +} // Implements the parsing rules in the Type Definition section. + +/** + * TypeSystemDefinition : + * - SchemaDefinition + * - TypeDefinition + * - DirectiveDefinition + * + * TypeDefinition : + * - ScalarTypeDefinition + * - ObjectTypeDefinition + * - InterfaceTypeDefinition + * - UnionTypeDefinition + * - EnumTypeDefinition + * - InputObjectTypeDefinition + */ + +function parseTypeSystemDefinition(lexer) { + // Many definitions begin with a description and require a lookahead. + var keywordToken = peekDescription(lexer) ? lexer.lookahead() : lexer.token; + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaDefinition(lexer); + + case 'scalar': + return parseScalarTypeDefinition(lexer); + + case 'type': + return parseObjectTypeDefinition(lexer); + + case 'interface': + return parseInterfaceTypeDefinition(lexer); + + case 'union': + return parseUnionTypeDefinition(lexer); + + case 'enum': + return parseEnumTypeDefinition(lexer); + + case 'input': + return parseInputObjectTypeDefinition(lexer); + + case 'directive': + return parseDirectiveDefinition(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} + +function peekDescription(lexer) { + return peek(lexer, TokenKind.STRING) || peek(lexer, TokenKind.BLOCK_STRING); +} +/** + * Description : StringValue + */ + + +function parseDescription(lexer) { + if (peekDescription(lexer)) { + return parseStringLiteral(lexer); + } +} +/** + * SchemaDefinition : schema Directives[Const]? { OperationTypeDefinition+ } + */ + + +function parseSchemaDefinition(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'schema'); + var directives = parseDirectives(lexer, true); + var operationTypes = many(lexer, TokenKind.BRACE_L, parseOperationTypeDefinition, TokenKind.BRACE_R); + return { + kind: Kind.SCHEMA_DEFINITION, + directives: directives, + operationTypes: operationTypes, + loc: loc(lexer, start) + }; +} +/** + * OperationTypeDefinition : OperationType : NamedType + */ + + +function parseOperationTypeDefinition(lexer) { + var start = lexer.token; + var operation = parseOperationType(lexer); + expect(lexer, TokenKind.COLON); + var type = parseNamedType(lexer); + return { + kind: Kind.OPERATION_TYPE_DEFINITION, + operation: operation, + type: type, + loc: loc(lexer, start) + }; +} +/** + * ScalarTypeDefinition : Description? scalar Name Directives[Const]? + */ + + +function parseScalarTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'scalar'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: Kind.SCALAR_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ObjectTypeDefinition : + * Description? + * type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition? + */ + + +function parseObjectTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'type'); + var name = parseName(lexer); + var interfaces = parseImplementsInterfaces(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + return { + kind: Kind.OBJECT_TYPE_DEFINITION, + description: description, + name: name, + interfaces: interfaces, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * ImplementsInterfaces : + * - implements `&`? NamedType + * - ImplementsInterfaces & NamedType + */ + + +function parseImplementsInterfaces(lexer) { + var types = []; + + if (lexer.token.value === 'implements') { + lexer.advance(); // Optional leading ampersand + + skip(lexer, TokenKind.AMP); + + do { + types.push(parseNamedType(lexer)); + } while (skip(lexer, TokenKind.AMP) || // Legacy support for the SDL? + lexer.options.allowLegacySDLImplementsInterfaces && peek(lexer, TokenKind.NAME)); + } + + return types; +} +/** + * FieldsDefinition : { FieldDefinition+ } + */ + + +function parseFieldsDefinition(lexer) { + // Legacy support for the SDL? + if (lexer.options.allowLegacySDLEmptyFields && peek(lexer, TokenKind.BRACE_L) && lexer.lookahead().kind === TokenKind.BRACE_R) { + lexer.advance(); + lexer.advance(); + return []; + } + + return peek(lexer, TokenKind.BRACE_L) ? many(lexer, TokenKind.BRACE_L, parseFieldDefinition, TokenKind.BRACE_R) : []; +} +/** + * FieldDefinition : + * - Description? Name ArgumentsDefinition? : Type Directives[Const]? + */ + + +function parseFieldDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + var args = parseArgumentDefs(lexer); + expect(lexer, TokenKind.COLON); + var type = parseTypeReference(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: Kind.FIELD_DEFINITION, + description: description, + name: name, + arguments: args, + type: type, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ArgumentsDefinition : ( InputValueDefinition+ ) + */ + + +function parseArgumentDefs(lexer) { + if (!peek(lexer, TokenKind.PAREN_L)) { + return []; + } + + return many(lexer, TokenKind.PAREN_L, parseInputValueDef, TokenKind.PAREN_R); +} +/** + * InputValueDefinition : + * - Description? Name : Type DefaultValue? Directives[Const]? + */ + + +function parseInputValueDef(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + expect(lexer, TokenKind.COLON); + var type = parseTypeReference(lexer); + var defaultValue; + + if (skip(lexer, TokenKind.EQUALS)) { + defaultValue = parseConstValue(lexer); + } + + var directives = parseDirectives(lexer, true); + return { + kind: Kind.INPUT_VALUE_DEFINITION, + description: description, + name: name, + type: type, + defaultValue: defaultValue, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * InterfaceTypeDefinition : + * - Description? interface Name Directives[Const]? FieldsDefinition? + */ + + +function parseInterfaceTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'interface'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + return { + kind: Kind.INTERFACE_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * UnionTypeDefinition : + * - Description? union Name Directives[Const]? UnionMemberTypes? + */ + + +function parseUnionTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'union'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var types = parseUnionMemberTypes(lexer); + return { + kind: Kind.UNION_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + types: types, + loc: loc(lexer, start) + }; +} +/** + * UnionMemberTypes : + * - = `|`? NamedType + * - UnionMemberTypes | NamedType + */ + + +function parseUnionMemberTypes(lexer) { + var types = []; + + if (skip(lexer, TokenKind.EQUALS)) { + // Optional leading pipe + skip(lexer, TokenKind.PIPE); + + do { + types.push(parseNamedType(lexer)); + } while (skip(lexer, TokenKind.PIPE)); + } + + return types; +} +/** + * EnumTypeDefinition : + * - Description? enum Name Directives[Const]? EnumValuesDefinition? + */ + + +function parseEnumTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'enum'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var values = parseEnumValuesDefinition(lexer); + return { + kind: Kind.ENUM_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + values: values, + loc: loc(lexer, start) + }; +} +/** + * EnumValuesDefinition : { EnumValueDefinition+ } + */ + + +function parseEnumValuesDefinition(lexer) { + return peek(lexer, TokenKind.BRACE_L) ? many(lexer, TokenKind.BRACE_L, parseEnumValueDefinition, TokenKind.BRACE_R) : []; +} +/** + * EnumValueDefinition : Description? EnumValue Directives[Const]? + * + * EnumValue : Name + */ + + +function parseEnumValueDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + return { + kind: Kind.ENUM_VALUE_DEFINITION, + description: description, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * InputObjectTypeDefinition : + * - Description? input Name Directives[Const]? InputFieldsDefinition? + */ + + +function parseInputObjectTypeDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'input'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseInputFieldsDefinition(lexer); + return { + kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, + description: description, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * InputFieldsDefinition : { InputValueDefinition+ } + */ + + +function parseInputFieldsDefinition(lexer) { + return peek(lexer, TokenKind.BRACE_L) ? many(lexer, TokenKind.BRACE_L, parseInputValueDef, TokenKind.BRACE_R) : []; +} +/** + * TypeSystemExtension : + * - SchemaExtension + * - TypeExtension + * + * TypeExtension : + * - ScalarTypeExtension + * - ObjectTypeExtension + * - InterfaceTypeExtension + * - UnionTypeExtension + * - EnumTypeExtension + * - InputObjectTypeDefinition + */ + + +function parseTypeSystemExtension(lexer) { + var keywordToken = lexer.lookahead(); + + if (keywordToken.kind === TokenKind.NAME) { + switch (keywordToken.value) { + case 'schema': + return parseSchemaExtension(lexer); + + case 'scalar': + return parseScalarTypeExtension(lexer); + + case 'type': + return parseObjectTypeExtension(lexer); + + case 'interface': + return parseInterfaceTypeExtension(lexer); + + case 'union': + return parseUnionTypeExtension(lexer); + + case 'enum': + return parseEnumTypeExtension(lexer); + + case 'input': + return parseInputObjectTypeExtension(lexer); + } + } + + throw unexpected(lexer, keywordToken); +} +/** + * SchemaExtension : + * - extend schema Directives[Const]? { OperationTypeDefinition+ } + * - extend schema Directives[Const] + */ + + +function parseSchemaExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'schema'); + var directives = parseDirectives(lexer, true); + var operationTypes = peek(lexer, TokenKind.BRACE_L) ? many(lexer, TokenKind.BRACE_L, parseOperationTypeDefinition, TokenKind.BRACE_R) : []; + + if (directives.length === 0 && operationTypes.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.SCHEMA_EXTENSION, + directives: directives, + operationTypes: operationTypes, + loc: loc(lexer, start) + }; +} +/** + * ScalarTypeExtension : + * - extend scalar Name Directives[Const] + */ + + +function parseScalarTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'scalar'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + + if (directives.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.SCALAR_TYPE_EXTENSION, + name: name, + directives: directives, + loc: loc(lexer, start) + }; +} +/** + * ObjectTypeExtension : + * - extend type Name ImplementsInterfaces? Directives[Const]? FieldsDefinition + * - extend type Name ImplementsInterfaces? Directives[Const] + * - extend type Name ImplementsInterfaces + */ + + +function parseObjectTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'type'); + var name = parseName(lexer); + var interfaces = parseImplementsInterfaces(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + + if (interfaces.length === 0 && directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.OBJECT_TYPE_EXTENSION, + name: name, + interfaces: interfaces, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * InterfaceTypeExtension : + * - extend interface Name Directives[Const]? FieldsDefinition + * - extend interface Name Directives[Const] + */ + + +function parseInterfaceTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'interface'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseFieldsDefinition(lexer); + + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.INTERFACE_TYPE_EXTENSION, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * UnionTypeExtension : + * - extend union Name Directives[Const]? UnionMemberTypes + * - extend union Name Directives[Const] + */ + + +function parseUnionTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'union'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var types = parseUnionMemberTypes(lexer); + + if (directives.length === 0 && types.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.UNION_TYPE_EXTENSION, + name: name, + directives: directives, + types: types, + loc: loc(lexer, start) + }; +} +/** + * EnumTypeExtension : + * - extend enum Name Directives[Const]? EnumValuesDefinition + * - extend enum Name Directives[Const] + */ + + +function parseEnumTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'enum'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var values = parseEnumValuesDefinition(lexer); + + if (directives.length === 0 && values.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.ENUM_TYPE_EXTENSION, + name: name, + directives: directives, + values: values, + loc: loc(lexer, start) + }; +} +/** + * InputObjectTypeExtension : + * - extend input Name Directives[Const]? InputFieldsDefinition + * - extend input Name Directives[Const] + */ + + +function parseInputObjectTypeExtension(lexer) { + var start = lexer.token; + expectKeyword(lexer, 'extend'); + expectKeyword(lexer, 'input'); + var name = parseName(lexer); + var directives = parseDirectives(lexer, true); + var fields = parseInputFieldsDefinition(lexer); + + if (directives.length === 0 && fields.length === 0) { + throw unexpected(lexer); + } + + return { + kind: Kind.INPUT_OBJECT_TYPE_EXTENSION, + name: name, + directives: directives, + fields: fields, + loc: loc(lexer, start) + }; +} +/** + * DirectiveDefinition : + * - Description? directive @ Name ArgumentsDefinition? on DirectiveLocations + */ + + +function parseDirectiveDefinition(lexer) { + var start = lexer.token; + var description = parseDescription(lexer); + expectKeyword(lexer, 'directive'); + expect(lexer, TokenKind.AT); + var name = parseName(lexer); + var args = parseArgumentDefs(lexer); + expectKeyword(lexer, 'on'); + var locations = parseDirectiveLocations(lexer); + return { + kind: Kind.DIRECTIVE_DEFINITION, + description: description, + name: name, + arguments: args, + locations: locations, + loc: loc(lexer, start) + }; +} +/** + * DirectiveLocations : + * - `|`? DirectiveLocation + * - DirectiveLocations | DirectiveLocation + */ + + +function parseDirectiveLocations(lexer) { + // Optional leading pipe + skip(lexer, TokenKind.PIPE); + var locations = []; + + do { + locations.push(parseDirectiveLocation(lexer)); + } while (skip(lexer, TokenKind.PIPE)); + + return locations; +} +/* + * DirectiveLocation : + * - ExecutableDirectiveLocation + * - TypeSystemDirectiveLocation + * + * ExecutableDirectiveLocation : one of + * `QUERY` + * `MUTATION` + * `SUBSCRIPTION` + * `FIELD` + * `FRAGMENT_DEFINITION` + * `FRAGMENT_SPREAD` + * `INLINE_FRAGMENT` + * + * TypeSystemDirectiveLocation : one of + * `SCHEMA` + * `SCALAR` + * `OBJECT` + * `FIELD_DEFINITION` + * `ARGUMENT_DEFINITION` + * `INTERFACE` + * `UNION` + * `ENUM` + * `ENUM_VALUE` + * `INPUT_OBJECT` + * `INPUT_FIELD_DEFINITION` + */ + + +function parseDirectiveLocation(lexer) { + var start = lexer.token; + var name = parseName(lexer); + + if (DirectiveLocation.hasOwnProperty(name.value)) { + return name; + } + + throw unexpected(lexer, start); +} // Core parsing utility functions + +/** + * Returns a location object, used to identify the place in + * the source that created a given parsed object. + */ + + +function loc(lexer, startToken) { + if (!lexer.options.noLocation) { + return new Loc(startToken, lexer.lastToken, lexer.source); + } +} + +function Loc(startToken, endToken, source) { + this.start = startToken.start; + this.end = endToken.end; + this.startToken = startToken; + this.endToken = endToken; + this.source = source; +} // Print a simplified form when appearing in JSON/util.inspect. + + +Loc.prototype.toJSON = Loc.prototype.inspect = function toJSON() { + return { + start: this.start, + end: this.end + }; +}; +/** + * Determines if the next token is of a given kind + */ + + +function peek(lexer, kind) { + return lexer.token.kind === kind; +} +/** + * If the next token is of the given kind, return true after advancing + * the lexer. Otherwise, do not change the parser state and return false. + */ + + +function skip(lexer, kind) { + var match = lexer.token.kind === kind; + + if (match) { + lexer.advance(); + } + + return match; +} +/** + * If the next token is of the given kind, return that token after advancing + * the lexer. Otherwise, do not change the parser state and throw an error. + */ + + +function expect(lexer, kind) { + var token = lexer.token; + + if (token.kind === kind) { + lexer.advance(); + return token; + } + + throw syntaxError(lexer.source, token.start, "Expected ".concat(kind, ", found ").concat(getTokenDesc(token))); +} +/** + * If the next token is a keyword with the given value, return that token after + * advancing the lexer. Otherwise, do not change the parser state and return + * false. + */ + + +function expectKeyword(lexer, value) { + var token = lexer.token; + + if (token.kind === TokenKind.NAME && token.value === value) { + lexer.advance(); + return token; + } + + throw syntaxError(lexer.source, token.start, "Expected \"".concat(value, "\", found ").concat(getTokenDesc(token))); +} +/** + * Helper function for creating an error when an unexpected lexed token + * is encountered. + */ + + +function unexpected(lexer, atToken) { + var token = atToken || lexer.token; + return syntaxError(lexer.source, token.start, "Unexpected ".concat(getTokenDesc(token))); +} +/** + * Returns a possibly empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ + + +function any(lexer, openKind, parseFn, closeKind) { + expect(lexer, openKind); + var nodes = []; + + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + + return nodes; +} +/** + * Returns a non-empty list of parse nodes, determined by + * the parseFn. This list begins with a lex token of openKind + * and ends with a lex token of closeKind. Advances the parser + * to the next lex token after the closing token. + */ + + +function many(lexer, openKind, parseFn, closeKind) { + expect(lexer, openKind); + var nodes = [parseFn(lexer)]; + + while (!skip(lexer, closeKind)) { + nodes.push(parseFn(lexer)); + } + + return nodes; +} \ No newline at end of file diff --git a/dist/language/printer.js b/dist/language/printer.js new file mode 100644 index 0000000000..3e493dac11 --- /dev/null +++ b/dist/language/printer.js @@ -0,0 +1,318 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.print = print; + +var _visitor = require("./visitor"); + +/** + * 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. + */ + +/** + * Converts an AST into a string, using one set of reasonable + * formatting rules. + */ +function print(ast) { + return (0, _visitor.visit)(ast, { + leave: printDocASTReducer + }); +} + +var printDocASTReducer = { + Name: function Name(node) { + return node.value; + }, + Variable: function Variable(node) { + return '$' + node.name; + }, + // Document + Document: function Document(node) { + return join(node.definitions, '\n\n') + '\n'; + }, + OperationDefinition: function OperationDefinition(node) { + var op = node.operation; + var name = node.name; + var varDefs = wrap('(', join(node.variableDefinitions, ', '), ')'); + var directives = join(node.directives, ' '); + var selectionSet = node.selectionSet; // Anonymous queries with no directives or variable definitions can use + // the query short form. + + return !name && !directives && !varDefs && op === 'query' ? selectionSet : join([op, join([name, varDefs]), directives, selectionSet], ' '); + }, + VariableDefinition: function VariableDefinition(_ref) { + var variable = _ref.variable, + type = _ref.type, + defaultValue = _ref.defaultValue; + return variable + ': ' + type + wrap(' = ', defaultValue); + }, + SelectionSet: function SelectionSet(_ref2) { + var selections = _ref2.selections; + return block(selections); + }, + Field: function Field(_ref3) { + var alias = _ref3.alias, + name = _ref3.name, + args = _ref3.arguments, + directives = _ref3.directives, + selectionSet = _ref3.selectionSet; + return join([wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), join(directives, ' '), selectionSet], ' '); + }, + Argument: function Argument(_ref4) { + var name = _ref4.name, + value = _ref4.value; + return name + ': ' + value; + }, + // Fragments + FragmentSpread: function FragmentSpread(_ref5) { + var name = _ref5.name, + directives = _ref5.directives; + return '...' + name + wrap(' ', join(directives, ' ')); + }, + InlineFragment: function InlineFragment(_ref6) { + var typeCondition = _ref6.typeCondition, + directives = _ref6.directives, + selectionSet = _ref6.selectionSet; + return join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '); + }, + FragmentDefinition: function FragmentDefinition(_ref7) { + var name = _ref7.name, + typeCondition = _ref7.typeCondition, + variableDefinitions = _ref7.variableDefinitions, + directives = _ref7.directives, + selectionSet = _ref7.selectionSet; + return (// Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + "fragment ".concat(name).concat(wrap('(', join(variableDefinitions, ', '), ')'), " ") + "on ".concat(typeCondition, " ").concat(wrap('', join(directives, ' '), ' ')) + selectionSet + ); + }, + // Value + IntValue: function IntValue(_ref8) { + var value = _ref8.value; + return value; + }, + FloatValue: function FloatValue(_ref9) { + var value = _ref9.value; + return value; + }, + StringValue: function StringValue(_ref10, key) { + var value = _ref10.value, + isBlockString = _ref10.block; + return isBlockString ? printBlockString(value, key === 'description') : JSON.stringify(value); + }, + BooleanValue: function BooleanValue(_ref11) { + var value = _ref11.value; + return value ? 'true' : 'false'; + }, + NullValue: function NullValue() { + return 'null'; + }, + EnumValue: function EnumValue(_ref12) { + var value = _ref12.value; + return value; + }, + ListValue: function ListValue(_ref13) { + var values = _ref13.values; + return '[' + join(values, ', ') + ']'; + }, + ObjectValue: function ObjectValue(_ref14) { + var fields = _ref14.fields; + return '{' + join(fields, ', ') + '}'; + }, + ObjectField: function ObjectField(_ref15) { + var name = _ref15.name, + value = _ref15.value; + return name + ': ' + value; + }, + // Directive + Directive: function Directive(_ref16) { + var name = _ref16.name, + args = _ref16.arguments; + return '@' + name + wrap('(', join(args, ', '), ')'); + }, + // Type + NamedType: function NamedType(_ref17) { + var name = _ref17.name; + return name; + }, + ListType: function ListType(_ref18) { + var type = _ref18.type; + return '[' + type + ']'; + }, + NonNullType: function NonNullType(_ref19) { + var type = _ref19.type; + return type + '!'; + }, + // Type System Definitions + SchemaDefinition: function SchemaDefinition(_ref20) { + var directives = _ref20.directives, + operationTypes = _ref20.operationTypes; + return join(['schema', join(directives, ' '), block(operationTypes)], ' '); + }, + OperationTypeDefinition: function OperationTypeDefinition(_ref21) { + var operation = _ref21.operation, + type = _ref21.type; + return operation + ': ' + type; + }, + ScalarTypeDefinition: addDescription(function (_ref22) { + var name = _ref22.name, + directives = _ref22.directives; + return join(['scalar', name, join(directives, ' ')], ' '); + }), + ObjectTypeDefinition: addDescription(function (_ref23) { + var name = _ref23.name, + interfaces = _ref23.interfaces, + directives = _ref23.directives, + fields = _ref23.fields; + return join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '); + }), + FieldDefinition: addDescription(function (_ref24) { + var name = _ref24.name, + args = _ref24.arguments, + type = _ref24.type, + directives = _ref24.directives; + return name + (args.every(function (arg) { + return arg.indexOf('\n') === -1; + }) ? wrap('(', join(args, ', '), ')') : wrap('(\n', indent(join(args, '\n')), '\n)')) + ': ' + type + wrap(' ', join(directives, ' ')); + }), + InputValueDefinition: addDescription(function (_ref25) { + var name = _ref25.name, + type = _ref25.type, + defaultValue = _ref25.defaultValue, + directives = _ref25.directives; + return join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' '); + }), + InterfaceTypeDefinition: addDescription(function (_ref26) { + var name = _ref26.name, + directives = _ref26.directives, + fields = _ref26.fields; + return join(['interface', name, join(directives, ' '), block(fields)], ' '); + }), + UnionTypeDefinition: addDescription(function (_ref27) { + var name = _ref27.name, + directives = _ref27.directives, + types = _ref27.types; + return join(['union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' '); + }), + EnumTypeDefinition: addDescription(function (_ref28) { + var name = _ref28.name, + directives = _ref28.directives, + values = _ref28.values; + return join(['enum', name, join(directives, ' '), block(values)], ' '); + }), + EnumValueDefinition: addDescription(function (_ref29) { + var name = _ref29.name, + directives = _ref29.directives; + return join([name, join(directives, ' ')], ' '); + }), + InputObjectTypeDefinition: addDescription(function (_ref30) { + var name = _ref30.name, + directives = _ref30.directives, + fields = _ref30.fields; + return join(['input', name, join(directives, ' '), block(fields)], ' '); + }), + DirectiveDefinition: addDescription(function (_ref31) { + var name = _ref31.name, + args = _ref31.arguments, + locations = _ref31.locations; + return 'directive @' + name + (args.every(function (arg) { + return arg.indexOf('\n') === -1; + }) ? wrap('(', join(args, ', '), ')') : wrap('(\n', indent(join(args, '\n')), '\n)')) + ' on ' + join(locations, ' | '); + }), + SchemaExtension: function SchemaExtension(_ref32) { + var directives = _ref32.directives, + operationTypes = _ref32.operationTypes; + return join(['extend schema', join(directives, ' '), block(operationTypes)], ' '); + }, + ScalarTypeExtension: function ScalarTypeExtension(_ref33) { + var name = _ref33.name, + directives = _ref33.directives; + return join(['extend scalar', name, join(directives, ' ')], ' '); + }, + ObjectTypeExtension: function ObjectTypeExtension(_ref34) { + var name = _ref34.name, + interfaces = _ref34.interfaces, + directives = _ref34.directives, + fields = _ref34.fields; + return join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '); + }, + InterfaceTypeExtension: function InterfaceTypeExtension(_ref35) { + var name = _ref35.name, + directives = _ref35.directives, + fields = _ref35.fields; + return join(['extend interface', name, join(directives, ' '), block(fields)], ' '); + }, + UnionTypeExtension: function UnionTypeExtension(_ref36) { + var name = _ref36.name, + directives = _ref36.directives, + types = _ref36.types; + return join(['extend union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' '); + }, + EnumTypeExtension: function EnumTypeExtension(_ref37) { + var name = _ref37.name, + directives = _ref37.directives, + values = _ref37.values; + return join(['extend enum', name, join(directives, ' '), block(values)], ' '); + }, + InputObjectTypeExtension: function InputObjectTypeExtension(_ref38) { + var name = _ref38.name, + directives = _ref38.directives, + fields = _ref38.fields; + return join(['extend input', name, join(directives, ' '), block(fields)], ' '); + } +}; + +function addDescription(cb) { + return function (node) { + return join([node.description, cb(node)], '\n'); + }; +} +/** + * Given maybeArray, print an empty string if it is null or empty, otherwise + * print all items together separated by separator if provided + */ + + +function join(maybeArray, separator) { + return maybeArray ? maybeArray.filter(function (x) { + return x; + }).join(separator || '') : ''; +} +/** + * Given array, print each item on its own line, wrapped in an + * indented "{ }" block. + */ + + +function block(array) { + return array && array.length !== 0 ? '{\n' + indent(join(array, '\n')) + '\n}' : ''; +} +/** + * If maybeString is not null or empty, then wrap with start and end, otherwise + * print an empty string. + */ + + +function wrap(start, maybeString, end) { + return maybeString ? start + maybeString + (end || '') : ''; +} + +function indent(maybeString) { + return maybeString && ' ' + maybeString.replace(/\n/g, '\n '); +} +/** + * Print a block string in the indented block form by adding a leading and + * trailing blank line. However, if a block string starts with whitespace and is + * a single-line, adding a leading blank line would strip that whitespace. + */ + + +function printBlockString(value, isDescription) { + var escaped = value.replace(/"""/g, '\\"""'); + return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1 ? "\"\"\"".concat(escaped.replace(/"$/, '"\n'), "\"\"\"") : "\"\"\"\n".concat(isDescription ? escaped : indent(escaped), "\n\"\"\""); +} \ No newline at end of file diff --git a/dist/language/printer.js.flow b/dist/language/printer.js.flow new file mode 100644 index 0000000000..0242898f52 --- /dev/null +++ b/dist/language/printer.js.flow @@ -0,0 +1,272 @@ +/** + * 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. + */ + +import { visit } from './visitor'; + +/** + * Converts an AST into a string, using one set of reasonable + * formatting rules. + */ +export function print(ast) { + return visit(ast, { leave: printDocASTReducer }); +} + +const printDocASTReducer = { + Name: node => node.value, + Variable: node => '$' + node.name, + + // Document + + Document: node => join(node.definitions, '\n\n') + '\n', + + OperationDefinition(node) { + const op = node.operation; + const name = node.name; + const varDefs = wrap('(', join(node.variableDefinitions, ', '), ')'); + const directives = join(node.directives, ' '); + const selectionSet = node.selectionSet; + // Anonymous queries with no directives or variable definitions can use + // the query short form. + return !name && !directives && !varDefs && op === 'query' + ? selectionSet + : join([op, join([name, varDefs]), directives, selectionSet], ' '); + }, + + VariableDefinition: ({ variable, type, defaultValue }) => + variable + ': ' + type + wrap(' = ', defaultValue), + + SelectionSet: ({ selections }) => block(selections), + + Field: ({ alias, name, arguments: args, directives, selectionSet }) => + join( + [ + wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), + join(directives, ' '), + selectionSet, + ], + ' ', + ), + + Argument: ({ name, value }) => name + ': ' + value, + + // Fragments + + FragmentSpread: ({ name, directives }) => + '...' + name + wrap(' ', join(directives, ' ')), + + InlineFragment: ({ typeCondition, directives, selectionSet }) => + join( + ['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], + ' ', + ), + + FragmentDefinition: ({ + name, + typeCondition, + variableDefinitions, + directives, + selectionSet, + }) => + // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + `fragment ${name}${wrap('(', join(variableDefinitions, ', '), ')')} ` + + `on ${typeCondition} ${wrap('', join(directives, ' '), ' ')}` + + selectionSet, + + // Value + + IntValue: ({ value }) => value, + FloatValue: ({ value }) => value, + StringValue: ({ value, block: isBlockString }, key) => + isBlockString + ? printBlockString(value, key === 'description') + : JSON.stringify(value), + BooleanValue: ({ value }) => (value ? 'true' : 'false'), + NullValue: () => 'null', + EnumValue: ({ value }) => value, + ListValue: ({ values }) => '[' + join(values, ', ') + ']', + ObjectValue: ({ fields }) => '{' + join(fields, ', ') + '}', + ObjectField: ({ name, value }) => name + ': ' + value, + + // Directive + + Directive: ({ name, arguments: args }) => + '@' + name + wrap('(', join(args, ', '), ')'), + + // Type + + NamedType: ({ name }) => name, + ListType: ({ type }) => '[' + type + ']', + NonNullType: ({ type }) => type + '!', + + // Type System Definitions + + SchemaDefinition: ({ directives, operationTypes }) => + join(['schema', join(directives, ' '), block(operationTypes)], ' '), + + OperationTypeDefinition: ({ operation, type }) => operation + ': ' + type, + + ScalarTypeDefinition: addDescription(({ name, directives }) => + join(['scalar', name, join(directives, ' ')], ' '), + ), + + ObjectTypeDefinition: addDescription( + ({ name, interfaces, directives, fields }) => + join( + [ + 'type', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', + ), + ), + + FieldDefinition: addDescription( + ({ name, arguments: args, type, directives }) => + name + + (args.every(arg => arg.indexOf('\n') === -1) + ? wrap('(', join(args, ', '), ')') + : wrap('(\n', indent(join(args, '\n')), '\n)')) + + ': ' + + type + + wrap(' ', join(directives, ' ')), + ), + + InputValueDefinition: addDescription( + ({ name, type, defaultValue, directives }) => + join( + [name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], + ' ', + ), + ), + + InterfaceTypeDefinition: addDescription(({ name, directives, fields }) => + join(['interface', name, join(directives, ' '), block(fields)], ' '), + ), + + UnionTypeDefinition: addDescription(({ name, directives, types }) => + join( + [ + 'union', + name, + join(directives, ' '), + types && types.length !== 0 ? '= ' + join(types, ' | ') : '', + ], + ' ', + ), + ), + + EnumTypeDefinition: addDescription(({ name, directives, values }) => + join(['enum', name, join(directives, ' '), block(values)], ' '), + ), + + EnumValueDefinition: addDescription(({ name, directives }) => + join([name, join(directives, ' ')], ' '), + ), + + InputObjectTypeDefinition: addDescription(({ name, directives, fields }) => + join(['input', name, join(directives, ' '), block(fields)], ' '), + ), + + DirectiveDefinition: addDescription( + ({ name, arguments: args, locations }) => + 'directive @' + + name + + (args.every(arg => arg.indexOf('\n') === -1) + ? wrap('(', join(args, ', '), ')') + : wrap('(\n', indent(join(args, '\n')), '\n)')) + + ' on ' + + join(locations, ' | '), + ), + + SchemaExtension: ({ directives, operationTypes }) => + join(['extend schema', join(directives, ' '), block(operationTypes)], ' '), + + ScalarTypeExtension: ({ name, directives }) => + join(['extend scalar', name, join(directives, ' ')], ' '), + + ObjectTypeExtension: ({ name, interfaces, directives, fields }) => + join( + [ + 'extend type', + name, + wrap('implements ', join(interfaces, ' & ')), + join(directives, ' '), + block(fields), + ], + ' ', + ), + + InterfaceTypeExtension: ({ name, directives, fields }) => + join(['extend interface', name, join(directives, ' '), block(fields)], ' '), + + UnionTypeExtension: ({ name, directives, types }) => + join( + [ + 'extend union', + name, + join(directives, ' '), + types && types.length !== 0 ? '= ' + join(types, ' | ') : '', + ], + ' ', + ), + + EnumTypeExtension: ({ name, directives, values }) => + join(['extend enum', name, join(directives, ' '), block(values)], ' '), + + InputObjectTypeExtension: ({ name, directives, fields }) => + join(['extend input', name, join(directives, ' '), block(fields)], ' '), +}; + +function addDescription(cb) { + return node => join([node.description, cb(node)], '\n'); +} + +/** + * Given maybeArray, print an empty string if it is null or empty, otherwise + * print all items together separated by separator if provided + */ +function join(maybeArray, separator) { + return maybeArray ? maybeArray.filter(x => x).join(separator || '') : ''; +} + +/** + * Given array, print each item on its own line, wrapped in an + * indented "{ }" block. + */ +function block(array) { + return array && array.length !== 0 + ? '{\n' + indent(join(array, '\n')) + '\n}' + : ''; +} + +/** + * If maybeString is not null or empty, then wrap with start and end, otherwise + * print an empty string. + */ +function wrap(start, maybeString, end) { + return maybeString ? start + maybeString + (end || '') : ''; +} + +function indent(maybeString) { + return maybeString && ' ' + maybeString.replace(/\n/g, '\n '); +} + +/** + * Print a block string in the indented block form by adding a leading and + * trailing blank line. However, if a block string starts with whitespace and is + * a single-line, adding a leading blank line would strip that whitespace. + */ +function printBlockString(value, isDescription) { + const escaped = value.replace(/"""/g, '\\"""'); + return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1 + ? `"""${escaped.replace(/"$/, '"\n')}"""` + : `"""\n${isDescription ? escaped : indent(escaped)}\n"""`; +} diff --git a/dist/language/printer.mjs b/dist/language/printer.mjs new file mode 100644 index 0000000000..fc6da6bc18 --- /dev/null +++ b/dist/language/printer.mjs @@ -0,0 +1,309 @@ +/** + * 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. + */ +import { visit } from './visitor'; +/** + * Converts an AST into a string, using one set of reasonable + * formatting rules. + */ + +export function print(ast) { + return visit(ast, { + leave: printDocASTReducer + }); +} +var printDocASTReducer = { + Name: function Name(node) { + return node.value; + }, + Variable: function Variable(node) { + return '$' + node.name; + }, + // Document + Document: function Document(node) { + return join(node.definitions, '\n\n') + '\n'; + }, + OperationDefinition: function OperationDefinition(node) { + var op = node.operation; + var name = node.name; + var varDefs = wrap('(', join(node.variableDefinitions, ', '), ')'); + var directives = join(node.directives, ' '); + var selectionSet = node.selectionSet; // Anonymous queries with no directives or variable definitions can use + // the query short form. + + return !name && !directives && !varDefs && op === 'query' ? selectionSet : join([op, join([name, varDefs]), directives, selectionSet], ' '); + }, + VariableDefinition: function VariableDefinition(_ref) { + var variable = _ref.variable, + type = _ref.type, + defaultValue = _ref.defaultValue; + return variable + ': ' + type + wrap(' = ', defaultValue); + }, + SelectionSet: function SelectionSet(_ref2) { + var selections = _ref2.selections; + return block(selections); + }, + Field: function Field(_ref3) { + var alias = _ref3.alias, + name = _ref3.name, + args = _ref3.arguments, + directives = _ref3.directives, + selectionSet = _ref3.selectionSet; + return join([wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), join(directives, ' '), selectionSet], ' '); + }, + Argument: function Argument(_ref4) { + var name = _ref4.name, + value = _ref4.value; + return name + ': ' + value; + }, + // Fragments + FragmentSpread: function FragmentSpread(_ref5) { + var name = _ref5.name, + directives = _ref5.directives; + return '...' + name + wrap(' ', join(directives, ' ')); + }, + InlineFragment: function InlineFragment(_ref6) { + var typeCondition = _ref6.typeCondition, + directives = _ref6.directives, + selectionSet = _ref6.selectionSet; + return join(['...', wrap('on ', typeCondition), join(directives, ' '), selectionSet], ' '); + }, + FragmentDefinition: function FragmentDefinition(_ref7) { + var name = _ref7.name, + typeCondition = _ref7.typeCondition, + variableDefinitions = _ref7.variableDefinitions, + directives = _ref7.directives, + selectionSet = _ref7.selectionSet; + return (// Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + "fragment ".concat(name).concat(wrap('(', join(variableDefinitions, ', '), ')'), " ") + "on ".concat(typeCondition, " ").concat(wrap('', join(directives, ' '), ' ')) + selectionSet + ); + }, + // Value + IntValue: function IntValue(_ref8) { + var value = _ref8.value; + return value; + }, + FloatValue: function FloatValue(_ref9) { + var value = _ref9.value; + return value; + }, + StringValue: function StringValue(_ref10, key) { + var value = _ref10.value, + isBlockString = _ref10.block; + return isBlockString ? printBlockString(value, key === 'description') : JSON.stringify(value); + }, + BooleanValue: function BooleanValue(_ref11) { + var value = _ref11.value; + return value ? 'true' : 'false'; + }, + NullValue: function NullValue() { + return 'null'; + }, + EnumValue: function EnumValue(_ref12) { + var value = _ref12.value; + return value; + }, + ListValue: function ListValue(_ref13) { + var values = _ref13.values; + return '[' + join(values, ', ') + ']'; + }, + ObjectValue: function ObjectValue(_ref14) { + var fields = _ref14.fields; + return '{' + join(fields, ', ') + '}'; + }, + ObjectField: function ObjectField(_ref15) { + var name = _ref15.name, + value = _ref15.value; + return name + ': ' + value; + }, + // Directive + Directive: function Directive(_ref16) { + var name = _ref16.name, + args = _ref16.arguments; + return '@' + name + wrap('(', join(args, ', '), ')'); + }, + // Type + NamedType: function NamedType(_ref17) { + var name = _ref17.name; + return name; + }, + ListType: function ListType(_ref18) { + var type = _ref18.type; + return '[' + type + ']'; + }, + NonNullType: function NonNullType(_ref19) { + var type = _ref19.type; + return type + '!'; + }, + // Type System Definitions + SchemaDefinition: function SchemaDefinition(_ref20) { + var directives = _ref20.directives, + operationTypes = _ref20.operationTypes; + return join(['schema', join(directives, ' '), block(operationTypes)], ' '); + }, + OperationTypeDefinition: function OperationTypeDefinition(_ref21) { + var operation = _ref21.operation, + type = _ref21.type; + return operation + ': ' + type; + }, + ScalarTypeDefinition: addDescription(function (_ref22) { + var name = _ref22.name, + directives = _ref22.directives; + return join(['scalar', name, join(directives, ' ')], ' '); + }), + ObjectTypeDefinition: addDescription(function (_ref23) { + var name = _ref23.name, + interfaces = _ref23.interfaces, + directives = _ref23.directives, + fields = _ref23.fields; + return join(['type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '); + }), + FieldDefinition: addDescription(function (_ref24) { + var name = _ref24.name, + args = _ref24.arguments, + type = _ref24.type, + directives = _ref24.directives; + return name + (args.every(function (arg) { + return arg.indexOf('\n') === -1; + }) ? wrap('(', join(args, ', '), ')') : wrap('(\n', indent(join(args, '\n')), '\n)')) + ': ' + type + wrap(' ', join(directives, ' ')); + }), + InputValueDefinition: addDescription(function (_ref25) { + var name = _ref25.name, + type = _ref25.type, + defaultValue = _ref25.defaultValue, + directives = _ref25.directives; + return join([name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')], ' '); + }), + InterfaceTypeDefinition: addDescription(function (_ref26) { + var name = _ref26.name, + directives = _ref26.directives, + fields = _ref26.fields; + return join(['interface', name, join(directives, ' '), block(fields)], ' '); + }), + UnionTypeDefinition: addDescription(function (_ref27) { + var name = _ref27.name, + directives = _ref27.directives, + types = _ref27.types; + return join(['union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' '); + }), + EnumTypeDefinition: addDescription(function (_ref28) { + var name = _ref28.name, + directives = _ref28.directives, + values = _ref28.values; + return join(['enum', name, join(directives, ' '), block(values)], ' '); + }), + EnumValueDefinition: addDescription(function (_ref29) { + var name = _ref29.name, + directives = _ref29.directives; + return join([name, join(directives, ' ')], ' '); + }), + InputObjectTypeDefinition: addDescription(function (_ref30) { + var name = _ref30.name, + directives = _ref30.directives, + fields = _ref30.fields; + return join(['input', name, join(directives, ' '), block(fields)], ' '); + }), + DirectiveDefinition: addDescription(function (_ref31) { + var name = _ref31.name, + args = _ref31.arguments, + locations = _ref31.locations; + return 'directive @' + name + (args.every(function (arg) { + return arg.indexOf('\n') === -1; + }) ? wrap('(', join(args, ', '), ')') : wrap('(\n', indent(join(args, '\n')), '\n)')) + ' on ' + join(locations, ' | '); + }), + SchemaExtension: function SchemaExtension(_ref32) { + var directives = _ref32.directives, + operationTypes = _ref32.operationTypes; + return join(['extend schema', join(directives, ' '), block(operationTypes)], ' '); + }, + ScalarTypeExtension: function ScalarTypeExtension(_ref33) { + var name = _ref33.name, + directives = _ref33.directives; + return join(['extend scalar', name, join(directives, ' ')], ' '); + }, + ObjectTypeExtension: function ObjectTypeExtension(_ref34) { + var name = _ref34.name, + interfaces = _ref34.interfaces, + directives = _ref34.directives, + fields = _ref34.fields; + return join(['extend type', name, wrap('implements ', join(interfaces, ' & ')), join(directives, ' '), block(fields)], ' '); + }, + InterfaceTypeExtension: function InterfaceTypeExtension(_ref35) { + var name = _ref35.name, + directives = _ref35.directives, + fields = _ref35.fields; + return join(['extend interface', name, join(directives, ' '), block(fields)], ' '); + }, + UnionTypeExtension: function UnionTypeExtension(_ref36) { + var name = _ref36.name, + directives = _ref36.directives, + types = _ref36.types; + return join(['extend union', name, join(directives, ' '), types && types.length !== 0 ? '= ' + join(types, ' | ') : ''], ' '); + }, + EnumTypeExtension: function EnumTypeExtension(_ref37) { + var name = _ref37.name, + directives = _ref37.directives, + values = _ref37.values; + return join(['extend enum', name, join(directives, ' '), block(values)], ' '); + }, + InputObjectTypeExtension: function InputObjectTypeExtension(_ref38) { + var name = _ref38.name, + directives = _ref38.directives, + fields = _ref38.fields; + return join(['extend input', name, join(directives, ' '), block(fields)], ' '); + } +}; + +function addDescription(cb) { + return function (node) { + return join([node.description, cb(node)], '\n'); + }; +} +/** + * Given maybeArray, print an empty string if it is null or empty, otherwise + * print all items together separated by separator if provided + */ + + +function join(maybeArray, separator) { + return maybeArray ? maybeArray.filter(function (x) { + return x; + }).join(separator || '') : ''; +} +/** + * Given array, print each item on its own line, wrapped in an + * indented "{ }" block. + */ + + +function block(array) { + return array && array.length !== 0 ? '{\n' + indent(join(array, '\n')) + '\n}' : ''; +} +/** + * If maybeString is not null or empty, then wrap with start and end, otherwise + * print an empty string. + */ + + +function wrap(start, maybeString, end) { + return maybeString ? start + maybeString + (end || '') : ''; +} + +function indent(maybeString) { + return maybeString && ' ' + maybeString.replace(/\n/g, '\n '); +} +/** + * Print a block string in the indented block form by adding a leading and + * trailing blank line. However, if a block string starts with whitespace and is + * a single-line, adding a leading blank line would strip that whitespace. + */ + + +function printBlockString(value, isDescription) { + var escaped = value.replace(/"""/g, '\\"""'); + return (value[0] === ' ' || value[0] === '\t') && value.indexOf('\n') === -1 ? "\"\"\"".concat(escaped.replace(/"$/, '"\n'), "\"\"\"") : "\"\"\"\n".concat(isDescription ? escaped : indent(escaped), "\n\"\"\""); +} \ No newline at end of file diff --git a/dist/language/source.js b/dist/language/source.js new file mode 100644 index 0000000000..3cf2033243 --- /dev/null +++ b/dist/language/source.js @@ -0,0 +1,39 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.Source = void 0; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * A representation of source input to GraphQL. + * `name` and `locationOffset` are optional. They are useful for clients who + * store GraphQL documents in source files; for example, if the GraphQL input + * starts at line 40 in a file named Foo.graphql, it might be useful for name to + * be "Foo.graphql" and location to be `{ line: 40, column: 0 }`. + * line and column in locationOffset are 1-indexed + */ +var Source = function Source(body, name, locationOffset) { + _defineProperty(this, "body", void 0); + + _defineProperty(this, "name", void 0); + + _defineProperty(this, "locationOffset", void 0); + + this.body = body; + this.name = name || 'GraphQL request'; + this.locationOffset = locationOffset || { + line: 1, + column: 1 + }; + !(this.locationOffset.line > 0) ? (0, _invariant.default)(0, 'line in locationOffset is 1-indexed and must be positive') : void 0; + !(this.locationOffset.column > 0) ? (0, _invariant.default)(0, 'column in locationOffset is 1-indexed and must be positive') : void 0; +}; + +exports.Source = Source; \ No newline at end of file diff --git a/dist/language/source.js.flow b/dist/language/source.js.flow new file mode 100644 index 0000000000..aae70b1df6 --- /dev/null +++ b/dist/language/source.js.flow @@ -0,0 +1,43 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; + +type Location = { + line: number, + column: number, +}; + +/** + * A representation of source input to GraphQL. + * `name` and `locationOffset` are optional. They are useful for clients who + * store GraphQL documents in source files; for example, if the GraphQL input + * starts at line 40 in a file named Foo.graphql, it might be useful for name to + * be "Foo.graphql" and location to be `{ line: 40, column: 0 }`. + * line and column in locationOffset are 1-indexed + */ +export class Source { + body: string; + name: string; + locationOffset: Location; + + constructor(body: string, name?: string, locationOffset?: Location): void { + this.body = body; + this.name = name || 'GraphQL request'; + this.locationOffset = locationOffset || { line: 1, column: 1 }; + invariant( + this.locationOffset.line > 0, + 'line in locationOffset is 1-indexed and must be positive', + ); + invariant( + this.locationOffset.column > 0, + 'column in locationOffset is 1-indexed and must be positive', + ); + } +} diff --git a/dist/language/source.mjs b/dist/language/source.mjs new file mode 100644 index 0000000000..00526eba5a --- /dev/null +++ b/dist/language/source.mjs @@ -0,0 +1,36 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; + +/** + * A representation of source input to GraphQL. + * `name` and `locationOffset` are optional. They are useful for clients who + * store GraphQL documents in source files; for example, if the GraphQL input + * starts at line 40 in a file named Foo.graphql, it might be useful for name to + * be "Foo.graphql" and location to be `{ line: 40, column: 0 }`. + * line and column in locationOffset are 1-indexed + */ +export var Source = function Source(body, name, locationOffset) { + _defineProperty(this, "body", void 0); + + _defineProperty(this, "name", void 0); + + _defineProperty(this, "locationOffset", void 0); + + this.body = body; + this.name = name || 'GraphQL request'; + this.locationOffset = locationOffset || { + line: 1, + column: 1 + }; + !(this.locationOffset.line > 0) ? invariant(0, 'line in locationOffset is 1-indexed and must be positive') : void 0; + !(this.locationOffset.column > 0) ? invariant(0, 'column in locationOffset is 1-indexed and must be positive') : void 0; +}; \ No newline at end of file diff --git a/dist/language/visitor.js b/dist/language/visitor.js new file mode 100644 index 0000000000..e515cce85d --- /dev/null +++ b/dist/language/visitor.js @@ -0,0 +1,460 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.visit = visit; +exports.visitInParallel = visitInParallel; +exports.visitWithTypeInfo = visitWithTypeInfo; +exports.getVisitFn = getVisitFn; +exports.BREAK = exports.QueryDocumentKeys = void 0; + +/** + * 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. + * + * strict + */ + +/** + * A visitor is provided to visit, it contains the collection of + * relevant functions to be called during the visitor's traversal. + */ + +/** + * A visitor is comprised of visit functions, which are called on each node + * during the visitor's traversal. + */ + +/** + * A KeyMap describes each the traversable properties of each kind of node. + */ +var QueryDocumentKeys = { + Name: [], + Document: ['definitions'], + OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'], + VariableDefinition: ['variable', 'type', 'defaultValue'], + Variable: ['name'], + SelectionSet: ['selections'], + Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'], + Argument: ['name', 'value'], + FragmentSpread: ['name', 'directives'], + InlineFragment: ['typeCondition', 'directives', 'selectionSet'], + FragmentDefinition: ['name', // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet'], + IntValue: [], + FloatValue: [], + StringValue: [], + BooleanValue: [], + NullValue: [], + EnumValue: [], + ListValue: ['values'], + ObjectValue: ['fields'], + ObjectField: ['name', 'value'], + Directive: ['name', 'arguments'], + NamedType: ['name'], + ListType: ['type'], + NonNullType: ['type'], + SchemaDefinition: ['directives', 'operationTypes'], + OperationTypeDefinition: ['type'], + ScalarTypeDefinition: ['description', 'name', 'directives'], + ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'], + FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'], + InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'], + InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'], + UnionTypeDefinition: ['description', 'name', 'directives', 'types'], + EnumTypeDefinition: ['description', 'name', 'directives', 'values'], + EnumValueDefinition: ['description', 'name', 'directives'], + InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'], + DirectiveDefinition: ['description', 'name', 'arguments', 'locations'], + SchemaExtension: ['directives', 'operationTypes'], + ScalarTypeExtension: ['name', 'directives'], + ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'], + InterfaceTypeExtension: ['name', 'directives', 'fields'], + UnionTypeExtension: ['name', 'directives', 'types'], + EnumTypeExtension: ['name', 'directives', 'values'], + InputObjectTypeExtension: ['name', 'directives', 'fields'] +}; +exports.QueryDocumentKeys = QueryDocumentKeys; +var BREAK = {}; +/** + * visit() will walk through an AST using a depth first traversal, calling + * the visitor's enter function at each node in the traversal, and calling the + * leave function after visiting that node and all of its child nodes. + * + * By returning different values from the enter and leave functions, the + * behavior of the visitor can be altered, including skipping over a sub-tree of + * the AST (by returning false), editing the AST by returning a value or null + * to remove the value, or to stop the whole traversal by returning BREAK. + * + * When using visit() to edit an AST, the original AST will not be modified, and + * a new version of the AST with the changes applied will be returned from the + * visit function. + * + * const editedAST = visit(ast, { + * enter(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: skip visiting this node + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * }, + * leave(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: no action + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * } + * }); + * + * Alternatively to providing enter() and leave() functions, a visitor can + * instead provide functions named the same as the kinds of AST nodes, or + * enter/leave visitors at a named key, leading to four permutations of + * visitor API: + * + * 1) Named visitors triggered when entering a node a specific kind. + * + * visit(ast, { + * Kind(node) { + * // enter the "Kind" node + * } + * }) + * + * 2) Named visitors that trigger upon entering and leaving a node of + * a specific kind. + * + * visit(ast, { + * Kind: { + * enter(node) { + * // enter the "Kind" node + * } + * leave(node) { + * // leave the "Kind" node + * } + * } + * }) + * + * 3) Generic visitors that trigger upon entering and leaving any node. + * + * visit(ast, { + * enter(node) { + * // enter any node + * }, + * leave(node) { + * // leave any node + * } + * }) + * + * 4) Parallel visitors for entering and leaving nodes of a specific kind. + * + * visit(ast, { + * enter: { + * Kind(node) { + * // enter the "Kind" node + * } + * }, + * leave: { + * Kind(node) { + * // leave the "Kind" node + * } + * } + * }) + */ + +exports.BREAK = BREAK; + +function visit(root, visitor) { + var visitorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : QueryDocumentKeys; + + /* eslint-disable no-undef-init */ + var stack = undefined; + var inArray = Array.isArray(root); + var keys = [root]; + var index = -1; + var edits = []; + var node = undefined; + var key = undefined; + var parent = undefined; + var path = []; + var ancestors = []; + var newRoot = root; + /* eslint-enable no-undef-init */ + + do { + index++; + var isLeaving = index === keys.length; + var isEdited = isLeaving && edits.length !== 0; + + if (isLeaving) { + key = ancestors.length === 0 ? undefined : path[path.length - 1]; + node = parent; + parent = ancestors.pop(); + + if (isEdited) { + if (inArray) { + node = node.slice(); + } else { + var clone = {}; + + for (var k in node) { + if (node.hasOwnProperty(k)) { + clone[k] = node[k]; + } + } + + node = clone; + } + + var editOffset = 0; + + for (var ii = 0; ii < edits.length; ii++) { + var editKey = edits[ii][0]; + var editValue = edits[ii][1]; + + if (inArray) { + editKey -= editOffset; + } + + if (inArray && editValue === null) { + node.splice(editKey, 1); + editOffset++; + } else { + node[editKey] = editValue; + } + } + } + + index = stack.index; + keys = stack.keys; + edits = stack.edits; + inArray = stack.inArray; + stack = stack.prev; + } else { + key = parent ? inArray ? index : keys[index] : undefined; + node = parent ? parent[key] : newRoot; + + if (node === null || node === undefined) { + continue; + } + + if (parent) { + path.push(key); + } + } + + var result = void 0; + + if (!Array.isArray(node)) { + if (!isNode(node)) { + throw new Error('Invalid AST Node: ' + JSON.stringify(node)); + } + + var visitFn = getVisitFn(visitor, node.kind, isLeaving); + + if (visitFn) { + result = visitFn.call(visitor, node, key, parent, path, ancestors); + + if (result === BREAK) { + break; + } + + if (result === false) { + if (!isLeaving) { + path.pop(); + continue; + } + } else if (result !== undefined) { + edits.push([key, result]); + + if (!isLeaving) { + if (isNode(result)) { + node = result; + } else { + path.pop(); + continue; + } + } + } + } + } + + if (result === undefined && isEdited) { + edits.push([key, node]); + } + + if (isLeaving) { + path.pop(); + } else { + stack = { + inArray: inArray, + index: index, + keys: keys, + edits: edits, + prev: stack + }; + inArray = Array.isArray(node); + keys = inArray ? node : visitorKeys[node.kind] || []; + index = -1; + edits = []; + + if (parent) { + ancestors.push(parent); + } + + parent = node; + } + } while (stack !== undefined); + + if (edits.length !== 0) { + newRoot = edits[edits.length - 1][1]; + } + + return newRoot; +} + +function isNode(maybeNode) { + return Boolean(maybeNode && typeof maybeNode.kind === 'string'); +} +/** + * Creates a new visitor instance which delegates to many visitors to run in + * parallel. Each visitor will be visited for each node before moving on. + * + * If a prior visitor edits a node, no following visitors will see that node. + */ + + +function visitInParallel(visitors) { + var skipping = new Array(visitors.length); + return { + enter: function enter(node) { + for (var i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + var fn = getVisitFn(visitors[i], node.kind, + /* isLeaving */ + false); + + if (fn) { + var result = fn.apply(visitors[i], arguments); + + if (result === false) { + skipping[i] = node; + } else if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined) { + return result; + } + } + } + } + }, + leave: function leave(node) { + for (var i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + var fn = getVisitFn(visitors[i], node.kind, + /* isLeaving */ + true); + + if (fn) { + var result = fn.apply(visitors[i], arguments); + + if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined && result !== false) { + return result; + } + } + } else if (skipping[i] === node) { + skipping[i] = null; + } + } + } + }; +} +/** + * Creates a new visitor instance which maintains a provided TypeInfo instance + * along with visiting visitor. + */ + + +function visitWithTypeInfo(typeInfo, visitor) { + return { + enter: function enter(node) { + typeInfo.enter(node); + var fn = getVisitFn(visitor, node.kind, + /* isLeaving */ + false); + + if (fn) { + var result = fn.apply(visitor, arguments); + + if (result !== undefined) { + typeInfo.leave(node); + + if (isNode(result)) { + typeInfo.enter(result); + } + } + + return result; + } + }, + leave: function leave(node) { + var fn = getVisitFn(visitor, node.kind, + /* isLeaving */ + true); + var result; + + if (fn) { + result = fn.apply(visitor, arguments); + } + + typeInfo.leave(node); + return result; + } + }; +} +/** + * Given a visitor instance, if it is leaving or not, and a node kind, return + * the function the visitor runtime should call. + */ + + +function getVisitFn(visitor, kind, isLeaving) { + var kindVisitor = visitor[kind]; + + if (kindVisitor) { + if (!isLeaving && typeof kindVisitor === 'function') { + // { Kind() {} } + return kindVisitor; + } + + var kindSpecificVisitor = isLeaving ? kindVisitor.leave : kindVisitor.enter; + + if (typeof kindSpecificVisitor === 'function') { + // { Kind: { enter() {}, leave() {} } } + return kindSpecificVisitor; + } + } else { + var specificVisitor = isLeaving ? visitor.leave : visitor.enter; + + if (specificVisitor) { + if (typeof specificVisitor === 'function') { + // { enter() {}, leave() {} } + return specificVisitor; + } + + var specificKindVisitor = specificVisitor[kind]; + + if (typeof specificKindVisitor === 'function') { + // { enter: { Kind() {} }, leave: { Kind() {} } } + return specificKindVisitor; + } + } + } +} \ No newline at end of file diff --git a/dist/language/visitor.js.flow b/dist/language/visitor.js.flow new file mode 100644 index 0000000000..00e4510992 --- /dev/null +++ b/dist/language/visitor.js.flow @@ -0,0 +1,478 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ASTNode, ASTKindToNode } from './ast'; +import type { TypeInfo } from '../utilities/TypeInfo'; + +/** + * A visitor is provided to visit, it contains the collection of + * relevant functions to be called during the visitor's traversal. + */ +export type ASTVisitor = Visitor; +export type Visitor> = + | EnterLeave< + | VisitFn + | ShapeMap(Node) => VisitFn>, + > + | ShapeMap< + KindToNode, + (Node) => VisitFn | EnterLeave>, + >; +type EnterLeave = {| +enter?: T, +leave?: T |}; +type ShapeMap = $Shape<$ObjMap>; + +/** + * A visitor is comprised of visit functions, which are called on each node + * during the visitor's traversal. + */ +export type VisitFn = ( + // The current node being visiting. + node: TVisitedNode, + // The index or key to this node from the parent node or Array. + key: string | number | void, + // The parent immediately above this node, which may be an Array. + parent: TAnyNode | $ReadOnlyArray | void, + // The key path to get to this node from the root node. + path: $ReadOnlyArray, + // All nodes and Arrays visited before reaching this node. + // These correspond to array indices in `path`. + // Note: ancestors includes arrays which contain the visited node. + ancestors: $ReadOnlyArray>, +) => any; + +/** + * A KeyMap describes each the traversable properties of each kind of node. + */ +export type VisitorKeyMap = $ObjMap< + KindToNode, + (T) => $ReadOnlyArray<$Keys>, +>; + +export const QueryDocumentKeys = { + Name: [], + + Document: ['definitions'], + OperationDefinition: [ + 'name', + 'variableDefinitions', + 'directives', + 'selectionSet', + ], + VariableDefinition: ['variable', 'type', 'defaultValue'], + Variable: ['name'], + SelectionSet: ['selections'], + Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'], + Argument: ['name', 'value'], + + FragmentSpread: ['name', 'directives'], + InlineFragment: ['typeCondition', 'directives', 'selectionSet'], + FragmentDefinition: [ + 'name', + // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + 'variableDefinitions', + 'typeCondition', + 'directives', + 'selectionSet', + ], + + IntValue: [], + FloatValue: [], + StringValue: [], + BooleanValue: [], + NullValue: [], + EnumValue: [], + ListValue: ['values'], + ObjectValue: ['fields'], + ObjectField: ['name', 'value'], + + Directive: ['name', 'arguments'], + + NamedType: ['name'], + ListType: ['type'], + NonNullType: ['type'], + + SchemaDefinition: ['directives', 'operationTypes'], + OperationTypeDefinition: ['type'], + + ScalarTypeDefinition: ['description', 'name', 'directives'], + ObjectTypeDefinition: [ + 'description', + 'name', + 'interfaces', + 'directives', + 'fields', + ], + FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'], + InputValueDefinition: [ + 'description', + 'name', + 'type', + 'defaultValue', + 'directives', + ], + InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'], + UnionTypeDefinition: ['description', 'name', 'directives', 'types'], + EnumTypeDefinition: ['description', 'name', 'directives', 'values'], + EnumValueDefinition: ['description', 'name', 'directives'], + InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'], + + DirectiveDefinition: ['description', 'name', 'arguments', 'locations'], + + SchemaExtension: ['directives', 'operationTypes'], + + ScalarTypeExtension: ['name', 'directives'], + ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'], + InterfaceTypeExtension: ['name', 'directives', 'fields'], + UnionTypeExtension: ['name', 'directives', 'types'], + EnumTypeExtension: ['name', 'directives', 'values'], + InputObjectTypeExtension: ['name', 'directives', 'fields'], +}; + +export const BREAK = {}; + +/** + * visit() will walk through an AST using a depth first traversal, calling + * the visitor's enter function at each node in the traversal, and calling the + * leave function after visiting that node and all of its child nodes. + * + * By returning different values from the enter and leave functions, the + * behavior of the visitor can be altered, including skipping over a sub-tree of + * the AST (by returning false), editing the AST by returning a value or null + * to remove the value, or to stop the whole traversal by returning BREAK. + * + * When using visit() to edit an AST, the original AST will not be modified, and + * a new version of the AST with the changes applied will be returned from the + * visit function. + * + * const editedAST = visit(ast, { + * enter(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: skip visiting this node + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * }, + * leave(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: no action + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * } + * }); + * + * Alternatively to providing enter() and leave() functions, a visitor can + * instead provide functions named the same as the kinds of AST nodes, or + * enter/leave visitors at a named key, leading to four permutations of + * visitor API: + * + * 1) Named visitors triggered when entering a node a specific kind. + * + * visit(ast, { + * Kind(node) { + * // enter the "Kind" node + * } + * }) + * + * 2) Named visitors that trigger upon entering and leaving a node of + * a specific kind. + * + * visit(ast, { + * Kind: { + * enter(node) { + * // enter the "Kind" node + * } + * leave(node) { + * // leave the "Kind" node + * } + * } + * }) + * + * 3) Generic visitors that trigger upon entering and leaving any node. + * + * visit(ast, { + * enter(node) { + * // enter any node + * }, + * leave(node) { + * // leave any node + * } + * }) + * + * 4) Parallel visitors for entering and leaving nodes of a specific kind. + * + * visit(ast, { + * enter: { + * Kind(node) { + * // enter the "Kind" node + * } + * }, + * leave: { + * Kind(node) { + * // leave the "Kind" node + * } + * } + * }) + */ +export function visit( + root: ASTNode, + visitor: Visitor, + visitorKeys: VisitorKeyMap = QueryDocumentKeys, +): any { + /* eslint-disable no-undef-init */ + let stack: any = undefined; + let inArray = Array.isArray(root); + let keys: any = [root]; + let index = -1; + let edits = []; + let node: any = undefined; + let key: any = undefined; + let parent: any = undefined; + const path: any = []; + const ancestors = []; + let newRoot = root; + /* eslint-enable no-undef-init */ + + do { + index++; + const isLeaving = index === keys.length; + const isEdited = isLeaving && edits.length !== 0; + if (isLeaving) { + key = ancestors.length === 0 ? undefined : path[path.length - 1]; + node = parent; + parent = ancestors.pop(); + if (isEdited) { + if (inArray) { + node = node.slice(); + } else { + const clone = {}; + for (const k in node) { + if (node.hasOwnProperty(k)) { + clone[k] = node[k]; + } + } + node = clone; + } + let editOffset = 0; + for (let ii = 0; ii < edits.length; ii++) { + let editKey: any = edits[ii][0]; + const editValue = edits[ii][1]; + if (inArray) { + editKey -= editOffset; + } + if (inArray && editValue === null) { + node.splice(editKey, 1); + editOffset++; + } else { + node[editKey] = editValue; + } + } + } + index = stack.index; + keys = stack.keys; + edits = stack.edits; + inArray = stack.inArray; + stack = stack.prev; + } else { + key = parent ? (inArray ? index : keys[index]) : undefined; + node = parent ? parent[key] : newRoot; + if (node === null || node === undefined) { + continue; + } + if (parent) { + path.push(key); + } + } + + let result; + if (!Array.isArray(node)) { + if (!isNode(node)) { + throw new Error('Invalid AST Node: ' + JSON.stringify(node)); + } + const visitFn = getVisitFn(visitor, node.kind, isLeaving); + if (visitFn) { + result = visitFn.call(visitor, node, key, parent, path, ancestors); + + if (result === BREAK) { + break; + } + + if (result === false) { + if (!isLeaving) { + path.pop(); + continue; + } + } else if (result !== undefined) { + edits.push([key, result]); + if (!isLeaving) { + if (isNode(result)) { + node = result; + } else { + path.pop(); + continue; + } + } + } + } + } + + if (result === undefined && isEdited) { + edits.push([key, node]); + } + + if (isLeaving) { + path.pop(); + } else { + stack = { inArray, index, keys, edits, prev: stack }; + inArray = Array.isArray(node); + keys = inArray ? node : visitorKeys[node.kind] || []; + index = -1; + edits = []; + if (parent) { + ancestors.push(parent); + } + parent = node; + } + } while (stack !== undefined); + + if (edits.length !== 0) { + newRoot = edits[edits.length - 1][1]; + } + + return newRoot; +} + +function isNode(maybeNode): boolean %checks { + return Boolean(maybeNode && typeof maybeNode.kind === 'string'); +} + +/** + * Creates a new visitor instance which delegates to many visitors to run in + * parallel. Each visitor will be visited for each node before moving on. + * + * If a prior visitor edits a node, no following visitors will see that node. + */ +export function visitInParallel( + visitors: Array>, +): Visitor { + const skipping = new Array(visitors.length); + + return { + enter(node) { + for (let i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + const fn = getVisitFn(visitors[i], node.kind, /* isLeaving */ false); + if (fn) { + const result = fn.apply(visitors[i], arguments); + if (result === false) { + skipping[i] = node; + } else if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined) { + return result; + } + } + } + } + }, + leave(node) { + for (let i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + const fn = getVisitFn(visitors[i], node.kind, /* isLeaving */ true); + if (fn) { + const result = fn.apply(visitors[i], arguments); + if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined && result !== false) { + return result; + } + } + } else if (skipping[i] === node) { + skipping[i] = null; + } + } + }, + }; +} + +/** + * Creates a new visitor instance which maintains a provided TypeInfo instance + * along with visiting visitor. + */ +export function visitWithTypeInfo( + typeInfo: TypeInfo, + visitor: Visitor, +): Visitor { + return { + enter(node) { + typeInfo.enter(node); + const fn = getVisitFn(visitor, node.kind, /* isLeaving */ false); + if (fn) { + const result = fn.apply(visitor, arguments); + if (result !== undefined) { + typeInfo.leave(node); + if (isNode(result)) { + typeInfo.enter(result); + } + } + return result; + } + }, + leave(node) { + const fn = getVisitFn(visitor, node.kind, /* isLeaving */ true); + let result; + if (fn) { + result = fn.apply(visitor, arguments); + } + typeInfo.leave(node); + return result; + }, + }; +} + +/** + * Given a visitor instance, if it is leaving or not, and a node kind, return + * the function the visitor runtime should call. + */ +export function getVisitFn( + visitor: Visitor, + kind: string, + isLeaving: boolean, +): ?VisitFn { + const kindVisitor = visitor[kind]; + if (kindVisitor) { + if (!isLeaving && typeof kindVisitor === 'function') { + // { Kind() {} } + return kindVisitor; + } + const kindSpecificVisitor = isLeaving + ? kindVisitor.leave + : kindVisitor.enter; + if (typeof kindSpecificVisitor === 'function') { + // { Kind: { enter() {}, leave() {} } } + return kindSpecificVisitor; + } + } else { + const specificVisitor = isLeaving ? visitor.leave : visitor.enter; + if (specificVisitor) { + if (typeof specificVisitor === 'function') { + // { enter() {}, leave() {} } + return specificVisitor; + } + const specificKindVisitor = specificVisitor[kind]; + if (typeof specificKindVisitor === 'function') { + // { enter: { Kind() {} }, leave: { Kind() {} } } + return specificKindVisitor; + } + } + } +} diff --git a/dist/language/visitor.mjs b/dist/language/visitor.mjs new file mode 100644 index 0000000000..7eb843fb36 --- /dev/null +++ b/dist/language/visitor.mjs @@ -0,0 +1,444 @@ +/** + * 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. + * + * strict + */ + +/** + * A visitor is provided to visit, it contains the collection of + * relevant functions to be called during the visitor's traversal. + */ + +/** + * A visitor is comprised of visit functions, which are called on each node + * during the visitor's traversal. + */ + +/** + * A KeyMap describes each the traversable properties of each kind of node. + */ +export var QueryDocumentKeys = { + Name: [], + Document: ['definitions'], + OperationDefinition: ['name', 'variableDefinitions', 'directives', 'selectionSet'], + VariableDefinition: ['variable', 'type', 'defaultValue'], + Variable: ['name'], + SelectionSet: ['selections'], + Field: ['alias', 'name', 'arguments', 'directives', 'selectionSet'], + Argument: ['name', 'value'], + FragmentSpread: ['name', 'directives'], + InlineFragment: ['typeCondition', 'directives', 'selectionSet'], + FragmentDefinition: ['name', // Note: fragment variable definitions are experimental and may be changed + // or removed in the future. + 'variableDefinitions', 'typeCondition', 'directives', 'selectionSet'], + IntValue: [], + FloatValue: [], + StringValue: [], + BooleanValue: [], + NullValue: [], + EnumValue: [], + ListValue: ['values'], + ObjectValue: ['fields'], + ObjectField: ['name', 'value'], + Directive: ['name', 'arguments'], + NamedType: ['name'], + ListType: ['type'], + NonNullType: ['type'], + SchemaDefinition: ['directives', 'operationTypes'], + OperationTypeDefinition: ['type'], + ScalarTypeDefinition: ['description', 'name', 'directives'], + ObjectTypeDefinition: ['description', 'name', 'interfaces', 'directives', 'fields'], + FieldDefinition: ['description', 'name', 'arguments', 'type', 'directives'], + InputValueDefinition: ['description', 'name', 'type', 'defaultValue', 'directives'], + InterfaceTypeDefinition: ['description', 'name', 'directives', 'fields'], + UnionTypeDefinition: ['description', 'name', 'directives', 'types'], + EnumTypeDefinition: ['description', 'name', 'directives', 'values'], + EnumValueDefinition: ['description', 'name', 'directives'], + InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'], + DirectiveDefinition: ['description', 'name', 'arguments', 'locations'], + SchemaExtension: ['directives', 'operationTypes'], + ScalarTypeExtension: ['name', 'directives'], + ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'], + InterfaceTypeExtension: ['name', 'directives', 'fields'], + UnionTypeExtension: ['name', 'directives', 'types'], + EnumTypeExtension: ['name', 'directives', 'values'], + InputObjectTypeExtension: ['name', 'directives', 'fields'] +}; +export var BREAK = {}; +/** + * visit() will walk through an AST using a depth first traversal, calling + * the visitor's enter function at each node in the traversal, and calling the + * leave function after visiting that node and all of its child nodes. + * + * By returning different values from the enter and leave functions, the + * behavior of the visitor can be altered, including skipping over a sub-tree of + * the AST (by returning false), editing the AST by returning a value or null + * to remove the value, or to stop the whole traversal by returning BREAK. + * + * When using visit() to edit an AST, the original AST will not be modified, and + * a new version of the AST with the changes applied will be returned from the + * visit function. + * + * const editedAST = visit(ast, { + * enter(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: skip visiting this node + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * }, + * leave(node, key, parent, path, ancestors) { + * // @return + * // undefined: no action + * // false: no action + * // visitor.BREAK: stop visiting altogether + * // null: delete this node + * // any value: replace this node with the returned value + * } + * }); + * + * Alternatively to providing enter() and leave() functions, a visitor can + * instead provide functions named the same as the kinds of AST nodes, or + * enter/leave visitors at a named key, leading to four permutations of + * visitor API: + * + * 1) Named visitors triggered when entering a node a specific kind. + * + * visit(ast, { + * Kind(node) { + * // enter the "Kind" node + * } + * }) + * + * 2) Named visitors that trigger upon entering and leaving a node of + * a specific kind. + * + * visit(ast, { + * Kind: { + * enter(node) { + * // enter the "Kind" node + * } + * leave(node) { + * // leave the "Kind" node + * } + * } + * }) + * + * 3) Generic visitors that trigger upon entering and leaving any node. + * + * visit(ast, { + * enter(node) { + * // enter any node + * }, + * leave(node) { + * // leave any node + * } + * }) + * + * 4) Parallel visitors for entering and leaving nodes of a specific kind. + * + * visit(ast, { + * enter: { + * Kind(node) { + * // enter the "Kind" node + * } + * }, + * leave: { + * Kind(node) { + * // leave the "Kind" node + * } + * } + * }) + */ + +export function visit(root, visitor) { + var visitorKeys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : QueryDocumentKeys; + + /* eslint-disable no-undef-init */ + var stack = undefined; + var inArray = Array.isArray(root); + var keys = [root]; + var index = -1; + var edits = []; + var node = undefined; + var key = undefined; + var parent = undefined; + var path = []; + var ancestors = []; + var newRoot = root; + /* eslint-enable no-undef-init */ + + do { + index++; + var isLeaving = index === keys.length; + var isEdited = isLeaving && edits.length !== 0; + + if (isLeaving) { + key = ancestors.length === 0 ? undefined : path[path.length - 1]; + node = parent; + parent = ancestors.pop(); + + if (isEdited) { + if (inArray) { + node = node.slice(); + } else { + var clone = {}; + + for (var k in node) { + if (node.hasOwnProperty(k)) { + clone[k] = node[k]; + } + } + + node = clone; + } + + var editOffset = 0; + + for (var ii = 0; ii < edits.length; ii++) { + var editKey = edits[ii][0]; + var editValue = edits[ii][1]; + + if (inArray) { + editKey -= editOffset; + } + + if (inArray && editValue === null) { + node.splice(editKey, 1); + editOffset++; + } else { + node[editKey] = editValue; + } + } + } + + index = stack.index; + keys = stack.keys; + edits = stack.edits; + inArray = stack.inArray; + stack = stack.prev; + } else { + key = parent ? inArray ? index : keys[index] : undefined; + node = parent ? parent[key] : newRoot; + + if (node === null || node === undefined) { + continue; + } + + if (parent) { + path.push(key); + } + } + + var result = void 0; + + if (!Array.isArray(node)) { + if (!isNode(node)) { + throw new Error('Invalid AST Node: ' + JSON.stringify(node)); + } + + var visitFn = getVisitFn(visitor, node.kind, isLeaving); + + if (visitFn) { + result = visitFn.call(visitor, node, key, parent, path, ancestors); + + if (result === BREAK) { + break; + } + + if (result === false) { + if (!isLeaving) { + path.pop(); + continue; + } + } else if (result !== undefined) { + edits.push([key, result]); + + if (!isLeaving) { + if (isNode(result)) { + node = result; + } else { + path.pop(); + continue; + } + } + } + } + } + + if (result === undefined && isEdited) { + edits.push([key, node]); + } + + if (isLeaving) { + path.pop(); + } else { + stack = { + inArray: inArray, + index: index, + keys: keys, + edits: edits, + prev: stack + }; + inArray = Array.isArray(node); + keys = inArray ? node : visitorKeys[node.kind] || []; + index = -1; + edits = []; + + if (parent) { + ancestors.push(parent); + } + + parent = node; + } + } while (stack !== undefined); + + if (edits.length !== 0) { + newRoot = edits[edits.length - 1][1]; + } + + return newRoot; +} + +function isNode(maybeNode) { + return Boolean(maybeNode && typeof maybeNode.kind === 'string'); +} +/** + * Creates a new visitor instance which delegates to many visitors to run in + * parallel. Each visitor will be visited for each node before moving on. + * + * If a prior visitor edits a node, no following visitors will see that node. + */ + + +export function visitInParallel(visitors) { + var skipping = new Array(visitors.length); + return { + enter: function enter(node) { + for (var i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + var fn = getVisitFn(visitors[i], node.kind, + /* isLeaving */ + false); + + if (fn) { + var result = fn.apply(visitors[i], arguments); + + if (result === false) { + skipping[i] = node; + } else if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined) { + return result; + } + } + } + } + }, + leave: function leave(node) { + for (var i = 0; i < visitors.length; i++) { + if (!skipping[i]) { + var fn = getVisitFn(visitors[i], node.kind, + /* isLeaving */ + true); + + if (fn) { + var result = fn.apply(visitors[i], arguments); + + if (result === BREAK) { + skipping[i] = BREAK; + } else if (result !== undefined && result !== false) { + return result; + } + } + } else if (skipping[i] === node) { + skipping[i] = null; + } + } + } + }; +} +/** + * Creates a new visitor instance which maintains a provided TypeInfo instance + * along with visiting visitor. + */ + +export function visitWithTypeInfo(typeInfo, visitor) { + return { + enter: function enter(node) { + typeInfo.enter(node); + var fn = getVisitFn(visitor, node.kind, + /* isLeaving */ + false); + + if (fn) { + var result = fn.apply(visitor, arguments); + + if (result !== undefined) { + typeInfo.leave(node); + + if (isNode(result)) { + typeInfo.enter(result); + } + } + + return result; + } + }, + leave: function leave(node) { + var fn = getVisitFn(visitor, node.kind, + /* isLeaving */ + true); + var result; + + if (fn) { + result = fn.apply(visitor, arguments); + } + + typeInfo.leave(node); + return result; + } + }; +} +/** + * Given a visitor instance, if it is leaving or not, and a node kind, return + * the function the visitor runtime should call. + */ + +export function getVisitFn(visitor, kind, isLeaving) { + var kindVisitor = visitor[kind]; + + if (kindVisitor) { + if (!isLeaving && typeof kindVisitor === 'function') { + // { Kind() {} } + return kindVisitor; + } + + var kindSpecificVisitor = isLeaving ? kindVisitor.leave : kindVisitor.enter; + + if (typeof kindSpecificVisitor === 'function') { + // { Kind: { enter() {}, leave() {} } } + return kindSpecificVisitor; + } + } else { + var specificVisitor = isLeaving ? visitor.leave : visitor.enter; + + if (specificVisitor) { + if (typeof specificVisitor === 'function') { + // { enter() {}, leave() {} } + return specificVisitor; + } + + var specificKindVisitor = specificVisitor[kind]; + + if (typeof specificKindVisitor === 'function') { + // { enter: { Kind() {} }, leave: { Kind() {} } } + return specificKindVisitor; + } + } + } +} \ No newline at end of file diff --git a/dist/package.json b/dist/package.json new file mode 100644 index 0000000000..fe08dcb1ec --- /dev/null +++ b/dist/package.json @@ -0,0 +1,20 @@ +{ + "name": "graphql", + "version": "0.13.2", + "description": "A Query Language and Runtime which can target any service.", + "license": "MIT", + "main": "index", + "module": "index.mjs", + "sideEffects": false, + "homepage": "https://github.com/graphql/graphql-js", + "bugs": { + "url": "https://github.com/graphql/graphql-js/issues" + }, + "repository": { + "type": "git", + "url": "http://github.com/graphql/graphql-js.git" + }, + "dependencies": { + "iterall": "^1.2.2" + } +} \ No newline at end of file diff --git a/dist/subscription/asyncIteratorReject.js b/dist/subscription/asyncIteratorReject.js new file mode 100644 index 0000000000..8e06da39d6 --- /dev/null +++ b/dist/subscription/asyncIteratorReject.js @@ -0,0 +1,45 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = asyncIteratorReject; + +var _iterall = require("iterall"); + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Given an error, returns an AsyncIterable which will fail with that error. + * + * Similar to Promise.reject(error) + */ +function asyncIteratorReject(error) { + var isComplete = false; + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + + return _defineProperty({ + next: function next() { + var result = isComplete ? Promise.resolve({ + value: undefined, + done: true + }) : Promise.reject(error); + isComplete = true; + return result; + }, + return: function _return() { + isComplete = true; + return Promise.resolve({ + value: undefined, + done: true + }); + }, + throw: function _throw() { + isComplete = true; + return Promise.reject(error); + } + }, _iterall.$$asyncIterator, function () { + return this; + }); +} \ No newline at end of file diff --git a/dist/subscription/asyncIteratorReject.js.flow b/dist/subscription/asyncIteratorReject.js.flow new file mode 100644 index 0000000000..7992dc6eb1 --- /dev/null +++ b/dist/subscription/asyncIteratorReject.js.flow @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +import { $$asyncIterator } from 'iterall'; + +/** + * Given an error, returns an AsyncIterable which will fail with that error. + * + * Similar to Promise.reject(error) + */ +export default function asyncIteratorReject(error: Error): AsyncIterator { + let isComplete = false; + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + return ({ + next() { + const result = isComplete + ? Promise.resolve({ value: undefined, done: true }) + : Promise.reject(error); + isComplete = true; + return result; + }, + return() { + isComplete = true; + return Promise.resolve({ value: undefined, done: true }); + }, + throw() { + isComplete = true; + return Promise.reject(error); + }, + [$$asyncIterator]() { + return this; + }, + }: any); +} diff --git a/dist/subscription/asyncIteratorReject.mjs b/dist/subscription/asyncIteratorReject.mjs new file mode 100644 index 0000000000..e2612cf7cf --- /dev/null +++ b/dist/subscription/asyncIteratorReject.mjs @@ -0,0 +1,46 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Copyright (c) 2017-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. + * + * strict + */ +import { $$asyncIterator } from 'iterall'; +/** + * Given an error, returns an AsyncIterable which will fail with that error. + * + * Similar to Promise.reject(error) + */ + +export default function asyncIteratorReject(error) { + var isComplete = false; + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + + return _defineProperty({ + next: function next() { + var result = isComplete ? Promise.resolve({ + value: undefined, + done: true + }) : Promise.reject(error); + isComplete = true; + return result; + }, + return: function _return() { + isComplete = true; + return Promise.resolve({ + value: undefined, + done: true + }); + }, + throw: function _throw() { + isComplete = true; + return Promise.reject(error); + } + }, $$asyncIterator, function () { + return this; + }); +} \ No newline at end of file diff --git a/dist/subscription/index.js b/dist/subscription/index.js new file mode 100644 index 0000000000..0b45f008f0 --- /dev/null +++ b/dist/subscription/index.js @@ -0,0 +1,19 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "subscribe", { + enumerable: true, + get: function get() { + return _subscribe.subscribe; + } +}); +Object.defineProperty(exports, "createSourceEventStream", { + enumerable: true, + get: function get() { + return _subscribe.createSourceEventStream; + } +}); + +var _subscribe = require("./subscribe"); \ No newline at end of file diff --git a/dist/subscription/index.js.flow b/dist/subscription/index.js.flow new file mode 100644 index 0000000000..18831657cf --- /dev/null +++ b/dist/subscription/index.js.flow @@ -0,0 +1,10 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +export { subscribe, createSourceEventStream } from './subscribe'; diff --git a/dist/subscription/index.mjs b/dist/subscription/index.mjs new file mode 100644 index 0000000000..a0e3646712 --- /dev/null +++ b/dist/subscription/index.mjs @@ -0,0 +1,9 @@ +/** + * Copyright (c) 2017-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. + * + * strict + */ +export { subscribe, createSourceEventStream } from './subscribe'; \ No newline at end of file diff --git a/dist/subscription/mapAsyncIterator.js b/dist/subscription/mapAsyncIterator.js new file mode 100644 index 0000000000..eec4f902f6 --- /dev/null +++ b/dist/subscription/mapAsyncIterator.js @@ -0,0 +1,85 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = mapAsyncIterator; + +var _iterall = require("iterall"); + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Given an AsyncIterable and a callback function, return an AsyncIterator + * which produces values mapped via calling the callback function. + */ +function mapAsyncIterator(iterable, callback, rejectCallback) { + var iterator = (0, _iterall.getAsyncIterator)(iterable); + var $return; + var abruptClose; // $FlowFixMe(>=0.68.0) + + if (typeof iterator.return === 'function') { + $return = iterator.return; + + abruptClose = function abruptClose(error) { + var rethrow = function rethrow() { + return Promise.reject(error); + }; + + return $return.call(iterator).then(rethrow, rethrow); + }; + } + + function mapResult(result) { + return result.done ? result : asyncMapValue(result.value, callback).then(iteratorResult, abruptClose); + } + + var mapReject; + + if (rejectCallback) { + // Capture rejectCallback to ensure it cannot be null. + var reject = rejectCallback; + + mapReject = function mapReject(error) { + return asyncMapValue(error, reject).then(iteratorResult, abruptClose); + }; + } + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + + + return _defineProperty({ + next: function next() { + return iterator.next().then(mapResult, mapReject); + }, + return: function _return() { + return $return ? $return.call(iterator).then(mapResult, mapReject) : Promise.resolve({ + value: undefined, + done: true + }); + }, + throw: function _throw(error) { + // $FlowFixMe(>=0.68.0) + if (typeof iterator.throw === 'function') { + return iterator.throw(error).then(mapResult, mapReject); + } + + return Promise.reject(error).catch(abruptClose); + } + }, _iterall.$$asyncIterator, function () { + return this; + }); +} + +function asyncMapValue(value, callback) { + return new Promise(function (resolve) { + return resolve(callback(value)); + }); +} + +function iteratorResult(value) { + return { + value: value, + done: false + }; +} \ No newline at end of file diff --git a/dist/subscription/mapAsyncIterator.js.flow b/dist/subscription/mapAsyncIterator.js.flow new file mode 100644 index 0000000000..c7a7e23ce2 --- /dev/null +++ b/dist/subscription/mapAsyncIterator.js.flow @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +import { $$asyncIterator, getAsyncIterator } from 'iterall'; +import type { MaybePromise } from '../jsutils/MaybePromise'; + +/** + * Given an AsyncIterable and a callback function, return an AsyncIterator + * which produces values mapped via calling the callback function. + */ +export default function mapAsyncIterator( + iterable: AsyncIterable, + callback: T => MaybePromise, + rejectCallback?: any => MaybePromise, +): AsyncGenerator { + const iterator = getAsyncIterator(iterable); + let $return; + let abruptClose; + // $FlowFixMe(>=0.68.0) + if (typeof iterator.return === 'function') { + $return = iterator.return; + abruptClose = error => { + const rethrow = () => Promise.reject(error); + return $return.call(iterator).then(rethrow, rethrow); + }; + } + + function mapResult(result) { + return result.done + ? result + : asyncMapValue(result.value, callback).then(iteratorResult, abruptClose); + } + + let mapReject; + if (rejectCallback) { + // Capture rejectCallback to ensure it cannot be null. + const reject = rejectCallback; + mapReject = error => + asyncMapValue(error, reject).then(iteratorResult, abruptClose); + } + + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + return ({ + next() { + return iterator.next().then(mapResult, mapReject); + }, + return() { + return $return + ? $return.call(iterator).then(mapResult, mapReject) + : Promise.resolve({ value: undefined, done: true }); + }, + throw(error) { + // $FlowFixMe(>=0.68.0) + if (typeof iterator.throw === 'function') { + return iterator.throw(error).then(mapResult, mapReject); + } + return Promise.reject(error).catch(abruptClose); + }, + [$$asyncIterator]() { + return this; + }, + }: any); +} + +function asyncMapValue( + value: T, + callback: T => MaybePromise, +): Promise { + return new Promise(resolve => resolve(callback(value))); +} + +function iteratorResult(value: T): IteratorResult { + return { value, done: false }; +} diff --git a/dist/subscription/mapAsyncIterator.mjs b/dist/subscription/mapAsyncIterator.mjs new file mode 100644 index 0000000000..fe20bb1d05 --- /dev/null +++ b/dist/subscription/mapAsyncIterator.mjs @@ -0,0 +1,86 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Copyright (c) 2017-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. + * + * strict + */ +import { $$asyncIterator, getAsyncIterator } from 'iterall'; + +/** + * Given an AsyncIterable and a callback function, return an AsyncIterator + * which produces values mapped via calling the callback function. + */ +export default function mapAsyncIterator(iterable, callback, rejectCallback) { + var iterator = getAsyncIterator(iterable); + var $return; + var abruptClose; // $FlowFixMe(>=0.68.0) + + if (typeof iterator.return === 'function') { + $return = iterator.return; + + abruptClose = function abruptClose(error) { + var rethrow = function rethrow() { + return Promise.reject(error); + }; + + return $return.call(iterator).then(rethrow, rethrow); + }; + } + + function mapResult(result) { + return result.done ? result : asyncMapValue(result.value, callback).then(iteratorResult, abruptClose); + } + + var mapReject; + + if (rejectCallback) { + // Capture rejectCallback to ensure it cannot be null. + var reject = rejectCallback; + + mapReject = function mapReject(error) { + return asyncMapValue(error, reject).then(iteratorResult, abruptClose); + }; + } + /* TODO: Flow doesn't support symbols as keys: + https://github.com/facebook/flow/issues/3258 */ + + + return _defineProperty({ + next: function next() { + return iterator.next().then(mapResult, mapReject); + }, + return: function _return() { + return $return ? $return.call(iterator).then(mapResult, mapReject) : Promise.resolve({ + value: undefined, + done: true + }); + }, + throw: function _throw(error) { + // $FlowFixMe(>=0.68.0) + if (typeof iterator.throw === 'function') { + return iterator.throw(error).then(mapResult, mapReject); + } + + return Promise.reject(error).catch(abruptClose); + } + }, $$asyncIterator, function () { + return this; + }); +} + +function asyncMapValue(value, callback) { + return new Promise(function (resolve) { + return resolve(callback(value)); + }); +} + +function iteratorResult(value) { + return { + value: value, + done: false + }; +} \ No newline at end of file diff --git a/dist/subscription/subscribe.js b/dist/subscription/subscribe.js new file mode 100644 index 0000000000..71b581512c --- /dev/null +++ b/dist/subscription/subscribe.js @@ -0,0 +1,151 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.subscribe = subscribe; +exports.createSourceEventStream = createSourceEventStream; + +var _iterall = require("iterall"); + +var _GraphQLError = require("../error/GraphQLError"); + +var _locatedError = require("../error/locatedError"); + +var _execute = require("../execution/execute"); + +var _schema = require("../type/schema"); + +var _mapAsyncIterator = _interopRequireDefault(require("./mapAsyncIterator")); + +var _getOperationRootType = require("../utilities/getOperationRootType"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Copyright (c) 2017-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. + * + * strict + */ +function subscribe(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 ? subscribeImpl(argsOrSchema.schema, argsOrSchema.document, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver, argsOrSchema.subscribeFieldResolver) : subscribeImpl(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver); +} +/** + * This function checks if the error is a GraphQLError. If it is, report it as + * an ExecutionResult, containing only errors and no data. Otherwise treat the + * error as a system-class error and re-throw it. + */ + + +function reportGraphQLError(error) { + if (error instanceof _GraphQLError.GraphQLError) { + return { + errors: [error] + }; + } + + throw error; +} + +function subscribeImpl(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) { + var sourcePromise = createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver); // For each payload yielded from a subscription, map it over the normal + // GraphQL `execute` function, with `payload` as the rootValue. + // This implements the "MapSourceToResponseEvent" algorithm described in + // the GraphQL specification. The `execute` function provides the + // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the + // "ExecuteQuery" algorithm, for which `execute` is also used. + + var mapSourceToResponse = function mapSourceToResponse(payload) { + return (0, _execute.execute)(schema, document, payload, contextValue, variableValues, operationName, fieldResolver); + }; // Resolve the Source Stream, then map every source value to a + // ExecutionResult value as described above. + + + return sourcePromise.then(function (resultOrStream) { + return (// Note: Flow can't refine isAsyncIterable, so explicit casts are used. + (0, _iterall.isAsyncIterable)(resultOrStream) ? (0, _mapAsyncIterator.default)(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream + ); + }, reportGraphQLError); +} +/** + * Implements the "CreateSourceEventStream" algorithm described in the + * GraphQL specification, resolving the subscription source event stream. + * + * Returns a Promise. + * + * If the client-provided invalid arguments, the source stream could not be + * created, or the resolver did not return an AsyncIterable, this function will + * will throw an error, which should be caught and handled by the caller. + * + * A Source Event Stream represents a sequence of events, each of which triggers + * a GraphQL execution for that event. + * + * This may be useful when hosting the stateful subscription service in a + * different process or machine than the stateless GraphQL execution engine, + * or otherwise separating these two steps. For more on this, see the + * "Supporting Subscriptions at Scale" information in the GraphQL specification. + */ + + +function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // If arguments are missing or incorrectly typed, this is an internal + // developer mistake which should throw an early error. + (0, _execute.assertValidExecutionArguments)(schema, document, variableValues); + + try { + // If a valid context cannot be created due to incorrect arguments, + // this will throw an error. + var exeContext = (0, _execute.buildExecutionContext)(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); // Return early errors if execution context failed. + + if (Array.isArray(exeContext)) { + return Promise.resolve({ + errors: exeContext + }); + } + + var type = (0, _getOperationRootType.getOperationRootType)(schema, exeContext.operation); + var fields = (0, _execute.collectFields)(exeContext, type, exeContext.operation.selectionSet, Object.create(null), Object.create(null)); + var responseNames = Object.keys(fields); + var responseName = responseNames[0]; + var fieldNodes = fields[responseName]; + var fieldNode = fieldNodes[0]; + var fieldName = fieldNode.name.value; + var fieldDef = (0, _execute.getFieldDef)(schema, type, fieldName); + + if (!fieldDef) { + throw new _GraphQLError.GraphQLError("The subscription field \"".concat(fieldName, "\" is not defined."), fieldNodes); + } // Call the `subscribe()` resolver or the default resolver to produce an + // AsyncIterable yielding raw payloads. + + + var resolveFn = fieldDef.subscribe || exeContext.fieldResolver; + var path = (0, _execute.addPath)(undefined, responseName); + var info = (0, _execute.buildResolveInfo)(exeContext, fieldDef, fieldNodes, type, path); // resolveFieldValueOrError implements the "ResolveFieldEventStream" + // algorithm from GraphQL specification. It differs from + // "ResolveFieldValue" due to providing a different `resolveFn`. + + var result = (0, _execute.resolveFieldValueOrError)(exeContext, fieldDef, fieldNodes, resolveFn, rootValue, info); // Coerce to Promise for easier error handling and consistent return type. + + return Promise.resolve(result).then(function (eventStream) { + // If eventStream is an Error, rethrow a located error. + if (eventStream instanceof Error) { + throw (0, _locatedError.locatedError)(eventStream, fieldNodes, (0, _execute.responsePathAsArray)(path)); + } // Assert field returned an event stream, otherwise yield an error. + + + if ((0, _iterall.isAsyncIterable)(eventStream)) { + // Note: isAsyncIterable above ensures this will be correct. + return eventStream; + } + + throw new Error('Subscription field must return Async Iterable. Received: ' + String(eventStream)); + }); + } catch (error) { + return Promise.reject(error); + } +} \ No newline at end of file diff --git a/dist/subscription/subscribe.js.flow b/dist/subscription/subscribe.js.flow new file mode 100644 index 0000000000..2c8eb4f65f --- /dev/null +++ b/dist/subscription/subscribe.js.flow @@ -0,0 +1,288 @@ +/** + * Copyright (c) 2017-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. + * + * @flow strict + */ + +import { isAsyncIterable } from 'iterall'; +import { GraphQLError } from '../error/GraphQLError'; +import { locatedError } from '../error/locatedError'; +import { + addPath, + assertValidExecutionArguments, + buildExecutionContext, + buildResolveInfo, + collectFields, + execute, + getFieldDef, + resolveFieldValueOrError, + responsePathAsArray, +} from '../execution/execute'; +import { GraphQLSchema } from '../type/schema'; +import mapAsyncIterator from './mapAsyncIterator'; + +import type { ObjMap } from '../jsutils/ObjMap'; +import type { ExecutionResult } from '../execution/execute'; +import type { DocumentNode } from '../language/ast'; +import type { GraphQLFieldResolver } from '../type/definition'; +import { getOperationRootType } from '../utilities/getOperationRootType'; + +/** + * Implements the "Subscribe" algorithm described in the GraphQL specification. + * + * Returns a Promise which resolves to either an AsyncIterator (if successful) + * or an ExecutionResult (client error). The promise will be rejected if a + * server error occurs. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to an AsyncIterator, which + * yields a stream of ExecutionResults representing the response stream. + * + * Accepts either an object with named arguments, or individual arguments. + */ +declare function subscribe( + {| + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, + subscribeFieldResolver?: ?GraphQLFieldResolver, + |}, + ..._: [] +): Promise | ExecutionResult>; +/* eslint-disable no-redeclare */ +declare function subscribe( + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ?ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, + subscribeFieldResolver?: ?GraphQLFieldResolver, +): Promise | ExecutionResult>; +export function subscribe( + argsOrSchema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + subscribeFieldResolver, +) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 + ? subscribeImpl( + argsOrSchema.schema, + argsOrSchema.document, + argsOrSchema.rootValue, + argsOrSchema.contextValue, + argsOrSchema.variableValues, + argsOrSchema.operationName, + argsOrSchema.fieldResolver, + argsOrSchema.subscribeFieldResolver, + ) + : subscribeImpl( + argsOrSchema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + subscribeFieldResolver, + ); +} + +/** + * This function checks if the error is a GraphQLError. If it is, report it as + * an ExecutionResult, containing only errors and no data. Otherwise treat the + * error as a system-class error and re-throw it. + */ +function reportGraphQLError(error) { + if (error instanceof GraphQLError) { + return { errors: [error] }; + } + throw error; +} + +function subscribeImpl( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + subscribeFieldResolver, +) { + const sourcePromise = createSourceEventStream( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + subscribeFieldResolver, + ); + + // For each payload yielded from a subscription, map it over the normal + // GraphQL `execute` function, with `payload` as the rootValue. + // This implements the "MapSourceToResponseEvent" algorithm described in + // the GraphQL specification. The `execute` function provides the + // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the + // "ExecuteQuery" algorithm, for which `execute` is also used. + const mapSourceToResponse = payload => + execute( + schema, + document, + payload, + contextValue, + variableValues, + operationName, + fieldResolver, + ); + + // Resolve the Source Stream, then map every source value to a + // ExecutionResult value as described above. + return sourcePromise.then( + resultOrStream => + // Note: Flow can't refine isAsyncIterable, so explicit casts are used. + isAsyncIterable(resultOrStream) + ? mapAsyncIterator( + ((resultOrStream: any): AsyncIterable), + mapSourceToResponse, + reportGraphQLError, + ) + : ((resultOrStream: any): ExecutionResult), + reportGraphQLError, + ); +} + +/** + * Implements the "CreateSourceEventStream" algorithm described in the + * GraphQL specification, resolving the subscription source event stream. + * + * Returns a Promise. + * + * If the client-provided invalid arguments, the source stream could not be + * created, or the resolver did not return an AsyncIterable, this function will + * will throw an error, which should be caught and handled by the caller. + * + * A Source Event Stream represents a sequence of events, each of which triggers + * a GraphQL execution for that event. + * + * This may be useful when hosting the stateful subscription service in a + * different process or machine than the stateless GraphQL execution engine, + * or otherwise separating these two steps. For more on this, see the + * "Supporting Subscriptions at Scale" information in the GraphQL specification. + */ +export function createSourceEventStream( + schema: GraphQLSchema, + document: DocumentNode, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: ObjMap, + operationName?: ?string, + fieldResolver?: ?GraphQLFieldResolver, +): Promise | ExecutionResult> { + // If arguments are missing or incorrectly typed, this is an internal + // developer mistake which should throw an early error. + assertValidExecutionArguments(schema, document, variableValues); + + try { + // If a valid context cannot be created due to incorrect arguments, + // this will throw an error. + const exeContext = buildExecutionContext( + schema, + document, + rootValue, + contextValue, + variableValues, + operationName, + fieldResolver, + ); + + // Return early errors if execution context failed. + if (Array.isArray(exeContext)) { + return Promise.resolve({ errors: exeContext }); + } + + const type = getOperationRootType(schema, exeContext.operation); + const fields = collectFields( + exeContext, + type, + exeContext.operation.selectionSet, + Object.create(null), + Object.create(null), + ); + const responseNames = Object.keys(fields); + const responseName = responseNames[0]; + const fieldNodes = fields[responseName]; + const fieldNode = fieldNodes[0]; + const fieldName = fieldNode.name.value; + const fieldDef = getFieldDef(schema, type, fieldName); + + if (!fieldDef) { + throw new GraphQLError( + `The subscription field "${fieldName}" is not defined.`, + fieldNodes, + ); + } + + // Call the `subscribe()` resolver or the default resolver to produce an + // AsyncIterable yielding raw payloads. + const resolveFn = fieldDef.subscribe || exeContext.fieldResolver; + + const path = addPath(undefined, responseName); + + const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path); + + // resolveFieldValueOrError implements the "ResolveFieldEventStream" + // algorithm from GraphQL specification. It differs from + // "ResolveFieldValue" due to providing a different `resolveFn`. + const result = resolveFieldValueOrError( + exeContext, + fieldDef, + fieldNodes, + resolveFn, + rootValue, + info, + ); + + // Coerce to Promise for easier error handling and consistent return type. + return Promise.resolve(result).then(eventStream => { + // If eventStream is an Error, rethrow a located error. + if (eventStream instanceof Error) { + throw locatedError(eventStream, fieldNodes, responsePathAsArray(path)); + } + + // Assert field returned an event stream, otherwise yield an error. + if (isAsyncIterable(eventStream)) { + // Note: isAsyncIterable above ensures this will be correct. + return ((eventStream: any): AsyncIterable); + } + throw new Error( + 'Subscription field must return Async Iterable. Received: ' + + String(eventStream), + ); + }); + } catch (error) { + return Promise.reject(error); + } +} diff --git a/dist/subscription/subscribe.mjs b/dist/subscription/subscribe.mjs new file mode 100644 index 0000000000..2cbe13f2c8 --- /dev/null +++ b/dist/subscription/subscribe.mjs @@ -0,0 +1,154 @@ +/** + * Copyright (c) 2017-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. + * + * strict + */ +import { isAsyncIterable } from 'iterall'; +import { GraphQLError } from '../error/GraphQLError'; +import { locatedError } from '../error/locatedError'; +import { addPath, assertValidExecutionArguments, buildExecutionContext, buildResolveInfo, collectFields, execute, getFieldDef, resolveFieldValueOrError, responsePathAsArray } from '../execution/execute'; +import { GraphQLSchema } from '../type/schema'; +import mapAsyncIterator from './mapAsyncIterator'; +import { getOperationRootType } from '../utilities/getOperationRootType'; +/** + * Implements the "Subscribe" algorithm described in the GraphQL specification. + * + * Returns a Promise which resolves to either an AsyncIterator (if successful) + * or an ExecutionResult (client error). The promise will be rejected if a + * server error occurs. + * + * If the client-provided arguments to this function do not result in a + * compliant subscription, a GraphQL Response (ExecutionResult) with + * descriptive errors and no data will be returned. + * + * If the the source stream could not be created due to faulty subscription + * resolver logic or underlying systems, the promise will resolve to a single + * ExecutionResult containing `errors` and no `data`. + * + * If the operation succeeded, the promise resolves to an AsyncIterator, which + * yields a stream of ExecutionResults representing the response stream. + * + * Accepts either an object with named arguments, or individual arguments. + */ + +export function subscribe(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) { + /* eslint-enable no-redeclare */ + // Extract arguments from object args if provided. + return arguments.length === 1 ? subscribeImpl(argsOrSchema.schema, argsOrSchema.document, argsOrSchema.rootValue, argsOrSchema.contextValue, argsOrSchema.variableValues, argsOrSchema.operationName, argsOrSchema.fieldResolver, argsOrSchema.subscribeFieldResolver) : subscribeImpl(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver); +} +/** + * This function checks if the error is a GraphQLError. If it is, report it as + * an ExecutionResult, containing only errors and no data. Otherwise treat the + * error as a system-class error and re-throw it. + */ + +function reportGraphQLError(error) { + if (error instanceof GraphQLError) { + return { + errors: [error] + }; + } + + throw error; +} + +function subscribeImpl(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver) { + var sourcePromise = createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver); // For each payload yielded from a subscription, map it over the normal + // GraphQL `execute` function, with `payload` as the rootValue. + // This implements the "MapSourceToResponseEvent" algorithm described in + // the GraphQL specification. The `execute` function provides the + // "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the + // "ExecuteQuery" algorithm, for which `execute` is also used. + + var mapSourceToResponse = function mapSourceToResponse(payload) { + return execute(schema, document, payload, contextValue, variableValues, operationName, fieldResolver); + }; // Resolve the Source Stream, then map every source value to a + // ExecutionResult value as described above. + + + return sourcePromise.then(function (resultOrStream) { + return (// Note: Flow can't refine isAsyncIterable, so explicit casts are used. + isAsyncIterable(resultOrStream) ? mapAsyncIterator(resultOrStream, mapSourceToResponse, reportGraphQLError) : resultOrStream + ); + }, reportGraphQLError); +} +/** + * Implements the "CreateSourceEventStream" algorithm described in the + * GraphQL specification, resolving the subscription source event stream. + * + * Returns a Promise. + * + * If the client-provided invalid arguments, the source stream could not be + * created, or the resolver did not return an AsyncIterable, this function will + * will throw an error, which should be caught and handled by the caller. + * + * A Source Event Stream represents a sequence of events, each of which triggers + * a GraphQL execution for that event. + * + * This may be useful when hosting the stateful subscription service in a + * different process or machine than the stateless GraphQL execution engine, + * or otherwise separating these two steps. For more on this, see the + * "Supporting Subscriptions at Scale" information in the GraphQL specification. + */ + + +export function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver) { + // If arguments are missing or incorrectly typed, this is an internal + // developer mistake which should throw an early error. + assertValidExecutionArguments(schema, document, variableValues); + + try { + // If a valid context cannot be created due to incorrect arguments, + // this will throw an error. + var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver); // Return early errors if execution context failed. + + if (Array.isArray(exeContext)) { + return Promise.resolve({ + errors: exeContext + }); + } + + var type = getOperationRootType(schema, exeContext.operation); + var fields = collectFields(exeContext, type, exeContext.operation.selectionSet, Object.create(null), Object.create(null)); + var responseNames = Object.keys(fields); + var responseName = responseNames[0]; + var fieldNodes = fields[responseName]; + var fieldNode = fieldNodes[0]; + var fieldName = fieldNode.name.value; + var fieldDef = getFieldDef(schema, type, fieldName); + + if (!fieldDef) { + throw new GraphQLError("The subscription field \"".concat(fieldName, "\" is not defined."), fieldNodes); + } // Call the `subscribe()` resolver or the default resolver to produce an + // AsyncIterable yielding raw payloads. + + + var resolveFn = fieldDef.subscribe || exeContext.fieldResolver; + var path = addPath(undefined, responseName); + var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, type, path); // resolveFieldValueOrError implements the "ResolveFieldEventStream" + // algorithm from GraphQL specification. It differs from + // "ResolveFieldValue" due to providing a different `resolveFn`. + + var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, rootValue, info); // Coerce to Promise for easier error handling and consistent return type. + + return Promise.resolve(result).then(function (eventStream) { + // If eventStream is an Error, rethrow a located error. + if (eventStream instanceof Error) { + throw locatedError(eventStream, fieldNodes, responsePathAsArray(path)); + } // Assert field returned an event stream, otherwise yield an error. + + + if (isAsyncIterable(eventStream)) { + // Note: isAsyncIterable above ensures this will be correct. + return eventStream; + } + + throw new Error('Subscription field must return Async Iterable. Received: ' + String(eventStream)); + }); + } catch (error) { + return Promise.reject(error); + } +} \ No newline at end of file diff --git a/dist/type/definition.js b/dist/type/definition.js new file mode 100644 index 0000000000..bff49bd9fc --- /dev/null +++ b/dist/type/definition.js @@ -0,0 +1,964 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isType = isType; +exports.assertType = assertType; +exports.isScalarType = isScalarType; +exports.assertScalarType = assertScalarType; +exports.isObjectType = isObjectType; +exports.assertObjectType = assertObjectType; +exports.isInterfaceType = isInterfaceType; +exports.assertInterfaceType = assertInterfaceType; +exports.isUnionType = isUnionType; +exports.assertUnionType = assertUnionType; +exports.isEnumType = isEnumType; +exports.assertEnumType = assertEnumType; +exports.isInputObjectType = isInputObjectType; +exports.assertInputObjectType = assertInputObjectType; +exports.isListType = isListType; +exports.assertListType = assertListType; +exports.isNonNullType = isNonNullType; +exports.assertNonNullType = assertNonNullType; +exports.isInputType = isInputType; +exports.assertInputType = assertInputType; +exports.isOutputType = isOutputType; +exports.assertOutputType = assertOutputType; +exports.isLeafType = isLeafType; +exports.assertLeafType = assertLeafType; +exports.isCompositeType = isCompositeType; +exports.assertCompositeType = assertCompositeType; +exports.isAbstractType = isAbstractType; +exports.assertAbstractType = assertAbstractType; +exports.GraphQLList = GraphQLList; +exports.GraphQLNonNull = GraphQLNonNull; +exports.isWrappingType = isWrappingType; +exports.assertWrappingType = assertWrappingType; +exports.isNullableType = isNullableType; +exports.assertNullableType = assertNullableType; +exports.getNullableType = getNullableType; +exports.isNamedType = isNamedType; +exports.assertNamedType = assertNamedType; +exports.getNamedType = getNamedType; +exports.GraphQLInputObjectType = exports.GraphQLEnumType = exports.GraphQLUnionType = exports.GraphQLInterfaceType = exports.GraphQLObjectType = exports.GraphQLScalarType = void 0; + +var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf")); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _kinds = require("../language/kinds"); + +var _valueFromASTUntyped = require("../utilities/valueFromASTUntyped"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function isType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type) || isListType(type) || isNonNullType(type); +} + +function assertType(type) { + !isType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL type.")) : void 0; + return type; +} +/** + * There are predicates for each kind of GraphQL type. + */ + + +// eslint-disable-next-line no-redeclare +function isScalarType(type) { + return (0, _instanceOf.default)(type, GraphQLScalarType); +} + +function assertScalarType(type) { + !isScalarType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Scalar type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isObjectType(type) { + return (0, _instanceOf.default)(type, GraphQLObjectType); +} + +function assertObjectType(type) { + !isObjectType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Object type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isInterfaceType(type) { + return (0, _instanceOf.default)(type, GraphQLInterfaceType); +} + +function assertInterfaceType(type) { + !isInterfaceType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Interface type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isUnionType(type) { + return (0, _instanceOf.default)(type, GraphQLUnionType); +} + +function assertUnionType(type) { + !isUnionType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Union type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isEnumType(type) { + return (0, _instanceOf.default)(type, GraphQLEnumType); +} + +function assertEnumType(type) { + !isEnumType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Enum type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isInputObjectType(type) { + return (0, _instanceOf.default)(type, GraphQLInputObjectType); +} + +function assertInputObjectType(type) { + !isInputObjectType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Input Object type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isListType(type) { + return (0, _instanceOf.default)(type, GraphQLList); +} + +function assertListType(type) { + !isListType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL List type.")) : void 0; + return type; +} + +// eslint-disable-next-line no-redeclare +function isNonNullType(type) { + return (0, _instanceOf.default)(type, GraphQLNonNull); +} + +function assertNonNullType(type) { + !isNonNullType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL Non-Null type.")) : void 0; + return type; +} +/** + * These types may be used as input types for arguments and directives. + */ + + +function isInputType(type) { + return isScalarType(type) || isEnumType(type) || isInputObjectType(type) || isWrappingType(type) && isInputType(type.ofType); +} + +function assertInputType(type) { + !isInputType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL input type.")) : void 0; + return type; +} +/** + * These types may be used as output types as the result of fields. + */ + + +function isOutputType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isWrappingType(type) && isOutputType(type.ofType); +} + +function assertOutputType(type) { + !isOutputType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL output type.")) : void 0; + return type; +} +/** + * These types may describe types which may be leaf values. + */ + + +function isLeafType(type) { + return isScalarType(type) || isEnumType(type); +} + +function assertLeafType(type) { + !isLeafType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL leaf type.")) : void 0; + return type; +} +/** + * These types may describe the parent context of a selection set. + */ + + +function isCompositeType(type) { + return isObjectType(type) || isInterfaceType(type) || isUnionType(type); +} + +function assertCompositeType(type) { + !isCompositeType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL composite type.")) : void 0; + return type; +} +/** + * These types may describe the parent context of a selection set. + */ + + +function isAbstractType(type) { + return isInterfaceType(type) || isUnionType(type); +} + +function assertAbstractType(type) { + !isAbstractType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL abstract type.")) : void 0; + return type; +} +/** + * List Type Wrapper + * + * A list is a wrapping type which points to another type. + * Lists are often created within the context of defining the fields of + * an object type. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * parents: { type: GraphQLList(PersonType) }, + * children: { type: GraphQLList(PersonType) }, + * }) + * }) + * + */ + + +// eslint-disable-next-line no-redeclare +function GraphQLList(ofType) { + if (this instanceof GraphQLList) { + this.ofType = assertType(ofType); + } else { + return new GraphQLList(ofType); + } +} // Also provide toJSON and inspect aliases for toString. + + +var listProto = GraphQLList.prototype; + +listProto.toString = listProto.toJSON = listProto.inspect = function toString() { + return '[' + String(this.ofType) + ']'; +}; +/** + * Non-Null Type Wrapper + * + * A non-null is a wrapping type which points to another type. + * Non-null types enforce that their values are never null and can ensure + * an error is raised if this ever occurs during a request. It is useful for + * fields which you can make a strong guarantee on non-nullability, for example + * usually the id field of a database row will never be null. + * + * Example: + * + * const RowType = new GraphQLObjectType({ + * name: 'Row', + * fields: () => ({ + * id: { type: GraphQLNonNull(GraphQLString) }, + * }) + * }) + * + * Note: the enforcement of non-nullability occurs within the executor. + */ + + +// eslint-disable-next-line no-redeclare +function GraphQLNonNull(ofType) { + if (this instanceof GraphQLNonNull) { + this.ofType = assertNullableType(ofType); + } else { + return new GraphQLNonNull(ofType); + } +} // Also provide toJSON and inspect aliases for toString. + + +var nonNullProto = GraphQLNonNull.prototype; + +nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() { + return String(this.ofType) + '!'; +}; +/** + * These types wrap and modify other types + */ + + +function isWrappingType(type) { + return isListType(type) || isNonNullType(type); +} + +function assertWrappingType(type) { + !isWrappingType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL wrapping type.")) : void 0; + return type; +} +/** + * These types can all accept null as a value. + */ + + +function isNullableType(type) { + return isType(type) && !isNonNullType(type); +} + +function assertNullableType(type) { + !isNullableType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL nullable type.")) : void 0; + return type; +} +/* eslint-disable no-redeclare */ + + +function getNullableType(type) { + /* eslint-enable no-redeclare */ + if (type) { + return isNonNullType(type) ? type.ofType : type; + } +} +/** + * These named types do not include modifiers like List or NonNull. + */ + + +function isNamedType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type); +} + +function assertNamedType(type) { + !isNamedType(type) ? (0, _invariant.default)(0, "Expected ".concat(String(type), " to be a GraphQL named type.")) : void 0; + return type; +} +/* eslint-disable no-redeclare */ + + +function getNamedType(type) { + /* eslint-enable no-redeclare */ + if (type) { + var unwrappedType = type; + + while (isWrappingType(unwrappedType)) { + unwrappedType = unwrappedType.ofType; + } + + return unwrappedType; + } +} +/** + * Used while defining GraphQL types to allow for circular references in + * otherwise immutable type definitions. + */ + + +function resolveThunk(thunk) { + return typeof thunk === 'function' ? thunk() : thunk; +} +/** + * Scalar Type Definition + * + * The leaf values of any request and input values to arguments are + * Scalars (or Enums) and are defined with a name and a series of functions + * used to parse input from ast or variables and to ensure validity. + * + * If a type's serialize function does not return a value (i.e. it returns + * `undefined`) then an error will be raised and a `null` value will be returned + * in the response. If the serialize function returns `null`, then no error will + * be included in the response. + * + * Example: + * + * const OddType = new GraphQLScalarType({ + * name: 'Odd', + * serialize(value) { + * if (value % 2 === 1) { + * return value; + * } + * } + * }); + * + */ + + +var GraphQLScalarType = +/*#__PURE__*/ +function () { + function GraphQLScalarType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_scalarConfig", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._scalarConfig = config; + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + !(typeof config.serialize === 'function') ? (0, _invariant.default)(0, "".concat(this.name, " must provide \"serialize\" function. If this custom Scalar ") + 'is also used as an input type, ensure "parseValue" and "parseLiteral" ' + 'functions are also provided.') : void 0; + + if (config.parseValue || config.parseLiteral) { + !(typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function') ? (0, _invariant.default)(0, "".concat(this.name, " must provide both \"parseValue\" and \"parseLiteral\" ") + 'functions.') : void 0; + } + } // Serializes an internal value to include in a response. + + + var _proto = GraphQLScalarType.prototype; + + _proto.serialize = function serialize(value) { + var serializer = this._scalarConfig.serialize; + return serializer(value); + }; // Parses an externally provided value to use as an input. + + + _proto.parseValue = function parseValue(value) { + var parser = this._scalarConfig.parseValue; + + if ((0, _isInvalid.default)(value)) { + return undefined; + } + + return parser ? parser(value) : value; + }; // Parses an externally provided literal value to use as an input. + + + _proto.parseLiteral = function parseLiteral(valueNode, variables) { + var parser = this._scalarConfig.parseLiteral; + return parser ? parser(valueNode, variables) : (0, _valueFromASTUntyped.valueFromASTUntyped)(valueNode, variables); + }; + + _proto.toString = function toString() { + return this.name; + }; + + return GraphQLScalarType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLScalarType = GraphQLScalarType; +GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect = GraphQLScalarType.prototype.toString; + +/** + * Object Type Definition + * + * Almost all of the GraphQL types you define will be object types. Object types + * have a name, but most importantly describe their fields. + * + * Example: + * + * const AddressType = new GraphQLObjectType({ + * name: 'Address', + * fields: { + * street: { type: GraphQLString }, + * number: { type: GraphQLInt }, + * formatted: { + * type: GraphQLString, + * resolve(obj) { + * return obj.number + ' ' + obj.street + * } + * } + * } + * }); + * + * When two types need to refer to each other, or a type needs to refer to + * itself in a field, you can use a function expression (aka a closure or a + * thunk) to supply the fields lazily. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * name: { type: GraphQLString }, + * bestFriend: { type: PersonType }, + * }) + * }); + * + */ +var GraphQLObjectType = +/*#__PURE__*/ +function () { + function GraphQLObjectType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "isTypeOf", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "_interfaces", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.isTypeOf = config.isTypeOf; + this._typeConfig = config; + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + + if (config.isTypeOf) { + !(typeof config.isTypeOf === 'function') ? (0, _invariant.default)(0, "".concat(this.name, " must provide \"isTypeOf\" as a function.")) : void 0; + } + } + + var _proto2 = GraphQLObjectType.prototype; + + _proto2.getFields = function getFields() { + return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields)); + }; + + _proto2.getInterfaces = function getInterfaces() { + return this._interfaces || (this._interfaces = defineInterfaces(this, this._typeConfig.interfaces)); + }; + + _proto2.toString = function toString() { + return this.name; + }; + + return GraphQLObjectType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLObjectType = GraphQLObjectType; +GraphQLObjectType.prototype.toJSON = GraphQLObjectType.prototype.inspect = GraphQLObjectType.prototype.toString; + +function defineInterfaces(type, interfacesThunk) { + var interfaces = resolveThunk(interfacesThunk) || []; + !Array.isArray(interfaces) ? (0, _invariant.default)(0, "".concat(type.name, " interfaces must be an Array or a function which returns ") + 'an Array.') : void 0; + return interfaces; +} + +function defineFieldMap(type, fieldsThunk) { + var fieldMap = resolveThunk(fieldsThunk) || {}; + !isPlainObj(fieldMap) ? (0, _invariant.default)(0, "".concat(type.name, " fields must be an object with field names as keys or a ") + 'function which returns such an object.') : void 0; + var resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(function (fieldName) { + var fieldConfig = fieldMap[fieldName]; + !isPlainObj(fieldConfig) ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(fieldName, " field config must be an object")) : void 0; + !!fieldConfig.hasOwnProperty('isDeprecated') ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(fieldName, " should provide \"deprecationReason\" instead ") + 'of "isDeprecated".') : void 0; + + var field = _objectSpread({}, fieldConfig, { + isDeprecated: Boolean(fieldConfig.deprecationReason), + name: fieldName + }); + + !isValidResolver(field.resolve) ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(fieldName, " field resolver must be a function if ") + "provided, but got: ".concat(String(field.resolve), ".")) : void 0; + var argsConfig = fieldConfig.args; + + if (!argsConfig) { + field.args = []; + } else { + !isPlainObj(argsConfig) ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(fieldName, " args must be an object with argument ") + 'names as keys.') : void 0; + field.args = Object.keys(argsConfig).map(function (argName) { + var arg = argsConfig[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode + }; + }); + } + + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; +} + +function isPlainObj(obj) { + return obj && _typeof(obj) === 'object' && !Array.isArray(obj); +} // If a resolver is defined, it must be a function. + + +function isValidResolver(resolver) { + return resolver == null || typeof resolver === 'function'; +} + +/** + * Interface Type Definition + * + * When a field can return one of a heterogeneous set of types, a Interface type + * is used to describe what types are possible, what fields are in common across + * all types, as well as a function to determine which type is actually used + * when the field is resolved. + * + * Example: + * + * const EntityType = new GraphQLInterfaceType({ + * name: 'Entity', + * fields: { + * name: { type: GraphQLString } + * } + * }); + * + */ +var GraphQLInterfaceType = +/*#__PURE__*/ +function () { + function GraphQLInterfaceType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "resolveType", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.resolveType = config.resolveType; + this._typeConfig = config; + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + + if (config.resolveType) { + !(typeof config.resolveType === 'function') ? (0, _invariant.default)(0, "".concat(this.name, " must provide \"resolveType\" as a function.")) : void 0; + } + } + + var _proto3 = GraphQLInterfaceType.prototype; + + _proto3.getFields = function getFields() { + return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields)); + }; + + _proto3.toString = function toString() { + return this.name; + }; + + return GraphQLInterfaceType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLInterfaceType = GraphQLInterfaceType; +GraphQLInterfaceType.prototype.toJSON = GraphQLInterfaceType.prototype.inspect = GraphQLInterfaceType.prototype.toString; + +/** + * Union Type Definition + * + * When a field can return one of a heterogeneous set of types, a Union type + * is used to describe what types are possible as well as providing a function + * to determine which type is actually used when the field is resolved. + * + * Example: + * + * const PetType = new GraphQLUnionType({ + * name: 'Pet', + * types: [ DogType, CatType ], + * resolveType(value) { + * if (value instanceof Dog) { + * return DogType; + * } + * if (value instanceof Cat) { + * return CatType; + * } + * } + * }); + * + */ +var GraphQLUnionType = +/*#__PURE__*/ +function () { + function GraphQLUnionType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "resolveType", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_types", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.resolveType = config.resolveType; + this._typeConfig = config; + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + + if (config.resolveType) { + !(typeof config.resolveType === 'function') ? (0, _invariant.default)(0, "".concat(this.name, " must provide \"resolveType\" as a function.")) : void 0; + } + } + + var _proto4 = GraphQLUnionType.prototype; + + _proto4.getTypes = function getTypes() { + return this._types || (this._types = defineTypes(this, this._typeConfig.types)); + }; + + _proto4.toString = function toString() { + return this.name; + }; + + return GraphQLUnionType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLUnionType = GraphQLUnionType; +GraphQLUnionType.prototype.toJSON = GraphQLUnionType.prototype.inspect = GraphQLUnionType.prototype.toString; + +function defineTypes(unionType, typesThunk) { + var types = resolveThunk(typesThunk) || []; + !Array.isArray(types) ? (0, _invariant.default)(0, 'Must provide Array of types or a function which returns ' + "such an array for Union ".concat(unionType.name, ".")) : void 0; + return types; +} + +/** + * Enum Type Definition + * + * Some leaf values of requests and input values are Enums. GraphQL serializes + * Enum values as strings, however internally Enums can be represented by any + * kind of type, often integers. + * + * Example: + * + * const RGBType = new GraphQLEnumType({ + * name: 'RGB', + * values: { + * RED: { value: 0 }, + * GREEN: { value: 1 }, + * BLUE: { value: 2 } + * } + * }); + * + * Note: If a value is not provided in a definition, the name of the enum value + * will be used as its internal value. + */ +var GraphQLEnumType +/* */ += +/*#__PURE__*/ +function () { + function GraphQLEnumType(config + /* */ + ) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_values", void 0); + + _defineProperty(this, "_valueLookup", void 0); + + _defineProperty(this, "_nameLookup", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._values = defineEnumValues(this, config.values); + this._valueLookup = new Map(this._values.map(function (enumValue) { + return [enumValue.value, enumValue]; + })); + this._nameLookup = (0, _keyMap.default)(this._values, function (value) { + return value.name; + }); + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + } + + var _proto5 = GraphQLEnumType.prototype; + + _proto5.getValues = function getValues() { + return this._values; + }; + + _proto5.getValue = function getValue(name) { + return this._nameLookup[name]; + }; + + _proto5.serialize = function serialize(value + /* T */ + ) { + var enumValue = this._valueLookup.get(value); + + if (enumValue) { + return enumValue.name; + } + }; + + _proto5.parseValue = function parseValue(value) + /* T */ + { + if (typeof value === 'string') { + var enumValue = this.getValue(value); + + if (enumValue) { + return enumValue.value; + } + } + }; + + _proto5.parseLiteral = function parseLiteral(valueNode, _variables) + /* T */ + { + // Note: variables will be resolved to a value before calling this function. + if (valueNode.kind === _kinds.Kind.ENUM) { + var enumValue = this.getValue(valueNode.value); + + if (enumValue) { + return enumValue.value; + } + } + }; + + _proto5.toString = function toString() { + return this.name; + }; + + return GraphQLEnumType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLEnumType = GraphQLEnumType; +GraphQLEnumType.prototype.toJSON = GraphQLEnumType.prototype.inspect = GraphQLEnumType.prototype.toString; + +function defineEnumValues(type, valueMap +/* */ +) { + !isPlainObj(valueMap) ? (0, _invariant.default)(0, "".concat(type.name, " values must be an object with value names as keys.")) : void 0; + return Object.keys(valueMap).map(function (valueName) { + var value = valueMap[valueName]; + !isPlainObj(value) ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(valueName, " must refer to an object with a \"value\" key ") + "representing an internal value but got: ".concat(String(value), ".")) : void 0; + !!value.hasOwnProperty('isDeprecated') ? (0, _invariant.default)(0, "".concat(type.name, ".").concat(valueName, " should provide \"deprecationReason\" instead ") + 'of "isDeprecated".') : void 0; + return { + name: valueName, + description: value.description, + isDeprecated: Boolean(value.deprecationReason), + deprecationReason: value.deprecationReason, + astNode: value.astNode, + value: value.hasOwnProperty('value') ? value.value : valueName + }; + }); +} + +/** + * Input Object Type Definition + * + * An input object defines a structured collection of fields which may be + * supplied to a field argument. + * + * Using `NonNull` will ensure that a value must be provided by the query + * + * Example: + * + * const GeoPoint = new GraphQLInputObjectType({ + * name: 'GeoPoint', + * fields: { + * lat: { type: GraphQLNonNull(GraphQLFloat) }, + * lon: { type: GraphQLNonNull(GraphQLFloat) }, + * alt: { type: GraphQLFloat, defaultValue: 0 }, + * } + * }); + * + */ +var GraphQLInputObjectType = +/*#__PURE__*/ +function () { + function GraphQLInputObjectType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._typeConfig = config; + !(typeof config.name === 'string') ? (0, _invariant.default)(0, 'Must provide name.') : void 0; + } + + var _proto6 = GraphQLInputObjectType.prototype; + + _proto6.getFields = function getFields() { + return this._fields || (this._fields = this._defineFieldMap()); + }; + + _proto6._defineFieldMap = function _defineFieldMap() { + var _this = this; + + var fieldMap = resolveThunk(this._typeConfig.fields) || {}; + !isPlainObj(fieldMap) ? (0, _invariant.default)(0, "".concat(this.name, " fields must be an object with field names as keys or a ") + 'function which returns such an object.') : void 0; + var resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(function (fieldName) { + var field = _objectSpread({}, fieldMap[fieldName], { + name: fieldName + }); + + !!field.hasOwnProperty('resolve') ? (0, _invariant.default)(0, "".concat(_this.name, ".").concat(fieldName, " field type has a resolve property, but ") + 'Input Types cannot define resolvers.') : void 0; + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; + }; + + _proto6.toString = function toString() { + return this.name; + }; + + return GraphQLInputObjectType; +}(); // Also provide toJSON and inspect aliases for toString. + + +exports.GraphQLInputObjectType = GraphQLInputObjectType; +GraphQLInputObjectType.prototype.toJSON = GraphQLInputObjectType.prototype.toString; +GraphQLInputObjectType.prototype.inspect = GraphQLInputObjectType.prototype.toString; \ No newline at end of file diff --git a/dist/type/definition.js.flow b/dist/type/definition.js.flow new file mode 100644 index 0000000000..2d7025a470 --- /dev/null +++ b/dist/type/definition.js.flow @@ -0,0 +1,1297 @@ +/** + * 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. + * + * @flow strict + */ + +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import isInvalid from '../jsutils/isInvalid'; +import keyMap from '../jsutils/keyMap'; +import type { ObjMap } from '../jsutils/ObjMap'; +import { Kind } from '../language/kinds'; +import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped'; +import type { + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + ObjectTypeExtensionNode, + InterfaceTypeExtensionNode, + OperationDefinitionNode, + FieldNode, + FragmentDefinitionNode, + ValueNode, +} from '../language/ast'; +import type { GraphQLSchema } from './schema'; +import type { MaybePromise } from '../jsutils/MaybePromise'; + +// Predicates & Assertions + +/** + * These are all of the possible kinds of types. + */ +export type GraphQLType = + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType + | GraphQLInputObjectType + | GraphQLList + | GraphQLNonNull; + +export function isType(type: mixed): boolean %checks { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + isInputObjectType(type) || + isListType(type) || + isNonNullType(type) + ); +} + +export function assertType(type: mixed): GraphQLType { + invariant(isType(type), `Expected ${String(type)} to be a GraphQL type.`); + return (type: any); +} + +/** + * There are predicates for each kind of GraphQL type. + */ + +declare function isScalarType(type: mixed): boolean %checks(type instanceof + GraphQLScalarType); +// eslint-disable-next-line no-redeclare +export function isScalarType(type) { + return instanceOf(type, GraphQLScalarType); +} + +export function assertScalarType(type: mixed): GraphQLScalarType { + invariant( + isScalarType(type), + `Expected ${String(type)} to be a GraphQL Scalar type.`, + ); + return type; +} + +declare function isObjectType(type: mixed): boolean %checks(type instanceof + GraphQLObjectType); +// eslint-disable-next-line no-redeclare +export function isObjectType(type) { + return instanceOf(type, GraphQLObjectType); +} + +export function assertObjectType(type: mixed): GraphQLObjectType { + invariant( + isObjectType(type), + `Expected ${String(type)} to be a GraphQL Object type.`, + ); + return type; +} + +declare function isInterfaceType(type: mixed): boolean %checks(type instanceof + GraphQLInterfaceType); +// eslint-disable-next-line no-redeclare +export function isInterfaceType(type) { + return instanceOf(type, GraphQLInterfaceType); +} + +export function assertInterfaceType(type: mixed): GraphQLInterfaceType { + invariant( + isInterfaceType(type), + `Expected ${String(type)} to be a GraphQL Interface type.`, + ); + return type; +} + +declare function isUnionType(type: mixed): boolean %checks(type instanceof + GraphQLUnionType); +// eslint-disable-next-line no-redeclare +export function isUnionType(type) { + return instanceOf(type, GraphQLUnionType); +} + +export function assertUnionType(type: mixed): GraphQLUnionType { + invariant( + isUnionType(type), + `Expected ${String(type)} to be a GraphQL Union type.`, + ); + return type; +} + +declare function isEnumType(type: mixed): boolean %checks(type instanceof + GraphQLEnumType); +// eslint-disable-next-line no-redeclare +export function isEnumType(type) { + return instanceOf(type, GraphQLEnumType); +} + +export function assertEnumType(type: mixed): GraphQLEnumType { + invariant( + isEnumType(type), + `Expected ${String(type)} to be a GraphQL Enum type.`, + ); + return type; +} + +declare function isInputObjectType(type: mixed): boolean %checks(type instanceof + GraphQLInputObjectType); +// eslint-disable-next-line no-redeclare +export function isInputObjectType(type) { + return instanceOf(type, GraphQLInputObjectType); +} + +export function assertInputObjectType(type: mixed): GraphQLInputObjectType { + invariant( + isInputObjectType(type), + `Expected ${String(type)} to be a GraphQL Input Object type.`, + ); + return type; +} + +declare function isListType(type: mixed): boolean %checks(type instanceof + GraphQLList); +// eslint-disable-next-line no-redeclare +export function isListType(type) { + return instanceOf(type, GraphQLList); +} + +export function assertListType(type: mixed): GraphQLList { + invariant( + isListType(type), + `Expected ${String(type)} to be a GraphQL List type.`, + ); + return type; +} + +declare function isNonNullType(type: mixed): boolean %checks(type instanceof + GraphQLNonNull); +// eslint-disable-next-line no-redeclare +export function isNonNullType(type) { + return instanceOf(type, GraphQLNonNull); +} + +export function assertNonNullType(type: mixed): GraphQLNonNull { + invariant( + isNonNullType(type), + `Expected ${String(type)} to be a GraphQL Non-Null type.`, + ); + return type; +} + +/** + * These types may be used as input types for arguments and directives. + */ +export type GraphQLInputType = + | GraphQLScalarType + | GraphQLEnumType + | GraphQLInputObjectType + | GraphQLList + | GraphQLNonNull< + | GraphQLScalarType + | GraphQLEnumType + | GraphQLInputObjectType + | GraphQLList, + >; + +export function isInputType(type: mixed): boolean %checks { + return ( + isScalarType(type) || + isEnumType(type) || + isInputObjectType(type) || + (isWrappingType(type) && isInputType(type.ofType)) + ); +} + +export function assertInputType(type: mixed): GraphQLInputType { + invariant( + isInputType(type), + `Expected ${String(type)} to be a GraphQL input type.`, + ); + return type; +} + +/** + * These types may be used as output types as the result of fields. + */ +export type GraphQLOutputType = + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType + | GraphQLList + | GraphQLNonNull< + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType + | GraphQLList, + >; + +export function isOutputType(type: mixed): boolean %checks { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + (isWrappingType(type) && isOutputType(type.ofType)) + ); +} + +export function assertOutputType(type: mixed): GraphQLOutputType { + invariant( + isOutputType(type), + `Expected ${String(type)} to be a GraphQL output type.`, + ); + return type; +} + +/** + * These types may describe types which may be leaf values. + */ +export type GraphQLLeafType = GraphQLScalarType | GraphQLEnumType; + +export function isLeafType(type: mixed): boolean %checks { + return isScalarType(type) || isEnumType(type); +} + +export function assertLeafType(type: mixed): GraphQLLeafType { + invariant( + isLeafType(type), + `Expected ${String(type)} to be a GraphQL leaf type.`, + ); + return type; +} + +/** + * These types may describe the parent context of a selection set. + */ +export type GraphQLCompositeType = + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType; + +export function isCompositeType(type: mixed): boolean %checks { + return isObjectType(type) || isInterfaceType(type) || isUnionType(type); +} + +export function assertCompositeType(type: mixed): GraphQLCompositeType { + invariant( + isCompositeType(type), + `Expected ${String(type)} to be a GraphQL composite type.`, + ); + return type; +} + +/** + * These types may describe the parent context of a selection set. + */ +export type GraphQLAbstractType = GraphQLInterfaceType | GraphQLUnionType; + +export function isAbstractType(type: mixed): boolean %checks { + return isInterfaceType(type) || isUnionType(type); +} + +export function assertAbstractType(type: mixed): GraphQLAbstractType { + invariant( + isAbstractType(type), + `Expected ${String(type)} to be a GraphQL abstract type.`, + ); + return type; +} + +/** + * List Type Wrapper + * + * A list is a wrapping type which points to another type. + * Lists are often created within the context of defining the fields of + * an object type. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * parents: { type: GraphQLList(PersonType) }, + * children: { type: GraphQLList(PersonType) }, + * }) + * }) + * + */ +declare class GraphQLList<+T: GraphQLType> { + +ofType: T; + static (ofType: T): GraphQLList; + // Note: constructors cannot be used for covariant types. Drop the "new". + constructor(ofType: any): void; +} +// eslint-disable-next-line no-redeclare +export function GraphQLList(ofType) { + if (this instanceof GraphQLList) { + this.ofType = assertType(ofType); + } else { + return new GraphQLList(ofType); + } +} + +// Also provide toJSON and inspect aliases for toString. +const listProto: any = GraphQLList.prototype; +listProto.toString = listProto.toJSON = listProto.inspect = function toString() { + return '[' + String(this.ofType) + ']'; +}; + +/** + * Non-Null Type Wrapper + * + * A non-null is a wrapping type which points to another type. + * Non-null types enforce that their values are never null and can ensure + * an error is raised if this ever occurs during a request. It is useful for + * fields which you can make a strong guarantee on non-nullability, for example + * usually the id field of a database row will never be null. + * + * Example: + * + * const RowType = new GraphQLObjectType({ + * name: 'Row', + * fields: () => ({ + * id: { type: GraphQLNonNull(GraphQLString) }, + * }) + * }) + * + * Note: the enforcement of non-nullability occurs within the executor. + */ +declare class GraphQLNonNull<+T: GraphQLNullableType> { + +ofType: T; + static (ofType: T): GraphQLNonNull; + // Note: constructors cannot be used for covariant types. Drop the "new". + constructor(ofType: any): void; +} +// eslint-disable-next-line no-redeclare +export function GraphQLNonNull(ofType) { + if (this instanceof GraphQLNonNull) { + this.ofType = assertNullableType(ofType); + } else { + return new GraphQLNonNull(ofType); + } +} + +// Also provide toJSON and inspect aliases for toString. +const nonNullProto: any = GraphQLNonNull.prototype; +nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() { + return String(this.ofType) + '!'; +}; + +/** + * These types wrap and modify other types + */ + +export type GraphQLWrappingType = GraphQLList | GraphQLNonNull; + +export function isWrappingType(type: mixed): boolean %checks { + return isListType(type) || isNonNullType(type); +} + +export function assertWrappingType(type: mixed): GraphQLWrappingType { + invariant( + isWrappingType(type), + `Expected ${String(type)} to be a GraphQL wrapping type.`, + ); + return type; +} + +/** + * These types can all accept null as a value. + */ +export type GraphQLNullableType = + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType + | GraphQLInputObjectType + | GraphQLList; + +export function isNullableType(type: mixed): boolean %checks { + return isType(type) && !isNonNullType(type); +} + +export function assertNullableType(type: mixed): GraphQLNullableType { + invariant( + isNullableType(type), + `Expected ${String(type)} to be a GraphQL nullable type.`, + ); + return type; +} + +/* eslint-disable no-redeclare */ +declare function getNullableType(type: void | null): void; +declare function getNullableType(type: T): T; +declare function getNullableType(type: GraphQLNonNull): T; +export function getNullableType(type) { + /* eslint-enable no-redeclare */ + if (type) { + return isNonNullType(type) ? type.ofType : type; + } +} + +/** + * These named types do not include modifiers like List or NonNull. + */ +export type GraphQLNamedType = + | GraphQLScalarType + | GraphQLObjectType + | GraphQLInterfaceType + | GraphQLUnionType + | GraphQLEnumType + | GraphQLInputObjectType; + +export function isNamedType(type: mixed): boolean %checks { + return ( + isScalarType(type) || + isObjectType(type) || + isInterfaceType(type) || + isUnionType(type) || + isEnumType(type) || + isInputObjectType(type) + ); +} + +export function assertNamedType(type: mixed): GraphQLNamedType { + invariant( + isNamedType(type), + `Expected ${String(type)} to be a GraphQL named type.`, + ); + return type; +} + +/* eslint-disable no-redeclare */ +declare function getNamedType(type: void | null): void; +declare function getNamedType(type: GraphQLType): GraphQLNamedType; +export function getNamedType(type) { + /* eslint-enable no-redeclare */ + if (type) { + let unwrappedType = type; + while (isWrappingType(unwrappedType)) { + unwrappedType = unwrappedType.ofType; + } + return unwrappedType; + } +} + +/** + * Used while defining GraphQL types to allow for circular references in + * otherwise immutable type definitions. + */ +export type Thunk<+T> = (() => T) | T; + +function resolveThunk<+T>(thunk: Thunk): T { + return typeof thunk === 'function' ? thunk() : thunk; +} + +/** + * Scalar Type Definition + * + * The leaf values of any request and input values to arguments are + * Scalars (or Enums) and are defined with a name and a series of functions + * used to parse input from ast or variables and to ensure validity. + * + * If a type's serialize function does not return a value (i.e. it returns + * `undefined`) then an error will be raised and a `null` value will be returned + * in the response. If the serialize function returns `null`, then no error will + * be included in the response. + * + * Example: + * + * const OddType = new GraphQLScalarType({ + * name: 'Odd', + * serialize(value) { + * if (value % 2 === 1) { + * return value; + * } + * } + * }); + * + */ +export class GraphQLScalarType { + name: string; + description: ?string; + astNode: ?ScalarTypeDefinitionNode; + + _scalarConfig: GraphQLScalarTypeConfig<*, *>; + + constructor(config: GraphQLScalarTypeConfig<*, *>): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._scalarConfig = config; + invariant(typeof config.name === 'string', 'Must provide name.'); + invariant( + typeof config.serialize === 'function', + `${this.name} must provide "serialize" function. If this custom Scalar ` + + 'is also used as an input type, ensure "parseValue" and "parseLiteral" ' + + 'functions are also provided.', + ); + if (config.parseValue || config.parseLiteral) { + invariant( + typeof config.parseValue === 'function' && + typeof config.parseLiteral === 'function', + `${this.name} must provide both "parseValue" and "parseLiteral" ` + + 'functions.', + ); + } + } + + // Serializes an internal value to include in a response. + serialize(value: mixed): mixed { + const serializer = this._scalarConfig.serialize; + return serializer(value); + } + + // Parses an externally provided value to use as an input. + parseValue(value: mixed): mixed { + const parser = this._scalarConfig.parseValue; + if (isInvalid(value)) { + return undefined; + } + return parser ? parser(value) : value; + } + + // Parses an externally provided literal value to use as an input. + parseLiteral(valueNode: ValueNode, variables: ?ObjMap): mixed { + const parser = this._scalarConfig.parseLiteral; + return parser + ? parser(valueNode, variables) + : valueFromASTUntyped(valueNode, variables); + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect = + GraphQLScalarType.prototype.toString; + +export type GraphQLScalarTypeConfig = { + name: string, + description?: ?string, + astNode?: ?ScalarTypeDefinitionNode, + serialize: (value: mixed) => ?TExternal, + parseValue?: (value: mixed) => ?TInternal, + parseLiteral?: ( + valueNode: ValueNode, + variables: ?ObjMap, + ) => ?TInternal, +}; + +/** + * Object Type Definition + * + * Almost all of the GraphQL types you define will be object types. Object types + * have a name, but most importantly describe their fields. + * + * Example: + * + * const AddressType = new GraphQLObjectType({ + * name: 'Address', + * fields: { + * street: { type: GraphQLString }, + * number: { type: GraphQLInt }, + * formatted: { + * type: GraphQLString, + * resolve(obj) { + * return obj.number + ' ' + obj.street + * } + * } + * } + * }); + * + * When two types need to refer to each other, or a type needs to refer to + * itself in a field, you can use a function expression (aka a closure or a + * thunk) to supply the fields lazily. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * name: { type: GraphQLString }, + * bestFriend: { type: PersonType }, + * }) + * }); + * + */ +export class GraphQLObjectType { + name: string; + description: ?string; + astNode: ?ObjectTypeDefinitionNode; + extensionASTNodes: ?$ReadOnlyArray; + isTypeOf: ?GraphQLIsTypeOfFn<*, *>; + + _typeConfig: GraphQLObjectTypeConfig<*, *>; + _fields: GraphQLFieldMap<*, *>; + _interfaces: Array; + + constructor(config: GraphQLObjectTypeConfig<*, *>): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.isTypeOf = config.isTypeOf; + this._typeConfig = config; + invariant(typeof config.name === 'string', 'Must provide name.'); + if (config.isTypeOf) { + invariant( + typeof config.isTypeOf === 'function', + `${this.name} must provide "isTypeOf" as a function.`, + ); + } + } + + getFields(): GraphQLFieldMap<*, *> { + return ( + this._fields || + (this._fields = defineFieldMap(this, this._typeConfig.fields)) + ); + } + + getInterfaces(): Array { + return ( + this._interfaces || + (this._interfaces = defineInterfaces(this, this._typeConfig.interfaces)) + ); + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLObjectType.prototype.toJSON = GraphQLObjectType.prototype.inspect = + GraphQLObjectType.prototype.toString; + +function defineInterfaces( + type: GraphQLObjectType, + interfacesThunk: Thunk>, +): Array { + const interfaces = resolveThunk(interfacesThunk) || []; + invariant( + Array.isArray(interfaces), + `${type.name} interfaces must be an Array or a function which returns ` + + 'an Array.', + ); + return interfaces; +} + +function defineFieldMap( + type: GraphQLNamedType, + fieldsThunk: Thunk>, +): GraphQLFieldMap { + const fieldMap = resolveThunk(fieldsThunk) || {}; + invariant( + isPlainObj(fieldMap), + `${type.name} fields must be an object with field names as keys or a ` + + 'function which returns such an object.', + ); + + const resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(fieldName => { + const fieldConfig = fieldMap[fieldName]; + invariant( + isPlainObj(fieldConfig), + `${type.name}.${fieldName} field config must be an object`, + ); + invariant( + !fieldConfig.hasOwnProperty('isDeprecated'), + `${type.name}.${fieldName} should provide "deprecationReason" instead ` + + 'of "isDeprecated".', + ); + const field = { + ...fieldConfig, + isDeprecated: Boolean(fieldConfig.deprecationReason), + name: fieldName, + }; + invariant( + isValidResolver(field.resolve), + `${type.name}.${fieldName} field resolver must be a function if ` + + `provided, but got: ${String(field.resolve)}.`, + ); + const argsConfig = fieldConfig.args; + if (!argsConfig) { + field.args = []; + } else { + invariant( + isPlainObj(argsConfig), + `${type.name}.${fieldName} args must be an object with argument ` + + 'names as keys.', + ); + field.args = Object.keys(argsConfig).map(argName => { + const arg = argsConfig[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode, + }; + }); + } + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; +} + +function isPlainObj(obj) { + return obj && typeof obj === 'object' && !Array.isArray(obj); +} + +// If a resolver is defined, it must be a function. +function isValidResolver(resolver: mixed): boolean { + return resolver == null || typeof resolver === 'function'; +} + +export type GraphQLObjectTypeConfig = { + name: string, + interfaces?: Thunk>, + fields: Thunk>, + isTypeOf?: ?GraphQLIsTypeOfFn, + description?: ?string, + astNode?: ?ObjectTypeDefinitionNode, + extensionASTNodes?: ?$ReadOnlyArray, +}; + +export type GraphQLTypeResolver = ( + value: TSource, + context: TContext, + info: GraphQLResolveInfo, +) => MaybePromise; + +export type GraphQLIsTypeOfFn = ( + source: TSource, + context: TContext, + info: GraphQLResolveInfo, +) => MaybePromise; + +export type GraphQLFieldResolver< + TSource, + TContext, + TArgs = { [argument: string]: any }, +> = ( + source: TSource, + args: TArgs, + context: TContext, + info: GraphQLResolveInfo, +) => mixed; + +export type GraphQLResolveInfo = {| + +fieldName: string, + +fieldNodes: $ReadOnlyArray, + +returnType: GraphQLOutputType, + +parentType: GraphQLObjectType, + +path: ResponsePath, + +schema: GraphQLSchema, + +fragments: ObjMap, + +rootValue: mixed, + +operation: OperationDefinitionNode, + +variableValues: { [variable: string]: mixed }, +|}; + +export type ResponsePath = {| + +prev: ResponsePath | void, + +key: string | number, +|}; + +export type GraphQLFieldConfig< + TSource, + TContext, + TArgs = { [argument: string]: any }, +> = { + type: GraphQLOutputType, + args?: GraphQLFieldConfigArgumentMap, + resolve?: GraphQLFieldResolver, + subscribe?: GraphQLFieldResolver, + deprecationReason?: ?string, + description?: ?string, + astNode?: ?FieldDefinitionNode, +}; + +export type GraphQLFieldConfigArgumentMap = ObjMap; + +export type GraphQLArgumentConfig = { + type: GraphQLInputType, + defaultValue?: mixed, + description?: ?string, + astNode?: ?InputValueDefinitionNode, +}; + +export type GraphQLFieldConfigMap = ObjMap< + GraphQLFieldConfig, +>; + +export type GraphQLField< + TSource, + TContext, + TArgs = { [argument: string]: any }, +> = { + name: string, + description: ?string, + type: GraphQLOutputType, + args: Array, + resolve?: GraphQLFieldResolver, + subscribe?: GraphQLFieldResolver, + isDeprecated?: boolean, + deprecationReason?: ?string, + astNode?: ?FieldDefinitionNode, +}; + +export type GraphQLArgument = { + name: string, + type: GraphQLInputType, + defaultValue?: mixed, + description?: ?string, + astNode?: ?InputValueDefinitionNode, +}; + +export type GraphQLFieldMap = ObjMap< + GraphQLField, +>; + +/** + * Interface Type Definition + * + * When a field can return one of a heterogeneous set of types, a Interface type + * is used to describe what types are possible, what fields are in common across + * all types, as well as a function to determine which type is actually used + * when the field is resolved. + * + * Example: + * + * const EntityType = new GraphQLInterfaceType({ + * name: 'Entity', + * fields: { + * name: { type: GraphQLString } + * } + * }); + * + */ +export class GraphQLInterfaceType { + name: string; + description: ?string; + astNode: ?InterfaceTypeDefinitionNode; + extensionASTNodes: ?$ReadOnlyArray; + resolveType: ?GraphQLTypeResolver<*, *>; + + _typeConfig: GraphQLInterfaceTypeConfig<*, *>; + _fields: GraphQLFieldMap<*, *>; + + constructor(config: GraphQLInterfaceTypeConfig<*, *>): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.resolveType = config.resolveType; + this._typeConfig = config; + invariant(typeof config.name === 'string', 'Must provide name.'); + if (config.resolveType) { + invariant( + typeof config.resolveType === 'function', + `${this.name} must provide "resolveType" as a function.`, + ); + } + } + + getFields(): GraphQLFieldMap<*, *> { + return ( + this._fields || + (this._fields = defineFieldMap(this, this._typeConfig.fields)) + ); + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLInterfaceType.prototype.toJSON = GraphQLInterfaceType.prototype.inspect = + GraphQLInterfaceType.prototype.toString; + +export type GraphQLInterfaceTypeConfig = { + name: string, + fields: Thunk>, + /** + * Optionally provide a custom type resolver function. If one is not provided, + * the default implementation will call `isTypeOf` on each implementing + * Object type. + */ + resolveType?: ?GraphQLTypeResolver, + description?: ?string, + astNode?: ?InterfaceTypeDefinitionNode, + extensionASTNodes?: ?$ReadOnlyArray, +}; + +/** + * Union Type Definition + * + * When a field can return one of a heterogeneous set of types, a Union type + * is used to describe what types are possible as well as providing a function + * to determine which type is actually used when the field is resolved. + * + * Example: + * + * const PetType = new GraphQLUnionType({ + * name: 'Pet', + * types: [ DogType, CatType ], + * resolveType(value) { + * if (value instanceof Dog) { + * return DogType; + * } + * if (value instanceof Cat) { + * return CatType; + * } + * } + * }); + * + */ +export class GraphQLUnionType { + name: string; + description: ?string; + astNode: ?UnionTypeDefinitionNode; + resolveType: ?GraphQLTypeResolver<*, *>; + + _typeConfig: GraphQLUnionTypeConfig<*, *>; + _types: Array; + + constructor(config: GraphQLUnionTypeConfig<*, *>): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.resolveType = config.resolveType; + this._typeConfig = config; + invariant(typeof config.name === 'string', 'Must provide name.'); + if (config.resolveType) { + invariant( + typeof config.resolveType === 'function', + `${this.name} must provide "resolveType" as a function.`, + ); + } + } + + getTypes(): Array { + return ( + this._types || (this._types = defineTypes(this, this._typeConfig.types)) + ); + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLUnionType.prototype.toJSON = GraphQLUnionType.prototype.inspect = + GraphQLUnionType.prototype.toString; + +function defineTypes( + unionType: GraphQLUnionType, + typesThunk: Thunk>, +): Array { + const types = resolveThunk(typesThunk) || []; + invariant( + Array.isArray(types), + 'Must provide Array of types or a function which returns ' + + `such an array for Union ${unionType.name}.`, + ); + return types; +} + +export type GraphQLUnionTypeConfig = { + name: string, + types: Thunk>, + /** + * Optionally provide a custom type resolver function. If one is not provided, + * the default implementation will call `isTypeOf` on each implementing + * Object type. + */ + resolveType?: ?GraphQLTypeResolver, + description?: ?string, + astNode?: ?UnionTypeDefinitionNode, +}; + +/** + * Enum Type Definition + * + * Some leaf values of requests and input values are Enums. GraphQL serializes + * Enum values as strings, however internally Enums can be represented by any + * kind of type, often integers. + * + * Example: + * + * const RGBType = new GraphQLEnumType({ + * name: 'RGB', + * values: { + * RED: { value: 0 }, + * GREEN: { value: 1 }, + * BLUE: { value: 2 } + * } + * }); + * + * Note: If a value is not provided in a definition, the name of the enum value + * will be used as its internal value. + */ +export class GraphQLEnumType /* */ { + name: string; + description: ?string; + astNode: ?EnumTypeDefinitionNode; + + _values: Array */>; + _valueLookup: Map; + _nameLookup: ObjMap; + + constructor(config: GraphQLEnumTypeConfig /* */): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._values = defineEnumValues(this, config.values); + this._valueLookup = new Map( + this._values.map(enumValue => [enumValue.value, enumValue]), + ); + this._nameLookup = keyMap(this._values, value => value.name); + + invariant(typeof config.name === 'string', 'Must provide name.'); + } + + getValues(): Array */> { + return this._values; + } + + getValue(name: string): ?GraphQLEnumValue { + return this._nameLookup[name]; + } + + serialize(value: any /* T */): ?string { + const enumValue = this._valueLookup.get(value); + if (enumValue) { + return enumValue.name; + } + } + + parseValue(value: mixed): ?any /* T */ { + if (typeof value === 'string') { + const enumValue = this.getValue(value); + if (enumValue) { + return enumValue.value; + } + } + } + + parseLiteral(valueNode: ValueNode, _variables: ?ObjMap): ?any /* T */ { + // Note: variables will be resolved to a value before calling this function. + if (valueNode.kind === Kind.ENUM) { + const enumValue = this.getValue(valueNode.value); + if (enumValue) { + return enumValue.value; + } + } + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLEnumType.prototype.toJSON = GraphQLEnumType.prototype.inspect = + GraphQLEnumType.prototype.toString; + +function defineEnumValues( + type: GraphQLEnumType, + valueMap: GraphQLEnumValueConfigMap /* */, +): Array */> { + invariant( + isPlainObj(valueMap), + `${type.name} values must be an object with value names as keys.`, + ); + return Object.keys(valueMap).map(valueName => { + const value = valueMap[valueName]; + invariant( + isPlainObj(value), + `${type.name}.${valueName} must refer to an object with a "value" key ` + + `representing an internal value but got: ${String(value)}.`, + ); + invariant( + !value.hasOwnProperty('isDeprecated'), + `${type.name}.${valueName} should provide "deprecationReason" instead ` + + 'of "isDeprecated".', + ); + return { + name: valueName, + description: value.description, + isDeprecated: Boolean(value.deprecationReason), + deprecationReason: value.deprecationReason, + astNode: value.astNode, + value: value.hasOwnProperty('value') ? value.value : valueName, + }; + }); +} + +export type GraphQLEnumTypeConfig /* */ = { + name: string, + values: GraphQLEnumValueConfigMap /* */, + description?: ?string, + astNode?: ?EnumTypeDefinitionNode, +}; + +export type GraphQLEnumValueConfigMap /* */ = ObjMap< + GraphQLEnumValueConfig /* */, +>; + +export type GraphQLEnumValueConfig /* */ = { + value?: any /* T */, + deprecationReason?: ?string, + description?: ?string, + astNode?: ?EnumValueDefinitionNode, +}; + +export type GraphQLEnumValue /* */ = { + name: string, + description: ?string, + isDeprecated?: boolean, + deprecationReason: ?string, + astNode?: ?EnumValueDefinitionNode, + value: any /* T */, +}; + +/** + * Input Object Type Definition + * + * An input object defines a structured collection of fields which may be + * supplied to a field argument. + * + * Using `NonNull` will ensure that a value must be provided by the query + * + * Example: + * + * const GeoPoint = new GraphQLInputObjectType({ + * name: 'GeoPoint', + * fields: { + * lat: { type: GraphQLNonNull(GraphQLFloat) }, + * lon: { type: GraphQLNonNull(GraphQLFloat) }, + * alt: { type: GraphQLFloat, defaultValue: 0 }, + * } + * }); + * + */ +export class GraphQLInputObjectType { + name: string; + description: ?string; + astNode: ?InputObjectTypeDefinitionNode; + + _typeConfig: GraphQLInputObjectTypeConfig; + _fields: GraphQLInputFieldMap; + + constructor(config: GraphQLInputObjectTypeConfig): void { + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._typeConfig = config; + invariant(typeof config.name === 'string', 'Must provide name.'); + } + + getFields(): GraphQLInputFieldMap { + return this._fields || (this._fields = this._defineFieldMap()); + } + + _defineFieldMap(): GraphQLInputFieldMap { + const fieldMap: any = resolveThunk(this._typeConfig.fields) || {}; + invariant( + isPlainObj(fieldMap), + `${this.name} fields must be an object with field names as keys or a ` + + 'function which returns such an object.', + ); + const resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(fieldName => { + const field = { + ...fieldMap[fieldName], + name: fieldName, + }; + invariant( + !field.hasOwnProperty('resolve'), + `${this.name}.${fieldName} field type has a resolve property, but ` + + 'Input Types cannot define resolvers.', + ); + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; + } + + toString(): string { + return this.name; + } + + toJSON: () => string; + inspect: () => string; +} + +// Also provide toJSON and inspect aliases for toString. +GraphQLInputObjectType.prototype.toJSON = + GraphQLInputObjectType.prototype.toString; +GraphQLInputObjectType.prototype.inspect = + GraphQLInputObjectType.prototype.toString; + +export type GraphQLInputObjectTypeConfig = { + name: string, + fields: Thunk, + description?: ?string, + astNode?: ?InputObjectTypeDefinitionNode, +}; + +export type GraphQLInputFieldConfig = { + type: GraphQLInputType, + defaultValue?: mixed, + description?: ?string, + astNode?: ?InputValueDefinitionNode, +}; + +export type GraphQLInputFieldConfigMap = ObjMap; + +export type GraphQLInputField = { + name: string, + type: GraphQLInputType, + defaultValue?: mixed, + description?: ?string, + astNode?: ?InputValueDefinitionNode, +}; + +export type GraphQLInputFieldMap = ObjMap; diff --git a/dist/type/definition.mjs b/dist/type/definition.mjs new file mode 100644 index 0000000000..d92d675812 --- /dev/null +++ b/dist/type/definition.mjs @@ -0,0 +1,869 @@ +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import isInvalid from '../jsutils/isInvalid'; +import keyMap from '../jsutils/keyMap'; +import { Kind } from '../language/kinds'; +import { valueFromASTUntyped } from '../utilities/valueFromASTUntyped'; +export function isType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type) || isListType(type) || isNonNullType(type); +} +export function assertType(type) { + !isType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL type.")) : void 0; + return type; +} +/** + * There are predicates for each kind of GraphQL type. + */ + +// eslint-disable-next-line no-redeclare +export function isScalarType(type) { + return instanceOf(type, GraphQLScalarType); +} +export function assertScalarType(type) { + !isScalarType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Scalar type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isObjectType(type) { + return instanceOf(type, GraphQLObjectType); +} +export function assertObjectType(type) { + !isObjectType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Object type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isInterfaceType(type) { + return instanceOf(type, GraphQLInterfaceType); +} +export function assertInterfaceType(type) { + !isInterfaceType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Interface type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isUnionType(type) { + return instanceOf(type, GraphQLUnionType); +} +export function assertUnionType(type) { + !isUnionType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Union type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isEnumType(type) { + return instanceOf(type, GraphQLEnumType); +} +export function assertEnumType(type) { + !isEnumType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Enum type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isInputObjectType(type) { + return instanceOf(type, GraphQLInputObjectType); +} +export function assertInputObjectType(type) { + !isInputObjectType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Input Object type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isListType(type) { + return instanceOf(type, GraphQLList); +} +export function assertListType(type) { + !isListType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL List type.")) : void 0; + return type; +} +// eslint-disable-next-line no-redeclare +export function isNonNullType(type) { + return instanceOf(type, GraphQLNonNull); +} +export function assertNonNullType(type) { + !isNonNullType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL Non-Null type.")) : void 0; + return type; +} +/** + * These types may be used as input types for arguments and directives. + */ + +export function isInputType(type) { + return isScalarType(type) || isEnumType(type) || isInputObjectType(type) || isWrappingType(type) && isInputType(type.ofType); +} +export function assertInputType(type) { + !isInputType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL input type.")) : void 0; + return type; +} +/** + * These types may be used as output types as the result of fields. + */ + +export function isOutputType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isWrappingType(type) && isOutputType(type.ofType); +} +export function assertOutputType(type) { + !isOutputType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL output type.")) : void 0; + return type; +} +/** + * These types may describe types which may be leaf values. + */ + +export function isLeafType(type) { + return isScalarType(type) || isEnumType(type); +} +export function assertLeafType(type) { + !isLeafType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL leaf type.")) : void 0; + return type; +} +/** + * These types may describe the parent context of a selection set. + */ + +export function isCompositeType(type) { + return isObjectType(type) || isInterfaceType(type) || isUnionType(type); +} +export function assertCompositeType(type) { + !isCompositeType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL composite type.")) : void 0; + return type; +} +/** + * These types may describe the parent context of a selection set. + */ + +export function isAbstractType(type) { + return isInterfaceType(type) || isUnionType(type); +} +export function assertAbstractType(type) { + !isAbstractType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL abstract type.")) : void 0; + return type; +} +/** + * List Type Wrapper + * + * A list is a wrapping type which points to another type. + * Lists are often created within the context of defining the fields of + * an object type. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * parents: { type: GraphQLList(PersonType) }, + * children: { type: GraphQLList(PersonType) }, + * }) + * }) + * + */ + +// eslint-disable-next-line no-redeclare +export function GraphQLList(ofType) { + if (this instanceof GraphQLList) { + this.ofType = assertType(ofType); + } else { + return new GraphQLList(ofType); + } +} // Also provide toJSON and inspect aliases for toString. + +var listProto = GraphQLList.prototype; + +listProto.toString = listProto.toJSON = listProto.inspect = function toString() { + return '[' + String(this.ofType) + ']'; +}; +/** + * Non-Null Type Wrapper + * + * A non-null is a wrapping type which points to another type. + * Non-null types enforce that their values are never null and can ensure + * an error is raised if this ever occurs during a request. It is useful for + * fields which you can make a strong guarantee on non-nullability, for example + * usually the id field of a database row will never be null. + * + * Example: + * + * const RowType = new GraphQLObjectType({ + * name: 'Row', + * fields: () => ({ + * id: { type: GraphQLNonNull(GraphQLString) }, + * }) + * }) + * + * Note: the enforcement of non-nullability occurs within the executor. + */ + + +// eslint-disable-next-line no-redeclare +export function GraphQLNonNull(ofType) { + if (this instanceof GraphQLNonNull) { + this.ofType = assertNullableType(ofType); + } else { + return new GraphQLNonNull(ofType); + } +} // Also provide toJSON and inspect aliases for toString. + +var nonNullProto = GraphQLNonNull.prototype; + +nonNullProto.toString = nonNullProto.toJSON = nonNullProto.inspect = function toString() { + return String(this.ofType) + '!'; +}; +/** + * These types wrap and modify other types + */ + + +export function isWrappingType(type) { + return isListType(type) || isNonNullType(type); +} +export function assertWrappingType(type) { + !isWrappingType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL wrapping type.")) : void 0; + return type; +} +/** + * These types can all accept null as a value. + */ + +export function isNullableType(type) { + return isType(type) && !isNonNullType(type); +} +export function assertNullableType(type) { + !isNullableType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL nullable type.")) : void 0; + return type; +} +/* eslint-disable no-redeclare */ + +export function getNullableType(type) { + /* eslint-enable no-redeclare */ + if (type) { + return isNonNullType(type) ? type.ofType : type; + } +} +/** + * These named types do not include modifiers like List or NonNull. + */ + +export function isNamedType(type) { + return isScalarType(type) || isObjectType(type) || isInterfaceType(type) || isUnionType(type) || isEnumType(type) || isInputObjectType(type); +} +export function assertNamedType(type) { + !isNamedType(type) ? invariant(0, "Expected ".concat(String(type), " to be a GraphQL named type.")) : void 0; + return type; +} +/* eslint-disable no-redeclare */ + +export function getNamedType(type) { + /* eslint-enable no-redeclare */ + if (type) { + var unwrappedType = type; + + while (isWrappingType(unwrappedType)) { + unwrappedType = unwrappedType.ofType; + } + + return unwrappedType; + } +} +/** + * Used while defining GraphQL types to allow for circular references in + * otherwise immutable type definitions. + */ + +function resolveThunk(thunk) { + return typeof thunk === 'function' ? thunk() : thunk; +} +/** + * Scalar Type Definition + * + * The leaf values of any request and input values to arguments are + * Scalars (or Enums) and are defined with a name and a series of functions + * used to parse input from ast or variables and to ensure validity. + * + * If a type's serialize function does not return a value (i.e. it returns + * `undefined`) then an error will be raised and a `null` value will be returned + * in the response. If the serialize function returns `null`, then no error will + * be included in the response. + * + * Example: + * + * const OddType = new GraphQLScalarType({ + * name: 'Odd', + * serialize(value) { + * if (value % 2 === 1) { + * return value; + * } + * } + * }); + * + */ + + +export var GraphQLScalarType = +/*#__PURE__*/ +function () { + function GraphQLScalarType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_scalarConfig", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._scalarConfig = config; + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + !(typeof config.serialize === 'function') ? invariant(0, "".concat(this.name, " must provide \"serialize\" function. If this custom Scalar ") + 'is also used as an input type, ensure "parseValue" and "parseLiteral" ' + 'functions are also provided.') : void 0; + + if (config.parseValue || config.parseLiteral) { + !(typeof config.parseValue === 'function' && typeof config.parseLiteral === 'function') ? invariant(0, "".concat(this.name, " must provide both \"parseValue\" and \"parseLiteral\" ") + 'functions.') : void 0; + } + } // Serializes an internal value to include in a response. + + + var _proto = GraphQLScalarType.prototype; + + _proto.serialize = function serialize(value) { + var serializer = this._scalarConfig.serialize; + return serializer(value); + }; // Parses an externally provided value to use as an input. + + + _proto.parseValue = function parseValue(value) { + var parser = this._scalarConfig.parseValue; + + if (isInvalid(value)) { + return undefined; + } + + return parser ? parser(value) : value; + }; // Parses an externally provided literal value to use as an input. + + + _proto.parseLiteral = function parseLiteral(valueNode, variables) { + var parser = this._scalarConfig.parseLiteral; + return parser ? parser(valueNode, variables) : valueFromASTUntyped(valueNode, variables); + }; + + _proto.toString = function toString() { + return this.name; + }; + + return GraphQLScalarType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLScalarType.prototype.toJSON = GraphQLScalarType.prototype.inspect = GraphQLScalarType.prototype.toString; + +/** + * Object Type Definition + * + * Almost all of the GraphQL types you define will be object types. Object types + * have a name, but most importantly describe their fields. + * + * Example: + * + * const AddressType = new GraphQLObjectType({ + * name: 'Address', + * fields: { + * street: { type: GraphQLString }, + * number: { type: GraphQLInt }, + * formatted: { + * type: GraphQLString, + * resolve(obj) { + * return obj.number + ' ' + obj.street + * } + * } + * } + * }); + * + * When two types need to refer to each other, or a type needs to refer to + * itself in a field, you can use a function expression (aka a closure or a + * thunk) to supply the fields lazily. + * + * Example: + * + * const PersonType = new GraphQLObjectType({ + * name: 'Person', + * fields: () => ({ + * name: { type: GraphQLString }, + * bestFriend: { type: PersonType }, + * }) + * }); + * + */ +export var GraphQLObjectType = +/*#__PURE__*/ +function () { + function GraphQLObjectType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "isTypeOf", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "_interfaces", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.isTypeOf = config.isTypeOf; + this._typeConfig = config; + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + + if (config.isTypeOf) { + !(typeof config.isTypeOf === 'function') ? invariant(0, "".concat(this.name, " must provide \"isTypeOf\" as a function.")) : void 0; + } + } + + var _proto2 = GraphQLObjectType.prototype; + + _proto2.getFields = function getFields() { + return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields)); + }; + + _proto2.getInterfaces = function getInterfaces() { + return this._interfaces || (this._interfaces = defineInterfaces(this, this._typeConfig.interfaces)); + }; + + _proto2.toString = function toString() { + return this.name; + }; + + return GraphQLObjectType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLObjectType.prototype.toJSON = GraphQLObjectType.prototype.inspect = GraphQLObjectType.prototype.toString; + +function defineInterfaces(type, interfacesThunk) { + var interfaces = resolveThunk(interfacesThunk) || []; + !Array.isArray(interfaces) ? invariant(0, "".concat(type.name, " interfaces must be an Array or a function which returns ") + 'an Array.') : void 0; + return interfaces; +} + +function defineFieldMap(type, fieldsThunk) { + var fieldMap = resolveThunk(fieldsThunk) || {}; + !isPlainObj(fieldMap) ? invariant(0, "".concat(type.name, " fields must be an object with field names as keys or a ") + 'function which returns such an object.') : void 0; + var resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(function (fieldName) { + var fieldConfig = fieldMap[fieldName]; + !isPlainObj(fieldConfig) ? invariant(0, "".concat(type.name, ".").concat(fieldName, " field config must be an object")) : void 0; + !!fieldConfig.hasOwnProperty('isDeprecated') ? invariant(0, "".concat(type.name, ".").concat(fieldName, " should provide \"deprecationReason\" instead ") + 'of "isDeprecated".') : void 0; + + var field = _objectSpread({}, fieldConfig, { + isDeprecated: Boolean(fieldConfig.deprecationReason), + name: fieldName + }); + + !isValidResolver(field.resolve) ? invariant(0, "".concat(type.name, ".").concat(fieldName, " field resolver must be a function if ") + "provided, but got: ".concat(String(field.resolve), ".")) : void 0; + var argsConfig = fieldConfig.args; + + if (!argsConfig) { + field.args = []; + } else { + !isPlainObj(argsConfig) ? invariant(0, "".concat(type.name, ".").concat(fieldName, " args must be an object with argument ") + 'names as keys.') : void 0; + field.args = Object.keys(argsConfig).map(function (argName) { + var arg = argsConfig[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode + }; + }); + } + + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; +} + +function isPlainObj(obj) { + return obj && _typeof(obj) === 'object' && !Array.isArray(obj); +} // If a resolver is defined, it must be a function. + + +function isValidResolver(resolver) { + return resolver == null || typeof resolver === 'function'; +} + +/** + * Interface Type Definition + * + * When a field can return one of a heterogeneous set of types, a Interface type + * is used to describe what types are possible, what fields are in common across + * all types, as well as a function to determine which type is actually used + * when the field is resolved. + * + * Example: + * + * const EntityType = new GraphQLInterfaceType({ + * name: 'Entity', + * fields: { + * name: { type: GraphQLString } + * } + * }); + * + */ +export var GraphQLInterfaceType = +/*#__PURE__*/ +function () { + function GraphQLInterfaceType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "resolveType", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + this.resolveType = config.resolveType; + this._typeConfig = config; + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + + if (config.resolveType) { + !(typeof config.resolveType === 'function') ? invariant(0, "".concat(this.name, " must provide \"resolveType\" as a function.")) : void 0; + } + } + + var _proto3 = GraphQLInterfaceType.prototype; + + _proto3.getFields = function getFields() { + return this._fields || (this._fields = defineFieldMap(this, this._typeConfig.fields)); + }; + + _proto3.toString = function toString() { + return this.name; + }; + + return GraphQLInterfaceType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLInterfaceType.prototype.toJSON = GraphQLInterfaceType.prototype.inspect = GraphQLInterfaceType.prototype.toString; + +/** + * Union Type Definition + * + * When a field can return one of a heterogeneous set of types, a Union type + * is used to describe what types are possible as well as providing a function + * to determine which type is actually used when the field is resolved. + * + * Example: + * + * const PetType = new GraphQLUnionType({ + * name: 'Pet', + * types: [ DogType, CatType ], + * resolveType(value) { + * if (value instanceof Dog) { + * return DogType; + * } + * if (value instanceof Cat) { + * return CatType; + * } + * } + * }); + * + */ +export var GraphQLUnionType = +/*#__PURE__*/ +function () { + function GraphQLUnionType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "resolveType", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_types", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this.resolveType = config.resolveType; + this._typeConfig = config; + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + + if (config.resolveType) { + !(typeof config.resolveType === 'function') ? invariant(0, "".concat(this.name, " must provide \"resolveType\" as a function.")) : void 0; + } + } + + var _proto4 = GraphQLUnionType.prototype; + + _proto4.getTypes = function getTypes() { + return this._types || (this._types = defineTypes(this, this._typeConfig.types)); + }; + + _proto4.toString = function toString() { + return this.name; + }; + + return GraphQLUnionType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLUnionType.prototype.toJSON = GraphQLUnionType.prototype.inspect = GraphQLUnionType.prototype.toString; + +function defineTypes(unionType, typesThunk) { + var types = resolveThunk(typesThunk) || []; + !Array.isArray(types) ? invariant(0, 'Must provide Array of types or a function which returns ' + "such an array for Union ".concat(unionType.name, ".")) : void 0; + return types; +} + +/** + * Enum Type Definition + * + * Some leaf values of requests and input values are Enums. GraphQL serializes + * Enum values as strings, however internally Enums can be represented by any + * kind of type, often integers. + * + * Example: + * + * const RGBType = new GraphQLEnumType({ + * name: 'RGB', + * values: { + * RED: { value: 0 }, + * GREEN: { value: 1 }, + * BLUE: { value: 2 } + * } + * }); + * + * Note: If a value is not provided in a definition, the name of the enum value + * will be used as its internal value. + */ +export var GraphQLEnumType +/* */ += +/*#__PURE__*/ +function () { + function GraphQLEnumType(config + /* */ + ) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_values", void 0); + + _defineProperty(this, "_valueLookup", void 0); + + _defineProperty(this, "_nameLookup", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._values = defineEnumValues(this, config.values); + this._valueLookup = new Map(this._values.map(function (enumValue) { + return [enumValue.value, enumValue]; + })); + this._nameLookup = keyMap(this._values, function (value) { + return value.name; + }); + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + } + + var _proto5 = GraphQLEnumType.prototype; + + _proto5.getValues = function getValues() { + return this._values; + }; + + _proto5.getValue = function getValue(name) { + return this._nameLookup[name]; + }; + + _proto5.serialize = function serialize(value + /* T */ + ) { + var enumValue = this._valueLookup.get(value); + + if (enumValue) { + return enumValue.name; + } + }; + + _proto5.parseValue = function parseValue(value) + /* T */ + { + if (typeof value === 'string') { + var enumValue = this.getValue(value); + + if (enumValue) { + return enumValue.value; + } + } + }; + + _proto5.parseLiteral = function parseLiteral(valueNode, _variables) + /* T */ + { + // Note: variables will be resolved to a value before calling this function. + if (valueNode.kind === Kind.ENUM) { + var enumValue = this.getValue(valueNode.value); + + if (enumValue) { + return enumValue.value; + } + } + }; + + _proto5.toString = function toString() { + return this.name; + }; + + return GraphQLEnumType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLEnumType.prototype.toJSON = GraphQLEnumType.prototype.inspect = GraphQLEnumType.prototype.toString; + +function defineEnumValues(type, valueMap +/* */ +) { + !isPlainObj(valueMap) ? invariant(0, "".concat(type.name, " values must be an object with value names as keys.")) : void 0; + return Object.keys(valueMap).map(function (valueName) { + var value = valueMap[valueName]; + !isPlainObj(value) ? invariant(0, "".concat(type.name, ".").concat(valueName, " must refer to an object with a \"value\" key ") + "representing an internal value but got: ".concat(String(value), ".")) : void 0; + !!value.hasOwnProperty('isDeprecated') ? invariant(0, "".concat(type.name, ".").concat(valueName, " should provide \"deprecationReason\" instead ") + 'of "isDeprecated".') : void 0; + return { + name: valueName, + description: value.description, + isDeprecated: Boolean(value.deprecationReason), + deprecationReason: value.deprecationReason, + astNode: value.astNode, + value: value.hasOwnProperty('value') ? value.value : valueName + }; + }); +} + +/** + * Input Object Type Definition + * + * An input object defines a structured collection of fields which may be + * supplied to a field argument. + * + * Using `NonNull` will ensure that a value must be provided by the query + * + * Example: + * + * const GeoPoint = new GraphQLInputObjectType({ + * name: 'GeoPoint', + * fields: { + * lat: { type: GraphQLNonNull(GraphQLFloat) }, + * lon: { type: GraphQLNonNull(GraphQLFloat) }, + * alt: { type: GraphQLFloat, defaultValue: 0 }, + * } + * }); + * + */ +export var GraphQLInputObjectType = +/*#__PURE__*/ +function () { + function GraphQLInputObjectType(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "_typeConfig", void 0); + + _defineProperty(this, "_fields", void 0); + + _defineProperty(this, "toJSON", void 0); + + _defineProperty(this, "inspect", void 0); + + this.name = config.name; + this.description = config.description; + this.astNode = config.astNode; + this._typeConfig = config; + !(typeof config.name === 'string') ? invariant(0, 'Must provide name.') : void 0; + } + + var _proto6 = GraphQLInputObjectType.prototype; + + _proto6.getFields = function getFields() { + return this._fields || (this._fields = this._defineFieldMap()); + }; + + _proto6._defineFieldMap = function _defineFieldMap() { + var _this = this; + + var fieldMap = resolveThunk(this._typeConfig.fields) || {}; + !isPlainObj(fieldMap) ? invariant(0, "".concat(this.name, " fields must be an object with field names as keys or a ") + 'function which returns such an object.') : void 0; + var resultFieldMap = Object.create(null); + Object.keys(fieldMap).forEach(function (fieldName) { + var field = _objectSpread({}, fieldMap[fieldName], { + name: fieldName + }); + + !!field.hasOwnProperty('resolve') ? invariant(0, "".concat(_this.name, ".").concat(fieldName, " field type has a resolve property, but ") + 'Input Types cannot define resolvers.') : void 0; + resultFieldMap[fieldName] = field; + }); + return resultFieldMap; + }; + + _proto6.toString = function toString() { + return this.name; + }; + + return GraphQLInputObjectType; +}(); // Also provide toJSON and inspect aliases for toString. + +GraphQLInputObjectType.prototype.toJSON = GraphQLInputObjectType.prototype.toString; +GraphQLInputObjectType.prototype.inspect = GraphQLInputObjectType.prototype.toString; \ No newline at end of file diff --git a/dist/type/directives.js b/dist/type/directives.js new file mode 100644 index 0000000000..b00d53f392 --- /dev/null +++ b/dist/type/directives.js @@ -0,0 +1,137 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isDirective = isDirective; +exports.isSpecifiedDirective = isSpecifiedDirective; +exports.specifiedDirectives = exports.GraphQLDeprecatedDirective = exports.DEFAULT_DEPRECATION_REASON = exports.GraphQLSkipDirective = exports.GraphQLIncludeDirective = exports.GraphQLDirective = void 0; + +var _definition = require("./definition"); + +var _scalars = require("./scalars"); + +var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf")); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _directiveLocation = require("../language/directiveLocation"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +// eslint-disable-next-line no-redeclare +function isDirective(directive) { + return (0, _instanceOf.default)(directive, GraphQLDirective); +} +/** + * Directives are used by the GraphQL runtime as a way of modifying execution + * behavior. Type system creators will usually not create these directly. + */ + + +var GraphQLDirective = function GraphQLDirective(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "locations", void 0); + + _defineProperty(this, "args", void 0); + + _defineProperty(this, "astNode", void 0); + + this.name = config.name; + this.description = config.description; + this.locations = config.locations; + this.astNode = config.astNode; + !config.name ? (0, _invariant.default)(0, 'Directive must be named.') : void 0; + !Array.isArray(config.locations) ? (0, _invariant.default)(0, 'Must provide locations for directive.') : void 0; + var args = config.args; + + if (!args) { + this.args = []; + } else { + !!Array.isArray(args) ? (0, _invariant.default)(0, "@".concat(config.name, " args must be an object with argument names as keys.")) : void 0; + this.args = Object.keys(args).map(function (argName) { + var arg = args[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode + }; + }); + } +}; + +exports.GraphQLDirective = GraphQLDirective; + +/** + * Used to conditionally include fields or fragments. + */ +var GraphQLIncludeDirective = new GraphQLDirective({ + name: 'include', + description: 'Directs the executor to include this field or fragment only when ' + 'the `if` argument is true.', + locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + description: 'Included when true.' + } + } +}); +/** + * Used to conditionally skip (exclude) fields or fragments. + */ + +exports.GraphQLIncludeDirective = GraphQLIncludeDirective; +var GraphQLSkipDirective = new GraphQLDirective({ + name: 'skip', + description: 'Directs the executor to skip this field or fragment when the `if` ' + 'argument is true.', + locations: [_directiveLocation.DirectiveLocation.FIELD, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + description: 'Skipped when true.' + } + } +}); +/** + * Constant string used for default reason for a deprecation. + */ + +exports.GraphQLSkipDirective = GraphQLSkipDirective; +var DEFAULT_DEPRECATION_REASON = 'No longer supported'; +/** + * Used to declare element of a GraphQL schema as deprecated. + */ + +exports.DEFAULT_DEPRECATION_REASON = DEFAULT_DEPRECATION_REASON; +var GraphQLDeprecatedDirective = new GraphQLDirective({ + name: 'deprecated', + description: 'Marks an element of a GraphQL schema as no longer supported.', + locations: [_directiveLocation.DirectiveLocation.FIELD_DEFINITION, _directiveLocation.DirectiveLocation.ENUM_VALUE], + args: { + reason: { + type: _scalars.GraphQLString, + 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/).', + defaultValue: DEFAULT_DEPRECATION_REASON + } + } +}); +/** + * The full list of specified directives. + */ + +exports.GraphQLDeprecatedDirective = GraphQLDeprecatedDirective; +var specifiedDirectives = [GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]; +exports.specifiedDirectives = specifiedDirectives; + +function isSpecifiedDirective(directive) { + return specifiedDirectives.some(function (specifiedDirective) { + return specifiedDirective.name === directive.name; + }); +} \ No newline at end of file diff --git a/dist/type/directives.js.flow b/dist/type/directives.js.flow new file mode 100644 index 0000000000..9d4d7764f8 --- /dev/null +++ b/dist/type/directives.js.flow @@ -0,0 +1,168 @@ +/** + * 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. + * + * @flow strict + */ + +import type { + GraphQLFieldConfigArgumentMap, + GraphQLArgument, +} from './definition'; +import { GraphQLNonNull } from './definition'; +import { GraphQLString, GraphQLBoolean } from './scalars'; +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import type { DirectiveDefinitionNode } from '../language/ast'; +import { + DirectiveLocation, + type DirectiveLocationEnum, +} from '../language/directiveLocation'; + +/** + * Test if the given value is a GraphQL directive. + */ +declare function isDirective( + directive: mixed, +): boolean %checks(directive instanceof GraphQLDirective); +// eslint-disable-next-line no-redeclare +export function isDirective(directive) { + return instanceOf(directive, GraphQLDirective); +} + +/** + * Directives are used by the GraphQL runtime as a way of modifying execution + * behavior. Type system creators will usually not create these directly. + */ +export class GraphQLDirective { + name: string; + description: ?string; + locations: Array; + args: Array; + astNode: ?DirectiveDefinitionNode; + + constructor(config: GraphQLDirectiveConfig): void { + this.name = config.name; + this.description = config.description; + this.locations = config.locations; + this.astNode = config.astNode; + invariant(config.name, 'Directive must be named.'); + invariant( + Array.isArray(config.locations), + 'Must provide locations for directive.', + ); + + const args = config.args; + if (!args) { + this.args = []; + } else { + invariant( + !Array.isArray(args), + `@${config.name} args must be an object with argument names as keys.`, + ); + this.args = Object.keys(args).map(argName => { + const arg = args[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode, + }; + }); + } + } +} + +export type GraphQLDirectiveConfig = { + name: string, + description?: ?string, + locations: Array, + args?: ?GraphQLFieldConfigArgumentMap, + astNode?: ?DirectiveDefinitionNode, +}; + +/** + * Used to conditionally include fields or fragments. + */ +export const GraphQLIncludeDirective = new GraphQLDirective({ + name: 'include', + description: + 'Directs the executor to include this field or fragment only when ' + + 'the `if` argument is true.', + locations: [ + DirectiveLocation.FIELD, + DirectiveLocation.FRAGMENT_SPREAD, + DirectiveLocation.INLINE_FRAGMENT, + ], + args: { + if: { + type: GraphQLNonNull(GraphQLBoolean), + description: 'Included when true.', + }, + }, +}); + +/** + * Used to conditionally skip (exclude) fields or fragments. + */ +export const GraphQLSkipDirective = new GraphQLDirective({ + name: 'skip', + description: + 'Directs the executor to skip this field or fragment when the `if` ' + + 'argument is true.', + locations: [ + DirectiveLocation.FIELD, + DirectiveLocation.FRAGMENT_SPREAD, + DirectiveLocation.INLINE_FRAGMENT, + ], + args: { + if: { + type: GraphQLNonNull(GraphQLBoolean), + description: 'Skipped when true.', + }, + }, +}); + +/** + * Constant string used for default reason for a deprecation. + */ +export const DEFAULT_DEPRECATION_REASON = 'No longer supported'; + +/** + * Used to declare element of a GraphQL schema as deprecated. + */ +export const GraphQLDeprecatedDirective = new GraphQLDirective({ + name: 'deprecated', + description: 'Marks an element of a GraphQL schema as no longer supported.', + locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE], + args: { + reason: { + type: GraphQLString, + 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/).', + defaultValue: DEFAULT_DEPRECATION_REASON, + }, + }, +}); + +/** + * The full list of specified directives. + */ +export const specifiedDirectives: $ReadOnlyArray<*> = [ + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, +]; + +export function isSpecifiedDirective( + directive: GraphQLDirective, +): boolean %checks { + return specifiedDirectives.some( + specifiedDirective => specifiedDirective.name === directive.name, + ); +} diff --git a/dist/type/directives.mjs b/dist/type/directives.mjs new file mode 100644 index 0000000000..ae9bbe25ed --- /dev/null +++ b/dist/type/directives.mjs @@ -0,0 +1,124 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { GraphQLNonNull } from './definition'; +import { GraphQLString, GraphQLBoolean } from './scalars'; +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import { DirectiveLocation } from '../language/directiveLocation'; +/** + * Test if the given value is a GraphQL directive. + */ + +// eslint-disable-next-line no-redeclare +export function isDirective(directive) { + return instanceOf(directive, GraphQLDirective); +} +/** + * Directives are used by the GraphQL runtime as a way of modifying execution + * behavior. Type system creators will usually not create these directly. + */ + +export var GraphQLDirective = function GraphQLDirective(config) { + _defineProperty(this, "name", void 0); + + _defineProperty(this, "description", void 0); + + _defineProperty(this, "locations", void 0); + + _defineProperty(this, "args", void 0); + + _defineProperty(this, "astNode", void 0); + + this.name = config.name; + this.description = config.description; + this.locations = config.locations; + this.astNode = config.astNode; + !config.name ? invariant(0, 'Directive must be named.') : void 0; + !Array.isArray(config.locations) ? invariant(0, 'Must provide locations for directive.') : void 0; + var args = config.args; + + if (!args) { + this.args = []; + } else { + !!Array.isArray(args) ? invariant(0, "@".concat(config.name, " args must be an object with argument names as keys.")) : void 0; + this.args = Object.keys(args).map(function (argName) { + var arg = args[argName]; + return { + name: argName, + description: arg.description === undefined ? null : arg.description, + type: arg.type, + defaultValue: arg.defaultValue, + astNode: arg.astNode + }; + }); + } +}; + +/** + * Used to conditionally include fields or fragments. + */ +export var GraphQLIncludeDirective = new GraphQLDirective({ + name: 'include', + description: 'Directs the executor to include this field or fragment only when ' + 'the `if` argument is true.', + locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: GraphQLNonNull(GraphQLBoolean), + description: 'Included when true.' + } + } +}); +/** + * Used to conditionally skip (exclude) fields or fragments. + */ + +export var GraphQLSkipDirective = new GraphQLDirective({ + name: 'skip', + description: 'Directs the executor to skip this field or fragment when the `if` ' + 'argument is true.', + locations: [DirectiveLocation.FIELD, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT], + args: { + if: { + type: GraphQLNonNull(GraphQLBoolean), + description: 'Skipped when true.' + } + } +}); +/** + * Constant string used for default reason for a deprecation. + */ + +export var DEFAULT_DEPRECATION_REASON = 'No longer supported'; +/** + * Used to declare element of a GraphQL schema as deprecated. + */ + +export var GraphQLDeprecatedDirective = new GraphQLDirective({ + name: 'deprecated', + description: 'Marks an element of a GraphQL schema as no longer supported.', + locations: [DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.ENUM_VALUE], + args: { + reason: { + type: GraphQLString, + 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/).', + defaultValue: DEFAULT_DEPRECATION_REASON + } + } +}); +/** + * The full list of specified directives. + */ + +export var specifiedDirectives = [GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective]; +export function isSpecifiedDirective(directive) { + return specifiedDirectives.some(function (specifiedDirective) { + return specifiedDirective.name === directive.name; + }); +} \ No newline at end of file diff --git a/dist/type/index.js b/dist/type/index.js new file mode 100644 index 0000000000..fa73403960 --- /dev/null +++ b/dist/type/index.js @@ -0,0 +1,479 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "isSchema", { + enumerable: true, + get: function get() { + return _schema.isSchema; + } +}); +Object.defineProperty(exports, "GraphQLSchema", { + enumerable: true, + get: function get() { + return _schema.GraphQLSchema; + } +}); +Object.defineProperty(exports, "isType", { + enumerable: true, + get: function get() { + return _definition.isType; + } +}); +Object.defineProperty(exports, "isScalarType", { + enumerable: true, + get: function get() { + return _definition.isScalarType; + } +}); +Object.defineProperty(exports, "isObjectType", { + enumerable: true, + get: function get() { + return _definition.isObjectType; + } +}); +Object.defineProperty(exports, "isInterfaceType", { + enumerable: true, + get: function get() { + return _definition.isInterfaceType; + } +}); +Object.defineProperty(exports, "isUnionType", { + enumerable: true, + get: function get() { + return _definition.isUnionType; + } +}); +Object.defineProperty(exports, "isEnumType", { + enumerable: true, + get: function get() { + return _definition.isEnumType; + } +}); +Object.defineProperty(exports, "isInputObjectType", { + enumerable: true, + get: function get() { + return _definition.isInputObjectType; + } +}); +Object.defineProperty(exports, "isListType", { + enumerable: true, + get: function get() { + return _definition.isListType; + } +}); +Object.defineProperty(exports, "isNonNullType", { + enumerable: true, + get: function get() { + return _definition.isNonNullType; + } +}); +Object.defineProperty(exports, "isInputType", { + enumerable: true, + get: function get() { + return _definition.isInputType; + } +}); +Object.defineProperty(exports, "isOutputType", { + enumerable: true, + get: function get() { + return _definition.isOutputType; + } +}); +Object.defineProperty(exports, "isLeafType", { + enumerable: true, + get: function get() { + return _definition.isLeafType; + } +}); +Object.defineProperty(exports, "isCompositeType", { + enumerable: true, + get: function get() { + return _definition.isCompositeType; + } +}); +Object.defineProperty(exports, "isAbstractType", { + enumerable: true, + get: function get() { + return _definition.isAbstractType; + } +}); +Object.defineProperty(exports, "isWrappingType", { + enumerable: true, + get: function get() { + return _definition.isWrappingType; + } +}); +Object.defineProperty(exports, "isNullableType", { + enumerable: true, + get: function get() { + return _definition.isNullableType; + } +}); +Object.defineProperty(exports, "isNamedType", { + enumerable: true, + get: function get() { + return _definition.isNamedType; + } +}); +Object.defineProperty(exports, "assertType", { + enumerable: true, + get: function get() { + return _definition.assertType; + } +}); +Object.defineProperty(exports, "assertScalarType", { + enumerable: true, + get: function get() { + return _definition.assertScalarType; + } +}); +Object.defineProperty(exports, "assertObjectType", { + enumerable: true, + get: function get() { + return _definition.assertObjectType; + } +}); +Object.defineProperty(exports, "assertInterfaceType", { + enumerable: true, + get: function get() { + return _definition.assertInterfaceType; + } +}); +Object.defineProperty(exports, "assertUnionType", { + enumerable: true, + get: function get() { + return _definition.assertUnionType; + } +}); +Object.defineProperty(exports, "assertEnumType", { + enumerable: true, + get: function get() { + return _definition.assertEnumType; + } +}); +Object.defineProperty(exports, "assertInputObjectType", { + enumerable: true, + get: function get() { + return _definition.assertInputObjectType; + } +}); +Object.defineProperty(exports, "assertListType", { + enumerable: true, + get: function get() { + return _definition.assertListType; + } +}); +Object.defineProperty(exports, "assertNonNullType", { + enumerable: true, + get: function get() { + return _definition.assertNonNullType; + } +}); +Object.defineProperty(exports, "assertInputType", { + enumerable: true, + get: function get() { + return _definition.assertInputType; + } +}); +Object.defineProperty(exports, "assertOutputType", { + enumerable: true, + get: function get() { + return _definition.assertOutputType; + } +}); +Object.defineProperty(exports, "assertLeafType", { + enumerable: true, + get: function get() { + return _definition.assertLeafType; + } +}); +Object.defineProperty(exports, "assertCompositeType", { + enumerable: true, + get: function get() { + return _definition.assertCompositeType; + } +}); +Object.defineProperty(exports, "assertAbstractType", { + enumerable: true, + get: function get() { + return _definition.assertAbstractType; + } +}); +Object.defineProperty(exports, "assertWrappingType", { + enumerable: true, + get: function get() { + return _definition.assertWrappingType; + } +}); +Object.defineProperty(exports, "assertNullableType", { + enumerable: true, + get: function get() { + return _definition.assertNullableType; + } +}); +Object.defineProperty(exports, "assertNamedType", { + enumerable: true, + get: function get() { + return _definition.assertNamedType; + } +}); +Object.defineProperty(exports, "getNullableType", { + enumerable: true, + get: function get() { + return _definition.getNullableType; + } +}); +Object.defineProperty(exports, "getNamedType", { + enumerable: true, + get: function get() { + return _definition.getNamedType; + } +}); +Object.defineProperty(exports, "GraphQLScalarType", { + enumerable: true, + get: function get() { + return _definition.GraphQLScalarType; + } +}); +Object.defineProperty(exports, "GraphQLObjectType", { + enumerable: true, + get: function get() { + return _definition.GraphQLObjectType; + } +}); +Object.defineProperty(exports, "GraphQLInterfaceType", { + enumerable: true, + get: function get() { + return _definition.GraphQLInterfaceType; + } +}); +Object.defineProperty(exports, "GraphQLUnionType", { + enumerable: true, + get: function get() { + return _definition.GraphQLUnionType; + } +}); +Object.defineProperty(exports, "GraphQLEnumType", { + enumerable: true, + get: function get() { + return _definition.GraphQLEnumType; + } +}); +Object.defineProperty(exports, "GraphQLInputObjectType", { + enumerable: true, + get: function get() { + return _definition.GraphQLInputObjectType; + } +}); +Object.defineProperty(exports, "GraphQLList", { + enumerable: true, + get: function get() { + return _definition.GraphQLList; + } +}); +Object.defineProperty(exports, "GraphQLNonNull", { + enumerable: true, + get: function get() { + return _definition.GraphQLNonNull; + } +}); +Object.defineProperty(exports, "isDirective", { + enumerable: true, + get: function get() { + return _directives.isDirective; + } +}); +Object.defineProperty(exports, "GraphQLDirective", { + enumerable: true, + get: function get() { + return _directives.GraphQLDirective; + } +}); +Object.defineProperty(exports, "isSpecifiedDirective", { + enumerable: true, + get: function get() { + return _directives.isSpecifiedDirective; + } +}); +Object.defineProperty(exports, "specifiedDirectives", { + enumerable: true, + get: function get() { + return _directives.specifiedDirectives; + } +}); +Object.defineProperty(exports, "GraphQLIncludeDirective", { + enumerable: true, + get: function get() { + return _directives.GraphQLIncludeDirective; + } +}); +Object.defineProperty(exports, "GraphQLSkipDirective", { + enumerable: true, + get: function get() { + return _directives.GraphQLSkipDirective; + } +}); +Object.defineProperty(exports, "GraphQLDeprecatedDirective", { + enumerable: true, + get: function get() { + return _directives.GraphQLDeprecatedDirective; + } +}); +Object.defineProperty(exports, "DEFAULT_DEPRECATION_REASON", { + enumerable: true, + get: function get() { + return _directives.DEFAULT_DEPRECATION_REASON; + } +}); +Object.defineProperty(exports, "isSpecifiedScalarType", { + enumerable: true, + get: function get() { + return _scalars.isSpecifiedScalarType; + } +}); +Object.defineProperty(exports, "specifiedScalarTypes", { + enumerable: true, + get: function get() { + return _scalars.specifiedScalarTypes; + } +}); +Object.defineProperty(exports, "GraphQLInt", { + enumerable: true, + get: function get() { + return _scalars.GraphQLInt; + } +}); +Object.defineProperty(exports, "GraphQLFloat", { + enumerable: true, + get: function get() { + return _scalars.GraphQLFloat; + } +}); +Object.defineProperty(exports, "GraphQLString", { + enumerable: true, + get: function get() { + return _scalars.GraphQLString; + } +}); +Object.defineProperty(exports, "GraphQLBoolean", { + enumerable: true, + get: function get() { + return _scalars.GraphQLBoolean; + } +}); +Object.defineProperty(exports, "GraphQLID", { + enumerable: true, + get: function get() { + return _scalars.GraphQLID; + } +}); +Object.defineProperty(exports, "TypeKind", { + enumerable: true, + get: function get() { + return _introspection.TypeKind; + } +}); +Object.defineProperty(exports, "isIntrospectionType", { + enumerable: true, + get: function get() { + return _introspection.isIntrospectionType; + } +}); +Object.defineProperty(exports, "introspectionTypes", { + enumerable: true, + get: function get() { + return _introspection.introspectionTypes; + } +}); +Object.defineProperty(exports, "__Schema", { + enumerable: true, + get: function get() { + return _introspection.__Schema; + } +}); +Object.defineProperty(exports, "__Directive", { + enumerable: true, + get: function get() { + return _introspection.__Directive; + } +}); +Object.defineProperty(exports, "__DirectiveLocation", { + enumerable: true, + get: function get() { + return _introspection.__DirectiveLocation; + } +}); +Object.defineProperty(exports, "__Type", { + enumerable: true, + get: function get() { + return _introspection.__Type; + } +}); +Object.defineProperty(exports, "__Field", { + enumerable: true, + get: function get() { + return _introspection.__Field; + } +}); +Object.defineProperty(exports, "__InputValue", { + enumerable: true, + get: function get() { + return _introspection.__InputValue; + } +}); +Object.defineProperty(exports, "__EnumValue", { + enumerable: true, + get: function get() { + return _introspection.__EnumValue; + } +}); +Object.defineProperty(exports, "__TypeKind", { + enumerable: true, + get: function get() { + return _introspection.__TypeKind; + } +}); +Object.defineProperty(exports, "SchemaMetaFieldDef", { + enumerable: true, + get: function get() { + return _introspection.SchemaMetaFieldDef; + } +}); +Object.defineProperty(exports, "TypeMetaFieldDef", { + enumerable: true, + get: function get() { + return _introspection.TypeMetaFieldDef; + } +}); +Object.defineProperty(exports, "TypeNameMetaFieldDef", { + enumerable: true, + get: function get() { + return _introspection.TypeNameMetaFieldDef; + } +}); +Object.defineProperty(exports, "validateSchema", { + enumerable: true, + get: function get() { + return _validate.validateSchema; + } +}); +Object.defineProperty(exports, "assertValidSchema", { + enumerable: true, + get: function get() { + return _validate.assertValidSchema; + } +}); + +var _schema = require("./schema"); + +var _definition = require("./definition"); + +var _directives = require("./directives"); + +var _scalars = require("./scalars"); + +var _introspection = require("./introspection"); + +var _validate = require("./validate"); \ No newline at end of file diff --git a/dist/type/index.js.flow b/dist/type/index.js.flow new file mode 100644 index 0000000000..f7ac4fd8cd --- /dev/null +++ b/dist/type/index.js.flow @@ -0,0 +1,157 @@ +/** + * 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. + * + * @flow strict + */ + +export { + // Predicate + isSchema, + // GraphQL Schema definition + GraphQLSchema, +} from './schema'; + +export type { GraphQLSchemaConfig } from './schema'; + +export { + // Predicates + isType, + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, + isInputType, + isOutputType, + isLeafType, + isCompositeType, + isAbstractType, + isWrappingType, + isNullableType, + isNamedType, + // Assertions + assertType, + assertScalarType, + assertObjectType, + assertInterfaceType, + assertUnionType, + assertEnumType, + assertInputObjectType, + assertListType, + assertNonNullType, + assertInputType, + assertOutputType, + assertLeafType, + assertCompositeType, + assertAbstractType, + assertWrappingType, + assertNullableType, + assertNamedType, + // Un-modifiers + getNullableType, + getNamedType, + // Definitions + GraphQLScalarType, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + // Type Wrappers + GraphQLList, + GraphQLNonNull, +} from './definition'; + +export { + // Predicate + isDirective, + // Directives Definition + GraphQLDirective, + // Built-in Directives defined by the Spec + isSpecifiedDirective, + specifiedDirectives, + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, + // Constant Deprecation Reason + DEFAULT_DEPRECATION_REASON, +} from './directives'; + +export type { GraphQLDirectiveConfig } from './directives'; + +// Common built-in scalar instances. +export { + isSpecifiedScalarType, + specifiedScalarTypes, + GraphQLInt, + GraphQLFloat, + GraphQLString, + GraphQLBoolean, + GraphQLID, +} from './scalars'; + +export { + // "Enum" of Type Kinds + TypeKind, + // GraphQL Types for introspection. + isIntrospectionType, + introspectionTypes, + __Schema, + __Directive, + __DirectiveLocation, + __Type, + __Field, + __InputValue, + __EnumValue, + __TypeKind, + // Meta-field definitions. + SchemaMetaFieldDef, + TypeMetaFieldDef, + TypeNameMetaFieldDef, +} from './introspection'; + +export type { + GraphQLType, + GraphQLInputType, + GraphQLOutputType, + GraphQLLeafType, + GraphQLCompositeType, + GraphQLAbstractType, + GraphQLWrappingType, + GraphQLNullableType, + GraphQLNamedType, + Thunk, + GraphQLArgument, + GraphQLArgumentConfig, + GraphQLEnumTypeConfig, + GraphQLEnumValue, + GraphQLEnumValueConfig, + GraphQLEnumValueConfigMap, + GraphQLField, + GraphQLFieldConfig, + GraphQLFieldConfigArgumentMap, + GraphQLFieldConfigMap, + GraphQLFieldMap, + GraphQLFieldResolver, + GraphQLInputField, + GraphQLInputFieldConfig, + GraphQLInputFieldConfigMap, + GraphQLInputFieldMap, + GraphQLInputObjectTypeConfig, + GraphQLInterfaceTypeConfig, + GraphQLIsTypeOfFn, + GraphQLObjectTypeConfig, + GraphQLResolveInfo, + ResponsePath, + GraphQLScalarTypeConfig, + GraphQLTypeResolver, + GraphQLUnionTypeConfig, +} from './definition'; + +export { validateSchema, assertValidSchema } from './validate'; diff --git a/dist/type/index.mjs b/dist/type/index.mjs new file mode 100644 index 0000000000..2a245ee219 --- /dev/null +++ b/dist/type/index.mjs @@ -0,0 +1,29 @@ +/** + * 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. + * + * strict + */ +export { // Predicate +isSchema, // GraphQL Schema definition +GraphQLSchema } from './schema'; +export { // Predicates +isType, isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isListType, isNonNullType, isInputType, isOutputType, isLeafType, isCompositeType, isAbstractType, isWrappingType, isNullableType, isNamedType, // Assertions +assertType, assertScalarType, assertObjectType, assertInterfaceType, assertUnionType, assertEnumType, assertInputObjectType, assertListType, assertNonNullType, assertInputType, assertOutputType, assertLeafType, assertCompositeType, assertAbstractType, assertWrappingType, assertNullableType, assertNamedType, // Un-modifiers +getNullableType, getNamedType, // Definitions +GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, // Type Wrappers +GraphQLList, GraphQLNonNull } from './definition'; +export { // Predicate +isDirective, // Directives Definition +GraphQLDirective, // Built-in Directives defined by the Spec +isSpecifiedDirective, specifiedDirectives, GraphQLIncludeDirective, GraphQLSkipDirective, GraphQLDeprecatedDirective, // Constant Deprecation Reason +DEFAULT_DEPRECATION_REASON } from './directives'; +// Common built-in scalar instances. +export { isSpecifiedScalarType, specifiedScalarTypes, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean, GraphQLID } from './scalars'; +export { // "Enum" of Type Kinds +TypeKind, // GraphQL Types for introspection. +isIntrospectionType, introspectionTypes, __Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue, __EnumValue, __TypeKind, // Meta-field definitions. +SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from './introspection'; +export { validateSchema, assertValidSchema } from './validate'; \ No newline at end of file diff --git a/dist/type/introspection.js b/dist/type/introspection.js new file mode 100644 index 0000000000..db2a123a5f --- /dev/null +++ b/dist/type/introspection.js @@ -0,0 +1,573 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isIntrospectionType = isIntrospectionType; +exports.introspectionTypes = exports.TypeNameMetaFieldDef = exports.TypeMetaFieldDef = exports.SchemaMetaFieldDef = exports.__TypeKind = exports.TypeKind = exports.__EnumValue = exports.__InputValue = exports.__Field = exports.__Type = exports.__DirectiveLocation = exports.__Directive = exports.__Schema = void 0; + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _astFromValue = require("../utilities/astFromValue"); + +var _printer = require("../language/printer"); + +var _definition = require("./definition"); + +var _scalars = require("./scalars"); + +var _directiveLocation = require("../language/directiveLocation"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +var __Schema = new _definition.GraphQLObjectType({ + name: '__Schema', + isIntrospection: true, + description: 'A GraphQL Schema defines the capabilities of a GraphQL server. It ' + 'exposes all available types and directives on the server, as well as ' + 'the entry points for query, mutation, and subscription operations.', + fields: function fields() { + return { + types: { + description: 'A list of all types supported by this server.', + type: (0, _definition.GraphQLNonNull)((0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__Type))), + resolve: function resolve(schema) { + return (0, _objectValues.default)(schema.getTypeMap()); + } + }, + queryType: { + description: 'The type that query operations will be rooted at.', + type: (0, _definition.GraphQLNonNull)(__Type), + resolve: function resolve(schema) { + return schema.getQueryType(); + } + }, + mutationType: { + description: 'If this server supports mutation, the type that ' + 'mutation operations will be rooted at.', + type: __Type, + resolve: function resolve(schema) { + return schema.getMutationType(); + } + }, + subscriptionType: { + description: 'If this server support subscription, the type that ' + 'subscription operations will be rooted at.', + type: __Type, + resolve: function resolve(schema) { + return schema.getSubscriptionType(); + } + }, + directives: { + description: 'A list of all directives supported by this server.', + type: (0, _definition.GraphQLNonNull)((0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__Directive))), + resolve: function resolve(schema) { + return schema.getDirectives(); + } + } + }; + } +}); + +exports.__Schema = __Schema; + +var __Directive = new _definition.GraphQLObjectType({ + name: '__Directive', + isIntrospection: true, + description: 'A Directive provides a way to describe alternate runtime execution and ' + 'type validation behavior in a GraphQL document.' + "\n\nIn some cases, you need to provide options to alter GraphQL's " + 'execution behavior in ways field arguments will not suffice, such as ' + 'conditionally including or skipping a field. Directives provide this by ' + 'describing additional information to the executor.', + fields: function fields() { + return { + name: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + locations: { + type: (0, _definition.GraphQLNonNull)((0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__DirectiveLocation))), + resolve: function resolve(obj) { + return obj.locations; + } + }, + args: { + type: (0, _definition.GraphQLNonNull)((0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__InputValue))), + resolve: function resolve(directive) { + return directive.args || []; + } + }, + // NOTE: the following three fields are deprecated and are no longer part + // of the GraphQL specification. + onOperation: { + deprecationReason: 'Use `locations`.', + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(_directiveLocation.DirectiveLocation.QUERY) !== -1 || d.locations.indexOf(_directiveLocation.DirectiveLocation.MUTATION) !== -1 || d.locations.indexOf(_directiveLocation.DirectiveLocation.SUBSCRIPTION) !== -1; + } + }, + onFragment: { + deprecationReason: 'Use `locations`.', + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(_directiveLocation.DirectiveLocation.FRAGMENT_SPREAD) !== -1 || d.locations.indexOf(_directiveLocation.DirectiveLocation.INLINE_FRAGMENT) !== -1 || d.locations.indexOf(_directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION) !== -1; + } + }, + onField: { + deprecationReason: 'Use `locations`.', + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(_directiveLocation.DirectiveLocation.FIELD) !== -1; + } + } + }; + } +}); + +exports.__Directive = __Directive; + +var __DirectiveLocation = new _definition.GraphQLEnumType({ + name: '__DirectiveLocation', + isIntrospection: true, + description: 'A Directive can be adjacent to many parts of the GraphQL language, a ' + '__DirectiveLocation describes one such possible adjacencies.', + values: { + QUERY: { + value: _directiveLocation.DirectiveLocation.QUERY, + description: 'Location adjacent to a query operation.' + }, + MUTATION: { + value: _directiveLocation.DirectiveLocation.MUTATION, + description: 'Location adjacent to a mutation operation.' + }, + SUBSCRIPTION: { + value: _directiveLocation.DirectiveLocation.SUBSCRIPTION, + description: 'Location adjacent to a subscription operation.' + }, + FIELD: { + value: _directiveLocation.DirectiveLocation.FIELD, + description: 'Location adjacent to a field.' + }, + FRAGMENT_DEFINITION: { + value: _directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION, + description: 'Location adjacent to a fragment definition.' + }, + FRAGMENT_SPREAD: { + value: _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, + description: 'Location adjacent to a fragment spread.' + }, + INLINE_FRAGMENT: { + value: _directiveLocation.DirectiveLocation.INLINE_FRAGMENT, + description: 'Location adjacent to an inline fragment.' + }, + SCHEMA: { + value: _directiveLocation.DirectiveLocation.SCHEMA, + description: 'Location adjacent to a schema definition.' + }, + SCALAR: { + value: _directiveLocation.DirectiveLocation.SCALAR, + description: 'Location adjacent to a scalar definition.' + }, + OBJECT: { + value: _directiveLocation.DirectiveLocation.OBJECT, + description: 'Location adjacent to an object type definition.' + }, + FIELD_DEFINITION: { + value: _directiveLocation.DirectiveLocation.FIELD_DEFINITION, + description: 'Location adjacent to a field definition.' + }, + ARGUMENT_DEFINITION: { + value: _directiveLocation.DirectiveLocation.ARGUMENT_DEFINITION, + description: 'Location adjacent to an argument definition.' + }, + INTERFACE: { + value: _directiveLocation.DirectiveLocation.INTERFACE, + description: 'Location adjacent to an interface definition.' + }, + UNION: { + value: _directiveLocation.DirectiveLocation.UNION, + description: 'Location adjacent to a union definition.' + }, + ENUM: { + value: _directiveLocation.DirectiveLocation.ENUM, + description: 'Location adjacent to an enum definition.' + }, + ENUM_VALUE: { + value: _directiveLocation.DirectiveLocation.ENUM_VALUE, + description: 'Location adjacent to an enum value definition.' + }, + INPUT_OBJECT: { + value: _directiveLocation.DirectiveLocation.INPUT_OBJECT, + description: 'Location adjacent to an input object type definition.' + }, + INPUT_FIELD_DEFINITION: { + value: _directiveLocation.DirectiveLocation.INPUT_FIELD_DEFINITION, + description: 'Location adjacent to an input object field definition.' + } + } +}); + +exports.__DirectiveLocation = __DirectiveLocation; + +var __Type = new _definition.GraphQLObjectType({ + name: '__Type', + isIntrospection: true, + description: 'The fundamental unit of any GraphQL Schema is the type. There are ' + 'many kinds of types in GraphQL as represented by the `__TypeKind` enum.' + '\n\nDepending on the kind of a type, certain fields describe ' + 'information about that type. Scalar types provide no information ' + 'beyond a name and description, while Enum types provide their values. ' + 'Object and Interface types provide the fields they describe. Abstract ' + 'types, Union and Interface, provide the Object types possible ' + 'at runtime. List and NonNull types compose other types.', + fields: function fields() { + return { + kind: { + type: (0, _definition.GraphQLNonNull)(__TypeKind), + resolve: function resolve(type) { + if ((0, _definition.isScalarType)(type)) { + return TypeKind.SCALAR; + } else if ((0, _definition.isObjectType)(type)) { + return TypeKind.OBJECT; + } else if ((0, _definition.isInterfaceType)(type)) { + return TypeKind.INTERFACE; + } else if ((0, _definition.isUnionType)(type)) { + return TypeKind.UNION; + } else if ((0, _definition.isEnumType)(type)) { + return TypeKind.ENUM; + } else if ((0, _definition.isInputObjectType)(type)) { + return TypeKind.INPUT_OBJECT; + } else if ((0, _definition.isListType)(type)) { + return TypeKind.LIST; + } else if ((0, _definition.isNonNullType)(type)) { + return TypeKind.NON_NULL; + } + + throw new Error('Unknown kind of type: ' + type); + } + }, + name: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + fields: { + type: (0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__Field)), + args: { + includeDeprecated: { + type: _scalars.GraphQLBoolean, + defaultValue: false + } + }, + resolve: function resolve(type, _ref) { + var includeDeprecated = _ref.includeDeprecated; + + if ((0, _definition.isObjectType)(type) || (0, _definition.isInterfaceType)(type)) { + var fields = (0, _objectValues.default)(type.getFields()); + + if (!includeDeprecated) { + fields = fields.filter(function (field) { + return !field.deprecationReason; + }); + } + + return fields; + } + + return null; + } + }, + interfaces: { + type: (0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__Type)), + resolve: function resolve(type) { + if ((0, _definition.isObjectType)(type)) { + return type.getInterfaces(); + } + } + }, + possibleTypes: { + type: (0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__Type)), + resolve: function resolve(type, args, context, _ref2) { + var schema = _ref2.schema; + + if ((0, _definition.isAbstractType)(type)) { + return schema.getPossibleTypes(type); + } + } + }, + enumValues: { + type: (0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__EnumValue)), + args: { + includeDeprecated: { + type: _scalars.GraphQLBoolean, + defaultValue: false + } + }, + resolve: function resolve(type, _ref3) { + var includeDeprecated = _ref3.includeDeprecated; + + if ((0, _definition.isEnumType)(type)) { + var values = type.getValues(); + + if (!includeDeprecated) { + values = values.filter(function (value) { + return !value.deprecationReason; + }); + } + + return values; + } + } + }, + inputFields: { + type: (0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__InputValue)), + resolve: function resolve(type) { + if ((0, _definition.isInputObjectType)(type)) { + return (0, _objectValues.default)(type.getFields()); + } + } + }, + ofType: { + type: __Type, + resolve: function resolve(obj) { + return obj.ofType; + } + } + }; + } +}); + +exports.__Type = __Type; + +var __Field = new _definition.GraphQLObjectType({ + name: '__Field', + isIntrospection: true, + description: 'Object and Interface types are described by a list of Fields, each of ' + 'which has a name, potentially a list of arguments, and a return type.', + fields: function fields() { + return { + name: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + args: { + type: (0, _definition.GraphQLNonNull)((0, _definition.GraphQLList)((0, _definition.GraphQLNonNull)(__InputValue))), + resolve: function resolve(field) { + return field.args || []; + } + }, + type: { + type: (0, _definition.GraphQLNonNull)(__Type), + resolve: function resolve(obj) { + return obj.type; + } + }, + isDeprecated: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + resolve: function resolve(obj) { + return obj.isDeprecated; + } + }, + deprecationReason: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.deprecationReason; + } + } + }; + } +}); + +exports.__Field = __Field; + +var __InputValue = new _definition.GraphQLObjectType({ + name: '__InputValue', + isIntrospection: true, + description: 'Arguments provided to Fields or Directives and the input fields of an ' + 'InputObject are represented as Input Values which describe their type ' + 'and optionally a default value.', + fields: function fields() { + return { + name: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + type: { + type: (0, _definition.GraphQLNonNull)(__Type), + resolve: function resolve(obj) { + return obj.type; + } + }, + defaultValue: { + type: _scalars.GraphQLString, + description: 'A GraphQL-formatted string representing the default value for this ' + 'input value.', + resolve: function resolve(inputVal) { + return (0, _isInvalid.default)(inputVal.defaultValue) ? null : (0, _printer.print)((0, _astFromValue.astFromValue)(inputVal.defaultValue, inputVal.type)); + } + } + }; + } +}); + +exports.__InputValue = __InputValue; + +var __EnumValue = new _definition.GraphQLObjectType({ + name: '__EnumValue', + isIntrospection: true, + description: 'One possible value for a given Enum. Enum values are unique values, not ' + 'a placeholder for a string or numeric value. However an Enum value is ' + 'returned in a JSON response as a string.', + fields: function fields() { + return { + name: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + isDeprecated: { + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLBoolean), + resolve: function resolve(obj) { + return obj.isDeprecated; + } + }, + deprecationReason: { + type: _scalars.GraphQLString, + resolve: function resolve(obj) { + return obj.deprecationReason; + } + } + }; + } +}); + +exports.__EnumValue = __EnumValue; +var TypeKind = { + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + INPUT_OBJECT: 'INPUT_OBJECT', + LIST: 'LIST', + NON_NULL: 'NON_NULL' +}; +exports.TypeKind = TypeKind; + +var __TypeKind = new _definition.GraphQLEnumType({ + name: '__TypeKind', + isIntrospection: true, + description: 'An enum describing what kind of type a given `__Type` is.', + values: { + SCALAR: { + value: TypeKind.SCALAR, + description: 'Indicates this type is a scalar.' + }, + OBJECT: { + value: TypeKind.OBJECT, + description: 'Indicates this type is an object. ' + '`fields` and `interfaces` are valid fields.' + }, + INTERFACE: { + value: TypeKind.INTERFACE, + description: 'Indicates this type is an interface. ' + '`fields` and `possibleTypes` are valid fields.' + }, + UNION: { + value: TypeKind.UNION, + description: 'Indicates this type is a union. ' + '`possibleTypes` is a valid field.' + }, + ENUM: { + value: TypeKind.ENUM, + description: 'Indicates this type is an enum. ' + '`enumValues` is a valid field.' + }, + INPUT_OBJECT: { + value: TypeKind.INPUT_OBJECT, + description: 'Indicates this type is an input object. ' + '`inputFields` is a valid field.' + }, + LIST: { + value: TypeKind.LIST, + description: 'Indicates this type is a list. ' + '`ofType` is a valid field.' + }, + NON_NULL: { + value: TypeKind.NON_NULL, + description: 'Indicates this type is a non-null. ' + '`ofType` is a valid field.' + } + } +}); +/** + * Note that these are GraphQLField and not GraphQLFieldConfig, + * so the format for args is different. + */ + + +exports.__TypeKind = __TypeKind; +var SchemaMetaFieldDef = { + name: '__schema', + type: (0, _definition.GraphQLNonNull)(__Schema), + description: 'Access the current type schema of this server.', + args: [], + resolve: function resolve(source, args, context, _ref4) { + var schema = _ref4.schema; + return schema; + } +}; +exports.SchemaMetaFieldDef = SchemaMetaFieldDef; +var TypeMetaFieldDef = { + name: '__type', + type: __Type, + description: 'Request the type information of a single type.', + args: [{ + name: 'name', + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString) + }], + resolve: function resolve(source, _ref5, context, _ref6) { + var name = _ref5.name; + var schema = _ref6.schema; + return schema.getType(name); + } +}; +exports.TypeMetaFieldDef = TypeMetaFieldDef; +var TypeNameMetaFieldDef = { + name: '__typename', + type: (0, _definition.GraphQLNonNull)(_scalars.GraphQLString), + description: 'The name of the current Object type at runtime.', + args: [], + resolve: function resolve(source, args, context, _ref7) { + var parentType = _ref7.parentType; + return parentType.name; + } +}; +exports.TypeNameMetaFieldDef = TypeNameMetaFieldDef; +var introspectionTypes = [__Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue, __EnumValue, __TypeKind]; +exports.introspectionTypes = introspectionTypes; + +function isIntrospectionType(type) { + return (0, _definition.isNamedType)(type) && ( // Would prefer to use introspectionTypes.some(), however %checks needs + // a simple expression. + type.name === __Schema.name || type.name === __Directive.name || type.name === __DirectiveLocation.name || type.name === __Type.name || type.name === __Field.name || type.name === __InputValue.name || type.name === __EnumValue.name || type.name === __TypeKind.name); +} \ No newline at end of file diff --git a/dist/type/introspection.js.flow b/dist/type/introspection.js.flow new file mode 100644 index 0000000000..e3787ba9e0 --- /dev/null +++ b/dist/type/introspection.js.flow @@ -0,0 +1,527 @@ +/** + * 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. + * + * @flow strict + */ + +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { astFromValue } from '../utilities/astFromValue'; +import { print } from '../language/printer'; +import { + GraphQLObjectType, + GraphQLEnumType, + GraphQLList, + GraphQLNonNull, + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, + isAbstractType, + isNamedType, +} from './definition'; +import { GraphQLString, GraphQLBoolean } from './scalars'; +import { DirectiveLocation } from '../language/directiveLocation'; +import type { GraphQLField } from './definition'; + +export const __Schema = new GraphQLObjectType({ + name: '__Schema', + isIntrospection: true, + description: + 'A GraphQL Schema defines the capabilities of a GraphQL server. It ' + + 'exposes all available types and directives on the server, as well as ' + + 'the entry points for query, mutation, and subscription operations.', + fields: () => ({ + types: { + description: 'A list of all types supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))), + resolve(schema) { + return objectValues(schema.getTypeMap()); + }, + }, + queryType: { + description: 'The type that query operations will be rooted at.', + type: GraphQLNonNull(__Type), + resolve: schema => schema.getQueryType(), + }, + mutationType: { + description: + 'If this server supports mutation, the type that ' + + 'mutation operations will be rooted at.', + type: __Type, + resolve: schema => schema.getMutationType(), + }, + subscriptionType: { + description: + 'If this server support subscription, the type that ' + + 'subscription operations will be rooted at.', + type: __Type, + resolve: schema => schema.getSubscriptionType(), + }, + directives: { + description: 'A list of all directives supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))), + resolve: schema => schema.getDirectives(), + }, + }), +}); + +export const __Directive = new GraphQLObjectType({ + name: '__Directive', + isIntrospection: true, + description: + 'A Directive provides a way to describe alternate runtime execution and ' + + 'type validation behavior in a GraphQL document.' + + "\n\nIn some cases, you need to provide options to alter GraphQL's " + + 'execution behavior in ways field arguments will not suffice, such as ' + + 'conditionally including or skipping a field. Directives provide this by ' + + 'describing additional information to the executor.', + fields: () => ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + locations: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))), + resolve: obj => obj.locations, + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: directive => directive.args || [], + }, + // NOTE: the following three fields are deprecated and are no longer part + // of the GraphQL specification. + onOperation: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: d => + d.locations.indexOf(DirectiveLocation.QUERY) !== -1 || + d.locations.indexOf(DirectiveLocation.MUTATION) !== -1 || + d.locations.indexOf(DirectiveLocation.SUBSCRIPTION) !== -1, + }, + onFragment: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: d => + d.locations.indexOf(DirectiveLocation.FRAGMENT_SPREAD) !== -1 || + d.locations.indexOf(DirectiveLocation.INLINE_FRAGMENT) !== -1 || + d.locations.indexOf(DirectiveLocation.FRAGMENT_DEFINITION) !== -1, + }, + onField: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: d => d.locations.indexOf(DirectiveLocation.FIELD) !== -1, + }, + }), +}); + +export const __DirectiveLocation = new GraphQLEnumType({ + name: '__DirectiveLocation', + isIntrospection: true, + description: + 'A Directive can be adjacent to many parts of the GraphQL language, a ' + + '__DirectiveLocation describes one such possible adjacencies.', + values: { + QUERY: { + value: DirectiveLocation.QUERY, + description: 'Location adjacent to a query operation.', + }, + MUTATION: { + value: DirectiveLocation.MUTATION, + description: 'Location adjacent to a mutation operation.', + }, + SUBSCRIPTION: { + value: DirectiveLocation.SUBSCRIPTION, + description: 'Location adjacent to a subscription operation.', + }, + FIELD: { + value: DirectiveLocation.FIELD, + description: 'Location adjacent to a field.', + }, + FRAGMENT_DEFINITION: { + value: DirectiveLocation.FRAGMENT_DEFINITION, + description: 'Location adjacent to a fragment definition.', + }, + FRAGMENT_SPREAD: { + value: DirectiveLocation.FRAGMENT_SPREAD, + description: 'Location adjacent to a fragment spread.', + }, + INLINE_FRAGMENT: { + value: DirectiveLocation.INLINE_FRAGMENT, + description: 'Location adjacent to an inline fragment.', + }, + SCHEMA: { + value: DirectiveLocation.SCHEMA, + description: 'Location adjacent to a schema definition.', + }, + SCALAR: { + value: DirectiveLocation.SCALAR, + description: 'Location adjacent to a scalar definition.', + }, + OBJECT: { + value: DirectiveLocation.OBJECT, + description: 'Location adjacent to an object type definition.', + }, + FIELD_DEFINITION: { + value: DirectiveLocation.FIELD_DEFINITION, + description: 'Location adjacent to a field definition.', + }, + ARGUMENT_DEFINITION: { + value: DirectiveLocation.ARGUMENT_DEFINITION, + description: 'Location adjacent to an argument definition.', + }, + INTERFACE: { + value: DirectiveLocation.INTERFACE, + description: 'Location adjacent to an interface definition.', + }, + UNION: { + value: DirectiveLocation.UNION, + description: 'Location adjacent to a union definition.', + }, + ENUM: { + value: DirectiveLocation.ENUM, + description: 'Location adjacent to an enum definition.', + }, + ENUM_VALUE: { + value: DirectiveLocation.ENUM_VALUE, + description: 'Location adjacent to an enum value definition.', + }, + INPUT_OBJECT: { + value: DirectiveLocation.INPUT_OBJECT, + description: 'Location adjacent to an input object type definition.', + }, + INPUT_FIELD_DEFINITION: { + value: DirectiveLocation.INPUT_FIELD_DEFINITION, + description: 'Location adjacent to an input object field definition.', + }, + }, +}); + +export const __Type = new GraphQLObjectType({ + name: '__Type', + isIntrospection: true, + description: + 'The fundamental unit of any GraphQL Schema is the type. There are ' + + 'many kinds of types in GraphQL as represented by the `__TypeKind` enum.' + + '\n\nDepending on the kind of a type, certain fields describe ' + + 'information about that type. Scalar types provide no information ' + + 'beyond a name and description, while Enum types provide their values. ' + + 'Object and Interface types provide the fields they describe. Abstract ' + + 'types, Union and Interface, provide the Object types possible ' + + 'at runtime. List and NonNull types compose other types.', + fields: () => ({ + kind: { + type: GraphQLNonNull(__TypeKind), + resolve(type) { + if (isScalarType(type)) { + return TypeKind.SCALAR; + } else if (isObjectType(type)) { + return TypeKind.OBJECT; + } else if (isInterfaceType(type)) { + return TypeKind.INTERFACE; + } else if (isUnionType(type)) { + return TypeKind.UNION; + } else if (isEnumType(type)) { + return TypeKind.ENUM; + } else if (isInputObjectType(type)) { + return TypeKind.INPUT_OBJECT; + } else if (isListType(type)) { + return TypeKind.LIST; + } else if (isNonNullType(type)) { + return TypeKind.NON_NULL; + } + throw new Error('Unknown kind of type: ' + type); + }, + }, + name: { + type: GraphQLString, + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + fields: { + type: GraphQLList(GraphQLNonNull(__Field)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isObjectType(type) || isInterfaceType(type)) { + let fields = objectValues(type.getFields()); + if (!includeDeprecated) { + fields = fields.filter(field => !field.deprecationReason); + } + return fields; + } + return null; + }, + }, + interfaces: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve(type) { + if (isObjectType(type)) { + return type.getInterfaces(); + } + }, + }, + possibleTypes: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve(type, args, context, { schema }) { + if (isAbstractType(type)) { + return schema.getPossibleTypes(type); + } + }, + }, + enumValues: { + type: GraphQLList(GraphQLNonNull(__EnumValue)), + args: { + includeDeprecated: { type: GraphQLBoolean, defaultValue: false }, + }, + resolve(type, { includeDeprecated }) { + if (isEnumType(type)) { + let values = type.getValues(); + if (!includeDeprecated) { + values = values.filter(value => !value.deprecationReason); + } + return values; + } + }, + }, + inputFields: { + type: GraphQLList(GraphQLNonNull(__InputValue)), + resolve(type) { + if (isInputObjectType(type)) { + return objectValues(type.getFields()); + } + }, + }, + ofType: { + type: __Type, + resolve: obj => obj.ofType, + }, + }), +}); + +export const __Field = new GraphQLObjectType({ + name: '__Field', + isIntrospection: true, + description: + 'Object and Interface types are described by a list of Fields, each of ' + + 'which has a name, potentially a list of arguments, and a return type.', + fields: () => ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: field => field.args || [], + }, + type: { + type: GraphQLNonNull(__Type), + resolve: obj => obj.type, + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: obj => obj.isDeprecated, + }, + deprecationReason: { + type: GraphQLString, + resolve: obj => obj.deprecationReason, + }, + }), +}); + +export const __InputValue = new GraphQLObjectType({ + name: '__InputValue', + isIntrospection: true, + description: + 'Arguments provided to Fields or Directives and the input fields of an ' + + 'InputObject are represented as Input Values which describe their type ' + + 'and optionally a default value.', + fields: () => ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + type: { + type: GraphQLNonNull(__Type), + resolve: obj => obj.type, + }, + defaultValue: { + type: GraphQLString, + description: + 'A GraphQL-formatted string representing the default value for this ' + + 'input value.', + resolve: inputVal => + isInvalid(inputVal.defaultValue) + ? null + : print(astFromValue(inputVal.defaultValue, inputVal.type)), + }, + }), +}); + +export const __EnumValue = new GraphQLObjectType({ + name: '__EnumValue', + isIntrospection: true, + description: + 'One possible value for a given Enum. Enum values are unique values, not ' + + 'a placeholder for a string or numeric value. However an Enum value is ' + + 'returned in a JSON response as a string.', + fields: () => ({ + name: { + type: GraphQLNonNull(GraphQLString), + resolve: obj => obj.name, + }, + description: { + type: GraphQLString, + resolve: obj => obj.description, + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: obj => obj.isDeprecated, + }, + deprecationReason: { + type: GraphQLString, + resolve: obj => obj.deprecationReason, + }, + }), +}); + +export const TypeKind = { + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + INPUT_OBJECT: 'INPUT_OBJECT', + LIST: 'LIST', + NON_NULL: 'NON_NULL', +}; + +export const __TypeKind = new GraphQLEnumType({ + name: '__TypeKind', + isIntrospection: true, + description: 'An enum describing what kind of type a given `__Type` is.', + values: { + SCALAR: { + value: TypeKind.SCALAR, + description: 'Indicates this type is a scalar.', + }, + OBJECT: { + value: TypeKind.OBJECT, + description: + 'Indicates this type is an object. ' + + '`fields` and `interfaces` are valid fields.', + }, + INTERFACE: { + value: TypeKind.INTERFACE, + description: + 'Indicates this type is an interface. ' + + '`fields` and `possibleTypes` are valid fields.', + }, + UNION: { + value: TypeKind.UNION, + description: + 'Indicates this type is a union. ' + + '`possibleTypes` is a valid field.', + }, + ENUM: { + value: TypeKind.ENUM, + description: + 'Indicates this type is an enum. ' + '`enumValues` is a valid field.', + }, + INPUT_OBJECT: { + value: TypeKind.INPUT_OBJECT, + description: + 'Indicates this type is an input object. ' + + '`inputFields` is a valid field.', + }, + LIST: { + value: TypeKind.LIST, + description: + 'Indicates this type is a list. ' + '`ofType` is a valid field.', + }, + NON_NULL: { + value: TypeKind.NON_NULL, + description: + 'Indicates this type is a non-null. ' + '`ofType` is a valid field.', + }, + }, +}); + +/** + * Note that these are GraphQLField and not GraphQLFieldConfig, + * so the format for args is different. + */ + +export const SchemaMetaFieldDef: GraphQLField<*, *> = { + name: '__schema', + type: GraphQLNonNull(__Schema), + description: 'Access the current type schema of this server.', + args: [], + resolve: (source, args, context, { schema }) => schema, +}; + +export const TypeMetaFieldDef: GraphQLField<*, *> = { + name: '__type', + type: __Type, + description: 'Request the type information of a single type.', + args: [{ name: 'name', type: GraphQLNonNull(GraphQLString) }], + resolve: (source, { name }, context, { schema }) => schema.getType(name), +}; + +export const TypeNameMetaFieldDef: GraphQLField<*, *> = { + name: '__typename', + type: GraphQLNonNull(GraphQLString), + description: 'The name of the current Object type at runtime.', + args: [], + resolve: (source, args, context, { parentType }) => parentType.name, +}; + +export const introspectionTypes: $ReadOnlyArray<*> = [ + __Schema, + __Directive, + __DirectiveLocation, + __Type, + __Field, + __InputValue, + __EnumValue, + __TypeKind, +]; + +export function isIntrospectionType(type: mixed): boolean %checks { + return ( + isNamedType(type) && + // Would prefer to use introspectionTypes.some(), however %checks needs + // a simple expression. + (type.name === __Schema.name || + type.name === __Directive.name || + type.name === __DirectiveLocation.name || + type.name === __Type.name || + type.name === __Field.name || + type.name === __InputValue.name || + type.name === __EnumValue.name || + type.name === __TypeKind.name) + ); +} diff --git a/dist/type/introspection.mjs b/dist/type/introspection.mjs new file mode 100644 index 0000000000..f9095eb346 --- /dev/null +++ b/dist/type/introspection.mjs @@ -0,0 +1,527 @@ +/** + * 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. + * + * strict + */ +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { astFromValue } from '../utilities/astFromValue'; +import { print } from '../language/printer'; +import { GraphQLObjectType, GraphQLEnumType, GraphQLList, GraphQLNonNull, isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isListType, isNonNullType, isAbstractType, isNamedType } from './definition'; +import { GraphQLString, GraphQLBoolean } from './scalars'; +import { DirectiveLocation } from '../language/directiveLocation'; +export var __Schema = new GraphQLObjectType({ + name: '__Schema', + isIntrospection: true, + description: 'A GraphQL Schema defines the capabilities of a GraphQL server. It ' + 'exposes all available types and directives on the server, as well as ' + 'the entry points for query, mutation, and subscription operations.', + fields: function fields() { + return { + types: { + description: 'A list of all types supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Type))), + resolve: function resolve(schema) { + return objectValues(schema.getTypeMap()); + } + }, + queryType: { + description: 'The type that query operations will be rooted at.', + type: GraphQLNonNull(__Type), + resolve: function resolve(schema) { + return schema.getQueryType(); + } + }, + mutationType: { + description: 'If this server supports mutation, the type that ' + 'mutation operations will be rooted at.', + type: __Type, + resolve: function resolve(schema) { + return schema.getMutationType(); + } + }, + subscriptionType: { + description: 'If this server support subscription, the type that ' + 'subscription operations will be rooted at.', + type: __Type, + resolve: function resolve(schema) { + return schema.getSubscriptionType(); + } + }, + directives: { + description: 'A list of all directives supported by this server.', + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__Directive))), + resolve: function resolve(schema) { + return schema.getDirectives(); + } + } + }; + } +}); +export var __Directive = new GraphQLObjectType({ + name: '__Directive', + isIntrospection: true, + description: 'A Directive provides a way to describe alternate runtime execution and ' + 'type validation behavior in a GraphQL document.' + "\n\nIn some cases, you need to provide options to alter GraphQL's " + 'execution behavior in ways field arguments will not suffice, such as ' + 'conditionally including or skipping a field. Directives provide this by ' + 'describing additional information to the executor.', + fields: function fields() { + return { + name: { + type: GraphQLNonNull(GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + locations: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))), + resolve: function resolve(obj) { + return obj.locations; + } + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: function resolve(directive) { + return directive.args || []; + } + }, + // NOTE: the following three fields are deprecated and are no longer part + // of the GraphQL specification. + onOperation: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(DirectiveLocation.QUERY) !== -1 || d.locations.indexOf(DirectiveLocation.MUTATION) !== -1 || d.locations.indexOf(DirectiveLocation.SUBSCRIPTION) !== -1; + } + }, + onFragment: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(DirectiveLocation.FRAGMENT_SPREAD) !== -1 || d.locations.indexOf(DirectiveLocation.INLINE_FRAGMENT) !== -1 || d.locations.indexOf(DirectiveLocation.FRAGMENT_DEFINITION) !== -1; + } + }, + onField: { + deprecationReason: 'Use `locations`.', + type: GraphQLNonNull(GraphQLBoolean), + resolve: function resolve(d) { + return d.locations.indexOf(DirectiveLocation.FIELD) !== -1; + } + } + }; + } +}); +export var __DirectiveLocation = new GraphQLEnumType({ + name: '__DirectiveLocation', + isIntrospection: true, + description: 'A Directive can be adjacent to many parts of the GraphQL language, a ' + '__DirectiveLocation describes one such possible adjacencies.', + values: { + QUERY: { + value: DirectiveLocation.QUERY, + description: 'Location adjacent to a query operation.' + }, + MUTATION: { + value: DirectiveLocation.MUTATION, + description: 'Location adjacent to a mutation operation.' + }, + SUBSCRIPTION: { + value: DirectiveLocation.SUBSCRIPTION, + description: 'Location adjacent to a subscription operation.' + }, + FIELD: { + value: DirectiveLocation.FIELD, + description: 'Location adjacent to a field.' + }, + FRAGMENT_DEFINITION: { + value: DirectiveLocation.FRAGMENT_DEFINITION, + description: 'Location adjacent to a fragment definition.' + }, + FRAGMENT_SPREAD: { + value: DirectiveLocation.FRAGMENT_SPREAD, + description: 'Location adjacent to a fragment spread.' + }, + INLINE_FRAGMENT: { + value: DirectiveLocation.INLINE_FRAGMENT, + description: 'Location adjacent to an inline fragment.' + }, + SCHEMA: { + value: DirectiveLocation.SCHEMA, + description: 'Location adjacent to a schema definition.' + }, + SCALAR: { + value: DirectiveLocation.SCALAR, + description: 'Location adjacent to a scalar definition.' + }, + OBJECT: { + value: DirectiveLocation.OBJECT, + description: 'Location adjacent to an object type definition.' + }, + FIELD_DEFINITION: { + value: DirectiveLocation.FIELD_DEFINITION, + description: 'Location adjacent to a field definition.' + }, + ARGUMENT_DEFINITION: { + value: DirectiveLocation.ARGUMENT_DEFINITION, + description: 'Location adjacent to an argument definition.' + }, + INTERFACE: { + value: DirectiveLocation.INTERFACE, + description: 'Location adjacent to an interface definition.' + }, + UNION: { + value: DirectiveLocation.UNION, + description: 'Location adjacent to a union definition.' + }, + ENUM: { + value: DirectiveLocation.ENUM, + description: 'Location adjacent to an enum definition.' + }, + ENUM_VALUE: { + value: DirectiveLocation.ENUM_VALUE, + description: 'Location adjacent to an enum value definition.' + }, + INPUT_OBJECT: { + value: DirectiveLocation.INPUT_OBJECT, + description: 'Location adjacent to an input object type definition.' + }, + INPUT_FIELD_DEFINITION: { + value: DirectiveLocation.INPUT_FIELD_DEFINITION, + description: 'Location adjacent to an input object field definition.' + } + } +}); +export var __Type = new GraphQLObjectType({ + name: '__Type', + isIntrospection: true, + description: 'The fundamental unit of any GraphQL Schema is the type. There are ' + 'many kinds of types in GraphQL as represented by the `__TypeKind` enum.' + '\n\nDepending on the kind of a type, certain fields describe ' + 'information about that type. Scalar types provide no information ' + 'beyond a name and description, while Enum types provide their values. ' + 'Object and Interface types provide the fields they describe. Abstract ' + 'types, Union and Interface, provide the Object types possible ' + 'at runtime. List and NonNull types compose other types.', + fields: function fields() { + return { + kind: { + type: GraphQLNonNull(__TypeKind), + resolve: function resolve(type) { + if (isScalarType(type)) { + return TypeKind.SCALAR; + } else if (isObjectType(type)) { + return TypeKind.OBJECT; + } else if (isInterfaceType(type)) { + return TypeKind.INTERFACE; + } else if (isUnionType(type)) { + return TypeKind.UNION; + } else if (isEnumType(type)) { + return TypeKind.ENUM; + } else if (isInputObjectType(type)) { + return TypeKind.INPUT_OBJECT; + } else if (isListType(type)) { + return TypeKind.LIST; + } else if (isNonNullType(type)) { + return TypeKind.NON_NULL; + } + + throw new Error('Unknown kind of type: ' + type); + } + }, + name: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + fields: { + type: GraphQLList(GraphQLNonNull(__Field)), + args: { + includeDeprecated: { + type: GraphQLBoolean, + defaultValue: false + } + }, + resolve: function resolve(type, _ref) { + var includeDeprecated = _ref.includeDeprecated; + + if (isObjectType(type) || isInterfaceType(type)) { + var fields = objectValues(type.getFields()); + + if (!includeDeprecated) { + fields = fields.filter(function (field) { + return !field.deprecationReason; + }); + } + + return fields; + } + + return null; + } + }, + interfaces: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve: function resolve(type) { + if (isObjectType(type)) { + return type.getInterfaces(); + } + } + }, + possibleTypes: { + type: GraphQLList(GraphQLNonNull(__Type)), + resolve: function resolve(type, args, context, _ref2) { + var schema = _ref2.schema; + + if (isAbstractType(type)) { + return schema.getPossibleTypes(type); + } + } + }, + enumValues: { + type: GraphQLList(GraphQLNonNull(__EnumValue)), + args: { + includeDeprecated: { + type: GraphQLBoolean, + defaultValue: false + } + }, + resolve: function resolve(type, _ref3) { + var includeDeprecated = _ref3.includeDeprecated; + + if (isEnumType(type)) { + var values = type.getValues(); + + if (!includeDeprecated) { + values = values.filter(function (value) { + return !value.deprecationReason; + }); + } + + return values; + } + } + }, + inputFields: { + type: GraphQLList(GraphQLNonNull(__InputValue)), + resolve: function resolve(type) { + if (isInputObjectType(type)) { + return objectValues(type.getFields()); + } + } + }, + ofType: { + type: __Type, + resolve: function resolve(obj) { + return obj.ofType; + } + } + }; + } +}); +export var __Field = new GraphQLObjectType({ + name: '__Field', + isIntrospection: true, + description: 'Object and Interface types are described by a list of Fields, each of ' + 'which has a name, potentially a list of arguments, and a return type.', + fields: function fields() { + return { + name: { + type: GraphQLNonNull(GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + args: { + type: GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))), + resolve: function resolve(field) { + return field.args || []; + } + }, + type: { + type: GraphQLNonNull(__Type), + resolve: function resolve(obj) { + return obj.type; + } + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: function resolve(obj) { + return obj.isDeprecated; + } + }, + deprecationReason: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.deprecationReason; + } + } + }; + } +}); +export var __InputValue = new GraphQLObjectType({ + name: '__InputValue', + isIntrospection: true, + description: 'Arguments provided to Fields or Directives and the input fields of an ' + 'InputObject are represented as Input Values which describe their type ' + 'and optionally a default value.', + fields: function fields() { + return { + name: { + type: GraphQLNonNull(GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + type: { + type: GraphQLNonNull(__Type), + resolve: function resolve(obj) { + return obj.type; + } + }, + defaultValue: { + type: GraphQLString, + description: 'A GraphQL-formatted string representing the default value for this ' + 'input value.', + resolve: function resolve(inputVal) { + return isInvalid(inputVal.defaultValue) ? null : print(astFromValue(inputVal.defaultValue, inputVal.type)); + } + } + }; + } +}); +export var __EnumValue = new GraphQLObjectType({ + name: '__EnumValue', + isIntrospection: true, + description: 'One possible value for a given Enum. Enum values are unique values, not ' + 'a placeholder for a string or numeric value. However an Enum value is ' + 'returned in a JSON response as a string.', + fields: function fields() { + return { + name: { + type: GraphQLNonNull(GraphQLString), + resolve: function resolve(obj) { + return obj.name; + } + }, + description: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.description; + } + }, + isDeprecated: { + type: GraphQLNonNull(GraphQLBoolean), + resolve: function resolve(obj) { + return obj.isDeprecated; + } + }, + deprecationReason: { + type: GraphQLString, + resolve: function resolve(obj) { + return obj.deprecationReason; + } + } + }; + } +}); +export var TypeKind = { + SCALAR: 'SCALAR', + OBJECT: 'OBJECT', + INTERFACE: 'INTERFACE', + UNION: 'UNION', + ENUM: 'ENUM', + INPUT_OBJECT: 'INPUT_OBJECT', + LIST: 'LIST', + NON_NULL: 'NON_NULL' +}; +export var __TypeKind = new GraphQLEnumType({ + name: '__TypeKind', + isIntrospection: true, + description: 'An enum describing what kind of type a given `__Type` is.', + values: { + SCALAR: { + value: TypeKind.SCALAR, + description: 'Indicates this type is a scalar.' + }, + OBJECT: { + value: TypeKind.OBJECT, + description: 'Indicates this type is an object. ' + '`fields` and `interfaces` are valid fields.' + }, + INTERFACE: { + value: TypeKind.INTERFACE, + description: 'Indicates this type is an interface. ' + '`fields` and `possibleTypes` are valid fields.' + }, + UNION: { + value: TypeKind.UNION, + description: 'Indicates this type is a union. ' + '`possibleTypes` is a valid field.' + }, + ENUM: { + value: TypeKind.ENUM, + description: 'Indicates this type is an enum. ' + '`enumValues` is a valid field.' + }, + INPUT_OBJECT: { + value: TypeKind.INPUT_OBJECT, + description: 'Indicates this type is an input object. ' + '`inputFields` is a valid field.' + }, + LIST: { + value: TypeKind.LIST, + description: 'Indicates this type is a list. ' + '`ofType` is a valid field.' + }, + NON_NULL: { + value: TypeKind.NON_NULL, + description: 'Indicates this type is a non-null. ' + '`ofType` is a valid field.' + } + } +}); +/** + * Note that these are GraphQLField and not GraphQLFieldConfig, + * so the format for args is different. + */ + +export var SchemaMetaFieldDef = { + name: '__schema', + type: GraphQLNonNull(__Schema), + description: 'Access the current type schema of this server.', + args: [], + resolve: function resolve(source, args, context, _ref4) { + var schema = _ref4.schema; + return schema; + } +}; +export var TypeMetaFieldDef = { + name: '__type', + type: __Type, + description: 'Request the type information of a single type.', + args: [{ + name: 'name', + type: GraphQLNonNull(GraphQLString) + }], + resolve: function resolve(source, _ref5, context, _ref6) { + var name = _ref5.name; + var schema = _ref6.schema; + return schema.getType(name); + } +}; +export var TypeNameMetaFieldDef = { + name: '__typename', + type: GraphQLNonNull(GraphQLString), + description: 'The name of the current Object type at runtime.', + args: [], + resolve: function resolve(source, args, context, _ref7) { + var parentType = _ref7.parentType; + return parentType.name; + } +}; +export var introspectionTypes = [__Schema, __Directive, __DirectiveLocation, __Type, __Field, __InputValue, __EnumValue, __TypeKind]; +export function isIntrospectionType(type) { + return isNamedType(type) && ( // Would prefer to use introspectionTypes.some(), however %checks needs + // a simple expression. + type.name === __Schema.name || type.name === __Directive.name || type.name === __DirectiveLocation.name || type.name === __Type.name || type.name === __Field.name || type.name === __InputValue.name || type.name === __EnumValue.name || type.name === __TypeKind.name); +} \ No newline at end of file diff --git a/dist/type/scalars.js b/dist/type/scalars.js new file mode 100644 index 0000000000..8cb6a5eb75 --- /dev/null +++ b/dist/type/scalars.js @@ -0,0 +1,138 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isSpecifiedScalarType = isSpecifiedScalarType; +exports.specifiedScalarTypes = exports.GraphQLID = exports.GraphQLBoolean = exports.GraphQLString = exports.GraphQLFloat = exports.GraphQLInt = void 0; + +var _definition = require("./definition"); + +var _kinds = require("../language/kinds"); + +/** + * 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. + * + * strict + */ +// As per the GraphQL Spec, Integers are only treated as valid when a valid +// 32-bit signed integer, providing the broadest support across platforms. +// +// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because +// they are internally represented as IEEE 754 doubles. +var MAX_INT = 2147483647; +var MIN_INT = -2147483648; + +function coerceInt(value) { + if (value === '') { + throw new TypeError('Int cannot represent non 32-bit signed integer value: (empty string)'); + } + + var num = Number(value); + + if (num !== num || num > MAX_INT || num < MIN_INT) { + throw new TypeError('Int cannot represent non 32-bit signed integer value: ' + String(value)); + } + + var int = Math.floor(num); + + if (int !== num) { + throw new TypeError('Int cannot represent non-integer value: ' + String(value)); + } + + return int; +} + +var GraphQLInt = new _definition.GraphQLScalarType({ + name: 'Int', + description: 'The `Int` scalar type represents non-fractional signed whole numeric ' + 'values. Int can represent values between -(2^31) and 2^31 - 1. ', + serialize: coerceInt, + parseValue: coerceInt, + parseLiteral: function parseLiteral(ast) { + if (ast.kind === _kinds.Kind.INT) { + var num = parseInt(ast.value, 10); + + if (num <= MAX_INT && num >= MIN_INT) { + return num; + } + } + + return undefined; + } +}); +exports.GraphQLInt = GraphQLInt; + +function coerceFloat(value) { + if (value === '') { + throw new TypeError('Float cannot represent non numeric value: (empty string)'); + } + + var num = Number(value); + + if (num === num) { + return num; + } + + throw new TypeError('Float cannot represent non numeric value: ' + String(value)); +} + +var GraphQLFloat = new _definition.GraphQLScalarType({ + name: 'Float', + description: 'The `Float` scalar type represents signed double-precision fractional ' + 'values as specified by ' + '[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ', + serialize: coerceFloat, + parseValue: coerceFloat, + parseLiteral: function parseLiteral(ast) { + return ast.kind === _kinds.Kind.FLOAT || ast.kind === _kinds.Kind.INT ? parseFloat(ast.value) : undefined; + } +}); +exports.GraphQLFloat = GraphQLFloat; + +function coerceString(value) { + if (Array.isArray(value)) { + throw new TypeError("String cannot represent an array value: [".concat(String(value), "]")); + } + + return String(value); +} + +var GraphQLString = new _definition.GraphQLScalarType({ + name: 'String', + description: 'The `String` scalar type represents textual data, represented as UTF-8 ' + 'character sequences. The String type is most often used by GraphQL to ' + 'represent free-form human-readable text.', + serialize: coerceString, + parseValue: coerceString, + parseLiteral: function parseLiteral(ast) { + return ast.kind === _kinds.Kind.STRING ? ast.value : undefined; + } +}); +exports.GraphQLString = GraphQLString; +var GraphQLBoolean = new _definition.GraphQLScalarType({ + name: 'Boolean', + description: 'The `Boolean` scalar type represents `true` or `false`.', + serialize: Boolean, + parseValue: Boolean, + parseLiteral: function parseLiteral(ast) { + return ast.kind === _kinds.Kind.BOOLEAN ? ast.value : undefined; + } +}); +exports.GraphQLBoolean = GraphQLBoolean; +var GraphQLID = new _definition.GraphQLScalarType({ + name: 'ID', + description: 'The `ID` scalar type represents a unique identifier, often used to ' + 'refetch an object or as key for a cache. The ID type appears in a JSON ' + 'response as a String; however, it is not intended to be human-readable. ' + 'When expected as an input type, any string (such as `"4"`) or integer ' + '(such as `4`) input value will be accepted as an ID.', + serialize: String, + parseValue: String, + parseLiteral: function parseLiteral(ast) { + return ast.kind === _kinds.Kind.STRING || ast.kind === _kinds.Kind.INT ? ast.value : undefined; + } +}); +exports.GraphQLID = GraphQLID; +var specifiedScalarTypes = [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID]; +exports.specifiedScalarTypes = specifiedScalarTypes; + +function isSpecifiedScalarType(type) { + return (0, _definition.isNamedType)(type) && ( // Would prefer to use specifiedScalarTypes.some(), however %checks needs + // a simple expression. + type.name === GraphQLString.name || type.name === GraphQLInt.name || type.name === GraphQLFloat.name || type.name === GraphQLBoolean.name || type.name === GraphQLID.name); +} \ No newline at end of file diff --git a/dist/type/scalars.js.flow b/dist/type/scalars.js.flow new file mode 100644 index 0000000000..c60bcc8cdc --- /dev/null +++ b/dist/type/scalars.js.flow @@ -0,0 +1,158 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLScalarType, isNamedType } from './definition'; +import { Kind } from '../language/kinds'; + +// As per the GraphQL Spec, Integers are only treated as valid when a valid +// 32-bit signed integer, providing the broadest support across platforms. +// +// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because +// they are internally represented as IEEE 754 doubles. +const MAX_INT = 2147483647; +const MIN_INT = -2147483648; + +function coerceInt(value: mixed): ?number { + if (value === '') { + throw new TypeError( + 'Int cannot represent non 32-bit signed integer value: (empty string)', + ); + } + const num = Number(value); + if (num !== num || num > MAX_INT || num < MIN_INT) { + throw new TypeError( + 'Int cannot represent non 32-bit signed integer value: ' + String(value), + ); + } + const int = Math.floor(num); + if (int !== num) { + throw new TypeError( + 'Int cannot represent non-integer value: ' + String(value), + ); + } + return int; +} + +export const GraphQLInt = new GraphQLScalarType({ + name: 'Int', + description: + 'The `Int` scalar type represents non-fractional signed whole numeric ' + + 'values. Int can represent values between -(2^31) and 2^31 - 1. ', + serialize: coerceInt, + parseValue: coerceInt, + parseLiteral(ast) { + if (ast.kind === Kind.INT) { + const num = parseInt(ast.value, 10); + if (num <= MAX_INT && num >= MIN_INT) { + return num; + } + } + return undefined; + }, +}); + +function coerceFloat(value: mixed): ?number { + if (value === '') { + throw new TypeError( + 'Float cannot represent non numeric value: (empty string)', + ); + } + const num = Number(value); + if (num === num) { + return num; + } + throw new TypeError( + 'Float cannot represent non numeric value: ' + String(value), + ); +} + +export const GraphQLFloat = new GraphQLScalarType({ + name: 'Float', + description: + 'The `Float` scalar type represents signed double-precision fractional ' + + 'values as specified by ' + + '[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ', + serialize: coerceFloat, + parseValue: coerceFloat, + parseLiteral(ast) { + return ast.kind === Kind.FLOAT || ast.kind === Kind.INT + ? parseFloat(ast.value) + : undefined; + }, +}); + +function coerceString(value: mixed): ?string { + if (Array.isArray(value)) { + throw new TypeError( + `String cannot represent an array value: [${String(value)}]`, + ); + } + return String(value); +} + +export const GraphQLString = new GraphQLScalarType({ + name: 'String', + description: + 'The `String` scalar type represents textual data, represented as UTF-8 ' + + 'character sequences. The String type is most often used by GraphQL to ' + + 'represent free-form human-readable text.', + serialize: coerceString, + parseValue: coerceString, + parseLiteral(ast) { + return ast.kind === Kind.STRING ? ast.value : undefined; + }, +}); + +export const GraphQLBoolean = new GraphQLScalarType({ + name: 'Boolean', + description: 'The `Boolean` scalar type represents `true` or `false`.', + serialize: Boolean, + parseValue: Boolean, + parseLiteral(ast) { + return ast.kind === Kind.BOOLEAN ? ast.value : undefined; + }, +}); + +export const GraphQLID = new GraphQLScalarType({ + name: 'ID', + description: + 'The `ID` scalar type represents a unique identifier, often used to ' + + 'refetch an object or as key for a cache. The ID type appears in a JSON ' + + 'response as a String; however, it is not intended to be human-readable. ' + + 'When expected as an input type, any string (such as `"4"`) or integer ' + + '(such as `4`) input value will be accepted as an ID.', + serialize: String, + parseValue: String, + parseLiteral(ast) { + return ast.kind === Kind.STRING || ast.kind === Kind.INT + ? ast.value + : undefined; + }, +}); + +export const specifiedScalarTypes: $ReadOnlyArray<*> = [ + GraphQLString, + GraphQLInt, + GraphQLFloat, + GraphQLBoolean, + GraphQLID, +]; + +export function isSpecifiedScalarType(type: mixed): boolean %checks { + return ( + isNamedType(type) && + // Would prefer to use specifiedScalarTypes.some(), however %checks needs + // a simple expression. + (type.name === GraphQLString.name || + type.name === GraphQLInt.name || + type.name === GraphQLFloat.name || + type.name === GraphQLBoolean.name || + type.name === GraphQLID.name) + ); +} diff --git a/dist/type/scalars.mjs b/dist/type/scalars.mjs new file mode 100644 index 0000000000..cab64a92d7 --- /dev/null +++ b/dist/type/scalars.mjs @@ -0,0 +1,121 @@ +/** + * 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. + * + * strict + */ +import { GraphQLScalarType, isNamedType } from './definition'; +import { Kind } from '../language/kinds'; // As per the GraphQL Spec, Integers are only treated as valid when a valid +// 32-bit signed integer, providing the broadest support across platforms. +// +// n.b. JavaScript's integers are safe between -(2^53 - 1) and 2^53 - 1 because +// they are internally represented as IEEE 754 doubles. + +var MAX_INT = 2147483647; +var MIN_INT = -2147483648; + +function coerceInt(value) { + if (value === '') { + throw new TypeError('Int cannot represent non 32-bit signed integer value: (empty string)'); + } + + var num = Number(value); + + if (num !== num || num > MAX_INT || num < MIN_INT) { + throw new TypeError('Int cannot represent non 32-bit signed integer value: ' + String(value)); + } + + var int = Math.floor(num); + + if (int !== num) { + throw new TypeError('Int cannot represent non-integer value: ' + String(value)); + } + + return int; +} + +export var GraphQLInt = new GraphQLScalarType({ + name: 'Int', + description: 'The `Int` scalar type represents non-fractional signed whole numeric ' + 'values. Int can represent values between -(2^31) and 2^31 - 1. ', + serialize: coerceInt, + parseValue: coerceInt, + parseLiteral: function parseLiteral(ast) { + if (ast.kind === Kind.INT) { + var num = parseInt(ast.value, 10); + + if (num <= MAX_INT && num >= MIN_INT) { + return num; + } + } + + return undefined; + } +}); + +function coerceFloat(value) { + if (value === '') { + throw new TypeError('Float cannot represent non numeric value: (empty string)'); + } + + var num = Number(value); + + if (num === num) { + return num; + } + + throw new TypeError('Float cannot represent non numeric value: ' + String(value)); +} + +export var GraphQLFloat = new GraphQLScalarType({ + name: 'Float', + description: 'The `Float` scalar type represents signed double-precision fractional ' + 'values as specified by ' + '[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ', + serialize: coerceFloat, + parseValue: coerceFloat, + parseLiteral: function parseLiteral(ast) { + return ast.kind === Kind.FLOAT || ast.kind === Kind.INT ? parseFloat(ast.value) : undefined; + } +}); + +function coerceString(value) { + if (Array.isArray(value)) { + throw new TypeError("String cannot represent an array value: [".concat(String(value), "]")); + } + + return String(value); +} + +export var GraphQLString = new GraphQLScalarType({ + name: 'String', + description: 'The `String` scalar type represents textual data, represented as UTF-8 ' + 'character sequences. The String type is most often used by GraphQL to ' + 'represent free-form human-readable text.', + serialize: coerceString, + parseValue: coerceString, + parseLiteral: function parseLiteral(ast) { + return ast.kind === Kind.STRING ? ast.value : undefined; + } +}); +export var GraphQLBoolean = new GraphQLScalarType({ + name: 'Boolean', + description: 'The `Boolean` scalar type represents `true` or `false`.', + serialize: Boolean, + parseValue: Boolean, + parseLiteral: function parseLiteral(ast) { + return ast.kind === Kind.BOOLEAN ? ast.value : undefined; + } +}); +export var GraphQLID = new GraphQLScalarType({ + name: 'ID', + description: 'The `ID` scalar type represents a unique identifier, often used to ' + 'refetch an object or as key for a cache. The ID type appears in a JSON ' + 'response as a String; however, it is not intended to be human-readable. ' + 'When expected as an input type, any string (such as `"4"`) or integer ' + '(such as `4`) input value will be accepted as an ID.', + serialize: String, + parseValue: String, + parseLiteral: function parseLiteral(ast) { + return ast.kind === Kind.STRING || ast.kind === Kind.INT ? ast.value : undefined; + } +}); +export var specifiedScalarTypes = [GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLID]; +export function isSpecifiedScalarType(type) { + return isNamedType(type) && ( // Would prefer to use specifiedScalarTypes.some(), however %checks needs + // a simple expression. + type.name === GraphQLString.name || type.name === GraphQLInt.name || type.name === GraphQLFloat.name || type.name === GraphQLBoolean.name || type.name === GraphQLID.name); +} \ No newline at end of file diff --git a/dist/type/schema.js b/dist/type/schema.js new file mode 100644 index 0000000000..cae7dd3f6f --- /dev/null +++ b/dist/type/schema.js @@ -0,0 +1,269 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isSchema = isSchema; +exports.GraphQLSchema = void 0; + +var _definition = require("./definition"); + +var _directives = require("./directives"); + +var _introspection = require("./introspection"); + +var _find = _interopRequireDefault(require("../jsutils/find")); + +var _instanceOf = _interopRequireDefault(require("../jsutils/instanceOf")); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +// eslint-disable-next-line no-redeclare +function isSchema(schema) { + return (0, _instanceOf.default)(schema, GraphQLSchema); +} +/** + * Schema Definition + * + * A Schema is created by supplying the root types of each type of operation, + * query and mutation (optional). A schema definition is then supplied to the + * validator and executor. + * + * Example: + * + * const MyAppSchema = new GraphQLSchema({ + * query: MyAppQueryRootType, + * mutation: MyAppMutationRootType, + * }) + * + * Note: If an array of `directives` are provided to GraphQLSchema, that will be + * the exact list of directives represented and allowed. If `directives` is not + * provided then a default set of the specified directives (e.g. @include and + * @skip) will be used. If you wish to provide *additional* directives to these + * specified directives, you must explicitly declare them. Example: + * + * const MyAppSchema = new GraphQLSchema({ + * ... + * directives: specifiedDirectives.concat([ myCustomDirective ]), + * }) + * + */ + + +var GraphQLSchema = +/*#__PURE__*/ +function () { + // Used as a cache for validateSchema(). + // Referenced by validateSchema(). + function GraphQLSchema(config) { + var _this = this; + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "_queryType", void 0); + + _defineProperty(this, "_mutationType", void 0); + + _defineProperty(this, "_subscriptionType", void 0); + + _defineProperty(this, "_directives", void 0); + + _defineProperty(this, "_typeMap", void 0); + + _defineProperty(this, "_implementations", void 0); + + _defineProperty(this, "_possibleTypeMap", void 0); + + _defineProperty(this, "__validationErrors", void 0); + + _defineProperty(this, "__allowedLegacyNames", void 0); + + // If this schema was built from a source known to be valid, then it may be + // marked with assumeValid to avoid an additional type system validation. + if (config && config.assumeValid) { + this.__validationErrors = []; + } else { + // Otherwise check for common mistakes during construction to produce + // clear and early error messages. + !(_typeof(config) === 'object') ? (0, _invariant.default)(0, 'Must provide configuration object.') : void 0; + !(!config.types || Array.isArray(config.types)) ? (0, _invariant.default)(0, "\"types\" must be Array if provided but got: ".concat(String(config.types), ".")) : void 0; + !(!config.directives || Array.isArray(config.directives)) ? (0, _invariant.default)(0, '"directives" must be Array if provided but got: ' + "".concat(String(config.directives), ".")) : void 0; + !(!config.allowedLegacyNames || Array.isArray(config.allowedLegacyNames)) ? (0, _invariant.default)(0, '"allowedLegacyNames" must be Array if provided but got: ' + "".concat(String(config.allowedLegacyNames), ".")) : void 0; + } + + this.__allowedLegacyNames = config.allowedLegacyNames; + this._queryType = config.query; + this._mutationType = config.mutation; + this._subscriptionType = config.subscription; // Provide specified directives (e.g. @include and @skip) by default. + + this._directives = config.directives || _directives.specifiedDirectives; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; // Build type map now to detect any errors within this schema. + + var initialTypes = [this.getQueryType(), this.getMutationType(), this.getSubscriptionType(), _introspection.__Schema]; + var types = config.types; + + if (types) { + initialTypes = initialTypes.concat(types); + } // Keep track of all types referenced within the schema. + + + var typeMap = Object.create(null); // First by deeply visiting all initial types. + + typeMap = initialTypes.reduce(typeMapReducer, typeMap); // Then by deeply visiting all directive types. + + typeMap = this._directives.reduce(typeMapDirectiveReducer, typeMap); // Storing the resulting map for reference by the schema. + + this._typeMap = typeMap; // Keep track of all implementations by interface name. + + this._implementations = Object.create(null); + Object.keys(this._typeMap).forEach(function (typeName) { + var type = _this._typeMap[typeName]; + + if ((0, _definition.isObjectType)(type)) { + type.getInterfaces().forEach(function (iface) { + if ((0, _definition.isInterfaceType)(iface)) { + var impls = _this._implementations[iface.name]; + + if (impls) { + impls.push(type); + } else { + _this._implementations[iface.name] = [type]; + } + } + }); + } else if ((0, _definition.isAbstractType)(type) && !_this._implementations[type.name]) { + _this._implementations[type.name] = []; + } + }); + } + + var _proto = GraphQLSchema.prototype; + + _proto.getQueryType = function getQueryType() { + return this._queryType; + }; + + _proto.getMutationType = function getMutationType() { + return this._mutationType; + }; + + _proto.getSubscriptionType = function getSubscriptionType() { + return this._subscriptionType; + }; + + _proto.getTypeMap = function getTypeMap() { + return this._typeMap; + }; + + _proto.getType = function getType(name) { + return this.getTypeMap()[name]; + }; + + _proto.getPossibleTypes = function getPossibleTypes(abstractType) { + if ((0, _definition.isUnionType)(abstractType)) { + return abstractType.getTypes(); + } + + return this._implementations[abstractType.name]; + }; + + _proto.isPossibleType = function isPossibleType(abstractType, possibleType) { + var possibleTypeMap = this._possibleTypeMap; + + if (!possibleTypeMap) { + this._possibleTypeMap = possibleTypeMap = Object.create(null); + } + + if (!possibleTypeMap[abstractType.name]) { + var possibleTypes = this.getPossibleTypes(abstractType); + possibleTypeMap[abstractType.name] = possibleTypes.reduce(function (map, type) { + return map[type.name] = true, map; + }, Object.create(null)); + } + + return Boolean(possibleTypeMap[abstractType.name][possibleType.name]); + }; + + _proto.getDirectives = function getDirectives() { + return this._directives; + }; + + _proto.getDirective = function getDirective(name) { + return (0, _find.default)(this.getDirectives(), function (directive) { + return directive.name === name; + }); + }; + + return GraphQLSchema; +}(); + +exports.GraphQLSchema = GraphQLSchema; + +function typeMapReducer(map, type) { + if (!type) { + return map; + } + + if ((0, _definition.isWrappingType)(type)) { + return typeMapReducer(map, type.ofType); + } + + if (map[type.name]) { + !(map[type.name] === type) ? (0, _invariant.default)(0, 'Schema must contain unique named types but contains multiple ' + "types named \"".concat(type.name, "\".")) : void 0; + return map; + } + + map[type.name] = type; + var reducedMap = map; + + if ((0, _definition.isUnionType)(type)) { + reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap); + } + + if ((0, _definition.isObjectType)(type)) { + reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap); + } + + if ((0, _definition.isObjectType)(type) || (0, _definition.isInterfaceType)(type)) { + (0, _objectValues.default)(type.getFields()).forEach(function (field) { + if (field.args) { + var fieldArgTypes = field.args.map(function (arg) { + return arg.type; + }); + reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap); + } + + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + if ((0, _definition.isInputObjectType)(type)) { + (0, _objectValues.default)(type.getFields()).forEach(function (field) { + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + return reducedMap; +} + +function typeMapDirectiveReducer(map, directive) { + // Directives are not validated until validateSchema() is called. + if (!(0, _directives.isDirective)(directive)) { + return map; + } + + return directive.args.reduce(function (_map, arg) { + return typeMapReducer(_map, arg.type); + }, map); +} \ No newline at end of file diff --git a/dist/type/schema.js.flow b/dist/type/schema.js.flow new file mode 100644 index 0000000000..96ac0167b3 --- /dev/null +++ b/dist/type/schema.js.flow @@ -0,0 +1,325 @@ +/** + * 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. + * + * @flow strict + */ + +import { + isAbstractType, + isObjectType, + isInterfaceType, + isUnionType, + isInputObjectType, + isWrappingType, +} from './definition'; +import type { + GraphQLType, + GraphQLNamedType, + GraphQLAbstractType, + GraphQLObjectType, +} from './definition'; +import type { + SchemaDefinitionNode, + SchemaExtensionNode, +} from '../language/ast'; +import { + GraphQLDirective, + isDirective, + specifiedDirectives, +} from './directives'; +import type { GraphQLError } from '../error/GraphQLError'; +import { __Schema } from './introspection'; +import find from '../jsutils/find'; +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import objectValues from '../jsutils/objectValues'; +import type { ObjMap } from '../jsutils/ObjMap'; + +/** + * Test if the given value is a GraphQL schema. + */ +declare function isSchema(schema: mixed): boolean %checks(schema instanceof + GraphQLSchema); +// eslint-disable-next-line no-redeclare +export function isSchema(schema) { + return instanceOf(schema, GraphQLSchema); +} + +/** + * Schema Definition + * + * A Schema is created by supplying the root types of each type of operation, + * query and mutation (optional). A schema definition is then supplied to the + * validator and executor. + * + * Example: + * + * const MyAppSchema = new GraphQLSchema({ + * query: MyAppQueryRootType, + * mutation: MyAppMutationRootType, + * }) + * + * Note: If an array of `directives` are provided to GraphQLSchema, that will be + * the exact list of directives represented and allowed. If `directives` is not + * provided then a default set of the specified directives (e.g. @include and + * @skip) will be used. If you wish to provide *additional* directives to these + * specified directives, you must explicitly declare them. Example: + * + * const MyAppSchema = new GraphQLSchema({ + * ... + * directives: specifiedDirectives.concat([ myCustomDirective ]), + * }) + * + */ +export class GraphQLSchema { + astNode: ?SchemaDefinitionNode; + extensionASTNodes: ?$ReadOnlyArray; + _queryType: ?GraphQLObjectType; + _mutationType: ?GraphQLObjectType; + _subscriptionType: ?GraphQLObjectType; + _directives: $ReadOnlyArray; + _typeMap: TypeMap; + _implementations: ObjMap>; + _possibleTypeMap: ?ObjMap>; + // Used as a cache for validateSchema(). + __validationErrors: ?$ReadOnlyArray; + // Referenced by validateSchema(). + __allowedLegacyNames: ?$ReadOnlyArray; + + constructor(config: GraphQLSchemaConfig): void { + // If this schema was built from a source known to be valid, then it may be + // marked with assumeValid to avoid an additional type system validation. + if (config && config.assumeValid) { + this.__validationErrors = []; + } else { + // Otherwise check for common mistakes during construction to produce + // clear and early error messages. + invariant( + typeof config === 'object', + 'Must provide configuration object.', + ); + invariant( + !config.types || Array.isArray(config.types), + `"types" must be Array if provided but got: ${String(config.types)}.`, + ); + invariant( + !config.directives || Array.isArray(config.directives), + '"directives" must be Array if provided but got: ' + + `${String(config.directives)}.`, + ); + invariant( + !config.allowedLegacyNames || Array.isArray(config.allowedLegacyNames), + '"allowedLegacyNames" must be Array if provided but got: ' + + `${String(config.allowedLegacyNames)}.`, + ); + } + + this.__allowedLegacyNames = config.allowedLegacyNames; + this._queryType = config.query; + this._mutationType = config.mutation; + this._subscriptionType = config.subscription; + // Provide specified directives (e.g. @include and @skip) by default. + this._directives = config.directives || specifiedDirectives; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; + + // Build type map now to detect any errors within this schema. + let initialTypes: Array = [ + this.getQueryType(), + this.getMutationType(), + this.getSubscriptionType(), + __Schema, + ]; + + const types = config.types; + if (types) { + initialTypes = initialTypes.concat(types); + } + + // Keep track of all types referenced within the schema. + let typeMap: TypeMap = Object.create(null); + + // First by deeply visiting all initial types. + typeMap = initialTypes.reduce(typeMapReducer, typeMap); + + // Then by deeply visiting all directive types. + typeMap = this._directives.reduce(typeMapDirectiveReducer, typeMap); + + // Storing the resulting map for reference by the schema. + this._typeMap = typeMap; + + // Keep track of all implementations by interface name. + this._implementations = Object.create(null); + Object.keys(this._typeMap).forEach(typeName => { + const type = this._typeMap[typeName]; + if (isObjectType(type)) { + type.getInterfaces().forEach(iface => { + if (isInterfaceType(iface)) { + const impls = this._implementations[iface.name]; + if (impls) { + impls.push(type); + } else { + this._implementations[iface.name] = [type]; + } + } + }); + } else if (isAbstractType(type) && !this._implementations[type.name]) { + this._implementations[type.name] = []; + } + }); + } + + getQueryType(): ?GraphQLObjectType { + return this._queryType; + } + + getMutationType(): ?GraphQLObjectType { + return this._mutationType; + } + + getSubscriptionType(): ?GraphQLObjectType { + return this._subscriptionType; + } + + getTypeMap(): TypeMap { + return this._typeMap; + } + + getType(name: string): ?GraphQLNamedType { + return this.getTypeMap()[name]; + } + + getPossibleTypes( + abstractType: GraphQLAbstractType, + ): $ReadOnlyArray { + if (isUnionType(abstractType)) { + return abstractType.getTypes(); + } + return this._implementations[abstractType.name]; + } + + isPossibleType( + abstractType: GraphQLAbstractType, + possibleType: GraphQLObjectType, + ): boolean { + let possibleTypeMap = this._possibleTypeMap; + if (!possibleTypeMap) { + this._possibleTypeMap = possibleTypeMap = Object.create(null); + } + + if (!possibleTypeMap[abstractType.name]) { + const possibleTypes = this.getPossibleTypes(abstractType); + possibleTypeMap[abstractType.name] = possibleTypes.reduce( + (map, type) => ((map[type.name] = true), map), + Object.create(null), + ); + } + + return Boolean(possibleTypeMap[abstractType.name][possibleType.name]); + } + + getDirectives(): $ReadOnlyArray { + return this._directives; + } + + getDirective(name: string): ?GraphQLDirective { + return find(this.getDirectives(), directive => directive.name === name); + } +} + +type TypeMap = ObjMap; + +export type GraphQLSchemaValidationOptions = {| + /** + * When building a schema from a GraphQL service's introspection result, it + * might be safe to assume the schema is valid. Set to true to assume the + * produced schema is valid. + * + * Default: false + */ + assumeValid?: boolean, + + /** + * If provided, the schema will consider fields or types with names included + * in this list valid, even if they do not adhere to the specification's + * schema validation rules. + * + * This option is provided to ease adoption and may be removed in a future + * major release. + */ + allowedLegacyNames?: ?$ReadOnlyArray, +|}; + +export type GraphQLSchemaConfig = { + query?: ?GraphQLObjectType, + mutation?: ?GraphQLObjectType, + subscription?: ?GraphQLObjectType, + types?: ?Array, + directives?: ?Array, + astNode?: ?SchemaDefinitionNode, + extensionASTNodes?: ?$ReadOnlyArray, + ...GraphQLSchemaValidationOptions, +}; + +function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap { + if (!type) { + return map; + } + if (isWrappingType(type)) { + return typeMapReducer(map, type.ofType); + } + if (map[type.name]) { + invariant( + map[type.name] === type, + 'Schema must contain unique named types but contains multiple ' + + `types named "${type.name}".`, + ); + return map; + } + map[type.name] = type; + + let reducedMap = map; + + if (isUnionType(type)) { + reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap); + } + + if (isObjectType(type)) { + reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap); + } + + if (isObjectType(type) || isInterfaceType(type)) { + objectValues(type.getFields()).forEach(field => { + if (field.args) { + const fieldArgTypes = field.args.map(arg => arg.type); + reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap); + } + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + if (isInputObjectType(type)) { + objectValues(type.getFields()).forEach(field => { + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + return reducedMap; +} + +function typeMapDirectiveReducer( + map: TypeMap, + directive: ?GraphQLDirective, +): TypeMap { + // Directives are not validated until validateSchema() is called. + if (!isDirective(directive)) { + return map; + } + return directive.args.reduce( + (_map, arg) => typeMapReducer(_map, arg.type), + map, + ); +} diff --git a/dist/type/schema.mjs b/dist/type/schema.mjs new file mode 100644 index 0000000000..26594bda56 --- /dev/null +++ b/dist/type/schema.mjs @@ -0,0 +1,257 @@ +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { isAbstractType, isObjectType, isInterfaceType, isUnionType, isInputObjectType, isWrappingType } from './definition'; +import { GraphQLDirective, isDirective, specifiedDirectives } from './directives'; +import { __Schema } from './introspection'; +import find from '../jsutils/find'; +import instanceOf from '../jsutils/instanceOf'; +import invariant from '../jsutils/invariant'; +import objectValues from '../jsutils/objectValues'; +// eslint-disable-next-line no-redeclare +export function isSchema(schema) { + return instanceOf(schema, GraphQLSchema); +} +/** + * Schema Definition + * + * A Schema is created by supplying the root types of each type of operation, + * query and mutation (optional). A schema definition is then supplied to the + * validator and executor. + * + * Example: + * + * const MyAppSchema = new GraphQLSchema({ + * query: MyAppQueryRootType, + * mutation: MyAppMutationRootType, + * }) + * + * Note: If an array of `directives` are provided to GraphQLSchema, that will be + * the exact list of directives represented and allowed. If `directives` is not + * provided then a default set of the specified directives (e.g. @include and + * @skip) will be used. If you wish to provide *additional* directives to these + * specified directives, you must explicitly declare them. Example: + * + * const MyAppSchema = new GraphQLSchema({ + * ... + * directives: specifiedDirectives.concat([ myCustomDirective ]), + * }) + * + */ + +export var GraphQLSchema = +/*#__PURE__*/ +function () { + // Used as a cache for validateSchema(). + // Referenced by validateSchema(). + function GraphQLSchema(config) { + var _this = this; + + _defineProperty(this, "astNode", void 0); + + _defineProperty(this, "extensionASTNodes", void 0); + + _defineProperty(this, "_queryType", void 0); + + _defineProperty(this, "_mutationType", void 0); + + _defineProperty(this, "_subscriptionType", void 0); + + _defineProperty(this, "_directives", void 0); + + _defineProperty(this, "_typeMap", void 0); + + _defineProperty(this, "_implementations", void 0); + + _defineProperty(this, "_possibleTypeMap", void 0); + + _defineProperty(this, "__validationErrors", void 0); + + _defineProperty(this, "__allowedLegacyNames", void 0); + + // If this schema was built from a source known to be valid, then it may be + // marked with assumeValid to avoid an additional type system validation. + if (config && config.assumeValid) { + this.__validationErrors = []; + } else { + // Otherwise check for common mistakes during construction to produce + // clear and early error messages. + !(_typeof(config) === 'object') ? invariant(0, 'Must provide configuration object.') : void 0; + !(!config.types || Array.isArray(config.types)) ? invariant(0, "\"types\" must be Array if provided but got: ".concat(String(config.types), ".")) : void 0; + !(!config.directives || Array.isArray(config.directives)) ? invariant(0, '"directives" must be Array if provided but got: ' + "".concat(String(config.directives), ".")) : void 0; + !(!config.allowedLegacyNames || Array.isArray(config.allowedLegacyNames)) ? invariant(0, '"allowedLegacyNames" must be Array if provided but got: ' + "".concat(String(config.allowedLegacyNames), ".")) : void 0; + } + + this.__allowedLegacyNames = config.allowedLegacyNames; + this._queryType = config.query; + this._mutationType = config.mutation; + this._subscriptionType = config.subscription; // Provide specified directives (e.g. @include and @skip) by default. + + this._directives = config.directives || specifiedDirectives; + this.astNode = config.astNode; + this.extensionASTNodes = config.extensionASTNodes; // Build type map now to detect any errors within this schema. + + var initialTypes = [this.getQueryType(), this.getMutationType(), this.getSubscriptionType(), __Schema]; + var types = config.types; + + if (types) { + initialTypes = initialTypes.concat(types); + } // Keep track of all types referenced within the schema. + + + var typeMap = Object.create(null); // First by deeply visiting all initial types. + + typeMap = initialTypes.reduce(typeMapReducer, typeMap); // Then by deeply visiting all directive types. + + typeMap = this._directives.reduce(typeMapDirectiveReducer, typeMap); // Storing the resulting map for reference by the schema. + + this._typeMap = typeMap; // Keep track of all implementations by interface name. + + this._implementations = Object.create(null); + Object.keys(this._typeMap).forEach(function (typeName) { + var type = _this._typeMap[typeName]; + + if (isObjectType(type)) { + type.getInterfaces().forEach(function (iface) { + if (isInterfaceType(iface)) { + var impls = _this._implementations[iface.name]; + + if (impls) { + impls.push(type); + } else { + _this._implementations[iface.name] = [type]; + } + } + }); + } else if (isAbstractType(type) && !_this._implementations[type.name]) { + _this._implementations[type.name] = []; + } + }); + } + + var _proto = GraphQLSchema.prototype; + + _proto.getQueryType = function getQueryType() { + return this._queryType; + }; + + _proto.getMutationType = function getMutationType() { + return this._mutationType; + }; + + _proto.getSubscriptionType = function getSubscriptionType() { + return this._subscriptionType; + }; + + _proto.getTypeMap = function getTypeMap() { + return this._typeMap; + }; + + _proto.getType = function getType(name) { + return this.getTypeMap()[name]; + }; + + _proto.getPossibleTypes = function getPossibleTypes(abstractType) { + if (isUnionType(abstractType)) { + return abstractType.getTypes(); + } + + return this._implementations[abstractType.name]; + }; + + _proto.isPossibleType = function isPossibleType(abstractType, possibleType) { + var possibleTypeMap = this._possibleTypeMap; + + if (!possibleTypeMap) { + this._possibleTypeMap = possibleTypeMap = Object.create(null); + } + + if (!possibleTypeMap[abstractType.name]) { + var possibleTypes = this.getPossibleTypes(abstractType); + possibleTypeMap[abstractType.name] = possibleTypes.reduce(function (map, type) { + return map[type.name] = true, map; + }, Object.create(null)); + } + + return Boolean(possibleTypeMap[abstractType.name][possibleType.name]); + }; + + _proto.getDirectives = function getDirectives() { + return this._directives; + }; + + _proto.getDirective = function getDirective(name) { + return find(this.getDirectives(), function (directive) { + return directive.name === name; + }); + }; + + return GraphQLSchema; +}(); + +function typeMapReducer(map, type) { + if (!type) { + return map; + } + + if (isWrappingType(type)) { + return typeMapReducer(map, type.ofType); + } + + if (map[type.name]) { + !(map[type.name] === type) ? invariant(0, 'Schema must contain unique named types but contains multiple ' + "types named \"".concat(type.name, "\".")) : void 0; + return map; + } + + map[type.name] = type; + var reducedMap = map; + + if (isUnionType(type)) { + reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap); + } + + if (isObjectType(type)) { + reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap); + } + + if (isObjectType(type) || isInterfaceType(type)) { + objectValues(type.getFields()).forEach(function (field) { + if (field.args) { + var fieldArgTypes = field.args.map(function (arg) { + return arg.type; + }); + reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap); + } + + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + if (isInputObjectType(type)) { + objectValues(type.getFields()).forEach(function (field) { + reducedMap = typeMapReducer(reducedMap, field.type); + }); + } + + return reducedMap; +} + +function typeMapDirectiveReducer(map, directive) { + // Directives are not validated until validateSchema() is called. + if (!isDirective(directive)) { + return map; + } + + return directive.args.reduce(function (_map, arg) { + return typeMapReducer(_map, arg.type); + }, map); +} \ No newline at end of file diff --git a/dist/type/validate.js b/dist/type/validate.js new file mode 100644 index 0000000000..7c3614fb14 --- /dev/null +++ b/dist/type/validate.js @@ -0,0 +1,571 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.validateSchema = validateSchema; +exports.assertValidSchema = assertValidSchema; + +var _definition = require("./definition"); + +var _directives = require("./directives"); + +var _introspection = require("./introspection"); + +var _schema = require("./schema"); + +var _find = _interopRequireDefault(require("../jsutils/find")); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _GraphQLError = require("../error/GraphQLError"); + +var _assertValidName = require("../utilities/assertValidName"); + +var _typeComparators = require("../utilities/typeComparators"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Implements the "Type Validation" sub-sections of the specification's + * "Type System" section. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the Schema is valid. + */ +function validateSchema(schema) { + // First check to ensure the provided value is in fact a GraphQLSchema. + !(0, _schema.isSchema)(schema) ? (0, _invariant.default)(0, "Expected ".concat(String(schema), " to be a GraphQL schema.")) : void 0; // If this Schema has already been validated, return the previous results. + + if (schema.__validationErrors) { + return schema.__validationErrors; + } // Validate the schema, producing a list of errors. + + + var context = new SchemaValidationContext(schema); + validateRootTypes(context); + validateDirectives(context); + validateTypes(context); // Persist the results of validation before returning to ensure validation + // does not run multiple times for this schema. + + var errors = context.getErrors(); + schema.__validationErrors = errors; + return errors; +} +/** + * Utility function which asserts a schema is valid by throwing an error if + * it is invalid. + */ + + +function assertValidSchema(schema) { + var errors = validateSchema(schema); + + if (errors.length !== 0) { + throw new Error(errors.map(function (error) { + return error.message; + }).join('\n\n')); + } +} + +var SchemaValidationContext = +/*#__PURE__*/ +function () { + function SchemaValidationContext(schema) { + _defineProperty(this, "_errors", void 0); + + _defineProperty(this, "schema", void 0); + + this._errors = []; + this.schema = schema; + } + + var _proto = SchemaValidationContext.prototype; + + _proto.reportError = function reportError(message, nodes) { + var _nodes = (Array.isArray(nodes) ? nodes : [nodes]).filter(Boolean); + + this.addError(new _GraphQLError.GraphQLError(message, _nodes)); + }; + + _proto.addError = function addError(error) { + this._errors.push(error); + }; + + _proto.getErrors = function getErrors() { + return this._errors; + }; + + return SchemaValidationContext; +}(); + +function validateRootTypes(context) { + var schema = context.schema; + var queryType = schema.getQueryType(); + + if (!queryType) { + context.reportError("Query root type must be provided.", schema.astNode); + } else if (!(0, _definition.isObjectType)(queryType)) { + context.reportError("Query root type must be Object type, it cannot be ".concat(String(queryType), "."), getOperationTypeNode(schema, queryType, 'query')); + } + + var mutationType = schema.getMutationType(); + + if (mutationType && !(0, _definition.isObjectType)(mutationType)) { + context.reportError('Mutation root type must be Object type if provided, it cannot be ' + "".concat(String(mutationType), "."), getOperationTypeNode(schema, mutationType, 'mutation')); + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType && !(0, _definition.isObjectType)(subscriptionType)) { + context.reportError('Subscription root type must be Object type if provided, it cannot be ' + "".concat(String(subscriptionType), "."), getOperationTypeNode(schema, subscriptionType, 'subscription')); + } +} + +function getOperationTypeNode(schema, type, operation) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = getAllNodes(schema)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var node = _step.value; + + if (node.operationTypes) { + var _iteratorNormalCompletion2 = true; + var _didIteratorError2 = false; + var _iteratorError2 = undefined; + + try { + for (var _iterator2 = node.operationTypes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { + var operationType = _step2.value; + + if (operationType.operation === operation) { + return operationType.type; + } + } + } catch (err) { + _didIteratorError2 = true; + _iteratorError2 = err; + } finally { + try { + if (!_iteratorNormalCompletion2 && _iterator2.return != null) { + _iterator2.return(); + } + } finally { + if (_didIteratorError2) { + throw _iteratorError2; + } + } + } + } + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return type.astNode; +} + +function validateDirectives(context) { + var directives = context.schema.getDirectives(); + directives.forEach(function (directive) { + // Ensure all directives are in fact GraphQL directives. + if (!(0, _directives.isDirective)(directive)) { + context.reportError("Expected directive but got: ".concat(String(directive), "."), directive && directive.astNode); + return; + } // Ensure they are named correctly. + + + validateName(context, directive); // TODO: Ensure proper locations. + // Ensure the arguments are valid. + + var argNames = Object.create(null); + directive.args.forEach(function (arg) { + var argName = arg.name; // Ensure they are named correctly. + + validateName(context, arg); // Ensure they are unique per directive. + + if (argNames[argName]) { + context.reportError("Argument @".concat(directive.name, "(").concat(argName, ":) can only be defined once."), getAllDirectiveArgNodes(directive, argName)); + return; // continue loop + } + + argNames[argName] = true; // Ensure the type is an input type. + + if (!(0, _definition.isInputType)(arg.type)) { + context.reportError("The type of @".concat(directive.name, "(").concat(argName, ":) must be Input Type ") + "but got: ".concat(String(arg.type), "."), getDirectiveArgTypeNode(directive, argName)); + } + }); + }); +} + +function validateName(context, node) { + // If a schema explicitly allows some legacy name which is no longer valid, + // allow it to be assumed valid. + if (context.schema.__allowedLegacyNames && context.schema.__allowedLegacyNames.indexOf(node.name) !== -1) { + return; + } // Ensure names are valid, however introspection types opt out. + + + var error = (0, _assertValidName.isValidNameError)(node.name, node.astNode || undefined); + + if (error) { + context.addError(error); + } +} + +function validateTypes(context) { + var typeMap = context.schema.getTypeMap(); + (0, _objectValues.default)(typeMap).forEach(function (type) { + // Ensure all provided types are in fact GraphQL type. + if (!(0, _definition.isNamedType)(type)) { + context.reportError("Expected GraphQL named type but got: ".concat(String(type), "."), type && type.astNode); + return; + } // Ensure it is named correctly (excluding introspection types). + + + if (!(0, _introspection.isIntrospectionType)(type)) { + validateName(context, type); + } + + if ((0, _definition.isObjectType)(type)) { + // Ensure fields are valid + validateFields(context, type); // Ensure objects implement the interfaces they claim to. + + validateObjectInterfaces(context, type); + } else if ((0, _definition.isInterfaceType)(type)) { + // Ensure fields are valid. + validateFields(context, type); // Ensure Interfaces include at least 1 Object type. + + validateInterfaces(context, type); + } else if ((0, _definition.isUnionType)(type)) { + // Ensure Unions include valid member types. + validateUnionMembers(context, type); + } else if ((0, _definition.isEnumType)(type)) { + // Ensure Enums have valid values. + validateEnumValues(context, type); + } else if ((0, _definition.isInputObjectType)(type)) { + // Ensure Input Object fields are valid. + validateInputFields(context, type); + } + }); +} + +function validateFields(context, type) { + var fields = (0, _objectValues.default)(type.getFields()); // Objects and Interfaces both must define one or more fields. + + if (fields.length === 0) { + context.reportError("Type ".concat(type.name, " must define one or more fields."), getAllNodes(type)); + } + + fields.forEach(function (field) { + // Ensure they are named correctly. + validateName(context, field); // Ensure they were defined at most once. + + var fieldNodes = getAllFieldNodes(type, field.name); + + if (fieldNodes.length > 1) { + context.reportError("Field ".concat(type.name, ".").concat(field.name, " can only be defined once."), fieldNodes); + return; // continue loop + } // Ensure the type is an output type + + + if (!(0, _definition.isOutputType)(field.type)) { + context.reportError("The type of ".concat(type.name, ".").concat(field.name, " must be Output Type ") + "but got: ".concat(String(field.type), "."), getFieldTypeNode(type, field.name)); + } // Ensure the arguments are valid + + + var argNames = Object.create(null); + field.args.forEach(function (arg) { + var argName = arg.name; // Ensure they are named correctly. + + validateName(context, arg); // Ensure they are unique per field. + + if (argNames[argName]) { + context.reportError("Field argument ".concat(type.name, ".").concat(field.name, "(").concat(argName, ":) can only ") + 'be defined once.', getAllFieldArgNodes(type, field.name, argName)); + } + + argNames[argName] = true; // Ensure the type is an input type + + if (!(0, _definition.isInputType)(arg.type)) { + context.reportError("The type of ".concat(type.name, ".").concat(field.name, "(").concat(argName, ":) must be Input ") + "Type but got: ".concat(String(arg.type), "."), getFieldArgTypeNode(type, field.name, argName)); + } + }); + }); +} + +function validateObjectInterfaces(context, object) { + var implementedTypeNames = Object.create(null); + object.getInterfaces().forEach(function (iface) { + if (!(0, _definition.isInterfaceType)(iface)) { + context.reportError("Type ".concat(String(object), " must only implement Interface types, ") + "it cannot implement ".concat(String(iface), "."), getImplementsInterfaceNode(object, iface)); + return; + } + + if (implementedTypeNames[iface.name]) { + context.reportError("Type ".concat(object.name, " can only implement ").concat(iface.name, " once."), getAllImplementsInterfaceNodes(object, iface)); + return; // continue loop + } + + implementedTypeNames[iface.name] = true; + validateObjectImplementsInterface(context, object, iface); + }); +} + +function validateInterfaces(context, iface) { + var possibleTypes = context.schema.getPossibleTypes(iface); + + if (possibleTypes.length === 0) { + context.reportError("Interface ".concat(iface.name, " must be implemented ") + "by at least one Object type.", iface.astNode); + } +} + +function validateObjectImplementsInterface(context, object, iface) { + var objectFieldMap = object.getFields(); + var ifaceFieldMap = iface.getFields(); // Assert each interface field is implemented. + + Object.keys(ifaceFieldMap).forEach(function (fieldName) { + var objectField = objectFieldMap[fieldName]; + var ifaceField = ifaceFieldMap[fieldName]; // Assert interface field exists on object. + + if (!objectField) { + context.reportError("Interface field ".concat(iface.name, ".").concat(fieldName, " expected but ") + "".concat(object.name, " does not provide it."), [getFieldNode(iface, fieldName), object.astNode]); // Continue loop over fields. + + return; + } // Assert interface field type is satisfied by object field type, by being + // a valid subtype. (covariant) + + + if (!(0, _typeComparators.isTypeSubTypeOf)(context.schema, objectField.type, ifaceField.type)) { + context.reportError("Interface field ".concat(iface.name, ".").concat(fieldName, " expects type ") + "".concat(String(ifaceField.type), " but ").concat(object.name, ".").concat(fieldName, " ") + "is type ".concat(String(objectField.type), "."), [getFieldTypeNode(iface, fieldName), getFieldTypeNode(object, fieldName)]); + } // Assert each interface field arg is implemented. + + + ifaceField.args.forEach(function (ifaceArg) { + var argName = ifaceArg.name; + var objectArg = (0, _find.default)(objectField.args, function (arg) { + return arg.name === argName; + }); // Assert interface field arg exists on object field. + + if (!objectArg) { + context.reportError("Interface field argument ".concat(iface.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "expected but ".concat(object.name, ".").concat(fieldName, " does not provide it."), [getFieldArgNode(iface, fieldName, argName), getFieldNode(object, fieldName)]); // Continue loop over arguments. + + return; + } // Assert interface field arg type matches object field arg type. + // (invariant) + // TODO: change to contravariant? + + + if (!(0, _typeComparators.isEqualType)(ifaceArg.type, objectArg.type)) { + context.reportError("Interface field argument ".concat(iface.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "expects type ".concat(String(ifaceArg.type), " but ") + "".concat(object.name, ".").concat(fieldName, "(").concat(argName, ":) is type ") + "".concat(String(objectArg.type), "."), [getFieldArgTypeNode(iface, fieldName, argName), getFieldArgTypeNode(object, fieldName, argName)]); + } // TODO: validate default values? + + }); // Assert additional arguments must not be required. + + objectField.args.forEach(function (objectArg) { + var argName = objectArg.name; + var ifaceArg = (0, _find.default)(ifaceField.args, function (arg) { + return arg.name === argName; + }); + + if (!ifaceArg && (0, _definition.isNonNullType)(objectArg.type)) { + context.reportError("Object field argument ".concat(object.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "is of required type ".concat(String(objectArg.type), " but is not also ") + "provided by the Interface field ".concat(iface.name, ".").concat(fieldName, "."), [getFieldArgTypeNode(object, fieldName, argName), getFieldNode(iface, fieldName)]); + } + }); + }); +} + +function validateUnionMembers(context, union) { + var memberTypes = union.getTypes(); + + if (memberTypes.length === 0) { + context.reportError("Union type ".concat(union.name, " must define one or more member types."), union.astNode); + } + + var includedTypeNames = Object.create(null); + memberTypes.forEach(function (memberType) { + if (includedTypeNames[memberType.name]) { + context.reportError("Union type ".concat(union.name, " can only include type ") + "".concat(memberType.name, " once."), getUnionMemberTypeNodes(union, memberType.name)); + return; // continue loop + } + + includedTypeNames[memberType.name] = true; + + if (!(0, _definition.isObjectType)(memberType)) { + context.reportError("Union type ".concat(union.name, " can only include Object types, ") + "it cannot include ".concat(String(memberType), "."), getUnionMemberTypeNodes(union, String(memberType))); + } + }); +} + +function validateEnumValues(context, enumType) { + var enumValues = enumType.getValues(); + + if (enumValues.length === 0) { + context.reportError("Enum type ".concat(enumType.name, " must define one or more values."), enumType.astNode); + } + + enumValues.forEach(function (enumValue) { + var valueName = enumValue.name; // Ensure no duplicates. + + var allNodes = getEnumValueNodes(enumType, valueName); + + if (allNodes && allNodes.length > 1) { + context.reportError("Enum type ".concat(enumType.name, " can include value ").concat(valueName, " only once."), allNodes); + } // Ensure valid name. + + + validateName(context, enumValue); + + if (valueName === 'true' || valueName === 'false' || valueName === 'null') { + context.reportError("Enum type ".concat(enumType.name, " cannot include value: ").concat(valueName, "."), enumValue.astNode); + } + }); +} + +function validateInputFields(context, inputObj) { + var fields = (0, _objectValues.default)(inputObj.getFields()); + + if (fields.length === 0) { + context.reportError("Input Object type ".concat(inputObj.name, " must define one or more fields."), inputObj.astNode); + } // Ensure the arguments are valid + + + fields.forEach(function (field) { + // Ensure they are named correctly. + validateName(context, field); // TODO: Ensure they are unique per field. + // Ensure the type is an input type + + if (!(0, _definition.isInputType)(field.type)) { + context.reportError("The type of ".concat(inputObj.name, ".").concat(field.name, " must be Input Type ") + "but got: ".concat(String(field.type), "."), field.astNode && field.astNode.type); + } + }); +} + +function getAllNodes(object) { + var astNode = object.astNode, + extensionASTNodes = object.extensionASTNodes; + return astNode ? extensionASTNodes ? [astNode].concat(extensionASTNodes) : [astNode] : extensionASTNodes || []; +} + +function getImplementsInterfaceNode(type, iface) { + return getAllImplementsInterfaceNodes(type, iface)[0]; +} + +function getAllImplementsInterfaceNodes(type, iface) { + var implementsNodes = []; + var astNodes = getAllNodes(type); + + for (var i = 0; i < astNodes.length; i++) { + var _astNode = astNodes[i]; + + if (_astNode && _astNode.interfaces) { + _astNode.interfaces.forEach(function (node) { + if (node.name.value === iface.name) { + implementsNodes.push(node); + } + }); + } + } + + return implementsNodes; +} + +function getFieldNode(type, fieldName) { + return getAllFieldNodes(type, fieldName)[0]; +} + +function getAllFieldNodes(type, fieldName) { + var fieldNodes = []; + var astNodes = getAllNodes(type); + + for (var i = 0; i < astNodes.length; i++) { + var _astNode2 = astNodes[i]; + + if (_astNode2 && _astNode2.fields) { + _astNode2.fields.forEach(function (node) { + if (node.name.value === fieldName) { + fieldNodes.push(node); + } + }); + } + } + + return fieldNodes; +} + +function getFieldTypeNode(type, fieldName) { + var fieldNode = getFieldNode(type, fieldName); + return fieldNode && fieldNode.type; +} + +function getFieldArgNode(type, fieldName, argName) { + return getAllFieldArgNodes(type, fieldName, argName)[0]; +} + +function getAllFieldArgNodes(type, fieldName, argName) { + var argNodes = []; + var fieldNode = getFieldNode(type, fieldName); + + if (fieldNode && fieldNode.arguments) { + fieldNode.arguments.forEach(function (node) { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + + return argNodes; +} + +function getFieldArgTypeNode(type, fieldName, argName) { + var fieldArgNode = getFieldArgNode(type, fieldName, argName); + return fieldArgNode && fieldArgNode.type; +} + +function getAllDirectiveArgNodes(directive, argName) { + var argNodes = []; + var directiveNode = directive.astNode; + + if (directiveNode && directiveNode.arguments) { + directiveNode.arguments.forEach(function (node) { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + + return argNodes; +} + +function getDirectiveArgTypeNode(directive, argName) { + var argNode = getAllDirectiveArgNodes(directive, argName)[0]; + return argNode && argNode.type; +} + +function getUnionMemberTypeNodes(union, typeName) { + return union.astNode && union.astNode.types && union.astNode.types.filter(function (type) { + return type.name.value === typeName; + }); +} + +function getEnumValueNodes(enumType, valueName) { + return enumType.astNode && enumType.astNode.values && enumType.astNode.values.filter(function (value) { + return value.name.value === valueName; + }); +} \ No newline at end of file diff --git a/dist/type/validate.js.flow b/dist/type/validate.js.flow new file mode 100644 index 0000000000..830418d122 --- /dev/null +++ b/dist/type/validate.js.flow @@ -0,0 +1,731 @@ +/** + * 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. + * + * @flow strict + */ + +import { + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, + isNonNullType, + isNamedType, + isInputType, + isOutputType, +} from './definition'; +import type { + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, +} from './definition'; +import { isDirective } from './directives'; +import type { GraphQLDirective } from './directives'; +import { isIntrospectionType } from './introspection'; +import { isSchema } from './schema'; +import type { GraphQLSchema } from './schema'; +import find from '../jsutils/find'; +import invariant from '../jsutils/invariant'; +import objectValues from '../jsutils/objectValues'; +import { GraphQLError } from '../error/GraphQLError'; +import type { + ASTNode, + FieldDefinitionNode, + EnumValueDefinitionNode, + InputValueDefinitionNode, + NamedTypeNode, + TypeNode, +} from '../language/ast'; +import { isValidNameError } from '../utilities/assertValidName'; +import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators'; + +/** + * Implements the "Type Validation" sub-sections of the specification's + * "Type System" section. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the Schema is valid. + */ +export function validateSchema( + schema: GraphQLSchema, +): $ReadOnlyArray { + // First check to ensure the provided value is in fact a GraphQLSchema. + invariant( + isSchema(schema), + `Expected ${String(schema)} to be a GraphQL schema.`, + ); + + // If this Schema has already been validated, return the previous results. + if (schema.__validationErrors) { + return schema.__validationErrors; + } + + // Validate the schema, producing a list of errors. + const context = new SchemaValidationContext(schema); + validateRootTypes(context); + validateDirectives(context); + validateTypes(context); + + // Persist the results of validation before returning to ensure validation + // does not run multiple times for this schema. + const errors = context.getErrors(); + schema.__validationErrors = errors; + return errors; +} + +/** + * Utility function which asserts a schema is valid by throwing an error if + * it is invalid. + */ +export function assertValidSchema(schema: GraphQLSchema): void { + const errors = validateSchema(schema); + if (errors.length !== 0) { + throw new Error(errors.map(error => error.message).join('\n\n')); + } +} + +class SchemaValidationContext { + +_errors: Array; + +schema: GraphQLSchema; + + constructor(schema) { + this._errors = []; + this.schema = schema; + } + + reportError( + message: string, + nodes?: $ReadOnlyArray | ?ASTNode, + ): void { + const _nodes = (Array.isArray(nodes) ? nodes : [nodes]).filter(Boolean); + this.addError(new GraphQLError(message, _nodes)); + } + + addError(error: GraphQLError): void { + this._errors.push(error); + } + + getErrors(): $ReadOnlyArray { + return this._errors; + } +} + +function validateRootTypes(context) { + const schema = context.schema; + const queryType = schema.getQueryType(); + if (!queryType) { + context.reportError(`Query root type must be provided.`, schema.astNode); + } else if (!isObjectType(queryType)) { + context.reportError( + `Query root type must be Object type, it cannot be ${String(queryType)}.`, + getOperationTypeNode(schema, queryType, 'query'), + ); + } + + const mutationType = schema.getMutationType(); + if (mutationType && !isObjectType(mutationType)) { + context.reportError( + 'Mutation root type must be Object type if provided, it cannot be ' + + `${String(mutationType)}.`, + getOperationTypeNode(schema, mutationType, 'mutation'), + ); + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType && !isObjectType(subscriptionType)) { + context.reportError( + 'Subscription root type must be Object type if provided, it cannot be ' + + `${String(subscriptionType)}.`, + getOperationTypeNode(schema, subscriptionType, 'subscription'), + ); + } +} + +function getOperationTypeNode( + schema: GraphQLSchema, + type: GraphQLObjectType, + operation: string, +): ?ASTNode { + for (const node of getAllNodes(schema)) { + if (node.operationTypes) { + for (const operationType of node.operationTypes) { + if (operationType.operation === operation) { + return operationType.type; + } + } + } + } + + return type.astNode; +} + +function validateDirectives(context: SchemaValidationContext): void { + const directives = context.schema.getDirectives(); + directives.forEach(directive => { + // Ensure all directives are in fact GraphQL directives. + if (!isDirective(directive)) { + context.reportError( + `Expected directive but got: ${String(directive)}.`, + directive && directive.astNode, + ); + return; + } + + // Ensure they are named correctly. + validateName(context, directive); + + // TODO: Ensure proper locations. + + // Ensure the arguments are valid. + const argNames = Object.create(null); + directive.args.forEach(arg => { + const argName = arg.name; + + // Ensure they are named correctly. + validateName(context, arg); + + // Ensure they are unique per directive. + if (argNames[argName]) { + context.reportError( + `Argument @${directive.name}(${argName}:) can only be defined once.`, + getAllDirectiveArgNodes(directive, argName), + ); + return; // continue loop + } + argNames[argName] = true; + + // Ensure the type is an input type. + if (!isInputType(arg.type)) { + context.reportError( + `The type of @${directive.name}(${argName}:) must be Input Type ` + + `but got: ${String(arg.type)}.`, + getDirectiveArgTypeNode(directive, argName), + ); + } + }); + }); +} + +function validateName( + context: SchemaValidationContext, + node: { +name: string, +astNode: ?ASTNode }, +): void { + // If a schema explicitly allows some legacy name which is no longer valid, + // allow it to be assumed valid. + if ( + context.schema.__allowedLegacyNames && + context.schema.__allowedLegacyNames.indexOf(node.name) !== -1 + ) { + return; + } + // Ensure names are valid, however introspection types opt out. + const error = isValidNameError(node.name, node.astNode || undefined); + if (error) { + context.addError(error); + } +} + +function validateTypes(context: SchemaValidationContext): void { + const typeMap = context.schema.getTypeMap(); + objectValues(typeMap).forEach(type => { + // Ensure all provided types are in fact GraphQL type. + if (!isNamedType(type)) { + context.reportError( + `Expected GraphQL named type but got: ${String(type)}.`, + type && type.astNode, + ); + return; + } + + // Ensure it is named correctly (excluding introspection types). + if (!isIntrospectionType(type)) { + validateName(context, type); + } + + if (isObjectType(type)) { + // Ensure fields are valid + validateFields(context, type); + + // Ensure objects implement the interfaces they claim to. + validateObjectInterfaces(context, type); + } else if (isInterfaceType(type)) { + // Ensure fields are valid. + validateFields(context, type); + + // Ensure Interfaces include at least 1 Object type. + validateInterfaces(context, type); + } else if (isUnionType(type)) { + // Ensure Unions include valid member types. + validateUnionMembers(context, type); + } else if (isEnumType(type)) { + // Ensure Enums have valid values. + validateEnumValues(context, type); + } else if (isInputObjectType(type)) { + // Ensure Input Object fields are valid. + validateInputFields(context, type); + } + }); +} + +function validateFields( + context: SchemaValidationContext, + type: GraphQLObjectType | GraphQLInterfaceType, +): void { + const fields = objectValues(type.getFields()); + + // Objects and Interfaces both must define one or more fields. + if (fields.length === 0) { + context.reportError( + `Type ${type.name} must define one or more fields.`, + getAllNodes(type), + ); + } + + fields.forEach(field => { + // Ensure they are named correctly. + validateName(context, field); + + // Ensure they were defined at most once. + const fieldNodes = getAllFieldNodes(type, field.name); + if (fieldNodes.length > 1) { + context.reportError( + `Field ${type.name}.${field.name} can only be defined once.`, + fieldNodes, + ); + return; // continue loop + } + + // Ensure the type is an output type + if (!isOutputType(field.type)) { + context.reportError( + `The type of ${type.name}.${field.name} must be Output Type ` + + `but got: ${String(field.type)}.`, + getFieldTypeNode(type, field.name), + ); + } + + // Ensure the arguments are valid + const argNames = Object.create(null); + field.args.forEach(arg => { + const argName = arg.name; + + // Ensure they are named correctly. + validateName(context, arg); + + // Ensure they are unique per field. + if (argNames[argName]) { + context.reportError( + `Field argument ${type.name}.${field.name}(${argName}:) can only ` + + 'be defined once.', + getAllFieldArgNodes(type, field.name, argName), + ); + } + argNames[argName] = true; + + // Ensure the type is an input type + if (!isInputType(arg.type)) { + context.reportError( + `The type of ${type.name}.${field.name}(${argName}:) must be Input ` + + `Type but got: ${String(arg.type)}.`, + getFieldArgTypeNode(type, field.name, argName), + ); + } + }); + }); +} + +function validateObjectInterfaces( + context: SchemaValidationContext, + object: GraphQLObjectType, +): void { + const implementedTypeNames = Object.create(null); + object.getInterfaces().forEach(iface => { + if (!isInterfaceType(iface)) { + context.reportError( + `Type ${String(object)} must only implement Interface types, ` + + `it cannot implement ${String(iface)}.`, + getImplementsInterfaceNode(object, iface), + ); + return; + } + + if (implementedTypeNames[iface.name]) { + context.reportError( + `Type ${object.name} can only implement ${iface.name} once.`, + getAllImplementsInterfaceNodes(object, iface), + ); + return; // continue loop + } + implementedTypeNames[iface.name] = true; + validateObjectImplementsInterface(context, object, iface); + }); +} + +function validateInterfaces( + context: SchemaValidationContext, + iface: GraphQLInterfaceType, +): void { + const possibleTypes = context.schema.getPossibleTypes(iface); + + if (possibleTypes.length === 0) { + context.reportError( + `Interface ${iface.name} must be implemented ` + + `by at least one Object type.`, + iface.astNode, + ); + } +} + +function validateObjectImplementsInterface( + context: SchemaValidationContext, + object: GraphQLObjectType, + iface: GraphQLInterfaceType, +): void { + const objectFieldMap = object.getFields(); + const ifaceFieldMap = iface.getFields(); + + // Assert each interface field is implemented. + Object.keys(ifaceFieldMap).forEach(fieldName => { + const objectField = objectFieldMap[fieldName]; + const ifaceField = ifaceFieldMap[fieldName]; + + // Assert interface field exists on object. + if (!objectField) { + context.reportError( + `Interface field ${iface.name}.${fieldName} expected but ` + + `${object.name} does not provide it.`, + [getFieldNode(iface, fieldName), object.astNode], + ); + // Continue loop over fields. + return; + } + + // Assert interface field type is satisfied by object field type, by being + // a valid subtype. (covariant) + if (!isTypeSubTypeOf(context.schema, objectField.type, ifaceField.type)) { + context.reportError( + `Interface field ${iface.name}.${fieldName} expects type ` + + `${String(ifaceField.type)} but ${object.name}.${fieldName} ` + + `is type ${String(objectField.type)}.`, + [ + getFieldTypeNode(iface, fieldName), + getFieldTypeNode(object, fieldName), + ], + ); + } + + // Assert each interface field arg is implemented. + ifaceField.args.forEach(ifaceArg => { + const argName = ifaceArg.name; + const objectArg = find(objectField.args, arg => arg.name === argName); + + // Assert interface field arg exists on object field. + if (!objectArg) { + context.reportError( + `Interface field argument ${iface.name}.${fieldName}(${argName}:) ` + + `expected but ${object.name}.${fieldName} does not provide it.`, + [ + getFieldArgNode(iface, fieldName, argName), + getFieldNode(object, fieldName), + ], + ); + // Continue loop over arguments. + return; + } + + // Assert interface field arg type matches object field arg type. + // (invariant) + // TODO: change to contravariant? + if (!isEqualType(ifaceArg.type, objectArg.type)) { + context.reportError( + `Interface field argument ${iface.name}.${fieldName}(${argName}:) ` + + `expects type ${String(ifaceArg.type)} but ` + + `${object.name}.${fieldName}(${argName}:) is type ` + + `${String(objectArg.type)}.`, + [ + getFieldArgTypeNode(iface, fieldName, argName), + getFieldArgTypeNode(object, fieldName, argName), + ], + ); + } + + // TODO: validate default values? + }); + + // Assert additional arguments must not be required. + objectField.args.forEach(objectArg => { + const argName = objectArg.name; + const ifaceArg = find(ifaceField.args, arg => arg.name === argName); + if (!ifaceArg && isNonNullType(objectArg.type)) { + context.reportError( + `Object field argument ${object.name}.${fieldName}(${argName}:) ` + + `is of required type ${String(objectArg.type)} but is not also ` + + `provided by the Interface field ${iface.name}.${fieldName}.`, + [ + getFieldArgTypeNode(object, fieldName, argName), + getFieldNode(iface, fieldName), + ], + ); + } + }); + }); +} + +function validateUnionMembers( + context: SchemaValidationContext, + union: GraphQLUnionType, +): void { + const memberTypes = union.getTypes(); + + if (memberTypes.length === 0) { + context.reportError( + `Union type ${union.name} must define one or more member types.`, + union.astNode, + ); + } + + const includedTypeNames = Object.create(null); + memberTypes.forEach(memberType => { + if (includedTypeNames[memberType.name]) { + context.reportError( + `Union type ${union.name} can only include type ` + + `${memberType.name} once.`, + getUnionMemberTypeNodes(union, memberType.name), + ); + return; // continue loop + } + includedTypeNames[memberType.name] = true; + if (!isObjectType(memberType)) { + context.reportError( + `Union type ${union.name} can only include Object types, ` + + `it cannot include ${String(memberType)}.`, + getUnionMemberTypeNodes(union, String(memberType)), + ); + } + }); +} + +function validateEnumValues( + context: SchemaValidationContext, + enumType: GraphQLEnumType, +): void { + const enumValues = enumType.getValues(); + + if (enumValues.length === 0) { + context.reportError( + `Enum type ${enumType.name} must define one or more values.`, + enumType.astNode, + ); + } + + enumValues.forEach(enumValue => { + const valueName = enumValue.name; + + // Ensure no duplicates. + const allNodes = getEnumValueNodes(enumType, valueName); + if (allNodes && allNodes.length > 1) { + context.reportError( + `Enum type ${enumType.name} can include value ${valueName} only once.`, + allNodes, + ); + } + + // Ensure valid name. + validateName(context, enumValue); + if (valueName === 'true' || valueName === 'false' || valueName === 'null') { + context.reportError( + `Enum type ${enumType.name} cannot include value: ${valueName}.`, + enumValue.astNode, + ); + } + }); +} + +function validateInputFields( + context: SchemaValidationContext, + inputObj: GraphQLInputObjectType, +): void { + const fields = objectValues(inputObj.getFields()); + + if (fields.length === 0) { + context.reportError( + `Input Object type ${inputObj.name} must define one or more fields.`, + inputObj.astNode, + ); + } + + // Ensure the arguments are valid + fields.forEach(field => { + // Ensure they are named correctly. + validateName(context, field); + + // TODO: Ensure they are unique per field. + + // Ensure the type is an input type + if (!isInputType(field.type)) { + context.reportError( + `The type of ${inputObj.name}.${field.name} must be Input Type ` + + `but got: ${String(field.type)}.`, + field.astNode && field.astNode.type, + ); + } + }); +} + +function getAllNodes(object: { + +astNode: ?T, + +extensionASTNodes?: ?$ReadOnlyArray, +}): $ReadOnlyArray { + const { astNode, extensionASTNodes } = object; + return astNode + ? extensionASTNodes + ? [astNode].concat(extensionASTNodes) + : [astNode] + : extensionASTNodes || []; +} + +function getImplementsInterfaceNode( + type: GraphQLObjectType, + iface: GraphQLInterfaceType, +): ?NamedTypeNode { + return getAllImplementsInterfaceNodes(type, iface)[0]; +} + +function getAllImplementsInterfaceNodes( + type: GraphQLObjectType, + iface: GraphQLInterfaceType, +): $ReadOnlyArray { + const implementsNodes = []; + const astNodes = getAllNodes(type); + for (let i = 0; i < astNodes.length; i++) { + const astNode = astNodes[i]; + if (astNode && astNode.interfaces) { + astNode.interfaces.forEach(node => { + if (node.name.value === iface.name) { + implementsNodes.push(node); + } + }); + } + } + return implementsNodes; +} + +function getFieldNode( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, +): ?FieldDefinitionNode { + return getAllFieldNodes(type, fieldName)[0]; +} + +function getAllFieldNodes( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, +): $ReadOnlyArray { + const fieldNodes = []; + const astNodes = getAllNodes(type); + for (let i = 0; i < astNodes.length; i++) { + const astNode = astNodes[i]; + if (astNode && astNode.fields) { + astNode.fields.forEach(node => { + if (node.name.value === fieldName) { + fieldNodes.push(node); + } + }); + } + } + return fieldNodes; +} + +function getFieldTypeNode( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, +): ?TypeNode { + const fieldNode = getFieldNode(type, fieldName); + return fieldNode && fieldNode.type; +} + +function getFieldArgNode( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, + argName: string, +): ?InputValueDefinitionNode { + return getAllFieldArgNodes(type, fieldName, argName)[0]; +} + +function getAllFieldArgNodes( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, + argName: string, +): $ReadOnlyArray { + const argNodes = []; + const fieldNode = getFieldNode(type, fieldName); + if (fieldNode && fieldNode.arguments) { + fieldNode.arguments.forEach(node => { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + return argNodes; +} + +function getFieldArgTypeNode( + type: GraphQLObjectType | GraphQLInterfaceType, + fieldName: string, + argName: string, +): ?TypeNode { + const fieldArgNode = getFieldArgNode(type, fieldName, argName); + return fieldArgNode && fieldArgNode.type; +} + +function getAllDirectiveArgNodes( + directive: GraphQLDirective, + argName: string, +): $ReadOnlyArray { + const argNodes = []; + const directiveNode = directive.astNode; + if (directiveNode && directiveNode.arguments) { + directiveNode.arguments.forEach(node => { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + return argNodes; +} + +function getDirectiveArgTypeNode( + directive: GraphQLDirective, + argName: string, +): ?TypeNode { + const argNode = getAllDirectiveArgNodes(directive, argName)[0]; + return argNode && argNode.type; +} + +function getUnionMemberTypeNodes( + union: GraphQLUnionType, + typeName: string, +): ?$ReadOnlyArray { + return ( + union.astNode && + union.astNode.types && + union.astNode.types.filter(type => type.name.value === typeName) + ); +} + +function getEnumValueNodes( + enumType: GraphQLEnumType, + valueName: string, +): ?$ReadOnlyArray { + return ( + enumType.astNode && + enumType.astNode.values && + enumType.astNode.values.filter(value => value.name.value === valueName) + ); +} diff --git a/dist/type/validate.mjs b/dist/type/validate.mjs new file mode 100644 index 0000000000..c7eb715dea --- /dev/null +++ b/dist/type/validate.mjs @@ -0,0 +1,559 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isNonNullType, isNamedType, isInputType, isOutputType } from './definition'; +import { isDirective } from './directives'; +import { isIntrospectionType } from './introspection'; +import { isSchema } from './schema'; +import find from '../jsutils/find'; +import invariant from '../jsutils/invariant'; +import objectValues from '../jsutils/objectValues'; +import { GraphQLError } from '../error/GraphQLError'; +import { isValidNameError } from '../utilities/assertValidName'; +import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators'; +/** + * Implements the "Type Validation" sub-sections of the specification's + * "Type System" section. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the Schema is valid. + */ + +export function validateSchema(schema) { + // First check to ensure the provided value is in fact a GraphQLSchema. + !isSchema(schema) ? invariant(0, "Expected ".concat(String(schema), " to be a GraphQL schema.")) : void 0; // If this Schema has already been validated, return the previous results. + + if (schema.__validationErrors) { + return schema.__validationErrors; + } // Validate the schema, producing a list of errors. + + + var context = new SchemaValidationContext(schema); + validateRootTypes(context); + validateDirectives(context); + validateTypes(context); // Persist the results of validation before returning to ensure validation + // does not run multiple times for this schema. + + var errors = context.getErrors(); + schema.__validationErrors = errors; + return errors; +} +/** + * Utility function which asserts a schema is valid by throwing an error if + * it is invalid. + */ + +export function assertValidSchema(schema) { + var errors = validateSchema(schema); + + if (errors.length !== 0) { + throw new Error(errors.map(function (error) { + return error.message; + }).join('\n\n')); + } +} + +var SchemaValidationContext = +/*#__PURE__*/ +function () { + function SchemaValidationContext(schema) { + _defineProperty(this, "_errors", void 0); + + _defineProperty(this, "schema", void 0); + + this._errors = []; + this.schema = schema; + } + + var _proto = SchemaValidationContext.prototype; + + _proto.reportError = function reportError(message, nodes) { + var _nodes = (Array.isArray(nodes) ? nodes : [nodes]).filter(Boolean); + + this.addError(new GraphQLError(message, _nodes)); + }; + + _proto.addError = function addError(error) { + this._errors.push(error); + }; + + _proto.getErrors = function getErrors() { + return this._errors; + }; + + return SchemaValidationContext; +}(); + +function validateRootTypes(context) { + var schema = context.schema; + var queryType = schema.getQueryType(); + + if (!queryType) { + context.reportError("Query root type must be provided.", schema.astNode); + } else if (!isObjectType(queryType)) { + context.reportError("Query root type must be Object type, it cannot be ".concat(String(queryType), "."), getOperationTypeNode(schema, queryType, 'query')); + } + + var mutationType = schema.getMutationType(); + + if (mutationType && !isObjectType(mutationType)) { + context.reportError('Mutation root type must be Object type if provided, it cannot be ' + "".concat(String(mutationType), "."), getOperationTypeNode(schema, mutationType, 'mutation')); + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType && !isObjectType(subscriptionType)) { + context.reportError('Subscription root type must be Object type if provided, it cannot be ' + "".concat(String(subscriptionType), "."), getOperationTypeNode(schema, subscriptionType, 'subscription')); + } +} + +function getOperationTypeNode(schema, type, operation) { + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = getAllNodes(schema)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var node = _step.value; + + if (node.operationTypes) { + var _iteratorNormalCompletion2 = true; + var _didIteratorError2 = false; + var _iteratorError2 = undefined; + + try { + for (var _iterator2 = node.operationTypes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { + var operationType = _step2.value; + + if (operationType.operation === operation) { + return operationType.type; + } + } + } catch (err) { + _didIteratorError2 = true; + _iteratorError2 = err; + } finally { + try { + if (!_iteratorNormalCompletion2 && _iterator2.return != null) { + _iterator2.return(); + } + } finally { + if (_didIteratorError2) { + throw _iteratorError2; + } + } + } + } + } + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator.return != null) { + _iterator.return(); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } + + return type.astNode; +} + +function validateDirectives(context) { + var directives = context.schema.getDirectives(); + directives.forEach(function (directive) { + // Ensure all directives are in fact GraphQL directives. + if (!isDirective(directive)) { + context.reportError("Expected directive but got: ".concat(String(directive), "."), directive && directive.astNode); + return; + } // Ensure they are named correctly. + + + validateName(context, directive); // TODO: Ensure proper locations. + // Ensure the arguments are valid. + + var argNames = Object.create(null); + directive.args.forEach(function (arg) { + var argName = arg.name; // Ensure they are named correctly. + + validateName(context, arg); // Ensure they are unique per directive. + + if (argNames[argName]) { + context.reportError("Argument @".concat(directive.name, "(").concat(argName, ":) can only be defined once."), getAllDirectiveArgNodes(directive, argName)); + return; // continue loop + } + + argNames[argName] = true; // Ensure the type is an input type. + + if (!isInputType(arg.type)) { + context.reportError("The type of @".concat(directive.name, "(").concat(argName, ":) must be Input Type ") + "but got: ".concat(String(arg.type), "."), getDirectiveArgTypeNode(directive, argName)); + } + }); + }); +} + +function validateName(context, node) { + // If a schema explicitly allows some legacy name which is no longer valid, + // allow it to be assumed valid. + if (context.schema.__allowedLegacyNames && context.schema.__allowedLegacyNames.indexOf(node.name) !== -1) { + return; + } // Ensure names are valid, however introspection types opt out. + + + var error = isValidNameError(node.name, node.astNode || undefined); + + if (error) { + context.addError(error); + } +} + +function validateTypes(context) { + var typeMap = context.schema.getTypeMap(); + objectValues(typeMap).forEach(function (type) { + // Ensure all provided types are in fact GraphQL type. + if (!isNamedType(type)) { + context.reportError("Expected GraphQL named type but got: ".concat(String(type), "."), type && type.astNode); + return; + } // Ensure it is named correctly (excluding introspection types). + + + if (!isIntrospectionType(type)) { + validateName(context, type); + } + + if (isObjectType(type)) { + // Ensure fields are valid + validateFields(context, type); // Ensure objects implement the interfaces they claim to. + + validateObjectInterfaces(context, type); + } else if (isInterfaceType(type)) { + // Ensure fields are valid. + validateFields(context, type); // Ensure Interfaces include at least 1 Object type. + + validateInterfaces(context, type); + } else if (isUnionType(type)) { + // Ensure Unions include valid member types. + validateUnionMembers(context, type); + } else if (isEnumType(type)) { + // Ensure Enums have valid values. + validateEnumValues(context, type); + } else if (isInputObjectType(type)) { + // Ensure Input Object fields are valid. + validateInputFields(context, type); + } + }); +} + +function validateFields(context, type) { + var fields = objectValues(type.getFields()); // Objects and Interfaces both must define one or more fields. + + if (fields.length === 0) { + context.reportError("Type ".concat(type.name, " must define one or more fields."), getAllNodes(type)); + } + + fields.forEach(function (field) { + // Ensure they are named correctly. + validateName(context, field); // Ensure they were defined at most once. + + var fieldNodes = getAllFieldNodes(type, field.name); + + if (fieldNodes.length > 1) { + context.reportError("Field ".concat(type.name, ".").concat(field.name, " can only be defined once."), fieldNodes); + return; // continue loop + } // Ensure the type is an output type + + + if (!isOutputType(field.type)) { + context.reportError("The type of ".concat(type.name, ".").concat(field.name, " must be Output Type ") + "but got: ".concat(String(field.type), "."), getFieldTypeNode(type, field.name)); + } // Ensure the arguments are valid + + + var argNames = Object.create(null); + field.args.forEach(function (arg) { + var argName = arg.name; // Ensure they are named correctly. + + validateName(context, arg); // Ensure they are unique per field. + + if (argNames[argName]) { + context.reportError("Field argument ".concat(type.name, ".").concat(field.name, "(").concat(argName, ":) can only ") + 'be defined once.', getAllFieldArgNodes(type, field.name, argName)); + } + + argNames[argName] = true; // Ensure the type is an input type + + if (!isInputType(arg.type)) { + context.reportError("The type of ".concat(type.name, ".").concat(field.name, "(").concat(argName, ":) must be Input ") + "Type but got: ".concat(String(arg.type), "."), getFieldArgTypeNode(type, field.name, argName)); + } + }); + }); +} + +function validateObjectInterfaces(context, object) { + var implementedTypeNames = Object.create(null); + object.getInterfaces().forEach(function (iface) { + if (!isInterfaceType(iface)) { + context.reportError("Type ".concat(String(object), " must only implement Interface types, ") + "it cannot implement ".concat(String(iface), "."), getImplementsInterfaceNode(object, iface)); + return; + } + + if (implementedTypeNames[iface.name]) { + context.reportError("Type ".concat(object.name, " can only implement ").concat(iface.name, " once."), getAllImplementsInterfaceNodes(object, iface)); + return; // continue loop + } + + implementedTypeNames[iface.name] = true; + validateObjectImplementsInterface(context, object, iface); + }); +} + +function validateInterfaces(context, iface) { + var possibleTypes = context.schema.getPossibleTypes(iface); + + if (possibleTypes.length === 0) { + context.reportError("Interface ".concat(iface.name, " must be implemented ") + "by at least one Object type.", iface.astNode); + } +} + +function validateObjectImplementsInterface(context, object, iface) { + var objectFieldMap = object.getFields(); + var ifaceFieldMap = iface.getFields(); // Assert each interface field is implemented. + + Object.keys(ifaceFieldMap).forEach(function (fieldName) { + var objectField = objectFieldMap[fieldName]; + var ifaceField = ifaceFieldMap[fieldName]; // Assert interface field exists on object. + + if (!objectField) { + context.reportError("Interface field ".concat(iface.name, ".").concat(fieldName, " expected but ") + "".concat(object.name, " does not provide it."), [getFieldNode(iface, fieldName), object.astNode]); // Continue loop over fields. + + return; + } // Assert interface field type is satisfied by object field type, by being + // a valid subtype. (covariant) + + + if (!isTypeSubTypeOf(context.schema, objectField.type, ifaceField.type)) { + context.reportError("Interface field ".concat(iface.name, ".").concat(fieldName, " expects type ") + "".concat(String(ifaceField.type), " but ").concat(object.name, ".").concat(fieldName, " ") + "is type ".concat(String(objectField.type), "."), [getFieldTypeNode(iface, fieldName), getFieldTypeNode(object, fieldName)]); + } // Assert each interface field arg is implemented. + + + ifaceField.args.forEach(function (ifaceArg) { + var argName = ifaceArg.name; + var objectArg = find(objectField.args, function (arg) { + return arg.name === argName; + }); // Assert interface field arg exists on object field. + + if (!objectArg) { + context.reportError("Interface field argument ".concat(iface.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "expected but ".concat(object.name, ".").concat(fieldName, " does not provide it."), [getFieldArgNode(iface, fieldName, argName), getFieldNode(object, fieldName)]); // Continue loop over arguments. + + return; + } // Assert interface field arg type matches object field arg type. + // (invariant) + // TODO: change to contravariant? + + + if (!isEqualType(ifaceArg.type, objectArg.type)) { + context.reportError("Interface field argument ".concat(iface.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "expects type ".concat(String(ifaceArg.type), " but ") + "".concat(object.name, ".").concat(fieldName, "(").concat(argName, ":) is type ") + "".concat(String(objectArg.type), "."), [getFieldArgTypeNode(iface, fieldName, argName), getFieldArgTypeNode(object, fieldName, argName)]); + } // TODO: validate default values? + + }); // Assert additional arguments must not be required. + + objectField.args.forEach(function (objectArg) { + var argName = objectArg.name; + var ifaceArg = find(ifaceField.args, function (arg) { + return arg.name === argName; + }); + + if (!ifaceArg && isNonNullType(objectArg.type)) { + context.reportError("Object field argument ".concat(object.name, ".").concat(fieldName, "(").concat(argName, ":) ") + "is of required type ".concat(String(objectArg.type), " but is not also ") + "provided by the Interface field ".concat(iface.name, ".").concat(fieldName, "."), [getFieldArgTypeNode(object, fieldName, argName), getFieldNode(iface, fieldName)]); + } + }); + }); +} + +function validateUnionMembers(context, union) { + var memberTypes = union.getTypes(); + + if (memberTypes.length === 0) { + context.reportError("Union type ".concat(union.name, " must define one or more member types."), union.astNode); + } + + var includedTypeNames = Object.create(null); + memberTypes.forEach(function (memberType) { + if (includedTypeNames[memberType.name]) { + context.reportError("Union type ".concat(union.name, " can only include type ") + "".concat(memberType.name, " once."), getUnionMemberTypeNodes(union, memberType.name)); + return; // continue loop + } + + includedTypeNames[memberType.name] = true; + + if (!isObjectType(memberType)) { + context.reportError("Union type ".concat(union.name, " can only include Object types, ") + "it cannot include ".concat(String(memberType), "."), getUnionMemberTypeNodes(union, String(memberType))); + } + }); +} + +function validateEnumValues(context, enumType) { + var enumValues = enumType.getValues(); + + if (enumValues.length === 0) { + context.reportError("Enum type ".concat(enumType.name, " must define one or more values."), enumType.astNode); + } + + enumValues.forEach(function (enumValue) { + var valueName = enumValue.name; // Ensure no duplicates. + + var allNodes = getEnumValueNodes(enumType, valueName); + + if (allNodes && allNodes.length > 1) { + context.reportError("Enum type ".concat(enumType.name, " can include value ").concat(valueName, " only once."), allNodes); + } // Ensure valid name. + + + validateName(context, enumValue); + + if (valueName === 'true' || valueName === 'false' || valueName === 'null') { + context.reportError("Enum type ".concat(enumType.name, " cannot include value: ").concat(valueName, "."), enumValue.astNode); + } + }); +} + +function validateInputFields(context, inputObj) { + var fields = objectValues(inputObj.getFields()); + + if (fields.length === 0) { + context.reportError("Input Object type ".concat(inputObj.name, " must define one or more fields."), inputObj.astNode); + } // Ensure the arguments are valid + + + fields.forEach(function (field) { + // Ensure they are named correctly. + validateName(context, field); // TODO: Ensure they are unique per field. + // Ensure the type is an input type + + if (!isInputType(field.type)) { + context.reportError("The type of ".concat(inputObj.name, ".").concat(field.name, " must be Input Type ") + "but got: ".concat(String(field.type), "."), field.astNode && field.astNode.type); + } + }); +} + +function getAllNodes(object) { + var astNode = object.astNode, + extensionASTNodes = object.extensionASTNodes; + return astNode ? extensionASTNodes ? [astNode].concat(extensionASTNodes) : [astNode] : extensionASTNodes || []; +} + +function getImplementsInterfaceNode(type, iface) { + return getAllImplementsInterfaceNodes(type, iface)[0]; +} + +function getAllImplementsInterfaceNodes(type, iface) { + var implementsNodes = []; + var astNodes = getAllNodes(type); + + for (var i = 0; i < astNodes.length; i++) { + var _astNode = astNodes[i]; + + if (_astNode && _astNode.interfaces) { + _astNode.interfaces.forEach(function (node) { + if (node.name.value === iface.name) { + implementsNodes.push(node); + } + }); + } + } + + return implementsNodes; +} + +function getFieldNode(type, fieldName) { + return getAllFieldNodes(type, fieldName)[0]; +} + +function getAllFieldNodes(type, fieldName) { + var fieldNodes = []; + var astNodes = getAllNodes(type); + + for (var i = 0; i < astNodes.length; i++) { + var _astNode2 = astNodes[i]; + + if (_astNode2 && _astNode2.fields) { + _astNode2.fields.forEach(function (node) { + if (node.name.value === fieldName) { + fieldNodes.push(node); + } + }); + } + } + + return fieldNodes; +} + +function getFieldTypeNode(type, fieldName) { + var fieldNode = getFieldNode(type, fieldName); + return fieldNode && fieldNode.type; +} + +function getFieldArgNode(type, fieldName, argName) { + return getAllFieldArgNodes(type, fieldName, argName)[0]; +} + +function getAllFieldArgNodes(type, fieldName, argName) { + var argNodes = []; + var fieldNode = getFieldNode(type, fieldName); + + if (fieldNode && fieldNode.arguments) { + fieldNode.arguments.forEach(function (node) { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + + return argNodes; +} + +function getFieldArgTypeNode(type, fieldName, argName) { + var fieldArgNode = getFieldArgNode(type, fieldName, argName); + return fieldArgNode && fieldArgNode.type; +} + +function getAllDirectiveArgNodes(directive, argName) { + var argNodes = []; + var directiveNode = directive.astNode; + + if (directiveNode && directiveNode.arguments) { + directiveNode.arguments.forEach(function (node) { + if (node.name.value === argName) { + argNodes.push(node); + } + }); + } + + return argNodes; +} + +function getDirectiveArgTypeNode(directive, argName) { + var argNode = getAllDirectiveArgNodes(directive, argName)[0]; + return argNode && argNode.type; +} + +function getUnionMemberTypeNodes(union, typeName) { + return union.astNode && union.astNode.types && union.astNode.types.filter(function (type) { + return type.name.value === typeName; + }); +} + +function getEnumValueNodes(enumType, valueName) { + return enumType.astNode && enumType.astNode.values && enumType.astNode.values.filter(function (value) { + return value.name.value === valueName; + }); +} \ No newline at end of file diff --git a/dist/utilities/TypeInfo.js b/dist/utilities/TypeInfo.js new file mode 100644 index 0000000000..c3b260bca3 --- /dev/null +++ b/dist/utilities/TypeInfo.js @@ -0,0 +1,348 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.TypeInfo = void 0; + +var _kinds = require("../language/kinds"); + +var _definition = require("../type/definition"); + +var _introspection = require("../type/introspection"); + +var _typeFromAST = require("./typeFromAST"); + +var _find = _interopRequireDefault(require("../jsutils/find")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * TypeInfo is a utility class which, given a GraphQL schema, can keep track + * of the current field and type definitions at any point in a GraphQL document + * AST during a recursive descent by calling `enter(node)` and `leave(node)`. + */ +var TypeInfo = +/*#__PURE__*/ +function () { + function TypeInfo(schema, // NOTE: this experimental optional second parameter is only needed in order + // to support non-spec-compliant codebases. You should never need to use it. + getFieldDefFn, // Initial type may be provided in rare cases to facilitate traversals + initialType) { + _defineProperty(this, "_schema", void 0); + + _defineProperty(this, "_typeStack", void 0); + + _defineProperty(this, "_parentTypeStack", void 0); + + _defineProperty(this, "_inputTypeStack", void 0); + + _defineProperty(this, "_fieldDefStack", void 0); + + _defineProperty(this, "_defaultValueStack", void 0); + + _defineProperty(this, "_directive", void 0); + + _defineProperty(this, "_argument", void 0); + + _defineProperty(this, "_enumValue", void 0); + + _defineProperty(this, "_getFieldDef", void 0); + + this._schema = schema; + this._typeStack = []; + this._parentTypeStack = []; + this._inputTypeStack = []; + this._fieldDefStack = []; + this._defaultValueStack = []; + this._directive = null; + this._argument = null; + this._enumValue = null; + this._getFieldDef = getFieldDefFn || getFieldDef; + + if (initialType) { + if ((0, _definition.isInputType)(initialType)) { + this._inputTypeStack.push(initialType); + } + + if ((0, _definition.isCompositeType)(initialType)) { + this._parentTypeStack.push(initialType); + } + + if ((0, _definition.isOutputType)(initialType)) { + this._typeStack.push(initialType); + } + } + } + + var _proto = TypeInfo.prototype; + + _proto.getType = function getType() { + if (this._typeStack.length > 0) { + return this._typeStack[this._typeStack.length - 1]; + } + }; + + _proto.getParentType = function getParentType() { + if (this._parentTypeStack.length > 0) { + return this._parentTypeStack[this._parentTypeStack.length - 1]; + } + }; + + _proto.getInputType = function getInputType() { + if (this._inputTypeStack.length > 0) { + return this._inputTypeStack[this._inputTypeStack.length - 1]; + } + }; + + _proto.getParentInputType = function getParentInputType() { + if (this._inputTypeStack.length > 1) { + return this._inputTypeStack[this._inputTypeStack.length - 2]; + } + }; + + _proto.getFieldDef = function getFieldDef() { + if (this._fieldDefStack.length > 0) { + return this._fieldDefStack[this._fieldDefStack.length - 1]; + } + }; + + _proto.getDefaultValue = function getDefaultValue() { + if (this._defaultValueStack.length > 0) { + return this._defaultValueStack[this._defaultValueStack.length - 1]; + } + }; + + _proto.getDirective = function getDirective() { + return this._directive; + }; + + _proto.getArgument = function getArgument() { + return this._argument; + }; + + _proto.getEnumValue = function getEnumValue() { + return this._enumValue; + }; // Flow does not yet handle this case. + + + _proto.enter = function enter(node + /* ASTNode */ + ) { + var schema = this._schema; // Note: many of the types below are explicitly typed as "mixed" to drop + // any assumptions of a valid schema to ensure runtime types are properly + // checked before continuing since TypeInfo is used as part of validation + // which occurs before guarantees of schema and document validity. + + switch (node.kind) { + case _kinds.Kind.SELECTION_SET: + var namedType = (0, _definition.getNamedType)(this.getType()); + + this._parentTypeStack.push((0, _definition.isCompositeType)(namedType) ? namedType : undefined); + + break; + + case _kinds.Kind.FIELD: + var parentType = this.getParentType(); + var fieldDef; + var fieldType; + + if (parentType) { + fieldDef = this._getFieldDef(schema, parentType, node); + + if (fieldDef) { + fieldType = fieldDef.type; + } + } + + this._fieldDefStack.push(fieldDef); + + this._typeStack.push((0, _definition.isOutputType)(fieldType) ? fieldType : undefined); + + break; + + case _kinds.Kind.DIRECTIVE: + this._directive = schema.getDirective(node.name.value); + break; + + case _kinds.Kind.OPERATION_DEFINITION: + var type; + + if (node.operation === 'query') { + type = schema.getQueryType(); + } else if (node.operation === 'mutation') { + type = schema.getMutationType(); + } else if (node.operation === 'subscription') { + type = schema.getSubscriptionType(); + } + + this._typeStack.push((0, _definition.isObjectType)(type) ? type : undefined); + + break; + + case _kinds.Kind.INLINE_FRAGMENT: + case _kinds.Kind.FRAGMENT_DEFINITION: + var typeConditionAST = node.typeCondition; + var outputType = typeConditionAST ? (0, _typeFromAST.typeFromAST)(schema, typeConditionAST) : (0, _definition.getNamedType)(this.getType()); + + this._typeStack.push((0, _definition.isOutputType)(outputType) ? outputType : undefined); + + break; + + case _kinds.Kind.VARIABLE_DEFINITION: + var inputType = (0, _typeFromAST.typeFromAST)(schema, node.type); + + this._inputTypeStack.push((0, _definition.isInputType)(inputType) ? inputType : undefined); + + break; + + case _kinds.Kind.ARGUMENT: + var argDef; + var argType; + var fieldOrDirective = this.getDirective() || this.getFieldDef(); + + if (fieldOrDirective) { + argDef = (0, _find.default)(fieldOrDirective.args, function (arg) { + return arg.name === node.name.value; + }); + + if (argDef) { + argType = argDef.type; + } + } + + this._argument = argDef; + + this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined); + + this._inputTypeStack.push((0, _definition.isInputType)(argType) ? argType : undefined); + + break; + + case _kinds.Kind.LIST: + var listType = (0, _definition.getNullableType)(this.getInputType()); + var itemType = (0, _definition.isListType)(listType) ? listType.ofType : listType; // List positions never have a default value. + + this._defaultValueStack.push(undefined); + + this._inputTypeStack.push((0, _definition.isInputType)(itemType) ? itemType : undefined); + + break; + + case _kinds.Kind.OBJECT_FIELD: + var objectType = (0, _definition.getNamedType)(this.getInputType()); + var inputFieldType; + var inputField; + + if ((0, _definition.isInputObjectType)(objectType)) { + inputField = objectType.getFields()[node.name.value]; + + if (inputField) { + inputFieldType = inputField.type; + } + } + + this._defaultValueStack.push(inputField ? inputField.defaultValue : undefined); + + this._inputTypeStack.push((0, _definition.isInputType)(inputFieldType) ? inputFieldType : undefined); + + break; + + case _kinds.Kind.ENUM: + var enumType = (0, _definition.getNamedType)(this.getInputType()); + var enumValue; + + if ((0, _definition.isEnumType)(enumType)) { + enumValue = enumType.getValue(node.value); + } + + this._enumValue = enumValue; + break; + } + }; + + _proto.leave = function leave(node) { + switch (node.kind) { + case _kinds.Kind.SELECTION_SET: + this._parentTypeStack.pop(); + + break; + + case _kinds.Kind.FIELD: + this._fieldDefStack.pop(); + + this._typeStack.pop(); + + break; + + case _kinds.Kind.DIRECTIVE: + this._directive = null; + break; + + case _kinds.Kind.OPERATION_DEFINITION: + case _kinds.Kind.INLINE_FRAGMENT: + case _kinds.Kind.FRAGMENT_DEFINITION: + this._typeStack.pop(); + + break; + + case _kinds.Kind.VARIABLE_DEFINITION: + this._inputTypeStack.pop(); + + break; + + case _kinds.Kind.ARGUMENT: + this._argument = null; + + this._defaultValueStack.pop(); + + this._inputTypeStack.pop(); + + break; + + case _kinds.Kind.LIST: + case _kinds.Kind.OBJECT_FIELD: + this._defaultValueStack.pop(); + + this._inputTypeStack.pop(); + + break; + + case _kinds.Kind.ENUM: + this._enumValue = null; + break; + } + }; + + return TypeInfo; +}(); +/** + * Not exactly the same as the executor's definition of getFieldDef, in this + * statically evaluated environment we do not always have an Object type, + * and need to handle Interface and Union types. + */ + + +exports.TypeInfo = TypeInfo; + +function getFieldDef(schema, parentType, fieldNode) { + var name = fieldNode.name.value; + + if (name === _introspection.SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { + return _introspection.SchemaMetaFieldDef; + } + + if (name === _introspection.TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return _introspection.TypeMetaFieldDef; + } + + if (name === _introspection.TypeNameMetaFieldDef.name && (0, _definition.isCompositeType)(parentType)) { + return _introspection.TypeNameMetaFieldDef; + } + + if ((0, _definition.isObjectType)(parentType) || (0, _definition.isInterfaceType)(parentType)) { + return parentType.getFields()[name]; + } +} \ No newline at end of file diff --git a/dist/utilities/TypeInfo.js.flow b/dist/utilities/TypeInfo.js.flow new file mode 100644 index 0000000000..67911758c2 --- /dev/null +++ b/dist/utilities/TypeInfo.js.flow @@ -0,0 +1,314 @@ +/** + * 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. + * + * @flow strict + */ + +import { Kind } from '../language/kinds'; +import { + isObjectType, + isInterfaceType, + isEnumType, + isInputObjectType, + isListType, + isCompositeType, + isInputType, + isOutputType, + getNullableType, + getNamedType, +} from '../type/definition'; +import type { + GraphQLType, + GraphQLInputType, + GraphQLOutputType, + GraphQLCompositeType, + GraphQLField, + GraphQLArgument, + GraphQLInputField, + GraphQLEnumValue, +} from '../type/definition'; +import type { GraphQLDirective } from '../type/directives'; +import { + SchemaMetaFieldDef, + TypeMetaFieldDef, + TypeNameMetaFieldDef, +} from '../type/introspection'; +import type { GraphQLSchema } from '../type/schema'; +import type { ASTNode, FieldNode } from '../language/ast'; +import { typeFromAST } from './typeFromAST'; +import find from '../jsutils/find'; + +/** + * TypeInfo is a utility class which, given a GraphQL schema, can keep track + * of the current field and type definitions at any point in a GraphQL document + * AST during a recursive descent by calling `enter(node)` and `leave(node)`. + */ +export class TypeInfo { + _schema: GraphQLSchema; + _typeStack: Array; + _parentTypeStack: Array; + _inputTypeStack: Array; + _fieldDefStack: Array>; + _defaultValueStack: Array; + _directive: ?GraphQLDirective; + _argument: ?GraphQLArgument; + _enumValue: ?GraphQLEnumValue; + _getFieldDef: typeof getFieldDef; + + constructor( + schema: GraphQLSchema, + // NOTE: this experimental optional second parameter is only needed in order + // to support non-spec-compliant codebases. You should never need to use it. + // It may disappear in the future. + getFieldDefFn?: typeof getFieldDef, + // Initial type may be provided in rare cases to facilitate traversals + // beginning somewhere other than documents. + initialType?: GraphQLType, + ): void { + this._schema = schema; + this._typeStack = []; + this._parentTypeStack = []; + this._inputTypeStack = []; + this._fieldDefStack = []; + this._defaultValueStack = []; + this._directive = null; + this._argument = null; + this._enumValue = null; + this._getFieldDef = getFieldDefFn || getFieldDef; + if (initialType) { + if (isInputType(initialType)) { + this._inputTypeStack.push(initialType); + } + if (isCompositeType(initialType)) { + this._parentTypeStack.push(initialType); + } + if (isOutputType(initialType)) { + this._typeStack.push(initialType); + } + } + } + + getType(): ?GraphQLOutputType { + if (this._typeStack.length > 0) { + return this._typeStack[this._typeStack.length - 1]; + } + } + + getParentType(): ?GraphQLCompositeType { + if (this._parentTypeStack.length > 0) { + return this._parentTypeStack[this._parentTypeStack.length - 1]; + } + } + + getInputType(): ?GraphQLInputType { + if (this._inputTypeStack.length > 0) { + return this._inputTypeStack[this._inputTypeStack.length - 1]; + } + } + + getParentInputType(): ?GraphQLInputType { + if (this._inputTypeStack.length > 1) { + return this._inputTypeStack[this._inputTypeStack.length - 2]; + } + } + + getFieldDef(): ?GraphQLField<*, *> { + if (this._fieldDefStack.length > 0) { + return this._fieldDefStack[this._fieldDefStack.length - 1]; + } + } + + getDefaultValue(): ?mixed { + if (this._defaultValueStack.length > 0) { + return this._defaultValueStack[this._defaultValueStack.length - 1]; + } + } + + getDirective(): ?GraphQLDirective { + return this._directive; + } + + getArgument(): ?GraphQLArgument { + return this._argument; + } + + getEnumValue(): ?GraphQLEnumValue { + return this._enumValue; + } + + // Flow does not yet handle this case. + enter(node: any /* ASTNode */) { + const schema = this._schema; + // Note: many of the types below are explicitly typed as "mixed" to drop + // any assumptions of a valid schema to ensure runtime types are properly + // checked before continuing since TypeInfo is used as part of validation + // which occurs before guarantees of schema and document validity. + switch (node.kind) { + case Kind.SELECTION_SET: + const namedType: mixed = getNamedType(this.getType()); + this._parentTypeStack.push( + isCompositeType(namedType) ? namedType : undefined, + ); + break; + case Kind.FIELD: + const parentType = this.getParentType(); + let fieldDef; + let fieldType: mixed; + if (parentType) { + fieldDef = this._getFieldDef(schema, parentType, node); + if (fieldDef) { + fieldType = fieldDef.type; + } + } + this._fieldDefStack.push(fieldDef); + this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined); + break; + case Kind.DIRECTIVE: + this._directive = schema.getDirective(node.name.value); + break; + case Kind.OPERATION_DEFINITION: + let type: mixed; + if (node.operation === 'query') { + type = schema.getQueryType(); + } else if (node.operation === 'mutation') { + type = schema.getMutationType(); + } else if (node.operation === 'subscription') { + type = schema.getSubscriptionType(); + } + this._typeStack.push(isObjectType(type) ? type : undefined); + break; + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: + const typeConditionAST = node.typeCondition; + const outputType: mixed = typeConditionAST + ? typeFromAST(schema, typeConditionAST) + : getNamedType(this.getType()); + this._typeStack.push(isOutputType(outputType) ? outputType : undefined); + break; + case Kind.VARIABLE_DEFINITION: + const inputType: mixed = typeFromAST(schema, node.type); + this._inputTypeStack.push( + isInputType(inputType) ? inputType : undefined, + ); + break; + case Kind.ARGUMENT: + let argDef; + let argType: mixed; + const fieldOrDirective = this.getDirective() || this.getFieldDef(); + if (fieldOrDirective) { + argDef = find( + fieldOrDirective.args, + arg => arg.name === node.name.value, + ); + if (argDef) { + argType = argDef.type; + } + } + this._argument = argDef; + this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined); + this._inputTypeStack.push(isInputType(argType) ? argType : undefined); + break; + case Kind.LIST: + const listType: mixed = getNullableType(this.getInputType()); + const itemType: mixed = isListType(listType) + ? listType.ofType + : listType; + // List positions never have a default value. + this._defaultValueStack.push(undefined); + this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined); + break; + case Kind.OBJECT_FIELD: + const objectType: mixed = getNamedType(this.getInputType()); + let inputFieldType: GraphQLInputType | void; + let inputField: GraphQLInputField | void; + if (isInputObjectType(objectType)) { + inputField = objectType.getFields()[node.name.value]; + if (inputField) { + inputFieldType = inputField.type; + } + } + this._defaultValueStack.push( + inputField ? inputField.defaultValue : undefined, + ); + this._inputTypeStack.push( + isInputType(inputFieldType) ? inputFieldType : undefined, + ); + break; + case Kind.ENUM: + const enumType: mixed = getNamedType(this.getInputType()); + let enumValue; + if (isEnumType(enumType)) { + enumValue = enumType.getValue(node.value); + } + this._enumValue = enumValue; + break; + } + } + + leave(node: ASTNode) { + switch (node.kind) { + case Kind.SELECTION_SET: + this._parentTypeStack.pop(); + break; + case Kind.FIELD: + this._fieldDefStack.pop(); + this._typeStack.pop(); + break; + case Kind.DIRECTIVE: + this._directive = null; + break; + case Kind.OPERATION_DEFINITION: + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: + this._typeStack.pop(); + break; + case Kind.VARIABLE_DEFINITION: + this._inputTypeStack.pop(); + break; + case Kind.ARGUMENT: + this._argument = null; + this._defaultValueStack.pop(); + this._inputTypeStack.pop(); + break; + case Kind.LIST: + case Kind.OBJECT_FIELD: + this._defaultValueStack.pop(); + this._inputTypeStack.pop(); + break; + case Kind.ENUM: + this._enumValue = null; + break; + } + } +} + +/** + * Not exactly the same as the executor's definition of getFieldDef, in this + * statically evaluated environment we do not always have an Object type, + * and need to handle Interface and Union types. + */ +function getFieldDef( + schema: GraphQLSchema, + parentType: GraphQLType, + fieldNode: FieldNode, +): ?GraphQLField<*, *> { + const name = fieldNode.name.value; + if ( + name === SchemaMetaFieldDef.name && + schema.getQueryType() === parentType + ) { + return SchemaMetaFieldDef; + } + if (name === TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return TypeMetaFieldDef; + } + if (name === TypeNameMetaFieldDef.name && isCompositeType(parentType)) { + return TypeNameMetaFieldDef; + } + if (isObjectType(parentType) || isInterfaceType(parentType)) { + return parentType.getFields()[name]; + } +} diff --git a/dist/utilities/TypeInfo.mjs b/dist/utilities/TypeInfo.mjs new file mode 100644 index 0000000000..7c06618bb1 --- /dev/null +++ b/dist/utilities/TypeInfo.mjs @@ -0,0 +1,340 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { Kind } from '../language/kinds'; +import { isObjectType, isInterfaceType, isEnumType, isInputObjectType, isListType, isCompositeType, isInputType, isOutputType, getNullableType, getNamedType } from '../type/definition'; +import { SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef } from '../type/introspection'; +import { typeFromAST } from './typeFromAST'; +import find from '../jsutils/find'; +/** + * TypeInfo is a utility class which, given a GraphQL schema, can keep track + * of the current field and type definitions at any point in a GraphQL document + * AST during a recursive descent by calling `enter(node)` and `leave(node)`. + */ + +export var TypeInfo = +/*#__PURE__*/ +function () { + function TypeInfo(schema, // NOTE: this experimental optional second parameter is only needed in order + // to support non-spec-compliant codebases. You should never need to use it. + getFieldDefFn, // Initial type may be provided in rare cases to facilitate traversals + initialType) { + _defineProperty(this, "_schema", void 0); + + _defineProperty(this, "_typeStack", void 0); + + _defineProperty(this, "_parentTypeStack", void 0); + + _defineProperty(this, "_inputTypeStack", void 0); + + _defineProperty(this, "_fieldDefStack", void 0); + + _defineProperty(this, "_defaultValueStack", void 0); + + _defineProperty(this, "_directive", void 0); + + _defineProperty(this, "_argument", void 0); + + _defineProperty(this, "_enumValue", void 0); + + _defineProperty(this, "_getFieldDef", void 0); + + this._schema = schema; + this._typeStack = []; + this._parentTypeStack = []; + this._inputTypeStack = []; + this._fieldDefStack = []; + this._defaultValueStack = []; + this._directive = null; + this._argument = null; + this._enumValue = null; + this._getFieldDef = getFieldDefFn || getFieldDef; + + if (initialType) { + if (isInputType(initialType)) { + this._inputTypeStack.push(initialType); + } + + if (isCompositeType(initialType)) { + this._parentTypeStack.push(initialType); + } + + if (isOutputType(initialType)) { + this._typeStack.push(initialType); + } + } + } + + var _proto = TypeInfo.prototype; + + _proto.getType = function getType() { + if (this._typeStack.length > 0) { + return this._typeStack[this._typeStack.length - 1]; + } + }; + + _proto.getParentType = function getParentType() { + if (this._parentTypeStack.length > 0) { + return this._parentTypeStack[this._parentTypeStack.length - 1]; + } + }; + + _proto.getInputType = function getInputType() { + if (this._inputTypeStack.length > 0) { + return this._inputTypeStack[this._inputTypeStack.length - 1]; + } + }; + + _proto.getParentInputType = function getParentInputType() { + if (this._inputTypeStack.length > 1) { + return this._inputTypeStack[this._inputTypeStack.length - 2]; + } + }; + + _proto.getFieldDef = function getFieldDef() { + if (this._fieldDefStack.length > 0) { + return this._fieldDefStack[this._fieldDefStack.length - 1]; + } + }; + + _proto.getDefaultValue = function getDefaultValue() { + if (this._defaultValueStack.length > 0) { + return this._defaultValueStack[this._defaultValueStack.length - 1]; + } + }; + + _proto.getDirective = function getDirective() { + return this._directive; + }; + + _proto.getArgument = function getArgument() { + return this._argument; + }; + + _proto.getEnumValue = function getEnumValue() { + return this._enumValue; + }; // Flow does not yet handle this case. + + + _proto.enter = function enter(node + /* ASTNode */ + ) { + var schema = this._schema; // Note: many of the types below are explicitly typed as "mixed" to drop + // any assumptions of a valid schema to ensure runtime types are properly + // checked before continuing since TypeInfo is used as part of validation + // which occurs before guarantees of schema and document validity. + + switch (node.kind) { + case Kind.SELECTION_SET: + var namedType = getNamedType(this.getType()); + + this._parentTypeStack.push(isCompositeType(namedType) ? namedType : undefined); + + break; + + case Kind.FIELD: + var parentType = this.getParentType(); + var fieldDef; + var fieldType; + + if (parentType) { + fieldDef = this._getFieldDef(schema, parentType, node); + + if (fieldDef) { + fieldType = fieldDef.type; + } + } + + this._fieldDefStack.push(fieldDef); + + this._typeStack.push(isOutputType(fieldType) ? fieldType : undefined); + + break; + + case Kind.DIRECTIVE: + this._directive = schema.getDirective(node.name.value); + break; + + case Kind.OPERATION_DEFINITION: + var type; + + if (node.operation === 'query') { + type = schema.getQueryType(); + } else if (node.operation === 'mutation') { + type = schema.getMutationType(); + } else if (node.operation === 'subscription') { + type = schema.getSubscriptionType(); + } + + this._typeStack.push(isObjectType(type) ? type : undefined); + + break; + + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: + var typeConditionAST = node.typeCondition; + var outputType = typeConditionAST ? typeFromAST(schema, typeConditionAST) : getNamedType(this.getType()); + + this._typeStack.push(isOutputType(outputType) ? outputType : undefined); + + break; + + case Kind.VARIABLE_DEFINITION: + var inputType = typeFromAST(schema, node.type); + + this._inputTypeStack.push(isInputType(inputType) ? inputType : undefined); + + break; + + case Kind.ARGUMENT: + var argDef; + var argType; + var fieldOrDirective = this.getDirective() || this.getFieldDef(); + + if (fieldOrDirective) { + argDef = find(fieldOrDirective.args, function (arg) { + return arg.name === node.name.value; + }); + + if (argDef) { + argType = argDef.type; + } + } + + this._argument = argDef; + + this._defaultValueStack.push(argDef ? argDef.defaultValue : undefined); + + this._inputTypeStack.push(isInputType(argType) ? argType : undefined); + + break; + + case Kind.LIST: + var listType = getNullableType(this.getInputType()); + var itemType = isListType(listType) ? listType.ofType : listType; // List positions never have a default value. + + this._defaultValueStack.push(undefined); + + this._inputTypeStack.push(isInputType(itemType) ? itemType : undefined); + + break; + + case Kind.OBJECT_FIELD: + var objectType = getNamedType(this.getInputType()); + var inputFieldType; + var inputField; + + if (isInputObjectType(objectType)) { + inputField = objectType.getFields()[node.name.value]; + + if (inputField) { + inputFieldType = inputField.type; + } + } + + this._defaultValueStack.push(inputField ? inputField.defaultValue : undefined); + + this._inputTypeStack.push(isInputType(inputFieldType) ? inputFieldType : undefined); + + break; + + case Kind.ENUM: + var enumType = getNamedType(this.getInputType()); + var enumValue; + + if (isEnumType(enumType)) { + enumValue = enumType.getValue(node.value); + } + + this._enumValue = enumValue; + break; + } + }; + + _proto.leave = function leave(node) { + switch (node.kind) { + case Kind.SELECTION_SET: + this._parentTypeStack.pop(); + + break; + + case Kind.FIELD: + this._fieldDefStack.pop(); + + this._typeStack.pop(); + + break; + + case Kind.DIRECTIVE: + this._directive = null; + break; + + case Kind.OPERATION_DEFINITION: + case Kind.INLINE_FRAGMENT: + case Kind.FRAGMENT_DEFINITION: + this._typeStack.pop(); + + break; + + case Kind.VARIABLE_DEFINITION: + this._inputTypeStack.pop(); + + break; + + case Kind.ARGUMENT: + this._argument = null; + + this._defaultValueStack.pop(); + + this._inputTypeStack.pop(); + + break; + + case Kind.LIST: + case Kind.OBJECT_FIELD: + this._defaultValueStack.pop(); + + this._inputTypeStack.pop(); + + break; + + case Kind.ENUM: + this._enumValue = null; + break; + } + }; + + return TypeInfo; +}(); +/** + * Not exactly the same as the executor's definition of getFieldDef, in this + * statically evaluated environment we do not always have an Object type, + * and need to handle Interface and Union types. + */ + +function getFieldDef(schema, parentType, fieldNode) { + var name = fieldNode.name.value; + + if (name === SchemaMetaFieldDef.name && schema.getQueryType() === parentType) { + return SchemaMetaFieldDef; + } + + if (name === TypeMetaFieldDef.name && schema.getQueryType() === parentType) { + return TypeMetaFieldDef; + } + + if (name === TypeNameMetaFieldDef.name && isCompositeType(parentType)) { + return TypeNameMetaFieldDef; + } + + if (isObjectType(parentType) || isInterfaceType(parentType)) { + return parentType.getFields()[name]; + } +} \ No newline at end of file diff --git a/dist/utilities/assertValidName.js b/dist/utilities/assertValidName.js new file mode 100644 index 0000000000..3d203083da --- /dev/null +++ b/dist/utilities/assertValidName.js @@ -0,0 +1,52 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.assertValidName = assertValidName; +exports.isValidNameError = isValidNameError; + +var _GraphQLError = require("../error/GraphQLError"); + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/; +/** + * Upholds the spec rules about naming. + */ + +function assertValidName(name) { + var error = isValidNameError(name); + + if (error) { + throw error; + } + + return name; +} +/** + * Returns an Error if a name is invalid. + */ + + +function isValidNameError(name, node) { + !(typeof name === 'string') ? (0, _invariant.default)(0, 'Expected string') : void 0; + + if (name.length > 1 && name[0] === '_' && name[1] === '_') { + return new _GraphQLError.GraphQLError("Name \"".concat(name, "\" must not begin with \"__\", which is reserved by ") + 'GraphQL introspection.', node); + } + + if (!NAME_RX.test(name)) { + return new _GraphQLError.GraphQLError("Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \"".concat(name, "\" does not."), node); + } +} \ No newline at end of file diff --git a/dist/utilities/assertValidName.js.flow b/dist/utilities/assertValidName.js.flow new file mode 100644 index 0000000000..cdc20b1d64 --- /dev/null +++ b/dist/utilities/assertValidName.js.flow @@ -0,0 +1,48 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLError } from '../error/GraphQLError'; +import type { ASTNode } from '../language/ast'; +import invariant from '../jsutils/invariant'; + +const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/; + +/** + * Upholds the spec rules about naming. + */ +export function assertValidName(name: string): string { + const error = isValidNameError(name); + if (error) { + throw error; + } + return name; +} + +/** + * Returns an Error if a name is invalid. + */ +export function isValidNameError( + name: string, + node?: ASTNode | void, +): GraphQLError | void { + invariant(typeof name === 'string', 'Expected string'); + if (name.length > 1 && name[0] === '_' && name[1] === '_') { + return new GraphQLError( + `Name "${name}" must not begin with "__", which is reserved by ` + + 'GraphQL introspection.', + node, + ); + } + if (!NAME_RX.test(name)) { + return new GraphQLError( + `Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`, + node, + ); + } +} diff --git a/dist/utilities/assertValidName.mjs b/dist/utilities/assertValidName.mjs new file mode 100644 index 0000000000..e737529345 --- /dev/null +++ b/dist/utilities/assertValidName.mjs @@ -0,0 +1,39 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../error/GraphQLError'; +import invariant from '../jsutils/invariant'; +var NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/; +/** + * Upholds the spec rules about naming. + */ + +export function assertValidName(name) { + var error = isValidNameError(name); + + if (error) { + throw error; + } + + return name; +} +/** + * Returns an Error if a name is invalid. + */ + +export function isValidNameError(name, node) { + !(typeof name === 'string') ? invariant(0, 'Expected string') : void 0; + + if (name.length > 1 && name[0] === '_' && name[1] === '_') { + return new GraphQLError("Name \"".concat(name, "\" must not begin with \"__\", which is reserved by ") + 'GraphQL introspection.', node); + } + + if (!NAME_RX.test(name)) { + return new GraphQLError("Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but \"".concat(name, "\" does not."), node); + } +} \ No newline at end of file diff --git a/dist/utilities/astFromValue.js b/dist/utilities/astFromValue.js new file mode 100644 index 0000000000..c5aa39cb5e --- /dev/null +++ b/dist/utilities/astFromValue.js @@ -0,0 +1,184 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.astFromValue = astFromValue; + +var _iterall = require("iterall"); + +var _isNullish = _interopRequireDefault(require("../jsutils/isNullish")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _kinds = require("../language/kinds"); + +var _definition = require("../type/definition"); + +var _scalars = require("../type/scalars"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +/** + * Produces a GraphQL Value AST given a JavaScript value. + * + * A GraphQL type must be provided, which will be used to interpret different + * JavaScript values. + * + * | JSON Value | GraphQL Value | + * | ------------- | -------------------- | + * | Object | Input Object | + * | Array | List | + * | Boolean | Boolean | + * | String | String / Enum Value | + * | Number | Int / Float | + * | Mixed | Enum Value | + * | null | NullValue | + * + */ +function astFromValue(value, type) { + if ((0, _definition.isNonNullType)(type)) { + var astValue = astFromValue(value, type.ofType); + + if (astValue && astValue.kind === _kinds.Kind.NULL) { + return null; + } + + return astValue; + } // only explicit null, not undefined, NaN + + + if (value === null) { + return { + kind: _kinds.Kind.NULL + }; + } // undefined, NaN + + + if ((0, _isInvalid.default)(value)) { + return null; + } // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but + // the value is not an array, convert the value using the list's item type. + + + if ((0, _definition.isListType)(type)) { + var itemType = type.ofType; + + if ((0, _iterall.isCollection)(value)) { + var valuesNodes = []; + (0, _iterall.forEach)(value, function (item) { + var itemNode = astFromValue(item, itemType); + + if (itemNode) { + valuesNodes.push(itemNode); + } + }); + return { + kind: _kinds.Kind.LIST, + values: valuesNodes + }; + } + + return astFromValue(value, itemType); + } // Populate the fields of the input object by creating ASTs from each value + // in the JavaScript object according to the fields in the input type. + + + if ((0, _definition.isInputObjectType)(type)) { + if (value === null || _typeof(value) !== 'object') { + return null; + } + + var fields = (0, _objectValues.default)(type.getFields()); + var fieldNodes = []; + fields.forEach(function (field) { + var fieldValue = astFromValue(value[field.name], field.type); + + if (fieldValue) { + fieldNodes.push({ + kind: _kinds.Kind.OBJECT_FIELD, + name: { + kind: _kinds.Kind.NAME, + value: field.name + }, + value: fieldValue + }); + } + }); + return { + kind: _kinds.Kind.OBJECT, + fields: fieldNodes + }; + } + + if ((0, _definition.isScalarType)(type) || (0, _definition.isEnumType)(type)) { + // Since value is an internally represented value, it must be serialized + // to an externally represented value before converting into an AST. + var serialized = type.serialize(value); + + if ((0, _isNullish.default)(serialized)) { + return null; + } // Others serialize based on their corresponding JavaScript scalar types. + + + if (typeof serialized === 'boolean') { + return { + kind: _kinds.Kind.BOOLEAN, + value: serialized + }; + } // JavaScript numbers can be Int or Float values. + + + if (typeof serialized === 'number') { + var stringNum = String(serialized); + return integerStringRegExp.test(stringNum) ? { + kind: _kinds.Kind.INT, + value: stringNum + } : { + kind: _kinds.Kind.FLOAT, + value: stringNum + }; + } + + if (typeof serialized === 'string') { + // Enum types use Enum literals. + if ((0, _definition.isEnumType)(type)) { + return { + kind: _kinds.Kind.ENUM, + value: serialized + }; + } // ID types can use Int literals. + + + if (type === _scalars.GraphQLID && integerStringRegExp.test(serialized)) { + return { + kind: _kinds.Kind.INT, + value: serialized + }; + } + + return { + kind: _kinds.Kind.STRING, + value: serialized + }; + } + + throw new TypeError('Cannot convert value to AST: ' + String(serialized)); + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} +/** + * IntValue: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit ( Digit+ )? + */ + + +var integerStringRegExp = /^-?(0|[1-9][0-9]*)$/; \ No newline at end of file diff --git a/dist/utilities/astFromValue.js.flow b/dist/utilities/astFromValue.js.flow new file mode 100644 index 0000000000..988430d61f --- /dev/null +++ b/dist/utilities/astFromValue.js.flow @@ -0,0 +1,151 @@ +/** + * 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. + * + * @flow strict + */ + +import { forEach, isCollection } from 'iterall'; + +import isNullish from '../jsutils/isNullish'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import type { ValueNode } from '../language/ast'; +import { Kind } from '../language/kinds'; +import type { GraphQLInputType } from '../type/definition'; +import { + isScalarType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, +} from '../type/definition'; +import { GraphQLID } from '../type/scalars'; + +/** + * Produces a GraphQL Value AST given a JavaScript value. + * + * A GraphQL type must be provided, which will be used to interpret different + * JavaScript values. + * + * | JSON Value | GraphQL Value | + * | ------------- | -------------------- | + * | Object | Input Object | + * | Array | List | + * | Boolean | Boolean | + * | String | String / Enum Value | + * | Number | Int / Float | + * | Mixed | Enum Value | + * | null | NullValue | + * + */ +export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode { + if (isNonNullType(type)) { + const astValue = astFromValue(value, type.ofType); + if (astValue && astValue.kind === Kind.NULL) { + return null; + } + return astValue; + } + + // only explicit null, not undefined, NaN + if (value === null) { + return { kind: Kind.NULL }; + } + + // undefined, NaN + if (isInvalid(value)) { + return null; + } + + // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but + // the value is not an array, convert the value using the list's item type. + if (isListType(type)) { + const itemType = type.ofType; + if (isCollection(value)) { + const valuesNodes = []; + forEach((value: any), item => { + const itemNode = astFromValue(item, itemType); + if (itemNode) { + valuesNodes.push(itemNode); + } + }); + return { kind: Kind.LIST, values: valuesNodes }; + } + return astFromValue(value, itemType); + } + + // Populate the fields of the input object by creating ASTs from each value + // in the JavaScript object according to the fields in the input type. + if (isInputObjectType(type)) { + if (value === null || typeof value !== 'object') { + return null; + } + const fields = objectValues(type.getFields()); + const fieldNodes = []; + fields.forEach(field => { + const fieldValue = astFromValue(value[field.name], field.type); + if (fieldValue) { + fieldNodes.push({ + kind: Kind.OBJECT_FIELD, + name: { kind: Kind.NAME, value: field.name }, + value: fieldValue, + }); + } + }); + return { kind: Kind.OBJECT, fields: fieldNodes }; + } + + if (isScalarType(type) || isEnumType(type)) { + // Since value is an internally represented value, it must be serialized + // to an externally represented value before converting into an AST. + const serialized = type.serialize(value); + if (isNullish(serialized)) { + return null; + } + + // Others serialize based on their corresponding JavaScript scalar types. + if (typeof serialized === 'boolean') { + return { kind: Kind.BOOLEAN, value: serialized }; + } + + // JavaScript numbers can be Int or Float values. + if (typeof serialized === 'number') { + const stringNum = String(serialized); + return integerStringRegExp.test(stringNum) + ? { kind: Kind.INT, value: stringNum } + : { kind: Kind.FLOAT, value: stringNum }; + } + + if (typeof serialized === 'string') { + // Enum types use Enum literals. + if (isEnumType(type)) { + return { kind: Kind.ENUM, value: serialized }; + } + + // ID types can use Int literals. + if (type === GraphQLID && integerStringRegExp.test(serialized)) { + return { kind: Kind.INT, value: serialized }; + } + + return { + kind: Kind.STRING, + value: serialized, + }; + } + + throw new TypeError('Cannot convert value to AST: ' + String(serialized)); + } + + /* istanbul ignore next */ + throw new Error(`Unknown type: ${(type: empty)}.`); +} + +/** + * IntValue: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit ( Digit+ )? + */ +const integerStringRegExp = /^-?(0|[1-9][0-9]*)$/; diff --git a/dist/utilities/astFromValue.mjs b/dist/utilities/astFromValue.mjs new file mode 100644 index 0000000000..ac261f5a49 --- /dev/null +++ b/dist/utilities/astFromValue.mjs @@ -0,0 +1,176 @@ +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +/** + * 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. + * + * strict + */ +import { forEach, isCollection } from 'iterall'; +import isNullish from '../jsutils/isNullish'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { Kind } from '../language/kinds'; +import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition'; +import { GraphQLID } from '../type/scalars'; +/** + * Produces a GraphQL Value AST given a JavaScript value. + * + * A GraphQL type must be provided, which will be used to interpret different + * JavaScript values. + * + * | JSON Value | GraphQL Value | + * | ------------- | -------------------- | + * | Object | Input Object | + * | Array | List | + * | Boolean | Boolean | + * | String | String / Enum Value | + * | Number | Int / Float | + * | Mixed | Enum Value | + * | null | NullValue | + * + */ + +export function astFromValue(value, type) { + if (isNonNullType(type)) { + var astValue = astFromValue(value, type.ofType); + + if (astValue && astValue.kind === Kind.NULL) { + return null; + } + + return astValue; + } // only explicit null, not undefined, NaN + + + if (value === null) { + return { + kind: Kind.NULL + }; + } // undefined, NaN + + + if (isInvalid(value)) { + return null; + } // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but + // the value is not an array, convert the value using the list's item type. + + + if (isListType(type)) { + var itemType = type.ofType; + + if (isCollection(value)) { + var valuesNodes = []; + forEach(value, function (item) { + var itemNode = astFromValue(item, itemType); + + if (itemNode) { + valuesNodes.push(itemNode); + } + }); + return { + kind: Kind.LIST, + values: valuesNodes + }; + } + + return astFromValue(value, itemType); + } // Populate the fields of the input object by creating ASTs from each value + // in the JavaScript object according to the fields in the input type. + + + if (isInputObjectType(type)) { + if (value === null || _typeof(value) !== 'object') { + return null; + } + + var fields = objectValues(type.getFields()); + var fieldNodes = []; + fields.forEach(function (field) { + var fieldValue = astFromValue(value[field.name], field.type); + + if (fieldValue) { + fieldNodes.push({ + kind: Kind.OBJECT_FIELD, + name: { + kind: Kind.NAME, + value: field.name + }, + value: fieldValue + }); + } + }); + return { + kind: Kind.OBJECT, + fields: fieldNodes + }; + } + + if (isScalarType(type) || isEnumType(type)) { + // Since value is an internally represented value, it must be serialized + // to an externally represented value before converting into an AST. + var serialized = type.serialize(value); + + if (isNullish(serialized)) { + return null; + } // Others serialize based on their corresponding JavaScript scalar types. + + + if (typeof serialized === 'boolean') { + return { + kind: Kind.BOOLEAN, + value: serialized + }; + } // JavaScript numbers can be Int or Float values. + + + if (typeof serialized === 'number') { + var stringNum = String(serialized); + return integerStringRegExp.test(stringNum) ? { + kind: Kind.INT, + value: stringNum + } : { + kind: Kind.FLOAT, + value: stringNum + }; + } + + if (typeof serialized === 'string') { + // Enum types use Enum literals. + if (isEnumType(type)) { + return { + kind: Kind.ENUM, + value: serialized + }; + } // ID types can use Int literals. + + + if (type === GraphQLID && integerStringRegExp.test(serialized)) { + return { + kind: Kind.INT, + value: serialized + }; + } + + return { + kind: Kind.STRING, + value: serialized + }; + } + + throw new TypeError('Cannot convert value to AST: ' + String(serialized)); + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} +/** + * IntValue: + * - NegativeSign? 0 + * - NegativeSign? NonZeroDigit ( Digit+ )? + */ + +var integerStringRegExp = /^-?(0|[1-9][0-9]*)$/; \ No newline at end of file diff --git a/dist/utilities/buildASTSchema.js b/dist/utilities/buildASTSchema.js new file mode 100644 index 0000000000..7f588c7094 --- /dev/null +++ b/dist/utilities/buildASTSchema.js @@ -0,0 +1,474 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.buildASTSchema = buildASTSchema; +exports.getDescription = getDescription; +exports.buildSchema = buildSchema; +exports.ASTDefinitionBuilder = void 0; + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap")); + +var _valueFromAST = require("./valueFromAST"); + +var _blockStringValue = _interopRequireDefault(require("../language/blockStringValue")); + +var _lexer = require("../language/lexer"); + +var _parser = require("../language/parser"); + +var _values = require("../execution/values"); + +var _kinds = require("../language/kinds"); + +var _definition = require("../type/definition"); + +var _directives = require("../type/directives"); + +var _introspection = require("../type/introspection"); + +var _scalars = require("../type/scalars"); + +var _schema = require("../type/schema"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function buildWrappedType(innerType, inputTypeNode) { + if (inputTypeNode.kind === _kinds.Kind.LIST_TYPE) { + return (0, _definition.GraphQLList)(buildWrappedType(innerType, inputTypeNode.type)); + } + + if (inputTypeNode.kind === _kinds.Kind.NON_NULL_TYPE) { + var wrappedType = buildWrappedType(innerType, inputTypeNode.type); + return (0, _definition.GraphQLNonNull)((0, _definition.assertNullableType)(wrappedType)); + } + + return innerType; +} + +function getNamedTypeNode(typeNode) { + var namedType = typeNode; + + while (namedType.kind === _kinds.Kind.LIST_TYPE || namedType.kind === _kinds.Kind.NON_NULL_TYPE) { + namedType = namedType.type; + } + + return namedType; +} +/** + * This takes the ast of a schema document produced by the parse function in + * src/language/parser.js. + * + * If no schema definition is provided, then it will look for types named Query + * and Mutation. + * + * Given that AST it constructs a GraphQLSchema. The resulting schema + * has no resolve methods, so execution will use default resolvers. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ + + +function buildASTSchema(ast, options) { + if (!ast || ast.kind !== _kinds.Kind.DOCUMENT) { + throw new Error('Must provide a document ast.'); + } + + var schemaDef; + var typeDefs = []; + var nodeMap = Object.create(null); + var directiveDefs = []; + + for (var i = 0; i < ast.definitions.length; i++) { + var d = ast.definitions[i]; + + switch (d.kind) { + case _kinds.Kind.SCHEMA_DEFINITION: + if (schemaDef) { + throw new Error('Must provide only one schema definition.'); + } + + schemaDef = d; + break; + + case _kinds.Kind.SCALAR_TYPE_DEFINITION: + case _kinds.Kind.OBJECT_TYPE_DEFINITION: + case _kinds.Kind.INTERFACE_TYPE_DEFINITION: + case _kinds.Kind.ENUM_TYPE_DEFINITION: + case _kinds.Kind.UNION_TYPE_DEFINITION: + case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION: + var typeName = d.name.value; + + if (nodeMap[typeName]) { + throw new Error("Type \"".concat(typeName, "\" was defined more than once.")); + } + + typeDefs.push(d); + nodeMap[typeName] = d; + break; + + case _kinds.Kind.DIRECTIVE_DEFINITION: + directiveDefs.push(d); + break; + } + } + + var operationTypes = schemaDef ? getOperationTypes(schemaDef) : { + query: nodeMap.Query, + mutation: nodeMap.Mutation, + subscription: nodeMap.Subscription + }; + var definitionBuilder = new ASTDefinitionBuilder(nodeMap, options, function (typeRef) { + throw new Error("Type \"".concat(typeRef.name.value, "\" not found in document.")); + }); + var types = definitionBuilder.buildTypes(typeDefs); + var directives = directiveDefs.map(function (def) { + return definitionBuilder.buildDirective(def); + }); // If specified directives were not explicitly declared, add them. + + if (!directives.some(function (directive) { + return directive.name === 'skip'; + })) { + directives.push(_directives.GraphQLSkipDirective); + } + + if (!directives.some(function (directive) { + return directive.name === 'include'; + })) { + directives.push(_directives.GraphQLIncludeDirective); + } + + if (!directives.some(function (directive) { + return directive.name === 'deprecated'; + })) { + directives.push(_directives.GraphQLDeprecatedDirective); + } // Note: While this could make early assertions to get the correctly + // typed values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + + + return new _schema.GraphQLSchema({ + query: operationTypes.query ? definitionBuilder.buildType(operationTypes.query) : null, + mutation: operationTypes.mutation ? definitionBuilder.buildType(operationTypes.mutation) : null, + subscription: operationTypes.subscription ? definitionBuilder.buildType(operationTypes.subscription) : null, + types: types, + directives: directives, + astNode: schemaDef, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames + }); + + function getOperationTypes(schema) { + var opTypes = {}; + schema.operationTypes.forEach(function (operationType) { + var typeName = operationType.type.name.value; + var operation = operationType.operation; + + if (opTypes[operation]) { + throw new Error("Must provide only one ".concat(operation, " type in schema.")); + } + + if (!nodeMap[typeName]) { + throw new Error("Specified ".concat(operation, " type \"").concat(typeName, "\" not found in document.")); + } + + opTypes[operation] = operationType.type; + }); + return opTypes; + } +} + +var ASTDefinitionBuilder = +/*#__PURE__*/ +function () { + function ASTDefinitionBuilder(typeDefinitionsMap, options, resolveType) { + _defineProperty(this, "_typeDefinitionsMap", void 0); + + _defineProperty(this, "_options", void 0); + + _defineProperty(this, "_resolveType", void 0); + + _defineProperty(this, "_cache", void 0); + + this._typeDefinitionsMap = typeDefinitionsMap; + this._options = options; + this._resolveType = resolveType; // Initialize to the GraphQL built in scalars and introspection types. + + this._cache = (0, _keyMap.default)(_scalars.specifiedScalarTypes.concat(_introspection.introspectionTypes), function (type) { + return type.name; + }); + } + + var _proto = ASTDefinitionBuilder.prototype; + + _proto.buildTypes = function buildTypes(nodes) { + var _this = this; + + return nodes.map(function (node) { + return _this.buildType(node); + }); + }; + + _proto.buildType = function buildType(node) { + var typeName = node.name.value; + + if (!this._cache[typeName]) { + if (node.kind === _kinds.Kind.NAMED_TYPE) { + var defNode = this._typeDefinitionsMap[typeName]; + this._cache[typeName] = defNode ? this._makeSchemaDef(defNode) : this._resolveType(node); + } else { + this._cache[typeName] = this._makeSchemaDef(node); + } + } + + return this._cache[typeName]; + }; + + _proto._buildWrappedType = function _buildWrappedType(typeNode) { + var typeDef = this.buildType(getNamedTypeNode(typeNode)); + return buildWrappedType(typeDef, typeNode); + }; + + _proto.buildDirective = function buildDirective(directiveNode) { + return new _directives.GraphQLDirective({ + name: directiveNode.name.value, + description: getDescription(directiveNode, this._options), + locations: directiveNode.locations.map(function (node) { + return node.value; + }), + args: directiveNode.arguments && this._makeInputValues(directiveNode.arguments), + astNode: directiveNode + }); + }; + + _proto.buildField = function buildField(field) { + return { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + type: this._buildWrappedType(field.type), + description: getDescription(field, this._options), + args: field.arguments && this._makeInputValues(field.arguments), + deprecationReason: getDeprecationReason(field), + astNode: field + }; + }; + + _proto._makeSchemaDef = function _makeSchemaDef(def) { + switch (def.kind) { + case _kinds.Kind.OBJECT_TYPE_DEFINITION: + return this._makeTypeDef(def); + + case _kinds.Kind.INTERFACE_TYPE_DEFINITION: + return this._makeInterfaceDef(def); + + case _kinds.Kind.ENUM_TYPE_DEFINITION: + return this._makeEnumDef(def); + + case _kinds.Kind.UNION_TYPE_DEFINITION: + return this._makeUnionDef(def); + + case _kinds.Kind.SCALAR_TYPE_DEFINITION: + return this._makeScalarDef(def); + + case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION: + return this._makeInputObjectDef(def); + + default: + throw new Error("Type kind \"".concat(def.kind, "\" not supported.")); + } + }; + + _proto._makeTypeDef = function _makeTypeDef(def) { + var _this2 = this; + + var typeName = def.name.value; + var interfaces = def.interfaces; + return new _definition.GraphQLObjectType({ + name: typeName, + description: getDescription(def, this._options), + fields: function fields() { + return _this2._makeFieldDefMap(def); + }, + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces: interfaces ? function () { + return _this2.buildTypes(interfaces); + } : [], + astNode: def + }); + }; + + _proto._makeFieldDefMap = function _makeFieldDefMap(def) { + var _this3 = this; + + return def.fields ? (0, _keyValMap.default)(def.fields, function (field) { + return field.name.value; + }, function (field) { + return _this3.buildField(field); + }) : {}; + }; + + _proto._makeInputValues = function _makeInputValues(values) { + var _this4 = this; + + return (0, _keyValMap.default)(values, function (value) { + return value.name.value; + }, function (value) { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + var type = _this4._buildWrappedType(value.type); + + return { + type: type, + description: getDescription(value, _this4._options), + defaultValue: (0, _valueFromAST.valueFromAST)(value.defaultValue, type), + astNode: value + }; + }); + }; + + _proto._makeInterfaceDef = function _makeInterfaceDef(def) { + var _this5 = this; + + return new _definition.GraphQLInterfaceType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: function fields() { + return _this5._makeFieldDefMap(def); + }, + astNode: def + }); + }; + + _proto._makeEnumDef = function _makeEnumDef(def) { + var _this6 = this; + + return new _definition.GraphQLEnumType({ + name: def.name.value, + description: getDescription(def, this._options), + values: def.values ? (0, _keyValMap.default)(def.values, function (enumValue) { + return enumValue.name.value; + }, function (enumValue) { + return { + description: getDescription(enumValue, _this6._options), + deprecationReason: getDeprecationReason(enumValue), + astNode: enumValue + }; + }) : {}, + astNode: def + }); + }; + + _proto._makeUnionDef = function _makeUnionDef(def) { + return new _definition.GraphQLUnionType({ + name: def.name.value, + description: getDescription(def, this._options), + // Note: While this could make assertions to get the correctly typed + // values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + types: def.types ? this.buildTypes(def.types) : [], + astNode: def + }); + }; + + _proto._makeScalarDef = function _makeScalarDef(def) { + return new _definition.GraphQLScalarType({ + name: def.name.value, + description: getDescription(def, this._options), + astNode: def, + serialize: function serialize(value) { + return value; + } + }); + }; + + _proto._makeInputObjectDef = function _makeInputObjectDef(def) { + var _this7 = this; + + return new _definition.GraphQLInputObjectType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: function fields() { + return def.fields ? _this7._makeInputValues(def.fields) : {}; + }, + astNode: def + }); + }; + + return ASTDefinitionBuilder; +}(); +/** + * Given a field or enum value node, returns the string value for the + * deprecation reason. + */ + + +exports.ASTDefinitionBuilder = ASTDefinitionBuilder; + +function getDeprecationReason(node) { + var deprecated = (0, _values.getDirectiveValues)(_directives.GraphQLDeprecatedDirective, node); + return deprecated && deprecated.reason; +} +/** + * Given an ast node, returns its string description. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ + + +function getDescription(node, options) { + if (node.description) { + return node.description.value; + } + + if (options && options.commentDescriptions) { + var rawValue = getLeadingCommentBlock(node); + + if (rawValue !== undefined) { + return (0, _blockStringValue.default)('\n' + rawValue); + } + } +} + +function getLeadingCommentBlock(node) { + var loc = node.loc; + + if (!loc) { + return; + } + + var comments = []; + var token = loc.startToken.prev; + + while (token && token.kind === _lexer.TokenKind.COMMENT && token.next && token.prev && token.line + 1 === token.next.line && token.line !== token.prev.line) { + var value = String(token.value); + comments.push(value); + token = token.prev; + } + + return comments.reverse().join('\n'); +} +/** + * A helper function to build a GraphQLSchema directly from a source + * document. + */ + + +function buildSchema(source, options) { + return buildASTSchema((0, _parser.parse)(source, options), options); +} \ No newline at end of file diff --git a/dist/utilities/buildASTSchema.js.flow b/dist/utilities/buildASTSchema.js.flow new file mode 100644 index 0000000000..2ff2f7dd1d --- /dev/null +++ b/dist/utilities/buildASTSchema.js.flow @@ -0,0 +1,511 @@ +/** + * 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. + * + * @flow strict + */ + +import keyMap from '../jsutils/keyMap'; +import keyValMap from '../jsutils/keyValMap'; +import type { ObjMap } from '../jsutils/ObjMap'; +import { valueFromAST } from './valueFromAST'; +import blockStringValue from '../language/blockStringValue'; +import { TokenKind } from '../language/lexer'; +import { parse } from '../language/parser'; +import type { ParseOptions } from '../language/parser'; +import type { Source } from '../language/source'; +import { getDirectiveValues } from '../execution/values'; +import { Kind } from '../language/kinds'; + +import type { + DocumentNode, + TypeNode, + NamedTypeNode, + SchemaDefinitionNode, + TypeDefinitionNode, + ScalarTypeDefinitionNode, + ObjectTypeDefinitionNode, + FieldDefinitionNode, + InputValueDefinitionNode, + InterfaceTypeDefinitionNode, + UnionTypeDefinitionNode, + EnumTypeDefinitionNode, + EnumValueDefinitionNode, + InputObjectTypeDefinitionNode, + DirectiveDefinitionNode, + StringValueNode, + Location, +} from '../language/ast'; + +import type { DirectiveLocationEnum } from '../language/directiveLocation'; + +import { + assertNullableType, + GraphQLScalarType, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, +} from '../type/definition'; + +import { + GraphQLDirective, + GraphQLSkipDirective, + GraphQLIncludeDirective, + GraphQLDeprecatedDirective, +} from '../type/directives'; + +import { introspectionTypes } from '../type/introspection'; + +import { specifiedScalarTypes } from '../type/scalars'; + +import { GraphQLSchema } from '../type/schema'; +import type { GraphQLSchemaValidationOptions } from '../type/schema'; + +import type { + GraphQLType, + GraphQLNamedType, + GraphQLFieldConfig, +} from '../type/definition'; + +export type BuildSchemaOptions = { + ...GraphQLSchemaValidationOptions, + + /** + * Descriptions are defined as preceding string literals, however an older + * experimental version of the SDL supported preceding comments as + * descriptions. Set to true to enable this deprecated behavior. + * + * Default: false + */ + commentDescriptions?: boolean, +}; + +function buildWrappedType( + innerType: GraphQLType, + inputTypeNode: TypeNode, +): GraphQLType { + if (inputTypeNode.kind === Kind.LIST_TYPE) { + return GraphQLList(buildWrappedType(innerType, inputTypeNode.type)); + } + if (inputTypeNode.kind === Kind.NON_NULL_TYPE) { + const wrappedType = buildWrappedType(innerType, inputTypeNode.type); + return GraphQLNonNull(assertNullableType(wrappedType)); + } + return innerType; +} + +function getNamedTypeNode(typeNode: TypeNode): NamedTypeNode { + let namedType = typeNode; + while ( + namedType.kind === Kind.LIST_TYPE || + namedType.kind === Kind.NON_NULL_TYPE + ) { + namedType = namedType.type; + } + return namedType; +} + +/** + * This takes the ast of a schema document produced by the parse function in + * src/language/parser.js. + * + * If no schema definition is provided, then it will look for types named Query + * and Mutation. + * + * Given that AST it constructs a GraphQLSchema. The resulting schema + * has no resolve methods, so execution will use default resolvers. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function buildASTSchema( + ast: DocumentNode, + options?: BuildSchemaOptions, +): GraphQLSchema { + if (!ast || ast.kind !== Kind.DOCUMENT) { + throw new Error('Must provide a document ast.'); + } + + let schemaDef: ?SchemaDefinitionNode; + + const typeDefs: Array = []; + const nodeMap: ObjMap = Object.create(null); + const directiveDefs: Array = []; + for (let i = 0; i < ast.definitions.length; i++) { + const d = ast.definitions[i]; + switch (d.kind) { + case Kind.SCHEMA_DEFINITION: + if (schemaDef) { + throw new Error('Must provide only one schema definition.'); + } + schemaDef = d; + break; + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.ENUM_TYPE_DEFINITION: + case Kind.UNION_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + const typeName = d.name.value; + if (nodeMap[typeName]) { + throw new Error(`Type "${typeName}" was defined more than once.`); + } + typeDefs.push(d); + nodeMap[typeName] = d; + break; + case Kind.DIRECTIVE_DEFINITION: + directiveDefs.push(d); + break; + } + } + + const operationTypes = schemaDef + ? getOperationTypes(schemaDef) + : { + query: nodeMap.Query, + mutation: nodeMap.Mutation, + subscription: nodeMap.Subscription, + }; + + const definitionBuilder = new ASTDefinitionBuilder( + nodeMap, + options, + typeRef => { + throw new Error(`Type "${typeRef.name.value}" not found in document.`); + }, + ); + + const types = definitionBuilder.buildTypes(typeDefs); + const directives = directiveDefs.map(def => + definitionBuilder.buildDirective(def), + ); + + // If specified directives were not explicitly declared, add them. + if (!directives.some(directive => directive.name === 'skip')) { + directives.push(GraphQLSkipDirective); + } + + if (!directives.some(directive => directive.name === 'include')) { + directives.push(GraphQLIncludeDirective); + } + + if (!directives.some(directive => directive.name === 'deprecated')) { + directives.push(GraphQLDeprecatedDirective); + } + + // Note: While this could make early assertions to get the correctly + // typed values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + return new GraphQLSchema({ + query: operationTypes.query + ? (definitionBuilder.buildType(operationTypes.query): any) + : null, + mutation: operationTypes.mutation + ? (definitionBuilder.buildType(operationTypes.mutation): any) + : null, + subscription: operationTypes.subscription + ? (definitionBuilder.buildType(operationTypes.subscription): any) + : null, + types, + directives, + astNode: schemaDef, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames, + }); + + function getOperationTypes(schema: SchemaDefinitionNode) { + const opTypes = {}; + schema.operationTypes.forEach(operationType => { + const typeName = operationType.type.name.value; + const operation = operationType.operation; + if (opTypes[operation]) { + throw new Error(`Must provide only one ${operation} type in schema.`); + } + if (!nodeMap[typeName]) { + throw new Error( + `Specified ${operation} type "${typeName}" not found in document.`, + ); + } + opTypes[operation] = operationType.type; + }); + return opTypes; + } +} + +type TypeDefinitionsMap = ObjMap; +type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType; + +export class ASTDefinitionBuilder { + _typeDefinitionsMap: TypeDefinitionsMap; + _options: ?BuildSchemaOptions; + _resolveType: TypeResolver; + _cache: ObjMap; + + constructor( + typeDefinitionsMap: TypeDefinitionsMap, + options: ?BuildSchemaOptions, + resolveType: TypeResolver, + ) { + this._typeDefinitionsMap = typeDefinitionsMap; + this._options = options; + this._resolveType = resolveType; + // Initialize to the GraphQL built in scalars and introspection types. + this._cache = keyMap( + specifiedScalarTypes.concat(introspectionTypes), + type => type.name, + ); + } + + buildTypes( + nodes: $ReadOnlyArray, + ): Array { + return nodes.map(node => this.buildType(node)); + } + + buildType(node: NamedTypeNode | TypeDefinitionNode): GraphQLNamedType { + const typeName = node.name.value; + if (!this._cache[typeName]) { + if (node.kind === Kind.NAMED_TYPE) { + const defNode = this._typeDefinitionsMap[typeName]; + this._cache[typeName] = defNode + ? this._makeSchemaDef(defNode) + : this._resolveType(node); + } else { + this._cache[typeName] = this._makeSchemaDef(node); + } + } + return this._cache[typeName]; + } + + _buildWrappedType(typeNode: TypeNode): GraphQLType { + const typeDef = this.buildType(getNamedTypeNode(typeNode)); + return buildWrappedType(typeDef, typeNode); + } + + buildDirective(directiveNode: DirectiveDefinitionNode): GraphQLDirective { + return new GraphQLDirective({ + name: directiveNode.name.value, + description: getDescription(directiveNode, this._options), + locations: directiveNode.locations.map( + node => ((node.value: any): DirectiveLocationEnum), + ), + args: + directiveNode.arguments && + this._makeInputValues(directiveNode.arguments), + astNode: directiveNode, + }); + } + + buildField(field: FieldDefinitionNode): GraphQLFieldConfig<*, *> { + return { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + type: (this._buildWrappedType(field.type): any), + description: getDescription(field, this._options), + args: field.arguments && this._makeInputValues(field.arguments), + deprecationReason: getDeprecationReason(field), + astNode: field, + }; + } + + _makeSchemaDef(def: TypeDefinitionNode): GraphQLNamedType { + switch (def.kind) { + case Kind.OBJECT_TYPE_DEFINITION: + return this._makeTypeDef(def); + case Kind.INTERFACE_TYPE_DEFINITION: + return this._makeInterfaceDef(def); + case Kind.ENUM_TYPE_DEFINITION: + return this._makeEnumDef(def); + case Kind.UNION_TYPE_DEFINITION: + return this._makeUnionDef(def); + case Kind.SCALAR_TYPE_DEFINITION: + return this._makeScalarDef(def); + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + return this._makeInputObjectDef(def); + default: + throw new Error(`Type kind "${def.kind}" not supported.`); + } + } + + _makeTypeDef(def: ObjectTypeDefinitionNode) { + const typeName = def.name.value; + const interfaces = def.interfaces; + return new GraphQLObjectType({ + name: typeName, + description: getDescription(def, this._options), + fields: () => this._makeFieldDefMap(def), + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces: interfaces ? () => (this.buildTypes(interfaces): any) : [], + astNode: def, + }); + } + + _makeFieldDefMap( + def: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode, + ) { + return def.fields + ? keyValMap( + def.fields, + field => field.name.value, + field => this.buildField(field), + ) + : {}; + } + + _makeInputValues(values: $ReadOnlyArray) { + return keyValMap( + values, + value => value.name.value, + value => { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + const type: any = this._buildWrappedType(value.type); + return { + type, + description: getDescription(value, this._options), + defaultValue: valueFromAST(value.defaultValue, type), + astNode: value, + }; + }, + ); + } + + _makeInterfaceDef(def: InterfaceTypeDefinitionNode) { + return new GraphQLInterfaceType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: () => this._makeFieldDefMap(def), + astNode: def, + }); + } + + _makeEnumDef(def: EnumTypeDefinitionNode) { + return new GraphQLEnumType({ + name: def.name.value, + description: getDescription(def, this._options), + values: def.values + ? keyValMap( + def.values, + enumValue => enumValue.name.value, + enumValue => ({ + description: getDescription(enumValue, this._options), + deprecationReason: getDeprecationReason(enumValue), + astNode: enumValue, + }), + ) + : {}, + astNode: def, + }); + } + + _makeUnionDef(def: UnionTypeDefinitionNode) { + return new GraphQLUnionType({ + name: def.name.value, + description: getDescription(def, this._options), + // Note: While this could make assertions to get the correctly typed + // values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + types: def.types ? (this.buildTypes(def.types): any) : [], + astNode: def, + }); + } + + _makeScalarDef(def: ScalarTypeDefinitionNode) { + return new GraphQLScalarType({ + name: def.name.value, + description: getDescription(def, this._options), + astNode: def, + serialize: value => value, + }); + } + + _makeInputObjectDef(def: InputObjectTypeDefinitionNode) { + return new GraphQLInputObjectType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: () => (def.fields ? this._makeInputValues(def.fields) : {}), + astNode: def, + }); + } +} + +/** + * Given a field or enum value node, returns the string value for the + * deprecation reason. + */ +function getDeprecationReason( + node: EnumValueDefinitionNode | FieldDefinitionNode, +): ?string { + const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node); + return deprecated && (deprecated.reason: any); +} + +/** + * Given an ast node, returns its string description. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function getDescription( + node: { +description?: StringValueNode, +loc?: Location }, + options: ?BuildSchemaOptions, +): void | string { + if (node.description) { + return node.description.value; + } + if (options && options.commentDescriptions) { + const rawValue = getLeadingCommentBlock(node); + if (rawValue !== undefined) { + return blockStringValue('\n' + rawValue); + } + } +} + +function getLeadingCommentBlock(node): void | string { + const loc = node.loc; + if (!loc) { + return; + } + const comments = []; + let token = loc.startToken.prev; + while ( + token && + token.kind === TokenKind.COMMENT && + token.next && + token.prev && + token.line + 1 === token.next.line && + token.line !== token.prev.line + ) { + const value = String(token.value); + comments.push(value); + token = token.prev; + } + return comments.reverse().join('\n'); +} + +/** + * A helper function to build a GraphQLSchema directly from a source + * document. + */ +export function buildSchema( + source: string | Source, + options?: BuildSchemaOptions & ParseOptions, +): GraphQLSchema { + return buildASTSchema(parse(source, options), options); +} diff --git a/dist/utilities/buildASTSchema.mjs b/dist/utilities/buildASTSchema.mjs new file mode 100644 index 0000000000..d7d61793a7 --- /dev/null +++ b/dist/utilities/buildASTSchema.mjs @@ -0,0 +1,454 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import keyMap from '../jsutils/keyMap'; +import keyValMap from '../jsutils/keyValMap'; +import { valueFromAST } from './valueFromAST'; +import blockStringValue from '../language/blockStringValue'; +import { TokenKind } from '../language/lexer'; +import { parse } from '../language/parser'; +import { getDirectiveValues } from '../execution/values'; +import { Kind } from '../language/kinds'; +import { assertNullableType, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull } from '../type/definition'; +import { GraphQLDirective, GraphQLSkipDirective, GraphQLIncludeDirective, GraphQLDeprecatedDirective } from '../type/directives'; +import { introspectionTypes } from '../type/introspection'; +import { specifiedScalarTypes } from '../type/scalars'; +import { GraphQLSchema } from '../type/schema'; + +function buildWrappedType(innerType, inputTypeNode) { + if (inputTypeNode.kind === Kind.LIST_TYPE) { + return GraphQLList(buildWrappedType(innerType, inputTypeNode.type)); + } + + if (inputTypeNode.kind === Kind.NON_NULL_TYPE) { + var wrappedType = buildWrappedType(innerType, inputTypeNode.type); + return GraphQLNonNull(assertNullableType(wrappedType)); + } + + return innerType; +} + +function getNamedTypeNode(typeNode) { + var namedType = typeNode; + + while (namedType.kind === Kind.LIST_TYPE || namedType.kind === Kind.NON_NULL_TYPE) { + namedType = namedType.type; + } + + return namedType; +} +/** + * This takes the ast of a schema document produced by the parse function in + * src/language/parser.js. + * + * If no schema definition is provided, then it will look for types named Query + * and Mutation. + * + * Given that AST it constructs a GraphQLSchema. The resulting schema + * has no resolve methods, so execution will use default resolvers. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ + + +export function buildASTSchema(ast, options) { + if (!ast || ast.kind !== Kind.DOCUMENT) { + throw new Error('Must provide a document ast.'); + } + + var schemaDef; + var typeDefs = []; + var nodeMap = Object.create(null); + var directiveDefs = []; + + for (var i = 0; i < ast.definitions.length; i++) { + var d = ast.definitions[i]; + + switch (d.kind) { + case Kind.SCHEMA_DEFINITION: + if (schemaDef) { + throw new Error('Must provide only one schema definition.'); + } + + schemaDef = d; + break; + + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.ENUM_TYPE_DEFINITION: + case Kind.UNION_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + var typeName = d.name.value; + + if (nodeMap[typeName]) { + throw new Error("Type \"".concat(typeName, "\" was defined more than once.")); + } + + typeDefs.push(d); + nodeMap[typeName] = d; + break; + + case Kind.DIRECTIVE_DEFINITION: + directiveDefs.push(d); + break; + } + } + + var operationTypes = schemaDef ? getOperationTypes(schemaDef) : { + query: nodeMap.Query, + mutation: nodeMap.Mutation, + subscription: nodeMap.Subscription + }; + var definitionBuilder = new ASTDefinitionBuilder(nodeMap, options, function (typeRef) { + throw new Error("Type \"".concat(typeRef.name.value, "\" not found in document.")); + }); + var types = definitionBuilder.buildTypes(typeDefs); + var directives = directiveDefs.map(function (def) { + return definitionBuilder.buildDirective(def); + }); // If specified directives were not explicitly declared, add them. + + if (!directives.some(function (directive) { + return directive.name === 'skip'; + })) { + directives.push(GraphQLSkipDirective); + } + + if (!directives.some(function (directive) { + return directive.name === 'include'; + })) { + directives.push(GraphQLIncludeDirective); + } + + if (!directives.some(function (directive) { + return directive.name === 'deprecated'; + })) { + directives.push(GraphQLDeprecatedDirective); + } // Note: While this could make early assertions to get the correctly + // typed values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + + + return new GraphQLSchema({ + query: operationTypes.query ? definitionBuilder.buildType(operationTypes.query) : null, + mutation: operationTypes.mutation ? definitionBuilder.buildType(operationTypes.mutation) : null, + subscription: operationTypes.subscription ? definitionBuilder.buildType(operationTypes.subscription) : null, + types: types, + directives: directives, + astNode: schemaDef, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames + }); + + function getOperationTypes(schema) { + var opTypes = {}; + schema.operationTypes.forEach(function (operationType) { + var typeName = operationType.type.name.value; + var operation = operationType.operation; + + if (opTypes[operation]) { + throw new Error("Must provide only one ".concat(operation, " type in schema.")); + } + + if (!nodeMap[typeName]) { + throw new Error("Specified ".concat(operation, " type \"").concat(typeName, "\" not found in document.")); + } + + opTypes[operation] = operationType.type; + }); + return opTypes; + } +} +export var ASTDefinitionBuilder = +/*#__PURE__*/ +function () { + function ASTDefinitionBuilder(typeDefinitionsMap, options, resolveType) { + _defineProperty(this, "_typeDefinitionsMap", void 0); + + _defineProperty(this, "_options", void 0); + + _defineProperty(this, "_resolveType", void 0); + + _defineProperty(this, "_cache", void 0); + + this._typeDefinitionsMap = typeDefinitionsMap; + this._options = options; + this._resolveType = resolveType; // Initialize to the GraphQL built in scalars and introspection types. + + this._cache = keyMap(specifiedScalarTypes.concat(introspectionTypes), function (type) { + return type.name; + }); + } + + var _proto = ASTDefinitionBuilder.prototype; + + _proto.buildTypes = function buildTypes(nodes) { + var _this = this; + + return nodes.map(function (node) { + return _this.buildType(node); + }); + }; + + _proto.buildType = function buildType(node) { + var typeName = node.name.value; + + if (!this._cache[typeName]) { + if (node.kind === Kind.NAMED_TYPE) { + var defNode = this._typeDefinitionsMap[typeName]; + this._cache[typeName] = defNode ? this._makeSchemaDef(defNode) : this._resolveType(node); + } else { + this._cache[typeName] = this._makeSchemaDef(node); + } + } + + return this._cache[typeName]; + }; + + _proto._buildWrappedType = function _buildWrappedType(typeNode) { + var typeDef = this.buildType(getNamedTypeNode(typeNode)); + return buildWrappedType(typeDef, typeNode); + }; + + _proto.buildDirective = function buildDirective(directiveNode) { + return new GraphQLDirective({ + name: directiveNode.name.value, + description: getDescription(directiveNode, this._options), + locations: directiveNode.locations.map(function (node) { + return node.value; + }), + args: directiveNode.arguments && this._makeInputValues(directiveNode.arguments), + astNode: directiveNode + }); + }; + + _proto.buildField = function buildField(field) { + return { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + // with validateSchema() will produce more actionable results. + type: this._buildWrappedType(field.type), + description: getDescription(field, this._options), + args: field.arguments && this._makeInputValues(field.arguments), + deprecationReason: getDeprecationReason(field), + astNode: field + }; + }; + + _proto._makeSchemaDef = function _makeSchemaDef(def) { + switch (def.kind) { + case Kind.OBJECT_TYPE_DEFINITION: + return this._makeTypeDef(def); + + case Kind.INTERFACE_TYPE_DEFINITION: + return this._makeInterfaceDef(def); + + case Kind.ENUM_TYPE_DEFINITION: + return this._makeEnumDef(def); + + case Kind.UNION_TYPE_DEFINITION: + return this._makeUnionDef(def); + + case Kind.SCALAR_TYPE_DEFINITION: + return this._makeScalarDef(def); + + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + return this._makeInputObjectDef(def); + + default: + throw new Error("Type kind \"".concat(def.kind, "\" not supported.")); + } + }; + + _proto._makeTypeDef = function _makeTypeDef(def) { + var _this2 = this; + + var typeName = def.name.value; + var interfaces = def.interfaces; + return new GraphQLObjectType({ + name: typeName, + description: getDescription(def, this._options), + fields: function fields() { + return _this2._makeFieldDefMap(def); + }, + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces: interfaces ? function () { + return _this2.buildTypes(interfaces); + } : [], + astNode: def + }); + }; + + _proto._makeFieldDefMap = function _makeFieldDefMap(def) { + var _this3 = this; + + return def.fields ? keyValMap(def.fields, function (field) { + return field.name.value; + }, function (field) { + return _this3.buildField(field); + }) : {}; + }; + + _proto._makeInputValues = function _makeInputValues(values) { + var _this4 = this; + + return keyValMap(values, function (value) { + return value.name.value; + }, function (value) { + // Note: While this could make assertions to get the correctly typed + // value, that would throw immediately while type system validation + var type = _this4._buildWrappedType(value.type); + + return { + type: type, + description: getDescription(value, _this4._options), + defaultValue: valueFromAST(value.defaultValue, type), + astNode: value + }; + }); + }; + + _proto._makeInterfaceDef = function _makeInterfaceDef(def) { + var _this5 = this; + + return new GraphQLInterfaceType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: function fields() { + return _this5._makeFieldDefMap(def); + }, + astNode: def + }); + }; + + _proto._makeEnumDef = function _makeEnumDef(def) { + var _this6 = this; + + return new GraphQLEnumType({ + name: def.name.value, + description: getDescription(def, this._options), + values: def.values ? keyValMap(def.values, function (enumValue) { + return enumValue.name.value; + }, function (enumValue) { + return { + description: getDescription(enumValue, _this6._options), + deprecationReason: getDeprecationReason(enumValue), + astNode: enumValue + }; + }) : {}, + astNode: def + }); + }; + + _proto._makeUnionDef = function _makeUnionDef(def) { + return new GraphQLUnionType({ + name: def.name.value, + description: getDescription(def, this._options), + // Note: While this could make assertions to get the correctly typed + // values below, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + types: def.types ? this.buildTypes(def.types) : [], + astNode: def + }); + }; + + _proto._makeScalarDef = function _makeScalarDef(def) { + return new GraphQLScalarType({ + name: def.name.value, + description: getDescription(def, this._options), + astNode: def, + serialize: function serialize(value) { + return value; + } + }); + }; + + _proto._makeInputObjectDef = function _makeInputObjectDef(def) { + var _this7 = this; + + return new GraphQLInputObjectType({ + name: def.name.value, + description: getDescription(def, this._options), + fields: function fields() { + return def.fields ? _this7._makeInputValues(def.fields) : {}; + }, + astNode: def + }); + }; + + return ASTDefinitionBuilder; +}(); +/** + * Given a field or enum value node, returns the string value for the + * deprecation reason. + */ + +function getDeprecationReason(node) { + var deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node); + return deprecated && deprecated.reason; +} +/** + * Given an ast node, returns its string description. + * + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ + + +export function getDescription(node, options) { + if (node.description) { + return node.description.value; + } + + if (options && options.commentDescriptions) { + var rawValue = getLeadingCommentBlock(node); + + if (rawValue !== undefined) { + return blockStringValue('\n' + rawValue); + } + } +} + +function getLeadingCommentBlock(node) { + var loc = node.loc; + + if (!loc) { + return; + } + + var comments = []; + var token = loc.startToken.prev; + + while (token && token.kind === TokenKind.COMMENT && token.next && token.prev && token.line + 1 === token.next.line && token.line !== token.prev.line) { + var value = String(token.value); + comments.push(value); + token = token.prev; + } + + return comments.reverse().join('\n'); +} +/** + * A helper function to build a GraphQLSchema directly from a source + * document. + */ + + +export function buildSchema(source, options) { + return buildASTSchema(parse(source, options), options); +} \ No newline at end of file diff --git a/dist/utilities/buildClientSchema.js b/dist/utilities/buildClientSchema.js new file mode 100644 index 0000000000..350900ba23 --- /dev/null +++ b/dist/utilities/buildClientSchema.js @@ -0,0 +1,320 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.buildClientSchema = buildClientSchema; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap")); + +var _valueFromAST = require("./valueFromAST"); + +var _parser = require("../language/parser"); + +var _schema = require("../type/schema"); + +var _directiveLocation = require("../language/directiveLocation"); + +var _definition = require("../type/definition"); + +var _directives = require("../type/directives"); + +var _introspection = require("../type/introspection"); + +var _scalars = require("../type/scalars"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Build a GraphQLSchema for use by client tools. + * + * Given the result of a client running the introspection query, creates and + * returns a GraphQLSchema instance which can be then used with all graphql-js + * tools, but cannot be used to execute a query, as introspection does not + * represent the "resolver", "parse" or "serialize" functions or any other + * server-internal mechanisms. + * + * This function expects a complete introspection result. Don't forget to check + * the "errors" field of a server response before calling this function. + */ +function buildClientSchema(introspection, options) { + // Get the schema from the introspection result. + var schemaIntrospection = introspection.__schema; // Converts the list of types into a keyMap based on the type names. + + var typeIntrospectionMap = (0, _keyMap.default)(schemaIntrospection.types, function (type) { + return type.name; + }); // A cache to use to store the actual GraphQLType definition objects by name. + // Initialize to the GraphQL built in scalars. All functions below are inline + // so that this type def cache is within the scope of the closure. + + var typeDefCache = (0, _keyMap.default)(_scalars.specifiedScalarTypes.concat(_introspection.introspectionTypes), function (type) { + return type.name; + }); // Given a type reference in introspection, return the GraphQLType instance. + // preferring cached instances before building new instances. + + function getType(typeRef) { + if (typeRef.kind === _introspection.TypeKind.LIST) { + var itemRef = typeRef.ofType; + + if (!itemRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + + return (0, _definition.GraphQLList)(getType(itemRef)); + } + + if (typeRef.kind === _introspection.TypeKind.NON_NULL) { + var nullableRef = typeRef.ofType; + + if (!nullableRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + + var nullableType = getType(nullableRef); + return (0, _definition.GraphQLNonNull)((0, _definition.assertNullableType)(nullableType)); + } + + if (!typeRef.name) { + throw new Error('Unknown type reference: ' + JSON.stringify(typeRef)); + } + + return getNamedType(typeRef.name); + } + + function getNamedType(typeName) { + if (typeDefCache[typeName]) { + return typeDefCache[typeName]; + } + + var typeIntrospection = typeIntrospectionMap[typeName]; + + if (!typeIntrospection) { + throw new Error("Invalid or incomplete schema, unknown type: ".concat(typeName, ". Ensure ") + 'that a full introspection query is used in order to build a ' + 'client schema.'); + } + + var typeDef = buildType(typeIntrospection); + typeDefCache[typeName] = typeDef; + return typeDef; + } + + function getInputType(typeRef) { + var type = getType(typeRef); + !(0, _definition.isInputType)(type) ? (0, _invariant.default)(0, 'Introspection must provide input type for arguments.') : void 0; + return type; + } + + function getOutputType(typeRef) { + var type = getType(typeRef); + !(0, _definition.isOutputType)(type) ? (0, _invariant.default)(0, 'Introspection must provide output type for fields.') : void 0; + return type; + } + + function getObjectType(typeRef) { + var type = getType(typeRef); + return (0, _definition.assertObjectType)(type); + } + + function getInterfaceType(typeRef) { + var type = getType(typeRef); + return (0, _definition.assertInterfaceType)(type); + } // Given a type's introspection result, construct the correct + // GraphQLType instance. + + + function buildType(type) { + if (type && type.name && type.kind) { + switch (type.kind) { + case _introspection.TypeKind.SCALAR: + return buildScalarDef(type); + + case _introspection.TypeKind.OBJECT: + return buildObjectDef(type); + + case _introspection.TypeKind.INTERFACE: + return buildInterfaceDef(type); + + case _introspection.TypeKind.UNION: + return buildUnionDef(type); + + case _introspection.TypeKind.ENUM: + return buildEnumDef(type); + + case _introspection.TypeKind.INPUT_OBJECT: + return buildInputObjectDef(type); + } + } + + throw new Error('Invalid or incomplete introspection result. Ensure that a full ' + 'introspection query is used in order to build a client schema:' + JSON.stringify(type)); + } + + function buildScalarDef(scalarIntrospection) { + return new _definition.GraphQLScalarType({ + name: scalarIntrospection.name, + description: scalarIntrospection.description, + serialize: function serialize(value) { + return value; + } + }); + } + + function buildObjectDef(objectIntrospection) { + if (!objectIntrospection.interfaces) { + throw new Error('Introspection result missing interfaces: ' + JSON.stringify(objectIntrospection)); + } + + return new _definition.GraphQLObjectType({ + name: objectIntrospection.name, + description: objectIntrospection.description, + interfaces: objectIntrospection.interfaces.map(getInterfaceType), + fields: function fields() { + return buildFieldDefMap(objectIntrospection); + } + }); + } + + function buildInterfaceDef(interfaceIntrospection) { + return new _definition.GraphQLInterfaceType({ + name: interfaceIntrospection.name, + description: interfaceIntrospection.description, + fields: function fields() { + return buildFieldDefMap(interfaceIntrospection); + } + }); + } + + function buildUnionDef(unionIntrospection) { + if (!unionIntrospection.possibleTypes) { + throw new Error('Introspection result missing possibleTypes: ' + JSON.stringify(unionIntrospection)); + } + + return new _definition.GraphQLUnionType({ + name: unionIntrospection.name, + description: unionIntrospection.description, + types: unionIntrospection.possibleTypes.map(getObjectType) + }); + } + + function buildEnumDef(enumIntrospection) { + if (!enumIntrospection.enumValues) { + throw new Error('Introspection result missing enumValues: ' + JSON.stringify(enumIntrospection)); + } + + return new _definition.GraphQLEnumType({ + name: enumIntrospection.name, + description: enumIntrospection.description, + values: (0, _keyValMap.default)(enumIntrospection.enumValues, function (valueIntrospection) { + return valueIntrospection.name; + }, function (valueIntrospection) { + return { + description: valueIntrospection.description, + deprecationReason: valueIntrospection.deprecationReason + }; + }) + }); + } + + function buildInputObjectDef(inputObjectIntrospection) { + if (!inputObjectIntrospection.inputFields) { + throw new Error('Introspection result missing inputFields: ' + JSON.stringify(inputObjectIntrospection)); + } + + return new _definition.GraphQLInputObjectType({ + name: inputObjectIntrospection.name, + description: inputObjectIntrospection.description, + fields: function fields() { + return buildInputValueDefMap(inputObjectIntrospection.inputFields); + } + }); + } + + function buildFieldDefMap(typeIntrospection) { + if (!typeIntrospection.fields) { + throw new Error('Introspection result missing fields: ' + JSON.stringify(typeIntrospection)); + } + + return (0, _keyValMap.default)(typeIntrospection.fields, function (fieldIntrospection) { + return fieldIntrospection.name; + }, function (fieldIntrospection) { + if (!fieldIntrospection.args) { + throw new Error('Introspection result missing field args: ' + JSON.stringify(fieldIntrospection)); + } + + return { + description: fieldIntrospection.description, + deprecationReason: fieldIntrospection.deprecationReason, + type: getOutputType(fieldIntrospection.type), + args: buildInputValueDefMap(fieldIntrospection.args) + }; + }); + } + + function buildInputValueDefMap(inputValueIntrospections) { + return (0, _keyValMap.default)(inputValueIntrospections, function (inputValue) { + return inputValue.name; + }, buildInputValue); + } + + function buildInputValue(inputValueIntrospection) { + var type = getInputType(inputValueIntrospection.type); + var defaultValue = inputValueIntrospection.defaultValue ? (0, _valueFromAST.valueFromAST)((0, _parser.parseValue)(inputValueIntrospection.defaultValue), type) : undefined; + return { + name: inputValueIntrospection.name, + description: inputValueIntrospection.description, + type: type, + defaultValue: defaultValue + }; + } + + function buildDirective(directiveIntrospection) { + // Support deprecated `on****` fields for building `locations`, as this + // is used by GraphiQL which may need to support outdated servers. + var locations = directiveIntrospection.locations ? directiveIntrospection.locations.slice() : [].concat(!directiveIntrospection.onField ? [] : [_directiveLocation.DirectiveLocation.FIELD], !directiveIntrospection.onOperation ? [] : [_directiveLocation.DirectiveLocation.QUERY, _directiveLocation.DirectiveLocation.MUTATION, _directiveLocation.DirectiveLocation.SUBSCRIPTION], !directiveIntrospection.onFragment ? [] : [_directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION, _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD, _directiveLocation.DirectiveLocation.INLINE_FRAGMENT]); + + if (!directiveIntrospection.args) { + throw new Error('Introspection result missing directive args: ' + JSON.stringify(directiveIntrospection)); + } + + return new _directives.GraphQLDirective({ + name: directiveIntrospection.name, + description: directiveIntrospection.description, + locations: locations, + args: buildInputValueDefMap(directiveIntrospection.args) + }); + } // Iterate through all types, getting the type definition for each, ensuring + // that any type not directly referenced by a field will get created. + + + var types = schemaIntrospection.types.map(function (typeIntrospection) { + return getNamedType(typeIntrospection.name); + }); // Get the root Query, Mutation, and Subscription types. + + var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null; + var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null; + var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null; // Get the directives supported by Introspection, assuming empty-set if + // directives were not queried for. + + var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; // Then produce and return a Schema with these types. + + return new _schema.GraphQLSchema({ + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + types: types, + directives: directives, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames + }); +} \ No newline at end of file diff --git a/dist/utilities/buildClientSchema.js.flow b/dist/utilities/buildClientSchema.js.flow new file mode 100644 index 0000000000..b86b9bff40 --- /dev/null +++ b/dist/utilities/buildClientSchema.js.flow @@ -0,0 +1,414 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import keyValMap from '../jsutils/keyValMap'; +import { valueFromAST } from './valueFromAST'; +import { parseValue } from '../language/parser'; +import { GraphQLSchema } from '../type/schema'; + +import { DirectiveLocation } from '../language/directiveLocation'; + +import { + isInputType, + isOutputType, + GraphQLScalarType, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + assertNullableType, + assertObjectType, + assertInterfaceType, +} from '../type/definition'; + +import type { + GraphQLType, + GraphQLInputType, + GraphQLOutputType, + GraphQLNamedType, +} from '../type/definition'; + +import { GraphQLDirective } from '../type/directives'; + +import { introspectionTypes, TypeKind } from '../type/introspection'; + +import { specifiedScalarTypes } from '../type/scalars'; + +import type { + IntrospectionQuery, + IntrospectionType, + IntrospectionScalarType, + IntrospectionObjectType, + IntrospectionInterfaceType, + IntrospectionUnionType, + IntrospectionEnumType, + IntrospectionInputObjectType, + IntrospectionTypeRef, + IntrospectionInputTypeRef, + IntrospectionOutputTypeRef, + IntrospectionNamedTypeRef, +} from './introspectionQuery'; + +import type { GraphQLSchemaValidationOptions } from '../type/schema'; + +type Options = {| + ...GraphQLSchemaValidationOptions, +|}; + +/** + * Build a GraphQLSchema for use by client tools. + * + * Given the result of a client running the introspection query, creates and + * returns a GraphQLSchema instance which can be then used with all graphql-js + * tools, but cannot be used to execute a query, as introspection does not + * represent the "resolver", "parse" or "serialize" functions or any other + * server-internal mechanisms. + * + * This function expects a complete introspection result. Don't forget to check + * the "errors" field of a server response before calling this function. + */ +export function buildClientSchema( + introspection: IntrospectionQuery, + options?: Options, +): GraphQLSchema { + // Get the schema from the introspection result. + const schemaIntrospection = introspection.__schema; + + // Converts the list of types into a keyMap based on the type names. + const typeIntrospectionMap = keyMap( + schemaIntrospection.types, + type => type.name, + ); + + // A cache to use to store the actual GraphQLType definition objects by name. + // Initialize to the GraphQL built in scalars. All functions below are inline + // so that this type def cache is within the scope of the closure. + const typeDefCache = keyMap( + specifiedScalarTypes.concat(introspectionTypes), + type => type.name, + ); + + // Given a type reference in introspection, return the GraphQLType instance. + // preferring cached instances before building new instances. + function getType(typeRef: IntrospectionTypeRef): GraphQLType { + if (typeRef.kind === TypeKind.LIST) { + const itemRef = typeRef.ofType; + if (!itemRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + return GraphQLList(getType(itemRef)); + } + if (typeRef.kind === TypeKind.NON_NULL) { + const nullableRef = typeRef.ofType; + if (!nullableRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + const nullableType = getType(nullableRef); + return GraphQLNonNull(assertNullableType(nullableType)); + } + if (!typeRef.name) { + throw new Error('Unknown type reference: ' + JSON.stringify(typeRef)); + } + return getNamedType(typeRef.name); + } + + function getNamedType(typeName: string): GraphQLNamedType { + if (typeDefCache[typeName]) { + return typeDefCache[typeName]; + } + const typeIntrospection = typeIntrospectionMap[typeName]; + if (!typeIntrospection) { + throw new Error( + `Invalid or incomplete schema, unknown type: ${typeName}. Ensure ` + + 'that a full introspection query is used in order to build a ' + + 'client schema.', + ); + } + const typeDef = buildType(typeIntrospection); + typeDefCache[typeName] = typeDef; + return typeDef; + } + + function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType { + const type = getType(typeRef); + invariant( + isInputType(type), + 'Introspection must provide input type for arguments.', + ); + return type; + } + + function getOutputType( + typeRef: IntrospectionOutputTypeRef, + ): GraphQLOutputType { + const type = getType(typeRef); + invariant( + isOutputType(type), + 'Introspection must provide output type for fields.', + ); + return type; + } + + function getObjectType( + typeRef: IntrospectionNamedTypeRef, + ): GraphQLObjectType { + const type = getType(typeRef); + return assertObjectType(type); + } + + function getInterfaceType( + typeRef: IntrospectionTypeRef, + ): GraphQLInterfaceType { + const type = getType(typeRef); + return assertInterfaceType(type); + } + + // Given a type's introspection result, construct the correct + // GraphQLType instance. + function buildType(type: IntrospectionType): GraphQLNamedType { + if (type && type.name && type.kind) { + switch (type.kind) { + case TypeKind.SCALAR: + return buildScalarDef(type); + case TypeKind.OBJECT: + return buildObjectDef(type); + case TypeKind.INTERFACE: + return buildInterfaceDef(type); + case TypeKind.UNION: + return buildUnionDef(type); + case TypeKind.ENUM: + return buildEnumDef(type); + case TypeKind.INPUT_OBJECT: + return buildInputObjectDef(type); + } + } + throw new Error( + 'Invalid or incomplete introspection result. Ensure that a full ' + + 'introspection query is used in order to build a client schema:' + + JSON.stringify(type), + ); + } + + function buildScalarDef( + scalarIntrospection: IntrospectionScalarType, + ): GraphQLScalarType { + return new GraphQLScalarType({ + name: scalarIntrospection.name, + description: scalarIntrospection.description, + serialize: value => value, + }); + } + + function buildObjectDef( + objectIntrospection: IntrospectionObjectType, + ): GraphQLObjectType { + if (!objectIntrospection.interfaces) { + throw new Error( + 'Introspection result missing interfaces: ' + + JSON.stringify(objectIntrospection), + ); + } + return new GraphQLObjectType({ + name: objectIntrospection.name, + description: objectIntrospection.description, + interfaces: objectIntrospection.interfaces.map(getInterfaceType), + fields: () => buildFieldDefMap(objectIntrospection), + }); + } + + function buildInterfaceDef( + interfaceIntrospection: IntrospectionInterfaceType, + ): GraphQLInterfaceType { + return new GraphQLInterfaceType({ + name: interfaceIntrospection.name, + description: interfaceIntrospection.description, + fields: () => buildFieldDefMap(interfaceIntrospection), + }); + } + + function buildUnionDef( + unionIntrospection: IntrospectionUnionType, + ): GraphQLUnionType { + if (!unionIntrospection.possibleTypes) { + throw new Error( + 'Introspection result missing possibleTypes: ' + + JSON.stringify(unionIntrospection), + ); + } + return new GraphQLUnionType({ + name: unionIntrospection.name, + description: unionIntrospection.description, + types: unionIntrospection.possibleTypes.map(getObjectType), + }); + } + + function buildEnumDef( + enumIntrospection: IntrospectionEnumType, + ): GraphQLEnumType { + if (!enumIntrospection.enumValues) { + throw new Error( + 'Introspection result missing enumValues: ' + + JSON.stringify(enumIntrospection), + ); + } + return new GraphQLEnumType({ + name: enumIntrospection.name, + description: enumIntrospection.description, + values: keyValMap( + enumIntrospection.enumValues, + valueIntrospection => valueIntrospection.name, + valueIntrospection => ({ + description: valueIntrospection.description, + deprecationReason: valueIntrospection.deprecationReason, + }), + ), + }); + } + + function buildInputObjectDef( + inputObjectIntrospection: IntrospectionInputObjectType, + ): GraphQLInputObjectType { + if (!inputObjectIntrospection.inputFields) { + throw new Error( + 'Introspection result missing inputFields: ' + + JSON.stringify(inputObjectIntrospection), + ); + } + return new GraphQLInputObjectType({ + name: inputObjectIntrospection.name, + description: inputObjectIntrospection.description, + fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields), + }); + } + + function buildFieldDefMap(typeIntrospection) { + if (!typeIntrospection.fields) { + throw new Error( + 'Introspection result missing fields: ' + + JSON.stringify(typeIntrospection), + ); + } + return keyValMap( + typeIntrospection.fields, + fieldIntrospection => fieldIntrospection.name, + fieldIntrospection => { + if (!fieldIntrospection.args) { + throw new Error( + 'Introspection result missing field args: ' + + JSON.stringify(fieldIntrospection), + ); + } + return { + description: fieldIntrospection.description, + deprecationReason: fieldIntrospection.deprecationReason, + type: getOutputType(fieldIntrospection.type), + args: buildInputValueDefMap(fieldIntrospection.args), + }; + }, + ); + } + + function buildInputValueDefMap(inputValueIntrospections) { + return keyValMap( + inputValueIntrospections, + inputValue => inputValue.name, + buildInputValue, + ); + } + + function buildInputValue(inputValueIntrospection) { + const type = getInputType(inputValueIntrospection.type); + const defaultValue = inputValueIntrospection.defaultValue + ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) + : undefined; + return { + name: inputValueIntrospection.name, + description: inputValueIntrospection.description, + type, + defaultValue, + }; + } + + function buildDirective(directiveIntrospection) { + // Support deprecated `on****` fields for building `locations`, as this + // is used by GraphiQL which may need to support outdated servers. + const locations = directiveIntrospection.locations + ? directiveIntrospection.locations.slice() + : [].concat( + !directiveIntrospection.onField ? [] : [DirectiveLocation.FIELD], + !directiveIntrospection.onOperation + ? [] + : [ + DirectiveLocation.QUERY, + DirectiveLocation.MUTATION, + DirectiveLocation.SUBSCRIPTION, + ], + !directiveIntrospection.onFragment + ? [] + : [ + DirectiveLocation.FRAGMENT_DEFINITION, + DirectiveLocation.FRAGMENT_SPREAD, + DirectiveLocation.INLINE_FRAGMENT, + ], + ); + if (!directiveIntrospection.args) { + throw new Error( + 'Introspection result missing directive args: ' + + JSON.stringify(directiveIntrospection), + ); + } + return new GraphQLDirective({ + name: directiveIntrospection.name, + description: directiveIntrospection.description, + locations, + args: buildInputValueDefMap(directiveIntrospection.args), + }); + } + + // Iterate through all types, getting the type definition for each, ensuring + // that any type not directly referenced by a field will get created. + const types = schemaIntrospection.types.map(typeIntrospection => + getNamedType(typeIntrospection.name), + ); + + // Get the root Query, Mutation, and Subscription types. + const queryType = schemaIntrospection.queryType + ? getObjectType(schemaIntrospection.queryType) + : null; + + const mutationType = schemaIntrospection.mutationType + ? getObjectType(schemaIntrospection.mutationType) + : null; + + const subscriptionType = schemaIntrospection.subscriptionType + ? getObjectType(schemaIntrospection.subscriptionType) + : null; + + // Get the directives supported by Introspection, assuming empty-set if + // directives were not queried for. + const directives = schemaIntrospection.directives + ? schemaIntrospection.directives.map(buildDirective) + : []; + + // Then produce and return a Schema with these types. + return new GraphQLSchema({ + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + types, + directives, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames, + }); +} diff --git a/dist/utilities/buildClientSchema.mjs b/dist/utilities/buildClientSchema.mjs new file mode 100644 index 0000000000..06b2cee975 --- /dev/null +++ b/dist/utilities/buildClientSchema.mjs @@ -0,0 +1,300 @@ +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import keyValMap from '../jsutils/keyValMap'; +import { valueFromAST } from './valueFromAST'; +import { parseValue } from '../language/parser'; +import { GraphQLSchema } from '../type/schema'; +import { DirectiveLocation } from '../language/directiveLocation'; +import { isInputType, isOutputType, GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, assertNullableType, assertObjectType, assertInterfaceType } from '../type/definition'; +import { GraphQLDirective } from '../type/directives'; +import { introspectionTypes, TypeKind } from '../type/introspection'; +import { specifiedScalarTypes } from '../type/scalars'; + +/** + * Build a GraphQLSchema for use by client tools. + * + * Given the result of a client running the introspection query, creates and + * returns a GraphQLSchema instance which can be then used with all graphql-js + * tools, but cannot be used to execute a query, as introspection does not + * represent the "resolver", "parse" or "serialize" functions or any other + * server-internal mechanisms. + * + * This function expects a complete introspection result. Don't forget to check + * the "errors" field of a server response before calling this function. + */ +export function buildClientSchema(introspection, options) { + // Get the schema from the introspection result. + var schemaIntrospection = introspection.__schema; // Converts the list of types into a keyMap based on the type names. + + var typeIntrospectionMap = keyMap(schemaIntrospection.types, function (type) { + return type.name; + }); // A cache to use to store the actual GraphQLType definition objects by name. + // Initialize to the GraphQL built in scalars. All functions below are inline + // so that this type def cache is within the scope of the closure. + + var typeDefCache = keyMap(specifiedScalarTypes.concat(introspectionTypes), function (type) { + return type.name; + }); // Given a type reference in introspection, return the GraphQLType instance. + // preferring cached instances before building new instances. + + function getType(typeRef) { + if (typeRef.kind === TypeKind.LIST) { + var itemRef = typeRef.ofType; + + if (!itemRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + + return GraphQLList(getType(itemRef)); + } + + if (typeRef.kind === TypeKind.NON_NULL) { + var nullableRef = typeRef.ofType; + + if (!nullableRef) { + throw new Error('Decorated type deeper than introspection query.'); + } + + var nullableType = getType(nullableRef); + return GraphQLNonNull(assertNullableType(nullableType)); + } + + if (!typeRef.name) { + throw new Error('Unknown type reference: ' + JSON.stringify(typeRef)); + } + + return getNamedType(typeRef.name); + } + + function getNamedType(typeName) { + if (typeDefCache[typeName]) { + return typeDefCache[typeName]; + } + + var typeIntrospection = typeIntrospectionMap[typeName]; + + if (!typeIntrospection) { + throw new Error("Invalid or incomplete schema, unknown type: ".concat(typeName, ". Ensure ") + 'that a full introspection query is used in order to build a ' + 'client schema.'); + } + + var typeDef = buildType(typeIntrospection); + typeDefCache[typeName] = typeDef; + return typeDef; + } + + function getInputType(typeRef) { + var type = getType(typeRef); + !isInputType(type) ? invariant(0, 'Introspection must provide input type for arguments.') : void 0; + return type; + } + + function getOutputType(typeRef) { + var type = getType(typeRef); + !isOutputType(type) ? invariant(0, 'Introspection must provide output type for fields.') : void 0; + return type; + } + + function getObjectType(typeRef) { + var type = getType(typeRef); + return assertObjectType(type); + } + + function getInterfaceType(typeRef) { + var type = getType(typeRef); + return assertInterfaceType(type); + } // Given a type's introspection result, construct the correct + // GraphQLType instance. + + + function buildType(type) { + if (type && type.name && type.kind) { + switch (type.kind) { + case TypeKind.SCALAR: + return buildScalarDef(type); + + case TypeKind.OBJECT: + return buildObjectDef(type); + + case TypeKind.INTERFACE: + return buildInterfaceDef(type); + + case TypeKind.UNION: + return buildUnionDef(type); + + case TypeKind.ENUM: + return buildEnumDef(type); + + case TypeKind.INPUT_OBJECT: + return buildInputObjectDef(type); + } + } + + throw new Error('Invalid or incomplete introspection result. Ensure that a full ' + 'introspection query is used in order to build a client schema:' + JSON.stringify(type)); + } + + function buildScalarDef(scalarIntrospection) { + return new GraphQLScalarType({ + name: scalarIntrospection.name, + description: scalarIntrospection.description, + serialize: function serialize(value) { + return value; + } + }); + } + + function buildObjectDef(objectIntrospection) { + if (!objectIntrospection.interfaces) { + throw new Error('Introspection result missing interfaces: ' + JSON.stringify(objectIntrospection)); + } + + return new GraphQLObjectType({ + name: objectIntrospection.name, + description: objectIntrospection.description, + interfaces: objectIntrospection.interfaces.map(getInterfaceType), + fields: function fields() { + return buildFieldDefMap(objectIntrospection); + } + }); + } + + function buildInterfaceDef(interfaceIntrospection) { + return new GraphQLInterfaceType({ + name: interfaceIntrospection.name, + description: interfaceIntrospection.description, + fields: function fields() { + return buildFieldDefMap(interfaceIntrospection); + } + }); + } + + function buildUnionDef(unionIntrospection) { + if (!unionIntrospection.possibleTypes) { + throw new Error('Introspection result missing possibleTypes: ' + JSON.stringify(unionIntrospection)); + } + + return new GraphQLUnionType({ + name: unionIntrospection.name, + description: unionIntrospection.description, + types: unionIntrospection.possibleTypes.map(getObjectType) + }); + } + + function buildEnumDef(enumIntrospection) { + if (!enumIntrospection.enumValues) { + throw new Error('Introspection result missing enumValues: ' + JSON.stringify(enumIntrospection)); + } + + return new GraphQLEnumType({ + name: enumIntrospection.name, + description: enumIntrospection.description, + values: keyValMap(enumIntrospection.enumValues, function (valueIntrospection) { + return valueIntrospection.name; + }, function (valueIntrospection) { + return { + description: valueIntrospection.description, + deprecationReason: valueIntrospection.deprecationReason + }; + }) + }); + } + + function buildInputObjectDef(inputObjectIntrospection) { + if (!inputObjectIntrospection.inputFields) { + throw new Error('Introspection result missing inputFields: ' + JSON.stringify(inputObjectIntrospection)); + } + + return new GraphQLInputObjectType({ + name: inputObjectIntrospection.name, + description: inputObjectIntrospection.description, + fields: function fields() { + return buildInputValueDefMap(inputObjectIntrospection.inputFields); + } + }); + } + + function buildFieldDefMap(typeIntrospection) { + if (!typeIntrospection.fields) { + throw new Error('Introspection result missing fields: ' + JSON.stringify(typeIntrospection)); + } + + return keyValMap(typeIntrospection.fields, function (fieldIntrospection) { + return fieldIntrospection.name; + }, function (fieldIntrospection) { + if (!fieldIntrospection.args) { + throw new Error('Introspection result missing field args: ' + JSON.stringify(fieldIntrospection)); + } + + return { + description: fieldIntrospection.description, + deprecationReason: fieldIntrospection.deprecationReason, + type: getOutputType(fieldIntrospection.type), + args: buildInputValueDefMap(fieldIntrospection.args) + }; + }); + } + + function buildInputValueDefMap(inputValueIntrospections) { + return keyValMap(inputValueIntrospections, function (inputValue) { + return inputValue.name; + }, buildInputValue); + } + + function buildInputValue(inputValueIntrospection) { + var type = getInputType(inputValueIntrospection.type); + var defaultValue = inputValueIntrospection.defaultValue ? valueFromAST(parseValue(inputValueIntrospection.defaultValue), type) : undefined; + return { + name: inputValueIntrospection.name, + description: inputValueIntrospection.description, + type: type, + defaultValue: defaultValue + }; + } + + function buildDirective(directiveIntrospection) { + // Support deprecated `on****` fields for building `locations`, as this + // is used by GraphiQL which may need to support outdated servers. + var locations = directiveIntrospection.locations ? directiveIntrospection.locations.slice() : [].concat(!directiveIntrospection.onField ? [] : [DirectiveLocation.FIELD], !directiveIntrospection.onOperation ? [] : [DirectiveLocation.QUERY, DirectiveLocation.MUTATION, DirectiveLocation.SUBSCRIPTION], !directiveIntrospection.onFragment ? [] : [DirectiveLocation.FRAGMENT_DEFINITION, DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT]); + + if (!directiveIntrospection.args) { + throw new Error('Introspection result missing directive args: ' + JSON.stringify(directiveIntrospection)); + } + + return new GraphQLDirective({ + name: directiveIntrospection.name, + description: directiveIntrospection.description, + locations: locations, + args: buildInputValueDefMap(directiveIntrospection.args) + }); + } // Iterate through all types, getting the type definition for each, ensuring + // that any type not directly referenced by a field will get created. + + + var types = schemaIntrospection.types.map(function (typeIntrospection) { + return getNamedType(typeIntrospection.name); + }); // Get the root Query, Mutation, and Subscription types. + + var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null; + var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null; + var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null; // Get the directives supported by Introspection, assuming empty-set if + // directives were not queried for. + + var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : []; // Then produce and return a Schema with these types. + + return new GraphQLSchema({ + query: queryType, + mutation: mutationType, + subscription: subscriptionType, + types: types, + directives: directives, + assumeValid: options && options.assumeValid, + allowedLegacyNames: options && options.allowedLegacyNames + }); +} \ No newline at end of file diff --git a/dist/utilities/coerceValue.js b/dist/utilities/coerceValue.js new file mode 100644 index 0000000000..f9a9b87ac4 --- /dev/null +++ b/dist/utilities/coerceValue.js @@ -0,0 +1,203 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.coerceValue = coerceValue; + +var _iterall = require("iterall"); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _isNullish = _interopRequireDefault(require("../jsutils/isNullish")); + +var _orList = _interopRequireDefault(require("../jsutils/orList")); + +var _suggestionList = _interopRequireDefault(require("../jsutils/suggestionList")); + +var _error = require("../error"); + +var _definition = require("../type/definition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +/** + * Coerces a JavaScript value given a GraphQL Type. + * + * Returns either a value which is valid for the provided type or a list of + * encountered coercion errors. + * + */ +function coerceValue(value, type, blameNode, path) { + // A value must be provided if the type is non-null. + if ((0, _definition.isNonNullType)(type)) { + if ((0, _isNullish.default)(value)) { + return ofErrors([coercionError("Expected non-nullable type ".concat(String(type), " not to be null"), blameNode, path)]); + } + + return coerceValue(value, type.ofType, blameNode, path); + } + + if ((0, _isNullish.default)(value)) { + // Explicitly return the value null. + return ofValue(null); + } + + if ((0, _definition.isScalarType)(type)) { + // Scalars determine if a value is valid via parseValue(), which can + // throw to indicate failure. If it throws, maintain a reference to + // the original error. + try { + var parseResult = type.parseValue(value); + + if ((0, _isInvalid.default)(parseResult)) { + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path)]); + } + + return ofValue(parseResult); + } catch (error) { + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path, error.message, error)]); + } + } + + if ((0, _definition.isEnumType)(type)) { + if (typeof value === 'string') { + var enumValue = type.getValue(value); + + if (enumValue) { + return ofValue(enumValue.value); + } + } + + var suggestions = (0, _suggestionList.default)(String(value), type.getValues().map(function (enumValue) { + return enumValue.name; + })); + var didYouMean = suggestions.length !== 0 ? "did you mean ".concat((0, _orList.default)(suggestions), "?") : undefined; + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path, didYouMean)]); + } + + if ((0, _definition.isListType)(type)) { + var itemType = type.ofType; + + if ((0, _iterall.isCollection)(value)) { + var _errors; + + var coercedValue = []; + (0, _iterall.forEach)(value, function (itemValue, index) { + var coercedItem = coerceValue(itemValue, itemType, blameNode, atPath(path, index)); + + if (coercedItem.errors) { + _errors = add(_errors, coercedItem.errors); + } else if (!_errors) { + coercedValue.push(coercedItem.value); + } + }); + return _errors ? ofErrors(_errors) : ofValue(coercedValue); + } // Lists accept a non-list value as a list of one. + + + var coercedItem = coerceValue(value, itemType, blameNode); + return coercedItem.errors ? coercedItem : ofValue([coercedItem.value]); + } + + if ((0, _definition.isInputObjectType)(type)) { + if (_typeof(value) !== 'object') { + return ofErrors([coercionError("Expected type ".concat(type.name, " to be an object"), blameNode, path)]); + } + + var _errors2; + + var _coercedValue = {}; + var fields = type.getFields(); // Ensure every defined field is valid. + + for (var fieldName in fields) { + if (hasOwnProperty.call(fields, fieldName)) { + var field = fields[fieldName]; + var fieldValue = value[fieldName]; + + if ((0, _isInvalid.default)(fieldValue)) { + if (!(0, _isInvalid.default)(field.defaultValue)) { + _coercedValue[fieldName] = field.defaultValue; + } else if ((0, _definition.isNonNullType)(field.type)) { + _errors2 = add(_errors2, coercionError("Field ".concat(printPath(atPath(path, fieldName)), " of required ") + "type ".concat(String(field.type), " was not provided"), blameNode)); + } + } else { + var coercedField = coerceValue(fieldValue, field.type, blameNode, atPath(path, fieldName)); + + if (coercedField.errors) { + _errors2 = add(_errors2, coercedField.errors); + } else if (!_errors2) { + _coercedValue[fieldName] = coercedField.value; + } + } + } + } // Ensure every provided field is defined. + + + for (var _fieldName in value) { + if (hasOwnProperty.call(value, _fieldName)) { + if (!fields[_fieldName]) { + var _suggestions = (0, _suggestionList.default)(_fieldName, Object.keys(fields)); + + var _didYouMean = _suggestions.length !== 0 ? "did you mean ".concat((0, _orList.default)(_suggestions), "?") : undefined; + + _errors2 = add(_errors2, coercionError("Field \"".concat(_fieldName, "\" is not defined by type ").concat(type.name), blameNode, path, _didYouMean)); + } + } + } + + return _errors2 ? ofErrors(_errors2) : ofValue(_coercedValue); + } + /* istanbul ignore next */ + + + throw new Error("Unexpected type: ".concat(type, ".")); +} + +function ofValue(value) { + return { + errors: undefined, + value: value + }; +} + +function ofErrors(errors) { + return { + errors: errors, + value: undefined + }; +} + +function add(errors, moreErrors) { + return (errors || []).concat(moreErrors); +} + +function atPath(prev, key) { + return { + prev: prev, + key: key + }; +} + +function coercionError(message, blameNode, path, subMessage, originalError) { + var pathStr = printPath(path); // Return a GraphQLError instance + + return new _error.GraphQLError(message + (pathStr ? ' at ' + pathStr : '') + (subMessage ? '; ' + subMessage : '.'), blameNode, undefined, undefined, undefined, originalError); +} // Build a string describing the path into the value where the error was found + + +function printPath(path) { + var pathStr = ''; + var currentPath = path; + + while (currentPath) { + pathStr = (typeof currentPath.key === 'string' ? '.' + currentPath.key : '[' + String(currentPath.key) + ']') + pathStr; + currentPath = currentPath.prev; + } + + return pathStr ? 'value' + pathStr : ''; +} + +var hasOwnProperty = Object.prototype.hasOwnProperty; \ No newline at end of file diff --git a/dist/utilities/coerceValue.js.flow b/dist/utilities/coerceValue.js.flow new file mode 100644 index 0000000000..38a42d635c --- /dev/null +++ b/dist/utilities/coerceValue.js.flow @@ -0,0 +1,257 @@ +/** + * 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. + * + * @flow strict + */ + +import { forEach, isCollection } from 'iterall'; +import isInvalid from '../jsutils/isInvalid'; +import isNullish from '../jsutils/isNullish'; +import orList from '../jsutils/orList'; +import suggestionList from '../jsutils/suggestionList'; +import { GraphQLError } from '../error'; +import type { ASTNode } from '../language/ast'; +import { + isScalarType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, +} from '../type/definition'; +import type { GraphQLInputType } from '../type/definition'; + +type CoercedValue = {| + +errors: $ReadOnlyArray | void, + +value: mixed, +|}; + +type Path = {| +prev: Path | void, +key: string | number |}; + +/** + * Coerces a JavaScript value given a GraphQL Type. + * + * Returns either a value which is valid for the provided type or a list of + * encountered coercion errors. + * + */ +export function coerceValue( + value: mixed, + type: GraphQLInputType, + blameNode?: ASTNode, + path?: Path, +): CoercedValue { + // A value must be provided if the type is non-null. + if (isNonNullType(type)) { + if (isNullish(value)) { + return ofErrors([ + coercionError( + `Expected non-nullable type ${String(type)} not to be null`, + blameNode, + path, + ), + ]); + } + return coerceValue(value, type.ofType, blameNode, path); + } + + if (isNullish(value)) { + // Explicitly return the value null. + return ofValue(null); + } + + if (isScalarType(type)) { + // Scalars determine if a value is valid via parseValue(), which can + // throw to indicate failure. If it throws, maintain a reference to + // the original error. + try { + const parseResult = type.parseValue(value); + if (isInvalid(parseResult)) { + return ofErrors([ + coercionError(`Expected type ${type.name}`, blameNode, path), + ]); + } + return ofValue(parseResult); + } catch (error) { + return ofErrors([ + coercionError( + `Expected type ${type.name}`, + blameNode, + path, + error.message, + error, + ), + ]); + } + } + + if (isEnumType(type)) { + if (typeof value === 'string') { + const enumValue = type.getValue(value); + if (enumValue) { + return ofValue(enumValue.value); + } + } + const suggestions = suggestionList( + String(value), + type.getValues().map(enumValue => enumValue.name), + ); + const didYouMean = + suggestions.length !== 0 + ? `did you mean ${orList(suggestions)}?` + : undefined; + return ofErrors([ + coercionError(`Expected type ${type.name}`, blameNode, path, didYouMean), + ]); + } + + if (isListType(type)) { + const itemType = type.ofType; + if (isCollection(value)) { + let errors; + const coercedValue = []; + forEach((value: any), (itemValue, index) => { + const coercedItem = coerceValue( + itemValue, + itemType, + blameNode, + atPath(path, index), + ); + if (coercedItem.errors) { + errors = add(errors, coercedItem.errors); + } else if (!errors) { + coercedValue.push(coercedItem.value); + } + }); + return errors ? ofErrors(errors) : ofValue(coercedValue); + } + // Lists accept a non-list value as a list of one. + const coercedItem = coerceValue(value, itemType, blameNode); + return coercedItem.errors ? coercedItem : ofValue([coercedItem.value]); + } + + if (isInputObjectType(type)) { + if (typeof value !== 'object') { + return ofErrors([ + coercionError( + `Expected type ${type.name} to be an object`, + blameNode, + path, + ), + ]); + } + let errors; + const coercedValue = {}; + const fields = type.getFields(); + + // Ensure every defined field is valid. + for (const fieldName in fields) { + if (hasOwnProperty.call(fields, fieldName)) { + const field = fields[fieldName]; + const fieldValue = value[fieldName]; + if (isInvalid(fieldValue)) { + if (!isInvalid(field.defaultValue)) { + coercedValue[fieldName] = field.defaultValue; + } else if (isNonNullType(field.type)) { + errors = add( + errors, + coercionError( + `Field ${printPath(atPath(path, fieldName))} of required ` + + `type ${String(field.type)} was not provided`, + blameNode, + ), + ); + } + } else { + const coercedField = coerceValue( + fieldValue, + field.type, + blameNode, + atPath(path, fieldName), + ); + if (coercedField.errors) { + errors = add(errors, coercedField.errors); + } else if (!errors) { + coercedValue[fieldName] = coercedField.value; + } + } + } + } + + // Ensure every provided field is defined. + for (const fieldName in value) { + if (hasOwnProperty.call(value, fieldName)) { + if (!fields[fieldName]) { + const suggestions = suggestionList(fieldName, Object.keys(fields)); + const didYouMean = + suggestions.length !== 0 + ? `did you mean ${orList(suggestions)}?` + : undefined; + errors = add( + errors, + coercionError( + `Field "${fieldName}" is not defined by type ${type.name}`, + blameNode, + path, + didYouMean, + ), + ); + } + } + } + + return errors ? ofErrors(errors) : ofValue(coercedValue); + } + + /* istanbul ignore next */ + throw new Error(`Unexpected type: ${(type: empty)}.`); +} + +function ofValue(value) { + return { errors: undefined, value }; +} + +function ofErrors(errors) { + return { errors, value: undefined }; +} + +function add(errors, moreErrors) { + return (errors || []).concat(moreErrors); +} + +function atPath(prev, key) { + return { prev, key }; +} + +function coercionError(message, blameNode, path, subMessage, originalError) { + const pathStr = printPath(path); + // Return a GraphQLError instance + return new GraphQLError( + message + + (pathStr ? ' at ' + pathStr : '') + + (subMessage ? '; ' + subMessage : '.'), + blameNode, + undefined, + undefined, + undefined, + originalError, + ); +} + +// Build a string describing the path into the value where the error was found +function printPath(path) { + let pathStr = ''; + let currentPath = path; + while (currentPath) { + pathStr = + (typeof currentPath.key === 'string' + ? '.' + currentPath.key + : '[' + String(currentPath.key) + ']') + pathStr; + currentPath = currentPath.prev; + } + return pathStr ? 'value' + pathStr : ''; +} + +const hasOwnProperty = Object.prototype.hasOwnProperty; diff --git a/dist/utilities/coerceValue.mjs b/dist/utilities/coerceValue.mjs new file mode 100644 index 0000000000..3ee0270670 --- /dev/null +++ b/dist/utilities/coerceValue.mjs @@ -0,0 +1,196 @@ +function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + +/** + * 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. + * + * strict + */ +import { forEach, isCollection } from 'iterall'; +import isInvalid from '../jsutils/isInvalid'; +import isNullish from '../jsutils/isNullish'; +import orList from '../jsutils/orList'; +import suggestionList from '../jsutils/suggestionList'; +import { GraphQLError } from '../error'; +import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition'; + +/** + * Coerces a JavaScript value given a GraphQL Type. + * + * Returns either a value which is valid for the provided type or a list of + * encountered coercion errors. + * + */ +export function coerceValue(value, type, blameNode, path) { + // A value must be provided if the type is non-null. + if (isNonNullType(type)) { + if (isNullish(value)) { + return ofErrors([coercionError("Expected non-nullable type ".concat(String(type), " not to be null"), blameNode, path)]); + } + + return coerceValue(value, type.ofType, blameNode, path); + } + + if (isNullish(value)) { + // Explicitly return the value null. + return ofValue(null); + } + + if (isScalarType(type)) { + // Scalars determine if a value is valid via parseValue(), which can + // throw to indicate failure. If it throws, maintain a reference to + // the original error. + try { + var parseResult = type.parseValue(value); + + if (isInvalid(parseResult)) { + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path)]); + } + + return ofValue(parseResult); + } catch (error) { + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path, error.message, error)]); + } + } + + if (isEnumType(type)) { + if (typeof value === 'string') { + var enumValue = type.getValue(value); + + if (enumValue) { + return ofValue(enumValue.value); + } + } + + var suggestions = suggestionList(String(value), type.getValues().map(function (enumValue) { + return enumValue.name; + })); + var didYouMean = suggestions.length !== 0 ? "did you mean ".concat(orList(suggestions), "?") : undefined; + return ofErrors([coercionError("Expected type ".concat(type.name), blameNode, path, didYouMean)]); + } + + if (isListType(type)) { + var itemType = type.ofType; + + if (isCollection(value)) { + var _errors; + + var coercedValue = []; + forEach(value, function (itemValue, index) { + var coercedItem = coerceValue(itemValue, itemType, blameNode, atPath(path, index)); + + if (coercedItem.errors) { + _errors = add(_errors, coercedItem.errors); + } else if (!_errors) { + coercedValue.push(coercedItem.value); + } + }); + return _errors ? ofErrors(_errors) : ofValue(coercedValue); + } // Lists accept a non-list value as a list of one. + + + var coercedItem = coerceValue(value, itemType, blameNode); + return coercedItem.errors ? coercedItem : ofValue([coercedItem.value]); + } + + if (isInputObjectType(type)) { + if (_typeof(value) !== 'object') { + return ofErrors([coercionError("Expected type ".concat(type.name, " to be an object"), blameNode, path)]); + } + + var _errors2; + + var _coercedValue = {}; + var fields = type.getFields(); // Ensure every defined field is valid. + + for (var fieldName in fields) { + if (hasOwnProperty.call(fields, fieldName)) { + var field = fields[fieldName]; + var fieldValue = value[fieldName]; + + if (isInvalid(fieldValue)) { + if (!isInvalid(field.defaultValue)) { + _coercedValue[fieldName] = field.defaultValue; + } else if (isNonNullType(field.type)) { + _errors2 = add(_errors2, coercionError("Field ".concat(printPath(atPath(path, fieldName)), " of required ") + "type ".concat(String(field.type), " was not provided"), blameNode)); + } + } else { + var coercedField = coerceValue(fieldValue, field.type, blameNode, atPath(path, fieldName)); + + if (coercedField.errors) { + _errors2 = add(_errors2, coercedField.errors); + } else if (!_errors2) { + _coercedValue[fieldName] = coercedField.value; + } + } + } + } // Ensure every provided field is defined. + + + for (var _fieldName in value) { + if (hasOwnProperty.call(value, _fieldName)) { + if (!fields[_fieldName]) { + var _suggestions = suggestionList(_fieldName, Object.keys(fields)); + + var _didYouMean = _suggestions.length !== 0 ? "did you mean ".concat(orList(_suggestions), "?") : undefined; + + _errors2 = add(_errors2, coercionError("Field \"".concat(_fieldName, "\" is not defined by type ").concat(type.name), blameNode, path, _didYouMean)); + } + } + } + + return _errors2 ? ofErrors(_errors2) : ofValue(_coercedValue); + } + /* istanbul ignore next */ + + + throw new Error("Unexpected type: ".concat(type, ".")); +} + +function ofValue(value) { + return { + errors: undefined, + value: value + }; +} + +function ofErrors(errors) { + return { + errors: errors, + value: undefined + }; +} + +function add(errors, moreErrors) { + return (errors || []).concat(moreErrors); +} + +function atPath(prev, key) { + return { + prev: prev, + key: key + }; +} + +function coercionError(message, blameNode, path, subMessage, originalError) { + var pathStr = printPath(path); // Return a GraphQLError instance + + return new GraphQLError(message + (pathStr ? ' at ' + pathStr : '') + (subMessage ? '; ' + subMessage : '.'), blameNode, undefined, undefined, undefined, originalError); +} // Build a string describing the path into the value where the error was found + + +function printPath(path) { + var pathStr = ''; + var currentPath = path; + + while (currentPath) { + pathStr = (typeof currentPath.key === 'string' ? '.' + currentPath.key : '[' + String(currentPath.key) + ']') + pathStr; + currentPath = currentPath.prev; + } + + return pathStr ? 'value' + pathStr : ''; +} + +var hasOwnProperty = Object.prototype.hasOwnProperty; \ No newline at end of file diff --git a/dist/utilities/concatAST.js b/dist/utilities/concatAST.js new file mode 100644 index 0000000000..609185dcef --- /dev/null +++ b/dist/utilities/concatAST.js @@ -0,0 +1,37 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.concatAST = concatAST; + +/** + * 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. + * + * strict + */ + +/** + * Provided a collection of ASTs, presumably each from different files, + * concatenate the ASTs together into batched AST, useful for validating many + * GraphQL source files which together represent one conceptual application. + */ +function concatAST(asts) { + var batchDefinitions = []; + + for (var i = 0; i < asts.length; i++) { + var definitions = asts[i].definitions; + + for (var j = 0; j < definitions.length; j++) { + batchDefinitions.push(definitions[j]); + } + } + + return { + kind: 'Document', + definitions: batchDefinitions + }; +} \ No newline at end of file diff --git a/dist/utilities/concatAST.js.flow b/dist/utilities/concatAST.js.flow new file mode 100644 index 0000000000..edeaa8f41d --- /dev/null +++ b/dist/utilities/concatAST.js.flow @@ -0,0 +1,29 @@ +/** + * 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. + * + * @flow strict + */ + +import type { DocumentNode } from '../language/ast'; + +/** + * Provided a collection of ASTs, presumably each from different files, + * concatenate the ASTs together into batched AST, useful for validating many + * GraphQL source files which together represent one conceptual application. + */ +export function concatAST(asts: $ReadOnlyArray): DocumentNode { + const batchDefinitions = []; + for (let i = 0; i < asts.length; i++) { + const definitions = asts[i].definitions; + for (let j = 0; j < definitions.length; j++) { + batchDefinitions.push(definitions[j]); + } + } + return { + kind: 'Document', + definitions: batchDefinitions, + }; +} diff --git a/dist/utilities/concatAST.mjs b/dist/utilities/concatAST.mjs new file mode 100644 index 0000000000..6e5ddc4ad0 --- /dev/null +++ b/dist/utilities/concatAST.mjs @@ -0,0 +1,30 @@ +/** + * 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. + * + * strict + */ + +/** + * Provided a collection of ASTs, presumably each from different files, + * concatenate the ASTs together into batched AST, useful for validating many + * GraphQL source files which together represent one conceptual application. + */ +export function concatAST(asts) { + var batchDefinitions = []; + + for (var i = 0; i < asts.length; i++) { + var definitions = asts[i].definitions; + + for (var j = 0; j < definitions.length; j++) { + batchDefinitions.push(definitions[j]); + } + } + + return { + kind: 'Document', + definitions: batchDefinitions + }; +} \ No newline at end of file diff --git a/dist/utilities/extendSchema.js b/dist/utilities/extendSchema.js new file mode 100644 index 0000000000..d0a2c9b4e4 --- /dev/null +++ b/dist/utilities/extendSchema.js @@ -0,0 +1,365 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.extendSchema = extendSchema; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _buildASTSchema = require("./buildASTSchema"); + +var _GraphQLError = require("../error/GraphQLError"); + +var _schema = require("../type/schema"); + +var _introspection = require("../type/introspection"); + +var _definition = require("../type/definition"); + +var _directives = require("../type/directives"); + +var _kinds = require("../language/kinds"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Produces a new schema given an existing schema and a document which may + * contain GraphQL type extensions and definitions. The original schema will + * remain unaltered. + * + * Because a schema represents a graph of references, a schema cannot be + * extended without effectively making an entire copy. We do not know until it's + * too late if subgraphs remain unchanged. + * + * This algorithm copies the provided schema, applying extensions while + * producing the copy. The original schema remains unaltered. + * + * Accepts options as a third argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +function extendSchema(schema, documentAST, options) { + !(0, _schema.isSchema)(schema) ? (0, _invariant.default)(0, 'Must provide valid GraphQLSchema') : void 0; + !(documentAST && documentAST.kind === _kinds.Kind.DOCUMENT) ? (0, _invariant.default)(0, 'Must provide valid Document AST') : void 0; // Collect the type definitions and extensions found in the document. + + var typeDefinitionMap = Object.create(null); + var typeExtensionsMap = Object.create(null); // New directives and types are separate because a directives and types can + // have the same name. For example, a type named "skip". + + var directiveDefinitions = []; // Schema extensions are collected which may add additional operation types. + + var schemaExtensions = []; + + for (var i = 0; i < documentAST.definitions.length; i++) { + var def = documentAST.definitions[i]; + + switch (def.kind) { + case _kinds.Kind.SCHEMA_DEFINITION: + // Sanity check that a schema extension is not defining a new schema + throw new _GraphQLError.GraphQLError('Cannot define a new schema within a schema extension.', [def]); + + case _kinds.Kind.SCHEMA_EXTENSION: + schemaExtensions.push(def); + break; + + case _kinds.Kind.OBJECT_TYPE_DEFINITION: + case _kinds.Kind.INTERFACE_TYPE_DEFINITION: + case _kinds.Kind.ENUM_TYPE_DEFINITION: + case _kinds.Kind.UNION_TYPE_DEFINITION: + case _kinds.Kind.SCALAR_TYPE_DEFINITION: + case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION: + // Sanity check that none of the defined types conflict with the + // schema's existing types. + var typeName = def.name.value; + + if (schema.getType(typeName)) { + throw new _GraphQLError.GraphQLError("Type \"".concat(typeName, "\" already exists in the schema. It cannot also ") + 'be defined in this type definition.', [def]); + } + + typeDefinitionMap[typeName] = def; + break; + + case _kinds.Kind.OBJECT_TYPE_EXTENSION: + case _kinds.Kind.INTERFACE_TYPE_EXTENSION: + // Sanity check that this type extension exists within the + // schema's existing types. + var extendedTypeName = def.name.value; + var existingType = schema.getType(extendedTypeName); + + if (!existingType) { + throw new _GraphQLError.GraphQLError("Cannot extend type \"".concat(extendedTypeName, "\" because it does not ") + 'exist in the existing schema.', [def]); + } + + checkExtensionNode(existingType, def); + var existingTypeExtensions = typeExtensionsMap[extendedTypeName]; + typeExtensionsMap[extendedTypeName] = existingTypeExtensions ? existingTypeExtensions.concat([def]) : [def]; + break; + + case _kinds.Kind.DIRECTIVE_DEFINITION: + var directiveName = def.name.value; + var existingDirective = schema.getDirective(directiveName); + + if (existingDirective) { + throw new _GraphQLError.GraphQLError("Directive \"".concat(directiveName, "\" already exists in the schema. It ") + 'cannot be redefined.', [def]); + } + + directiveDefinitions.push(def); + break; + + case _kinds.Kind.SCALAR_TYPE_EXTENSION: + case _kinds.Kind.UNION_TYPE_EXTENSION: + case _kinds.Kind.ENUM_TYPE_EXTENSION: + case _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION: + throw new Error("The ".concat(def.kind, " kind is not yet supported by extendSchema().")); + } + } // If this document contains no new types, extensions, or directives then + // return the same unmodified GraphQLSchema instance. + + + if (Object.keys(typeExtensionsMap).length === 0 && Object.keys(typeDefinitionMap).length === 0 && directiveDefinitions.length === 0 && schemaExtensions.length === 0) { + return schema; + } + + var astBuilder = new _buildASTSchema.ASTDefinitionBuilder(typeDefinitionMap, options, function (typeRef) { + var typeName = typeRef.name.value; + var existingType = schema.getType(typeName); + + if (existingType) { + return getExtendedType(existingType); + } + + throw new _GraphQLError.GraphQLError("Unknown type: \"".concat(typeName, "\". Ensure that this type exists ") + 'either in the original schema, or is added in a type definition.', [typeRef]); + }); + var extendTypeCache = Object.create(null); // Get the extended root operation types. + + var operationTypes = { + query: getExtendedMaybeType(schema.getQueryType()), + mutation: getExtendedMaybeType(schema.getMutationType()), + subscription: getExtendedMaybeType(schema.getSubscriptionType()) + }; // Then, incorporate all schema extensions. + + schemaExtensions.forEach(function (schemaExtension) { + if (schemaExtension.operationTypes) { + schemaExtension.operationTypes.forEach(function (operationType) { + var operation = operationType.operation; + + if (operationTypes[operation]) { + throw new Error("Must provide only one ".concat(operation, " type in schema.")); + } + + var typeRef = operationType.type; // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + + operationTypes[operation] = astBuilder.buildType(typeRef); + }); + } + }); + var schemaExtensionASTNodes = schemaExtensions ? schema.extensionASTNodes ? schema.extensionASTNodes.concat(schemaExtensions) : schemaExtensions : schema.extensionASTNodes; + var types = (0, _objectValues.default)(schema.getTypeMap()).map(function (type) { + return getExtendedType(type); + }).concat(astBuilder.buildTypes((0, _objectValues.default)(typeDefinitionMap))); // Support both original legacy names and extended legacy names. + + var schemaAllowedLegacyNames = schema.__allowedLegacyNames; + var extendAllowedLegacyNames = options && options.allowedLegacyNames; + var allowedLegacyNames = schemaAllowedLegacyNames && extendAllowedLegacyNames ? schemaAllowedLegacyNames.concat(extendAllowedLegacyNames) : schemaAllowedLegacyNames || extendAllowedLegacyNames; // Then produce and return a Schema with these types. + + return new _schema.GraphQLSchema({ + query: operationTypes.query, + mutation: operationTypes.mutation, + subscription: operationTypes.subscription, + types: types, + directives: getMergedDirectives(), + astNode: schema.astNode, + extensionASTNodes: schemaExtensionASTNodes, + allowedLegacyNames: allowedLegacyNames + }); // Below are functions used for producing this schema that have closed over + // this scope and have access to the schema, cache, and newly defined types. + + function getMergedDirectives() { + var existingDirectives = schema.getDirectives(); + !existingDirectives ? (0, _invariant.default)(0, 'schema must have default directives') : void 0; + return existingDirectives.concat(directiveDefinitions.map(function (node) { + return astBuilder.buildDirective(node); + })); + } + + function getExtendedMaybeType(type) { + return type ? getExtendedType(type) : null; + } + + function getExtendedType(type) { + if (!extendTypeCache[type.name]) { + extendTypeCache[type.name] = extendType(type); + } + + return extendTypeCache[type.name]; + } // To be called at most once per type. Only getExtendedType should call this. + + + function extendType(type) { + if ((0, _introspection.isIntrospectionType)(type)) { + // Introspection types are not extended. + return type; + } + + if ((0, _definition.isObjectType)(type)) { + return extendObjectType(type); + } + + if ((0, _definition.isInterfaceType)(type)) { + return extendInterfaceType(type); + } + + if ((0, _definition.isUnionType)(type)) { + return extendUnionType(type); + } // This type is not yet extendable. + + + return type; + } + + function extendObjectType(type) { + var name = type.name; + var extensionASTNodes = typeExtensionsMap[name] ? type.extensionASTNodes ? type.extensionASTNodes.concat(typeExtensionsMap[name]) : typeExtensionsMap[name] : type.extensionASTNodes; + return new _definition.GraphQLObjectType({ + name: name, + description: type.description, + interfaces: function interfaces() { + return extendImplementedInterfaces(type); + }, + fields: function fields() { + return extendFieldMap(type); + }, + astNode: type.astNode, + extensionASTNodes: extensionASTNodes, + isTypeOf: type.isTypeOf + }); + } + + function extendInterfaceType(type) { + var name = type.name; + var extensionASTNodes = typeExtensionsMap[name] ? type.extensionASTNodes ? type.extensionASTNodes.concat(typeExtensionsMap[name]) : typeExtensionsMap[name] : type.extensionASTNodes; + return new _definition.GraphQLInterfaceType({ + name: type.name, + description: type.description, + fields: function fields() { + return extendFieldMap(type); + }, + astNode: type.astNode, + extensionASTNodes: extensionASTNodes, + resolveType: type.resolveType + }); + } + + function extendUnionType(type) { + return new _definition.GraphQLUnionType({ + name: type.name, + description: type.description, + types: type.getTypes().map(getExtendedType), + astNode: type.astNode, + resolveType: type.resolveType + }); + } + + function extendImplementedInterfaces(type) { + var interfaces = type.getInterfaces().map(getExtendedType); // If there are any extensions to the interfaces, apply those here. + + var extensions = typeExtensionsMap[type.name]; + + if (extensions) { + extensions.forEach(function (extension) { + extension.interfaces.forEach(function (namedType) { + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces.push(astBuilder.buildType(namedType)); + }); + }); + } + + return interfaces; + } + + function extendFieldMap(type) { + var newFieldMap = Object.create(null); + var oldFieldMap = type.getFields(); + Object.keys(oldFieldMap).forEach(function (fieldName) { + var field = oldFieldMap[fieldName]; + newFieldMap[fieldName] = { + description: field.description, + deprecationReason: field.deprecationReason, + type: extendFieldType(field.type), + args: (0, _keyMap.default)(field.args, function (arg) { + return arg.name; + }), + astNode: field.astNode, + resolve: field.resolve + }; + }); // If there are any extensions to the fields, apply those here. + + var extensions = typeExtensionsMap[type.name]; + + if (extensions) { + extensions.forEach(function (extension) { + extension.fields.forEach(function (field) { + var fieldName = field.name.value; + + if (oldFieldMap[fieldName]) { + throw new _GraphQLError.GraphQLError("Field \"".concat(type.name, ".").concat(fieldName, "\" already exists in the ") + 'schema. It cannot also be defined in this type extension.', [field]); + } + + newFieldMap[fieldName] = astBuilder.buildField(field); + }); + }); + } + + return newFieldMap; + } + + function extendFieldType(typeDef) { + if ((0, _definition.isListType)(typeDef)) { + return (0, _definition.GraphQLList)(extendFieldType(typeDef.ofType)); + } + + if ((0, _definition.isNonNullType)(typeDef)) { + return (0, _definition.GraphQLNonNull)(extendFieldType(typeDef.ofType)); + } + + return getExtendedType(typeDef); + } +} + +function checkExtensionNode(type, node) { + switch (node.kind) { + case _kinds.Kind.OBJECT_TYPE_EXTENSION: + if (!(0, _definition.isObjectType)(type)) { + throw new _GraphQLError.GraphQLError("Cannot extend non-object type \"".concat(type.name, "\"."), [node]); + } + + break; + + case _kinds.Kind.INTERFACE_TYPE_EXTENSION: + if (!(0, _definition.isInterfaceType)(type)) { + throw new _GraphQLError.GraphQLError("Cannot extend non-interface type \"".concat(type.name, "\"."), [node]); + } + + break; + } +} \ No newline at end of file diff --git a/dist/utilities/extendSchema.js.flow b/dist/utilities/extendSchema.js.flow new file mode 100644 index 0000000000..d56bacf2dc --- /dev/null +++ b/dist/utilities/extendSchema.js.flow @@ -0,0 +1,435 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import objectValues from '../jsutils/objectValues'; +import { ASTDefinitionBuilder } from './buildASTSchema'; +import { GraphQLError } from '../error/GraphQLError'; +import { isSchema, GraphQLSchema } from '../type/schema'; +import { isIntrospectionType } from '../type/introspection'; + +import type { GraphQLSchemaValidationOptions } from '../type/schema'; + +import { + isObjectType, + isInterfaceType, + isUnionType, + isListType, + isNonNullType, + GraphQLList, + GraphQLNonNull, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, +} from '../type/definition'; + +import { GraphQLDirective } from '../type/directives'; + +import { Kind } from '../language/kinds'; + +import type { GraphQLType, GraphQLNamedType } from '../type/definition'; +import type { + DocumentNode, + DirectiveDefinitionNode, + SchemaExtensionNode, +} from '../language/ast'; + +type Options = {| + ...GraphQLSchemaValidationOptions, + + /** + * Descriptions are defined as preceding string literals, however an older + * experimental version of the SDL supported preceding comments as + * descriptions. Set to true to enable this deprecated behavior. + * + * Default: false + */ + commentDescriptions?: boolean, +|}; + +/** + * Produces a new schema given an existing schema and a document which may + * contain GraphQL type extensions and definitions. The original schema will + * remain unaltered. + * + * Because a schema represents a graph of references, a schema cannot be + * extended without effectively making an entire copy. We do not know until it's + * too late if subgraphs remain unchanged. + * + * This algorithm copies the provided schema, applying extensions while + * producing the copy. The original schema remains unaltered. + * + * Accepts options as a third argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function extendSchema( + schema: GraphQLSchema, + documentAST: DocumentNode, + options?: Options, +): GraphQLSchema { + invariant(isSchema(schema), 'Must provide valid GraphQLSchema'); + + invariant( + documentAST && documentAST.kind === Kind.DOCUMENT, + 'Must provide valid Document AST', + ); + + // Collect the type definitions and extensions found in the document. + const typeDefinitionMap = Object.create(null); + const typeExtensionsMap = Object.create(null); + + // New directives and types are separate because a directives and types can + // have the same name. For example, a type named "skip". + const directiveDefinitions: Array = []; + + // Schema extensions are collected which may add additional operation types. + const schemaExtensions: Array = []; + + for (let i = 0; i < documentAST.definitions.length; i++) { + const def = documentAST.definitions[i]; + switch (def.kind) { + case Kind.SCHEMA_DEFINITION: + // Sanity check that a schema extension is not defining a new schema + throw new GraphQLError( + 'Cannot define a new schema within a schema extension.', + [def], + ); + case Kind.SCHEMA_EXTENSION: + schemaExtensions.push(def); + break; + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.ENUM_TYPE_DEFINITION: + case Kind.UNION_TYPE_DEFINITION: + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + // Sanity check that none of the defined types conflict with the + // schema's existing types. + const typeName = def.name.value; + if (schema.getType(typeName)) { + throw new GraphQLError( + `Type "${typeName}" already exists in the schema. It cannot also ` + + 'be defined in this type definition.', + [def], + ); + } + typeDefinitionMap[typeName] = def; + break; + case Kind.OBJECT_TYPE_EXTENSION: + case Kind.INTERFACE_TYPE_EXTENSION: + // Sanity check that this type extension exists within the + // schema's existing types. + const extendedTypeName = def.name.value; + const existingType = schema.getType(extendedTypeName); + if (!existingType) { + throw new GraphQLError( + `Cannot extend type "${extendedTypeName}" because it does not ` + + 'exist in the existing schema.', + [def], + ); + } + checkExtensionNode(existingType, def); + + const existingTypeExtensions = typeExtensionsMap[extendedTypeName]; + typeExtensionsMap[extendedTypeName] = existingTypeExtensions + ? existingTypeExtensions.concat([def]) + : [def]; + break; + case Kind.DIRECTIVE_DEFINITION: + const directiveName = def.name.value; + const existingDirective = schema.getDirective(directiveName); + if (existingDirective) { + throw new GraphQLError( + `Directive "${directiveName}" already exists in the schema. It ` + + 'cannot be redefined.', + [def], + ); + } + directiveDefinitions.push(def); + break; + case Kind.SCALAR_TYPE_EXTENSION: + case Kind.UNION_TYPE_EXTENSION: + case Kind.ENUM_TYPE_EXTENSION: + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + throw new Error( + `The ${def.kind} kind is not yet supported by extendSchema().`, + ); + } + } + + // If this document contains no new types, extensions, or directives then + // return the same unmodified GraphQLSchema instance. + if ( + Object.keys(typeExtensionsMap).length === 0 && + Object.keys(typeDefinitionMap).length === 0 && + directiveDefinitions.length === 0 && + schemaExtensions.length === 0 + ) { + return schema; + } + + const astBuilder = new ASTDefinitionBuilder( + typeDefinitionMap, + options, + typeRef => { + const typeName = typeRef.name.value; + const existingType = schema.getType(typeName); + if (existingType) { + return getExtendedType(existingType); + } + + throw new GraphQLError( + `Unknown type: "${typeName}". Ensure that this type exists ` + + 'either in the original schema, or is added in a type definition.', + [typeRef], + ); + }, + ); + + const extendTypeCache = Object.create(null); + + // Get the extended root operation types. + const operationTypes = { + query: getExtendedMaybeType(schema.getQueryType()), + mutation: getExtendedMaybeType(schema.getMutationType()), + subscription: getExtendedMaybeType(schema.getSubscriptionType()), + }; + + // Then, incorporate all schema extensions. + schemaExtensions.forEach(schemaExtension => { + if (schemaExtension.operationTypes) { + schemaExtension.operationTypes.forEach(operationType => { + const operation = operationType.operation; + if (operationTypes[operation]) { + throw new Error(`Must provide only one ${operation} type in schema.`); + } + const typeRef = operationType.type; + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + operationTypes[operation] = (astBuilder.buildType(typeRef): any); + }); + } + }); + + const schemaExtensionASTNodes = schemaExtensions + ? schema.extensionASTNodes + ? schema.extensionASTNodes.concat(schemaExtensions) + : schemaExtensions + : schema.extensionASTNodes; + + const types = [ + // Iterate through all types, getting the type definition for each, ensuring + // that any type not directly referenced by a field will get created. + ...objectValues(schema.getTypeMap()).map(type => getExtendedType(type)), + // Do the same with new types. + ...astBuilder.buildTypes(objectValues(typeDefinitionMap)), + ]; + + // Support both original legacy names and extended legacy names. + const schemaAllowedLegacyNames = schema.__allowedLegacyNames; + const extendAllowedLegacyNames = options && options.allowedLegacyNames; + const allowedLegacyNames = + schemaAllowedLegacyNames && extendAllowedLegacyNames + ? schemaAllowedLegacyNames.concat(extendAllowedLegacyNames) + : schemaAllowedLegacyNames || extendAllowedLegacyNames; + + // Then produce and return a Schema with these types. + return new GraphQLSchema({ + query: operationTypes.query, + mutation: operationTypes.mutation, + subscription: operationTypes.subscription, + types, + directives: getMergedDirectives(), + astNode: schema.astNode, + extensionASTNodes: schemaExtensionASTNodes, + allowedLegacyNames, + }); + + // Below are functions used for producing this schema that have closed over + // this scope and have access to the schema, cache, and newly defined types. + + function getMergedDirectives(): Array { + const existingDirectives = schema.getDirectives(); + invariant(existingDirectives, 'schema must have default directives'); + + return existingDirectives.concat( + directiveDefinitions.map(node => astBuilder.buildDirective(node)), + ); + } + + function getExtendedMaybeType(type: ?T): ?T { + return type ? getExtendedType(type) : null; + } + + function getExtendedType(type: T): T { + if (!extendTypeCache[type.name]) { + extendTypeCache[type.name] = extendType(type); + } + return (extendTypeCache[type.name]: any); + } + + // To be called at most once per type. Only getExtendedType should call this. + function extendType(type) { + if (isIntrospectionType(type)) { + // Introspection types are not extended. + return type; + } + if (isObjectType(type)) { + return extendObjectType(type); + } + if (isInterfaceType(type)) { + return extendInterfaceType(type); + } + if (isUnionType(type)) { + return extendUnionType(type); + } + // This type is not yet extendable. + return type; + } + + function extendObjectType(type: GraphQLObjectType): GraphQLObjectType { + const name = type.name; + const extensionASTNodes = typeExtensionsMap[name] + ? type.extensionASTNodes + ? type.extensionASTNodes.concat(typeExtensionsMap[name]) + : typeExtensionsMap[name] + : type.extensionASTNodes; + return new GraphQLObjectType({ + name, + description: type.description, + interfaces: () => extendImplementedInterfaces(type), + fields: () => extendFieldMap(type), + astNode: type.astNode, + extensionASTNodes, + isTypeOf: type.isTypeOf, + }); + } + + function extendInterfaceType( + type: GraphQLInterfaceType, + ): GraphQLInterfaceType { + const name = type.name; + const extensionASTNodes = typeExtensionsMap[name] + ? type.extensionASTNodes + ? type.extensionASTNodes.concat(typeExtensionsMap[name]) + : typeExtensionsMap[name] + : type.extensionASTNodes; + return new GraphQLInterfaceType({ + name: type.name, + description: type.description, + fields: () => extendFieldMap(type), + astNode: type.astNode, + extensionASTNodes, + resolveType: type.resolveType, + }); + } + + function extendUnionType(type: GraphQLUnionType): GraphQLUnionType { + return new GraphQLUnionType({ + name: type.name, + description: type.description, + types: type.getTypes().map(getExtendedType), + astNode: type.astNode, + resolveType: type.resolveType, + }); + } + + function extendImplementedInterfaces( + type: GraphQLObjectType, + ): Array { + const interfaces = type.getInterfaces().map(getExtendedType); + + // If there are any extensions to the interfaces, apply those here. + const extensions = typeExtensionsMap[type.name]; + if (extensions) { + extensions.forEach(extension => { + extension.interfaces.forEach(namedType => { + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces.push((astBuilder.buildType(namedType): any)); + }); + }); + } + + return interfaces; + } + + function extendFieldMap(type: GraphQLObjectType | GraphQLInterfaceType) { + const newFieldMap = Object.create(null); + const oldFieldMap = type.getFields(); + Object.keys(oldFieldMap).forEach(fieldName => { + const field = oldFieldMap[fieldName]; + newFieldMap[fieldName] = { + description: field.description, + deprecationReason: field.deprecationReason, + type: extendFieldType(field.type), + args: keyMap(field.args, arg => arg.name), + astNode: field.astNode, + resolve: field.resolve, + }; + }); + + // If there are any extensions to the fields, apply those here. + const extensions = typeExtensionsMap[type.name]; + if (extensions) { + extensions.forEach(extension => { + extension.fields.forEach(field => { + const fieldName = field.name.value; + if (oldFieldMap[fieldName]) { + throw new GraphQLError( + `Field "${type.name}.${fieldName}" already exists in the ` + + 'schema. It cannot also be defined in this type extension.', + [field], + ); + } + newFieldMap[fieldName] = astBuilder.buildField(field); + }); + }); + } + + return newFieldMap; + } + + function extendFieldType(typeDef: T): T { + if (isListType(typeDef)) { + return (GraphQLList(extendFieldType(typeDef.ofType)): any); + } + if (isNonNullType(typeDef)) { + return (GraphQLNonNull(extendFieldType(typeDef.ofType)): any); + } + return getExtendedType(typeDef); + } +} + +function checkExtensionNode(type, node) { + switch (node.kind) { + case Kind.OBJECT_TYPE_EXTENSION: + if (!isObjectType(type)) { + throw new GraphQLError( + `Cannot extend non-object type "${type.name}".`, + [node], + ); + } + break; + case Kind.INTERFACE_TYPE_EXTENSION: + if (!isInterfaceType(type)) { + throw new GraphQLError( + `Cannot extend non-interface type "${type.name}".`, + [node], + ); + } + break; + } +} diff --git a/dist/utilities/extendSchema.mjs b/dist/utilities/extendSchema.mjs new file mode 100644 index 0000000000..d8c40d0088 --- /dev/null +++ b/dist/utilities/extendSchema.mjs @@ -0,0 +1,346 @@ +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; +import keyMap from '../jsutils/keyMap'; +import objectValues from '../jsutils/objectValues'; +import { ASTDefinitionBuilder } from './buildASTSchema'; +import { GraphQLError } from '../error/GraphQLError'; +import { isSchema, GraphQLSchema } from '../type/schema'; +import { isIntrospectionType } from '../type/introspection'; +import { isObjectType, isInterfaceType, isUnionType, isListType, isNonNullType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType } from '../type/definition'; +import { GraphQLDirective } from '../type/directives'; +import { Kind } from '../language/kinds'; + +/** + * Produces a new schema given an existing schema and a document which may + * contain GraphQL type extensions and definitions. The original schema will + * remain unaltered. + * + * Because a schema represents a graph of references, a schema cannot be + * extended without effectively making an entire copy. We do not know until it's + * too late if subgraphs remain unchanged. + * + * This algorithm copies the provided schema, applying extensions while + * producing the copy. The original schema remains unaltered. + * + * Accepts options as a third argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function extendSchema(schema, documentAST, options) { + !isSchema(schema) ? invariant(0, 'Must provide valid GraphQLSchema') : void 0; + !(documentAST && documentAST.kind === Kind.DOCUMENT) ? invariant(0, 'Must provide valid Document AST') : void 0; // Collect the type definitions and extensions found in the document. + + var typeDefinitionMap = Object.create(null); + var typeExtensionsMap = Object.create(null); // New directives and types are separate because a directives and types can + // have the same name. For example, a type named "skip". + + var directiveDefinitions = []; // Schema extensions are collected which may add additional operation types. + + var schemaExtensions = []; + + for (var i = 0; i < documentAST.definitions.length; i++) { + var def = documentAST.definitions[i]; + + switch (def.kind) { + case Kind.SCHEMA_DEFINITION: + // Sanity check that a schema extension is not defining a new schema + throw new GraphQLError('Cannot define a new schema within a schema extension.', [def]); + + case Kind.SCHEMA_EXTENSION: + schemaExtensions.push(def); + break; + + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.ENUM_TYPE_DEFINITION: + case Kind.UNION_TYPE_DEFINITION: + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + // Sanity check that none of the defined types conflict with the + // schema's existing types. + var typeName = def.name.value; + + if (schema.getType(typeName)) { + throw new GraphQLError("Type \"".concat(typeName, "\" already exists in the schema. It cannot also ") + 'be defined in this type definition.', [def]); + } + + typeDefinitionMap[typeName] = def; + break; + + case Kind.OBJECT_TYPE_EXTENSION: + case Kind.INTERFACE_TYPE_EXTENSION: + // Sanity check that this type extension exists within the + // schema's existing types. + var extendedTypeName = def.name.value; + var existingType = schema.getType(extendedTypeName); + + if (!existingType) { + throw new GraphQLError("Cannot extend type \"".concat(extendedTypeName, "\" because it does not ") + 'exist in the existing schema.', [def]); + } + + checkExtensionNode(existingType, def); + var existingTypeExtensions = typeExtensionsMap[extendedTypeName]; + typeExtensionsMap[extendedTypeName] = existingTypeExtensions ? existingTypeExtensions.concat([def]) : [def]; + break; + + case Kind.DIRECTIVE_DEFINITION: + var directiveName = def.name.value; + var existingDirective = schema.getDirective(directiveName); + + if (existingDirective) { + throw new GraphQLError("Directive \"".concat(directiveName, "\" already exists in the schema. It ") + 'cannot be redefined.', [def]); + } + + directiveDefinitions.push(def); + break; + + case Kind.SCALAR_TYPE_EXTENSION: + case Kind.UNION_TYPE_EXTENSION: + case Kind.ENUM_TYPE_EXTENSION: + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + throw new Error("The ".concat(def.kind, " kind is not yet supported by extendSchema().")); + } + } // If this document contains no new types, extensions, or directives then + // return the same unmodified GraphQLSchema instance. + + + if (Object.keys(typeExtensionsMap).length === 0 && Object.keys(typeDefinitionMap).length === 0 && directiveDefinitions.length === 0 && schemaExtensions.length === 0) { + return schema; + } + + var astBuilder = new ASTDefinitionBuilder(typeDefinitionMap, options, function (typeRef) { + var typeName = typeRef.name.value; + var existingType = schema.getType(typeName); + + if (existingType) { + return getExtendedType(existingType); + } + + throw new GraphQLError("Unknown type: \"".concat(typeName, "\". Ensure that this type exists ") + 'either in the original schema, or is added in a type definition.', [typeRef]); + }); + var extendTypeCache = Object.create(null); // Get the extended root operation types. + + var operationTypes = { + query: getExtendedMaybeType(schema.getQueryType()), + mutation: getExtendedMaybeType(schema.getMutationType()), + subscription: getExtendedMaybeType(schema.getSubscriptionType()) + }; // Then, incorporate all schema extensions. + + schemaExtensions.forEach(function (schemaExtension) { + if (schemaExtension.operationTypes) { + schemaExtension.operationTypes.forEach(function (operationType) { + var operation = operationType.operation; + + if (operationTypes[operation]) { + throw new Error("Must provide only one ".concat(operation, " type in schema.")); + } + + var typeRef = operationType.type; // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + + operationTypes[operation] = astBuilder.buildType(typeRef); + }); + } + }); + var schemaExtensionASTNodes = schemaExtensions ? schema.extensionASTNodes ? schema.extensionASTNodes.concat(schemaExtensions) : schemaExtensions : schema.extensionASTNodes; + var types = objectValues(schema.getTypeMap()).map(function (type) { + return getExtendedType(type); + }).concat(astBuilder.buildTypes(objectValues(typeDefinitionMap))); // Support both original legacy names and extended legacy names. + + var schemaAllowedLegacyNames = schema.__allowedLegacyNames; + var extendAllowedLegacyNames = options && options.allowedLegacyNames; + var allowedLegacyNames = schemaAllowedLegacyNames && extendAllowedLegacyNames ? schemaAllowedLegacyNames.concat(extendAllowedLegacyNames) : schemaAllowedLegacyNames || extendAllowedLegacyNames; // Then produce and return a Schema with these types. + + return new GraphQLSchema({ + query: operationTypes.query, + mutation: operationTypes.mutation, + subscription: operationTypes.subscription, + types: types, + directives: getMergedDirectives(), + astNode: schema.astNode, + extensionASTNodes: schemaExtensionASTNodes, + allowedLegacyNames: allowedLegacyNames + }); // Below are functions used for producing this schema that have closed over + // this scope and have access to the schema, cache, and newly defined types. + + function getMergedDirectives() { + var existingDirectives = schema.getDirectives(); + !existingDirectives ? invariant(0, 'schema must have default directives') : void 0; + return existingDirectives.concat(directiveDefinitions.map(function (node) { + return astBuilder.buildDirective(node); + })); + } + + function getExtendedMaybeType(type) { + return type ? getExtendedType(type) : null; + } + + function getExtendedType(type) { + if (!extendTypeCache[type.name]) { + extendTypeCache[type.name] = extendType(type); + } + + return extendTypeCache[type.name]; + } // To be called at most once per type. Only getExtendedType should call this. + + + function extendType(type) { + if (isIntrospectionType(type)) { + // Introspection types are not extended. + return type; + } + + if (isObjectType(type)) { + return extendObjectType(type); + } + + if (isInterfaceType(type)) { + return extendInterfaceType(type); + } + + if (isUnionType(type)) { + return extendUnionType(type); + } // This type is not yet extendable. + + + return type; + } + + function extendObjectType(type) { + var name = type.name; + var extensionASTNodes = typeExtensionsMap[name] ? type.extensionASTNodes ? type.extensionASTNodes.concat(typeExtensionsMap[name]) : typeExtensionsMap[name] : type.extensionASTNodes; + return new GraphQLObjectType({ + name: name, + description: type.description, + interfaces: function interfaces() { + return extendImplementedInterfaces(type); + }, + fields: function fields() { + return extendFieldMap(type); + }, + astNode: type.astNode, + extensionASTNodes: extensionASTNodes, + isTypeOf: type.isTypeOf + }); + } + + function extendInterfaceType(type) { + var name = type.name; + var extensionASTNodes = typeExtensionsMap[name] ? type.extensionASTNodes ? type.extensionASTNodes.concat(typeExtensionsMap[name]) : typeExtensionsMap[name] : type.extensionASTNodes; + return new GraphQLInterfaceType({ + name: type.name, + description: type.description, + fields: function fields() { + return extendFieldMap(type); + }, + astNode: type.astNode, + extensionASTNodes: extensionASTNodes, + resolveType: type.resolveType + }); + } + + function extendUnionType(type) { + return new GraphQLUnionType({ + name: type.name, + description: type.description, + types: type.getTypes().map(getExtendedType), + astNode: type.astNode, + resolveType: type.resolveType + }); + } + + function extendImplementedInterfaces(type) { + var interfaces = type.getInterfaces().map(getExtendedType); // If there are any extensions to the interfaces, apply those here. + + var extensions = typeExtensionsMap[type.name]; + + if (extensions) { + extensions.forEach(function (extension) { + extension.interfaces.forEach(function (namedType) { + // Note: While this could make early assertions to get the correctly + // typed values, that would throw immediately while type system + // validation with validateSchema() will produce more actionable results. + interfaces.push(astBuilder.buildType(namedType)); + }); + }); + } + + return interfaces; + } + + function extendFieldMap(type) { + var newFieldMap = Object.create(null); + var oldFieldMap = type.getFields(); + Object.keys(oldFieldMap).forEach(function (fieldName) { + var field = oldFieldMap[fieldName]; + newFieldMap[fieldName] = { + description: field.description, + deprecationReason: field.deprecationReason, + type: extendFieldType(field.type), + args: keyMap(field.args, function (arg) { + return arg.name; + }), + astNode: field.astNode, + resolve: field.resolve + }; + }); // If there are any extensions to the fields, apply those here. + + var extensions = typeExtensionsMap[type.name]; + + if (extensions) { + extensions.forEach(function (extension) { + extension.fields.forEach(function (field) { + var fieldName = field.name.value; + + if (oldFieldMap[fieldName]) { + throw new GraphQLError("Field \"".concat(type.name, ".").concat(fieldName, "\" already exists in the ") + 'schema. It cannot also be defined in this type extension.', [field]); + } + + newFieldMap[fieldName] = astBuilder.buildField(field); + }); + }); + } + + return newFieldMap; + } + + function extendFieldType(typeDef) { + if (isListType(typeDef)) { + return GraphQLList(extendFieldType(typeDef.ofType)); + } + + if (isNonNullType(typeDef)) { + return GraphQLNonNull(extendFieldType(typeDef.ofType)); + } + + return getExtendedType(typeDef); + } +} + +function checkExtensionNode(type, node) { + switch (node.kind) { + case Kind.OBJECT_TYPE_EXTENSION: + if (!isObjectType(type)) { + throw new GraphQLError("Cannot extend non-object type \"".concat(type.name, "\"."), [node]); + } + + break; + + case Kind.INTERFACE_TYPE_EXTENSION: + if (!isInterfaceType(type)) { + throw new GraphQLError("Cannot extend non-interface type \"".concat(type.name, "\"."), [node]); + } + + break; + } +} \ No newline at end of file diff --git a/dist/utilities/findBreakingChanges.js b/dist/utilities/findBreakingChanges.js new file mode 100644 index 0000000000..90d209ac22 --- /dev/null +++ b/dist/utilities/findBreakingChanges.js @@ -0,0 +1,693 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.findBreakingChanges = findBreakingChanges; +exports.findDangerousChanges = findDangerousChanges; +exports.findRemovedTypes = findRemovedTypes; +exports.findTypesThatChangedKind = findTypesThatChangedKind; +exports.findArgChanges = findArgChanges; +exports.findFieldsThatChangedTypeOnObjectOrInterfaceTypes = findFieldsThatChangedTypeOnObjectOrInterfaceTypes; +exports.findFieldsThatChangedTypeOnInputObjectTypes = findFieldsThatChangedTypeOnInputObjectTypes; +exports.findTypesRemovedFromUnions = findTypesRemovedFromUnions; +exports.findTypesAddedToUnions = findTypesAddedToUnions; +exports.findValuesRemovedFromEnums = findValuesRemovedFromEnums; +exports.findValuesAddedToEnums = findValuesAddedToEnums; +exports.findInterfacesRemovedFromObjectTypes = findInterfacesRemovedFromObjectTypes; +exports.findInterfacesAddedToObjectTypes = findInterfacesAddedToObjectTypes; +exports.findRemovedDirectives = findRemovedDirectives; +exports.findRemovedDirectiveArgs = findRemovedDirectiveArgs; +exports.findAddedNonNullDirectiveArgs = findAddedNonNullDirectiveArgs; +exports.findRemovedLocationsForDirective = findRemovedLocationsForDirective; +exports.findRemovedDirectiveLocations = findRemovedDirectiveLocations; +exports.DangerousChangeType = exports.BreakingChangeType = void 0; + +var _definition = require("../type/definition"); + +var _directives = require("../type/directives"); + +var _schema = require("../type/schema"); + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * Copyright (c) 2016-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. + * + * strict + */ +var BreakingChangeType = { + FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND', + FIELD_REMOVED: 'FIELD_REMOVED', + TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND', + TYPE_REMOVED: 'TYPE_REMOVED', + TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION', + VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM', + ARG_REMOVED: 'ARG_REMOVED', + ARG_CHANGED_KIND: 'ARG_CHANGED_KIND', + NON_NULL_ARG_ADDED: 'NON_NULL_ARG_ADDED', + NON_NULL_INPUT_FIELD_ADDED: 'NON_NULL_INPUT_FIELD_ADDED', + INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT', + DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED', + DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED', + DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED', + NON_NULL_DIRECTIVE_ARG_ADDED: 'NON_NULL_DIRECTIVE_ARG_ADDED' +}; +exports.BreakingChangeType = BreakingChangeType; +var DangerousChangeType = { + ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE', + VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM', + INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT', + TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION', + NULLABLE_INPUT_FIELD_ADDED: 'NULLABLE_INPUT_FIELD_ADDED', + NULLABLE_ARG_ADDED: 'NULLABLE_ARG_ADDED' +}; +exports.DangerousChangeType = DangerousChangeType; + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of breaking changes covered by the other functions down below. + */ +function findBreakingChanges(oldSchema, newSchema) { + return findRemovedTypes(oldSchema, newSchema).concat(findTypesThatChangedKind(oldSchema, newSchema), findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema), findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema).breakingChanges, findTypesRemovedFromUnions(oldSchema, newSchema), findValuesRemovedFromEnums(oldSchema, newSchema), findArgChanges(oldSchema, newSchema).breakingChanges, findInterfacesRemovedFromObjectTypes(oldSchema, newSchema), findRemovedDirectives(oldSchema, newSchema), findRemovedDirectiveArgs(oldSchema, newSchema), findAddedNonNullDirectiveArgs(oldSchema, newSchema), findRemovedDirectiveLocations(oldSchema, newSchema)); +} +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of potentially dangerous changes covered by the other functions down below. + */ + + +function findDangerousChanges(oldSchema, newSchema) { + return findArgChanges(oldSchema, newSchema).dangerousChanges.concat(findValuesAddedToEnums(oldSchema, newSchema), findInterfacesAddedToObjectTypes(oldSchema, newSchema), findTypesAddedToUnions(oldSchema, newSchema), findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema).dangerousChanges); +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing an entire type. + */ + + +function findRemovedTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + if (!newTypeMap[typeName]) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_REMOVED, + description: "".concat(typeName, " was removed.") + }); + } + }); + return breakingChanges; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to changing the type of a type. + */ + + +function findTypesThatChangedKind(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + if (!newTypeMap[typeName]) { + return; + } + + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (oldType.constructor !== newType.constructor) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: "".concat(typeName, " changed from ") + "".concat(typeKindName(oldType), " to ").concat(typeKindName(newType), ".") + }); + } + }); + return breakingChanges; +} +/** + * Given two schemas, returns an Array containing descriptions of any + * breaking or dangerous changes in the newSchema related to arguments + * (such as removal or change of type of an argument, or a change in an + * argument's default value). + */ + + +function findArgChanges(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + var dangerousChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!((0, _definition.isObjectType)(oldType) || (0, _definition.isInterfaceType)(oldType)) || !((0, _definition.isObjectType)(newType) || (0, _definition.isInterfaceType)(newType)) || newType.constructor !== oldType.constructor) { + return; + } + + var oldTypeFields = oldType.getFields(); + var newTypeFields = newType.getFields(); + Object.keys(oldTypeFields).forEach(function (fieldName) { + if (!newTypeFields[fieldName]) { + return; + } + + oldTypeFields[fieldName].args.forEach(function (oldArgDef) { + var newArgs = newTypeFields[fieldName].args; + var newArgDef = newArgs.find(function (arg) { + return arg.name === oldArgDef.name; + }); // Arg not present + + if (!newArgDef) { + breakingChanges.push({ + type: BreakingChangeType.ARG_REMOVED, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " was removed") + }); + } else { + var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldArgDef.type, newArgDef.type); + + if (!isSafe) { + breakingChanges.push({ + type: BreakingChangeType.ARG_CHANGED_KIND, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " has changed type from ") + "".concat(oldArgDef.type.toString(), " to ").concat(newArgDef.type.toString()) + }); + } else if (oldArgDef.defaultValue !== undefined && oldArgDef.defaultValue !== newArgDef.defaultValue) { + dangerousChanges.push({ + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " has changed defaultValue") + }); + } + } + }); // Check if a non-null arg was added to the field + + newTypeFields[fieldName].args.forEach(function (newArgDef) { + var oldArgs = oldTypeFields[fieldName].args; + var oldArgDef = oldArgs.find(function (arg) { + return arg.name === newArgDef.name; + }); + + if (!oldArgDef) { + if ((0, _definition.isNonNullType)(newArgDef.type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_ARG_ADDED, + description: "A non-null arg ".concat(newArgDef.name, " on ") + "".concat(newType.name, ".").concat(fieldName, " was added") + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_ARG_ADDED, + description: "A nullable arg ".concat(newArgDef.name, " on ") + "".concat(newType.name, ".").concat(fieldName, " was added") + }); + } + } + }); + }); + }); + return { + breakingChanges: breakingChanges, + dangerousChanges: dangerousChanges + }; +} + +function typeKindName(type) { + if ((0, _definition.isScalarType)(type)) { + return 'a Scalar type'; + } + + if ((0, _definition.isObjectType)(type)) { + return 'an Object type'; + } + + if ((0, _definition.isInterfaceType)(type)) { + return 'an Interface type'; + } + + if ((0, _definition.isUnionType)(type)) { + return 'a Union type'; + } + + if ((0, _definition.isEnumType)(type)) { + return 'an Enum type'; + } + + if ((0, _definition.isInputObjectType)(type)) { + return 'an Input type'; + } + + throw new TypeError('Unknown type ' + type.constructor.name); +} + +function findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!((0, _definition.isObjectType)(oldType) || (0, _definition.isInterfaceType)(oldType)) || !((0, _definition.isObjectType)(newType) || (0, _definition.isInterfaceType)(newType)) || newType.constructor !== oldType.constructor) { + return; + } + + var oldTypeFieldsDef = oldType.getFields(); + var newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(function (fieldName) { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: "".concat(typeName, ".").concat(fieldName, " was removed.") + }); + } else { + var oldFieldType = oldTypeFieldsDef[fieldName].type; + var newFieldType = newTypeFieldsDef[fieldName].type; + var isSafe = isChangeSafeForObjectOrInterfaceField(oldFieldType, newFieldType); + + if (!isSafe) { + var oldFieldTypeString = (0, _definition.isNamedType)(oldFieldType) ? oldFieldType.name : oldFieldType.toString(); + var newFieldTypeString = (0, _definition.isNamedType)(newFieldType) ? newFieldType.name : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: "".concat(typeName, ".").concat(fieldName, " changed type from ") + "".concat(oldFieldTypeString, " to ").concat(newFieldTypeString, ".") + }); + } + } + }); + }); + return breakingChanges; +} + +function findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + var dangerousChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isInputObjectType)(oldType) || !(0, _definition.isInputObjectType)(newType)) { + return; + } + + var oldTypeFieldsDef = oldType.getFields(); + var newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(function (fieldName) { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: "".concat(typeName, ".").concat(fieldName, " was removed.") + }); + } else { + var oldFieldType = oldTypeFieldsDef[fieldName].type; + var newFieldType = newTypeFieldsDef[fieldName].type; + var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldFieldType, newFieldType); + + if (!isSafe) { + var oldFieldTypeString = (0, _definition.isNamedType)(oldFieldType) ? oldFieldType.name : oldFieldType.toString(); + var newFieldTypeString = (0, _definition.isNamedType)(newFieldType) ? newFieldType.name : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: "".concat(typeName, ".").concat(fieldName, " changed type from ") + "".concat(oldFieldTypeString, " to ").concat(newFieldTypeString, ".") + }); + } + } + }); // Check if a field was added to the input object type + + Object.keys(newTypeFieldsDef).forEach(function (fieldName) { + if (!(fieldName in oldTypeFieldsDef)) { + if ((0, _definition.isNonNullType)(newTypeFieldsDef[fieldName].type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED, + description: "A non-null field ".concat(fieldName, " on ") + "input type ".concat(newType.name, " was added.") + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_INPUT_FIELD_ADDED, + description: "A nullable field ".concat(fieldName, " on ") + "input type ".concat(newType.name, " was added.") + }); + } + } + }); + }); + return { + breakingChanges: breakingChanges, + dangerousChanges: dangerousChanges + }; +} + +function isChangeSafeForObjectOrInterfaceField(oldType, newType) { + if ((0, _definition.isNamedType)(oldType)) { + return (// if they're both named types, see if their names are equivalent + (0, _definition.isNamedType)(newType) && oldType.name === newType.name || // moving from nullable to non-null of the same underlying type is safe + (0, _definition.isNonNullType)(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) + ); + } else if ((0, _definition.isListType)(oldType)) { + return (// if they're both lists, make sure the underlying types are compatible + (0, _definition.isListType)(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) || // moving from nullable to non-null of the same underlying type is safe + (0, _definition.isNonNullType)(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) + ); + } else if ((0, _definition.isNonNullType)(oldType)) { + // if they're both non-null, make sure the underlying types are compatible + return (0, _definition.isNonNullType)(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType); + } + + return false; +} + +function isChangeSafeForInputObjectFieldOrFieldArg(oldType, newType) { + if ((0, _definition.isNamedType)(oldType)) { + // if they're both named types, see if their names are equivalent + return (0, _definition.isNamedType)(newType) && oldType.name === newType.name; + } else if ((0, _definition.isListType)(oldType)) { + // if they're both lists, make sure the underlying types are compatible + return (0, _definition.isListType)(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType); + } else if ((0, _definition.isNonNullType)(oldType)) { + return (// if they're both non-null, make sure the underlying types are + // compatible + (0, _definition.isNonNullType)(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) || // moving from non-null to nullable of the same underlying type is safe + !(0, _definition.isNonNullType)(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType) + ); + } + + return false; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing types from a union type. + */ + + +function findTypesRemovedFromUnions(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var typesRemovedFromUnion = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isUnionType)(oldType) || !(0, _definition.isUnionType)(newType)) { + return; + } + + var typeNamesInNewUnion = Object.create(null); + newType.getTypes().forEach(function (type) { + typeNamesInNewUnion[type.name] = true; + }); + oldType.getTypes().forEach(function (type) { + if (!typeNamesInNewUnion[type.name]) { + typesRemovedFromUnion.push({ + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: "".concat(type.name, " was removed from union type ").concat(typeName, ".") + }); + } + }); + }); + return typesRemovedFromUnion; +} +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding types to a union type. + */ + + +function findTypesAddedToUnions(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var typesAddedToUnion = []; + Object.keys(newTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isUnionType)(oldType) || !(0, _definition.isUnionType)(newType)) { + return; + } + + var typeNamesInOldUnion = Object.create(null); + oldType.getTypes().forEach(function (type) { + typeNamesInOldUnion[type.name] = true; + }); + newType.getTypes().forEach(function (type) { + if (!typeNamesInOldUnion[type.name]) { + typesAddedToUnion.push({ + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: "".concat(type.name, " was added to union type ").concat(typeName, ".") + }); + } + }); + }); + return typesAddedToUnion; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing values from an enum type. + */ + + +function findValuesRemovedFromEnums(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var valuesRemovedFromEnums = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isEnumType)(oldType) || !(0, _definition.isEnumType)(newType)) { + return; + } + + var valuesInNewEnum = Object.create(null); + newType.getValues().forEach(function (value) { + valuesInNewEnum[value.name] = true; + }); + oldType.getValues().forEach(function (value) { + if (!valuesInNewEnum[value.name]) { + valuesRemovedFromEnums.push({ + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: "".concat(value.name, " was removed from enum type ").concat(typeName, ".") + }); + } + }); + }); + return valuesRemovedFromEnums; +} +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding values to an enum type. + */ + + +function findValuesAddedToEnums(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var valuesAddedToEnums = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isEnumType)(oldType) || !(0, _definition.isEnumType)(newType)) { + return; + } + + var valuesInOldEnum = Object.create(null); + oldType.getValues().forEach(function (value) { + valuesInOldEnum[value.name] = true; + }); + newType.getValues().forEach(function (value) { + if (!valuesInOldEnum[value.name]) { + valuesAddedToEnums.push({ + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: "".concat(value.name, " was added to enum type ").concat(typeName, ".") + }); + } + }); + }); + return valuesAddedToEnums; +} + +function findInterfacesRemovedFromObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isObjectType)(oldType) || !(0, _definition.isObjectType)(newType)) { + return; + } + + var oldInterfaces = oldType.getInterfaces(); + var newInterfaces = newType.getInterfaces(); + oldInterfaces.forEach(function (oldInterface) { + if (!newInterfaces.some(function (int) { + return int.name === oldInterface.name; + })) { + breakingChanges.push({ + type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT, + description: "".concat(typeName, " no longer implements interface ") + "".concat(oldInterface.name, ".") + }); + } + }); + }); + return breakingChanges; +} + +function findInterfacesAddedToObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var interfacesAddedToObjectTypes = []; + Object.keys(newTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(0, _definition.isObjectType)(oldType) || !(0, _definition.isObjectType)(newType)) { + return; + } + + var oldInterfaces = oldType.getInterfaces(); + var newInterfaces = newType.getInterfaces(); + newInterfaces.forEach(function (newInterface) { + if (!oldInterfaces.some(function (int) { + return int.name === newInterface.name; + })) { + interfacesAddedToObjectTypes.push({ + type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT, + description: "".concat(newInterface.name, " added to interfaces implemented ") + "by ".concat(typeName, ".") + }); + } + }); + }); + return interfacesAddedToObjectTypes; +} + +function findRemovedDirectives(oldSchema, newSchema) { + var removedDirectives = []; + var newSchemaDirectiveMap = getDirectiveMapForSchema(newSchema); + oldSchema.getDirectives().forEach(function (directive) { + if (!newSchemaDirectiveMap[directive.name]) { + removedDirectives.push({ + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: "".concat(directive.name, " was removed") + }); + } + }); + return removedDirectives; +} + +function findRemovedArgsForDirective(oldDirective, newDirective) { + var removedArgs = []; + var newArgMap = getArgumentMapForDirective(newDirective); + oldDirective.args.forEach(function (arg) { + if (!newArgMap[arg.name]) { + removedArgs.push(arg); + } + }); + return removedArgs; +} + +function findRemovedDirectiveArgs(oldSchema, newSchema) { + var removedDirectiveArgs = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findRemovedArgsForDirective(oldDirective, newDirective).forEach(function (arg) { + removedDirectiveArgs.push({ + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: "".concat(arg.name, " was removed from ").concat(newDirective.name) + }); + }); + }); + return removedDirectiveArgs; +} + +function findAddedArgsForDirective(oldDirective, newDirective) { + var addedArgs = []; + var oldArgMap = getArgumentMapForDirective(oldDirective); + newDirective.args.forEach(function (arg) { + if (!oldArgMap[arg.name]) { + addedArgs.push(arg); + } + }); + return addedArgs; +} + +function findAddedNonNullDirectiveArgs(oldSchema, newSchema) { + var addedNonNullableArgs = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findAddedArgsForDirective(oldDirective, newDirective).forEach(function (arg) { + if (!(0, _definition.isNonNullType)(arg.type)) { + return; + } + + addedNonNullableArgs.push({ + type: BreakingChangeType.NON_NULL_DIRECTIVE_ARG_ADDED, + description: "A non-null arg ".concat(arg.name, " on directive ") + "".concat(newDirective.name, " was added") + }); + }); + }); + return addedNonNullableArgs; +} + +function findRemovedLocationsForDirective(oldDirective, newDirective) { + var removedLocations = []; + var newLocationSet = new Set(newDirective.locations); + oldDirective.locations.forEach(function (oldLocation) { + if (!newLocationSet.has(oldLocation)) { + removedLocations.push(oldLocation); + } + }); + return removedLocations; +} + +function findRemovedDirectiveLocations(oldSchema, newSchema) { + var removedLocations = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findRemovedLocationsForDirective(oldDirective, newDirective).forEach(function (location) { + removedLocations.push({ + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: "".concat(location, " was removed from ").concat(newDirective.name) + }); + }); + }); + return removedLocations; +} + +function getDirectiveMapForSchema(schema) { + return (0, _keyMap.default)(schema.getDirectives(), function (dir) { + return dir.name; + }); +} + +function getArgumentMapForDirective(directive) { + return (0, _keyMap.default)(directive.args, function (arg) { + return arg.name; + }); +} \ No newline at end of file diff --git a/dist/utilities/findBreakingChanges.js.flow b/dist/utilities/findBreakingChanges.js.flow new file mode 100644 index 0000000000..f4b65fdda4 --- /dev/null +++ b/dist/utilities/findBreakingChanges.js.flow @@ -0,0 +1,851 @@ +/** + * Copyright (c) 2016-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. + * + * @flow strict + */ + +import { + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, + isNonNullType, + isListType, + isNamedType, +} from '../type/definition'; + +import type { + GraphQLNamedType, + GraphQLFieldMap, + GraphQLType, + GraphQLArgument, +} from '../type/definition'; + +import { GraphQLDirective } from '../type/directives'; +import { GraphQLSchema } from '../type/schema'; +import keyMap from '../jsutils/keyMap'; + +import type { ObjMap } from '../jsutils/ObjMap'; +import type { DirectiveLocationEnum } from '../language/directiveLocation'; + +export const BreakingChangeType = { + FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND', + FIELD_REMOVED: 'FIELD_REMOVED', + TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND', + TYPE_REMOVED: 'TYPE_REMOVED', + TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION', + VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM', + ARG_REMOVED: 'ARG_REMOVED', + ARG_CHANGED_KIND: 'ARG_CHANGED_KIND', + NON_NULL_ARG_ADDED: 'NON_NULL_ARG_ADDED', + NON_NULL_INPUT_FIELD_ADDED: 'NON_NULL_INPUT_FIELD_ADDED', + INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT', + DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED', + DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED', + DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED', + NON_NULL_DIRECTIVE_ARG_ADDED: 'NON_NULL_DIRECTIVE_ARG_ADDED', +}; + +export const DangerousChangeType = { + ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE', + VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM', + INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT', + TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION', + NULLABLE_INPUT_FIELD_ADDED: 'NULLABLE_INPUT_FIELD_ADDED', + NULLABLE_ARG_ADDED: 'NULLABLE_ARG_ADDED', +}; + +export type BreakingChange = { + type: $Keys, + description: string, +}; + +export type DangerousChange = { + type: $Keys, + description: string, +}; + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of breaking changes covered by the other functions down below. + */ +export function findBreakingChanges( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + return [ + ...findRemovedTypes(oldSchema, newSchema), + ...findTypesThatChangedKind(oldSchema, newSchema), + ...findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema), + ...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema) + .breakingChanges, + ...findTypesRemovedFromUnions(oldSchema, newSchema), + ...findValuesRemovedFromEnums(oldSchema, newSchema), + ...findArgChanges(oldSchema, newSchema).breakingChanges, + ...findInterfacesRemovedFromObjectTypes(oldSchema, newSchema), + ...findRemovedDirectives(oldSchema, newSchema), + ...findRemovedDirectiveArgs(oldSchema, newSchema), + ...findAddedNonNullDirectiveArgs(oldSchema, newSchema), + ...findRemovedDirectiveLocations(oldSchema, newSchema), + ]; +} + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of potentially dangerous changes covered by the other functions down below. + */ +export function findDangerousChanges( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + return [ + ...findArgChanges(oldSchema, newSchema).dangerousChanges, + ...findValuesAddedToEnums(oldSchema, newSchema), + ...findInterfacesAddedToObjectTypes(oldSchema, newSchema), + ...findTypesAddedToUnions(oldSchema, newSchema), + ...findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema) + .dangerousChanges, + ]; +} + +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing an entire type. + */ +export function findRemovedTypes( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const breakingChanges = []; + Object.keys(oldTypeMap).forEach(typeName => { + if (!newTypeMap[typeName]) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_REMOVED, + description: `${typeName} was removed.`, + }); + } + }); + return breakingChanges; +} + +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to changing the type of a type. + */ +export function findTypesThatChangedKind( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const breakingChanges = []; + Object.keys(oldTypeMap).forEach(typeName => { + if (!newTypeMap[typeName]) { + return; + } + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (oldType.constructor !== newType.constructor) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: + `${typeName} changed from ` + + `${typeKindName(oldType)} to ${typeKindName(newType)}.`, + }); + } + }); + return breakingChanges; +} + +/** + * Given two schemas, returns an Array containing descriptions of any + * breaking or dangerous changes in the newSchema related to arguments + * (such as removal or change of type of an argument, or a change in an + * argument's default value). + */ +export function findArgChanges( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): { + breakingChanges: Array, + dangerousChanges: Array, +} { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const breakingChanges = []; + const dangerousChanges = []; + + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if ( + !(isObjectType(oldType) || isInterfaceType(oldType)) || + !(isObjectType(newType) || isInterfaceType(newType)) || + newType.constructor !== oldType.constructor + ) { + return; + } + + const oldTypeFields: GraphQLFieldMap<*, *> = oldType.getFields(); + const newTypeFields: GraphQLFieldMap<*, *> = newType.getFields(); + + Object.keys(oldTypeFields).forEach(fieldName => { + if (!newTypeFields[fieldName]) { + return; + } + + oldTypeFields[fieldName].args.forEach(oldArgDef => { + const newArgs = newTypeFields[fieldName].args; + const newArgDef = newArgs.find(arg => arg.name === oldArgDef.name); + + // Arg not present + if (!newArgDef) { + breakingChanges.push({ + type: BreakingChangeType.ARG_REMOVED, + description: + `${oldType.name}.${fieldName} arg ` + + `${oldArgDef.name} was removed`, + }); + } else { + const isSafe = isChangeSafeForInputObjectFieldOrFieldArg( + oldArgDef.type, + newArgDef.type, + ); + if (!isSafe) { + breakingChanges.push({ + type: BreakingChangeType.ARG_CHANGED_KIND, + description: + `${oldType.name}.${fieldName} arg ` + + `${oldArgDef.name} has changed type from ` + + `${oldArgDef.type.toString()} to ${newArgDef.type.toString()}`, + }); + } else if ( + oldArgDef.defaultValue !== undefined && + oldArgDef.defaultValue !== newArgDef.defaultValue + ) { + dangerousChanges.push({ + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: + `${oldType.name}.${fieldName} arg ` + + `${oldArgDef.name} has changed defaultValue`, + }); + } + } + }); + // Check if a non-null arg was added to the field + newTypeFields[fieldName].args.forEach(newArgDef => { + const oldArgs = oldTypeFields[fieldName].args; + const oldArgDef = oldArgs.find(arg => arg.name === newArgDef.name); + if (!oldArgDef) { + if (isNonNullType(newArgDef.type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_ARG_ADDED, + description: + `A non-null arg ${newArgDef.name} on ` + + `${newType.name}.${fieldName} was added`, + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_ARG_ADDED, + description: + `A nullable arg ${newArgDef.name} on ` + + `${newType.name}.${fieldName} was added`, + }); + } + } + }); + }); + }); + + return { + breakingChanges, + dangerousChanges, + }; +} + +function typeKindName(type: GraphQLNamedType): string { + if (isScalarType(type)) { + return 'a Scalar type'; + } + if (isObjectType(type)) { + return 'an Object type'; + } + if (isInterfaceType(type)) { + return 'an Interface type'; + } + if (isUnionType(type)) { + return 'a Union type'; + } + if (isEnumType(type)) { + return 'an Enum type'; + } + if (isInputObjectType(type)) { + return 'an Input type'; + } + throw new TypeError('Unknown type ' + type.constructor.name); +} + +export function findFieldsThatChangedTypeOnObjectOrInterfaceTypes( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const breakingChanges = []; + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if ( + !(isObjectType(oldType) || isInterfaceType(oldType)) || + !(isObjectType(newType) || isInterfaceType(newType)) || + newType.constructor !== oldType.constructor + ) { + return; + } + + const oldTypeFieldsDef = oldType.getFields(); + const newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(fieldName => { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: `${typeName}.${fieldName} was removed.`, + }); + } else { + const oldFieldType = oldTypeFieldsDef[fieldName].type; + const newFieldType = newTypeFieldsDef[fieldName].type; + const isSafe = isChangeSafeForObjectOrInterfaceField( + oldFieldType, + newFieldType, + ); + if (!isSafe) { + const oldFieldTypeString = isNamedType(oldFieldType) + ? oldFieldType.name + : oldFieldType.toString(); + const newFieldTypeString = isNamedType(newFieldType) + ? newFieldType.name + : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: + `${typeName}.${fieldName} changed type from ` + + `${oldFieldTypeString} to ${newFieldTypeString}.`, + }); + } + } + }); + }); + return breakingChanges; +} + +export function findFieldsThatChangedTypeOnInputObjectTypes( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): { + breakingChanges: Array, + dangerousChanges: Array, +} { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const breakingChanges = []; + const dangerousChanges = []; + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isInputObjectType(oldType) || !isInputObjectType(newType)) { + return; + } + + const oldTypeFieldsDef = oldType.getFields(); + const newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(fieldName => { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: `${typeName}.${fieldName} was removed.`, + }); + } else { + const oldFieldType = oldTypeFieldsDef[fieldName].type; + const newFieldType = newTypeFieldsDef[fieldName].type; + + const isSafe = isChangeSafeForInputObjectFieldOrFieldArg( + oldFieldType, + newFieldType, + ); + if (!isSafe) { + const oldFieldTypeString = isNamedType(oldFieldType) + ? oldFieldType.name + : oldFieldType.toString(); + const newFieldTypeString = isNamedType(newFieldType) + ? newFieldType.name + : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: + `${typeName}.${fieldName} changed type from ` + + `${oldFieldTypeString} to ${newFieldTypeString}.`, + }); + } + } + }); + // Check if a field was added to the input object type + Object.keys(newTypeFieldsDef).forEach(fieldName => { + if (!(fieldName in oldTypeFieldsDef)) { + if (isNonNullType(newTypeFieldsDef[fieldName].type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED, + description: + `A non-null field ${fieldName} on ` + + `input type ${newType.name} was added.`, + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_INPUT_FIELD_ADDED, + description: + `A nullable field ${fieldName} on ` + + `input type ${newType.name} was added.`, + }); + } + } + }); + }); + return { + breakingChanges, + dangerousChanges, + }; +} + +function isChangeSafeForObjectOrInterfaceField( + oldType: GraphQLType, + newType: GraphQLType, +): boolean { + if (isNamedType(oldType)) { + return ( + // if they're both named types, see if their names are equivalent + (isNamedType(newType) && oldType.name === newType.name) || + // moving from nullable to non-null of the same underlying type is safe + (isNonNullType(newType) && + isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)) + ); + } else if (isListType(oldType)) { + return ( + // if they're both lists, make sure the underlying types are compatible + (isListType(newType) && + isChangeSafeForObjectOrInterfaceField( + oldType.ofType, + newType.ofType, + )) || + // moving from nullable to non-null of the same underlying type is safe + (isNonNullType(newType) && + isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType)) + ); + } else if (isNonNullType(oldType)) { + // if they're both non-null, make sure the underlying types are compatible + return ( + isNonNullType(newType) && + isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) + ); + } + return false; +} + +function isChangeSafeForInputObjectFieldOrFieldArg( + oldType: GraphQLType, + newType: GraphQLType, +): boolean { + if (isNamedType(oldType)) { + // if they're both named types, see if their names are equivalent + return isNamedType(newType) && oldType.name === newType.name; + } else if (isListType(oldType)) { + // if they're both lists, make sure the underlying types are compatible + return ( + isListType(newType) && + isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) + ); + } else if (isNonNullType(oldType)) { + return ( + // if they're both non-null, make sure the underlying types are + // compatible + (isNonNullType(newType) && + isChangeSafeForInputObjectFieldOrFieldArg( + oldType.ofType, + newType.ofType, + )) || + // moving from non-null to nullable of the same underlying type is safe + (!isNonNullType(newType) && + isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType)) + ); + } + return false; +} + +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing types from a union type. + */ +export function findTypesRemovedFromUnions( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const typesRemovedFromUnion = []; + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isUnionType(oldType) || !isUnionType(newType)) { + return; + } + const typeNamesInNewUnion = Object.create(null); + newType.getTypes().forEach(type => { + typeNamesInNewUnion[type.name] = true; + }); + oldType.getTypes().forEach(type => { + if (!typeNamesInNewUnion[type.name]) { + typesRemovedFromUnion.push({ + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: `${type.name} was removed from union type ${typeName}.`, + }); + } + }); + }); + return typesRemovedFromUnion; +} + +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding types to a union type. + */ +export function findTypesAddedToUnions( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const typesAddedToUnion = []; + Object.keys(newTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isUnionType(oldType) || !isUnionType(newType)) { + return; + } + const typeNamesInOldUnion = Object.create(null); + oldType.getTypes().forEach(type => { + typeNamesInOldUnion[type.name] = true; + }); + newType.getTypes().forEach(type => { + if (!typeNamesInOldUnion[type.name]) { + typesAddedToUnion.push({ + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: `${type.name} was added to union type ${typeName}.`, + }); + } + }); + }); + return typesAddedToUnion; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing values from an enum type. + */ +export function findValuesRemovedFromEnums( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const valuesRemovedFromEnums = []; + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isEnumType(oldType) || !isEnumType(newType)) { + return; + } + const valuesInNewEnum = Object.create(null); + newType.getValues().forEach(value => { + valuesInNewEnum[value.name] = true; + }); + oldType.getValues().forEach(value => { + if (!valuesInNewEnum[value.name]) { + valuesRemovedFromEnums.push({ + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: `${value.name} was removed from enum type ${typeName}.`, + }); + } + }); + }); + return valuesRemovedFromEnums; +} + +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding values to an enum type. + */ +export function findValuesAddedToEnums( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + + const valuesAddedToEnums = []; + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isEnumType(oldType) || !isEnumType(newType)) { + return; + } + + const valuesInOldEnum = Object.create(null); + oldType.getValues().forEach(value => { + valuesInOldEnum[value.name] = true; + }); + newType.getValues().forEach(value => { + if (!valuesInOldEnum[value.name]) { + valuesAddedToEnums.push({ + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: `${value.name} was added to enum type ${typeName}.`, + }); + } + }); + }); + return valuesAddedToEnums; +} + +export function findInterfacesRemovedFromObjectTypes( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + const breakingChanges = []; + + Object.keys(oldTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isObjectType(oldType) || !isObjectType(newType)) { + return; + } + + const oldInterfaces = oldType.getInterfaces(); + const newInterfaces = newType.getInterfaces(); + oldInterfaces.forEach(oldInterface => { + if (!newInterfaces.some(int => int.name === oldInterface.name)) { + breakingChanges.push({ + type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT, + description: + `${typeName} no longer implements interface ` + + `${oldInterface.name}.`, + }); + } + }); + }); + return breakingChanges; +} + +export function findInterfacesAddedToObjectTypes( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const oldTypeMap = oldSchema.getTypeMap(); + const newTypeMap = newSchema.getTypeMap(); + const interfacesAddedToObjectTypes = []; + + Object.keys(newTypeMap).forEach(typeName => { + const oldType = oldTypeMap[typeName]; + const newType = newTypeMap[typeName]; + if (!isObjectType(oldType) || !isObjectType(newType)) { + return; + } + + const oldInterfaces = oldType.getInterfaces(); + const newInterfaces = newType.getInterfaces(); + newInterfaces.forEach(newInterface => { + if (!oldInterfaces.some(int => int.name === newInterface.name)) { + interfacesAddedToObjectTypes.push({ + type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT, + description: + `${newInterface.name} added to interfaces implemented ` + + `by ${typeName}.`, + }); + } + }); + }); + return interfacesAddedToObjectTypes; +} + +export function findRemovedDirectives( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const removedDirectives = []; + + const newSchemaDirectiveMap = getDirectiveMapForSchema(newSchema); + oldSchema.getDirectives().forEach(directive => { + if (!newSchemaDirectiveMap[directive.name]) { + removedDirectives.push({ + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: `${directive.name} was removed`, + }); + } + }); + + return removedDirectives; +} + +function findRemovedArgsForDirective( + oldDirective: GraphQLDirective, + newDirective: GraphQLDirective, +): Array { + const removedArgs = []; + const newArgMap = getArgumentMapForDirective(newDirective); + + oldDirective.args.forEach(arg => { + if (!newArgMap[arg.name]) { + removedArgs.push(arg); + } + }); + + return removedArgs; +} + +export function findRemovedDirectiveArgs( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const removedDirectiveArgs = []; + const oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + + newSchema.getDirectives().forEach(newDirective => { + const oldDirective = oldSchemaDirectiveMap[newDirective.name]; + if (!oldDirective) { + return; + } + + findRemovedArgsForDirective(oldDirective, newDirective).forEach(arg => { + removedDirectiveArgs.push({ + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: `${arg.name} was removed from ${newDirective.name}`, + }); + }); + }); + + return removedDirectiveArgs; +} + +function findAddedArgsForDirective( + oldDirective: GraphQLDirective, + newDirective: GraphQLDirective, +): Array { + const addedArgs = []; + const oldArgMap = getArgumentMapForDirective(oldDirective); + + newDirective.args.forEach(arg => { + if (!oldArgMap[arg.name]) { + addedArgs.push(arg); + } + }); + + return addedArgs; +} + +export function findAddedNonNullDirectiveArgs( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const addedNonNullableArgs = []; + const oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + + newSchema.getDirectives().forEach(newDirective => { + const oldDirective = oldSchemaDirectiveMap[newDirective.name]; + if (!oldDirective) { + return; + } + + findAddedArgsForDirective(oldDirective, newDirective).forEach(arg => { + if (!isNonNullType(arg.type)) { + return; + } + + addedNonNullableArgs.push({ + type: BreakingChangeType.NON_NULL_DIRECTIVE_ARG_ADDED, + description: + `A non-null arg ${arg.name} on directive ` + + `${newDirective.name} was added`, + }); + }); + }); + + return addedNonNullableArgs; +} + +export function findRemovedLocationsForDirective( + oldDirective: GraphQLDirective, + newDirective: GraphQLDirective, +): Array { + const removedLocations = []; + const newLocationSet = new Set(newDirective.locations); + + oldDirective.locations.forEach(oldLocation => { + if (!newLocationSet.has(oldLocation)) { + removedLocations.push(oldLocation); + } + }); + + return removedLocations; +} + +export function findRemovedDirectiveLocations( + oldSchema: GraphQLSchema, + newSchema: GraphQLSchema, +): Array { + const removedLocations = []; + const oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + + newSchema.getDirectives().forEach(newDirective => { + const oldDirective = oldSchemaDirectiveMap[newDirective.name]; + if (!oldDirective) { + return; + } + + findRemovedLocationsForDirective(oldDirective, newDirective).forEach( + location => { + removedLocations.push({ + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: `${location} was removed from ${newDirective.name}`, + }); + }, + ); + }); + + return removedLocations; +} + +function getDirectiveMapForSchema( + schema: GraphQLSchema, +): ObjMap { + return keyMap(schema.getDirectives(), dir => dir.name); +} + +function getArgumentMapForDirective( + directive: GraphQLDirective, +): ObjMap { + return keyMap(directive.args, arg => arg.name); +} diff --git a/dist/utilities/findBreakingChanges.mjs b/dist/utilities/findBreakingChanges.mjs new file mode 100644 index 0000000000..c37b690f4c --- /dev/null +++ b/dist/utilities/findBreakingChanges.mjs @@ -0,0 +1,647 @@ +/** + * Copyright (c) 2016-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. + * + * strict + */ +import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType, isNonNullType, isListType, isNamedType } from '../type/definition'; +import { GraphQLDirective } from '../type/directives'; +import { GraphQLSchema } from '../type/schema'; +import keyMap from '../jsutils/keyMap'; +export var BreakingChangeType = { + FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND', + FIELD_REMOVED: 'FIELD_REMOVED', + TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND', + TYPE_REMOVED: 'TYPE_REMOVED', + TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION', + VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM', + ARG_REMOVED: 'ARG_REMOVED', + ARG_CHANGED_KIND: 'ARG_CHANGED_KIND', + NON_NULL_ARG_ADDED: 'NON_NULL_ARG_ADDED', + NON_NULL_INPUT_FIELD_ADDED: 'NON_NULL_INPUT_FIELD_ADDED', + INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT', + DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED', + DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED', + DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED', + NON_NULL_DIRECTIVE_ARG_ADDED: 'NON_NULL_DIRECTIVE_ARG_ADDED' +}; +export var DangerousChangeType = { + ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE', + VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM', + INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT', + TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION', + NULLABLE_INPUT_FIELD_ADDED: 'NULLABLE_INPUT_FIELD_ADDED', + NULLABLE_ARG_ADDED: 'NULLABLE_ARG_ADDED' +}; + +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of breaking changes covered by the other functions down below. + */ +export function findBreakingChanges(oldSchema, newSchema) { + return findRemovedTypes(oldSchema, newSchema).concat(findTypesThatChangedKind(oldSchema, newSchema), findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema), findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema).breakingChanges, findTypesRemovedFromUnions(oldSchema, newSchema), findValuesRemovedFromEnums(oldSchema, newSchema), findArgChanges(oldSchema, newSchema).breakingChanges, findInterfacesRemovedFromObjectTypes(oldSchema, newSchema), findRemovedDirectives(oldSchema, newSchema), findRemovedDirectiveArgs(oldSchema, newSchema), findAddedNonNullDirectiveArgs(oldSchema, newSchema), findRemovedDirectiveLocations(oldSchema, newSchema)); +} +/** + * Given two schemas, returns an Array containing descriptions of all the types + * of potentially dangerous changes covered by the other functions down below. + */ + +export function findDangerousChanges(oldSchema, newSchema) { + return findArgChanges(oldSchema, newSchema).dangerousChanges.concat(findValuesAddedToEnums(oldSchema, newSchema), findInterfacesAddedToObjectTypes(oldSchema, newSchema), findTypesAddedToUnions(oldSchema, newSchema), findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema).dangerousChanges); +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing an entire type. + */ + +export function findRemovedTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + if (!newTypeMap[typeName]) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_REMOVED, + description: "".concat(typeName, " was removed.") + }); + } + }); + return breakingChanges; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to changing the type of a type. + */ + +export function findTypesThatChangedKind(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + if (!newTypeMap[typeName]) { + return; + } + + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (oldType.constructor !== newType.constructor) { + breakingChanges.push({ + type: BreakingChangeType.TYPE_CHANGED_KIND, + description: "".concat(typeName, " changed from ") + "".concat(typeKindName(oldType), " to ").concat(typeKindName(newType), ".") + }); + } + }); + return breakingChanges; +} +/** + * Given two schemas, returns an Array containing descriptions of any + * breaking or dangerous changes in the newSchema related to arguments + * (such as removal or change of type of an argument, or a change in an + * argument's default value). + */ + +export function findArgChanges(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + var dangerousChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(isObjectType(oldType) || isInterfaceType(oldType)) || !(isObjectType(newType) || isInterfaceType(newType)) || newType.constructor !== oldType.constructor) { + return; + } + + var oldTypeFields = oldType.getFields(); + var newTypeFields = newType.getFields(); + Object.keys(oldTypeFields).forEach(function (fieldName) { + if (!newTypeFields[fieldName]) { + return; + } + + oldTypeFields[fieldName].args.forEach(function (oldArgDef) { + var newArgs = newTypeFields[fieldName].args; + var newArgDef = newArgs.find(function (arg) { + return arg.name === oldArgDef.name; + }); // Arg not present + + if (!newArgDef) { + breakingChanges.push({ + type: BreakingChangeType.ARG_REMOVED, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " was removed") + }); + } else { + var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldArgDef.type, newArgDef.type); + + if (!isSafe) { + breakingChanges.push({ + type: BreakingChangeType.ARG_CHANGED_KIND, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " has changed type from ") + "".concat(oldArgDef.type.toString(), " to ").concat(newArgDef.type.toString()) + }); + } else if (oldArgDef.defaultValue !== undefined && oldArgDef.defaultValue !== newArgDef.defaultValue) { + dangerousChanges.push({ + type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE, + description: "".concat(oldType.name, ".").concat(fieldName, " arg ") + "".concat(oldArgDef.name, " has changed defaultValue") + }); + } + } + }); // Check if a non-null arg was added to the field + + newTypeFields[fieldName].args.forEach(function (newArgDef) { + var oldArgs = oldTypeFields[fieldName].args; + var oldArgDef = oldArgs.find(function (arg) { + return arg.name === newArgDef.name; + }); + + if (!oldArgDef) { + if (isNonNullType(newArgDef.type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_ARG_ADDED, + description: "A non-null arg ".concat(newArgDef.name, " on ") + "".concat(newType.name, ".").concat(fieldName, " was added") + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_ARG_ADDED, + description: "A nullable arg ".concat(newArgDef.name, " on ") + "".concat(newType.name, ".").concat(fieldName, " was added") + }); + } + } + }); + }); + }); + return { + breakingChanges: breakingChanges, + dangerousChanges: dangerousChanges + }; +} + +function typeKindName(type) { + if (isScalarType(type)) { + return 'a Scalar type'; + } + + if (isObjectType(type)) { + return 'an Object type'; + } + + if (isInterfaceType(type)) { + return 'an Interface type'; + } + + if (isUnionType(type)) { + return 'a Union type'; + } + + if (isEnumType(type)) { + return 'an Enum type'; + } + + if (isInputObjectType(type)) { + return 'an Input type'; + } + + throw new TypeError('Unknown type ' + type.constructor.name); +} + +export function findFieldsThatChangedTypeOnObjectOrInterfaceTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!(isObjectType(oldType) || isInterfaceType(oldType)) || !(isObjectType(newType) || isInterfaceType(newType)) || newType.constructor !== oldType.constructor) { + return; + } + + var oldTypeFieldsDef = oldType.getFields(); + var newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(function (fieldName) { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: "".concat(typeName, ".").concat(fieldName, " was removed.") + }); + } else { + var oldFieldType = oldTypeFieldsDef[fieldName].type; + var newFieldType = newTypeFieldsDef[fieldName].type; + var isSafe = isChangeSafeForObjectOrInterfaceField(oldFieldType, newFieldType); + + if (!isSafe) { + var oldFieldTypeString = isNamedType(oldFieldType) ? oldFieldType.name : oldFieldType.toString(); + var newFieldTypeString = isNamedType(newFieldType) ? newFieldType.name : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: "".concat(typeName, ".").concat(fieldName, " changed type from ") + "".concat(oldFieldTypeString, " to ").concat(newFieldTypeString, ".") + }); + } + } + }); + }); + return breakingChanges; +} +export function findFieldsThatChangedTypeOnInputObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + var dangerousChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isInputObjectType(oldType) || !isInputObjectType(newType)) { + return; + } + + var oldTypeFieldsDef = oldType.getFields(); + var newTypeFieldsDef = newType.getFields(); + Object.keys(oldTypeFieldsDef).forEach(function (fieldName) { + // Check if the field is missing on the type in the new schema. + if (!(fieldName in newTypeFieldsDef)) { + breakingChanges.push({ + type: BreakingChangeType.FIELD_REMOVED, + description: "".concat(typeName, ".").concat(fieldName, " was removed.") + }); + } else { + var oldFieldType = oldTypeFieldsDef[fieldName].type; + var newFieldType = newTypeFieldsDef[fieldName].type; + var isSafe = isChangeSafeForInputObjectFieldOrFieldArg(oldFieldType, newFieldType); + + if (!isSafe) { + var oldFieldTypeString = isNamedType(oldFieldType) ? oldFieldType.name : oldFieldType.toString(); + var newFieldTypeString = isNamedType(newFieldType) ? newFieldType.name : newFieldType.toString(); + breakingChanges.push({ + type: BreakingChangeType.FIELD_CHANGED_KIND, + description: "".concat(typeName, ".").concat(fieldName, " changed type from ") + "".concat(oldFieldTypeString, " to ").concat(newFieldTypeString, ".") + }); + } + } + }); // Check if a field was added to the input object type + + Object.keys(newTypeFieldsDef).forEach(function (fieldName) { + if (!(fieldName in oldTypeFieldsDef)) { + if (isNonNullType(newTypeFieldsDef[fieldName].type)) { + breakingChanges.push({ + type: BreakingChangeType.NON_NULL_INPUT_FIELD_ADDED, + description: "A non-null field ".concat(fieldName, " on ") + "input type ".concat(newType.name, " was added.") + }); + } else { + dangerousChanges.push({ + type: DangerousChangeType.NULLABLE_INPUT_FIELD_ADDED, + description: "A nullable field ".concat(fieldName, " on ") + "input type ".concat(newType.name, " was added.") + }); + } + } + }); + }); + return { + breakingChanges: breakingChanges, + dangerousChanges: dangerousChanges + }; +} + +function isChangeSafeForObjectOrInterfaceField(oldType, newType) { + if (isNamedType(oldType)) { + return (// if they're both named types, see if their names are equivalent + isNamedType(newType) && oldType.name === newType.name || // moving from nullable to non-null of the same underlying type is safe + isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) + ); + } else if (isListType(oldType)) { + return (// if they're both lists, make sure the underlying types are compatible + isListType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType) || // moving from nullable to non-null of the same underlying type is safe + isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType) + ); + } else if (isNonNullType(oldType)) { + // if they're both non-null, make sure the underlying types are compatible + return isNonNullType(newType) && isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType); + } + + return false; +} + +function isChangeSafeForInputObjectFieldOrFieldArg(oldType, newType) { + if (isNamedType(oldType)) { + // if they're both named types, see if their names are equivalent + return isNamedType(newType) && oldType.name === newType.name; + } else if (isListType(oldType)) { + // if they're both lists, make sure the underlying types are compatible + return isListType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType); + } else if (isNonNullType(oldType)) { + return (// if they're both non-null, make sure the underlying types are + // compatible + isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType) || // moving from non-null to nullable of the same underlying type is safe + !isNonNullType(newType) && isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType) + ); + } + + return false; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing types from a union type. + */ + + +export function findTypesRemovedFromUnions(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var typesRemovedFromUnion = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isUnionType(oldType) || !isUnionType(newType)) { + return; + } + + var typeNamesInNewUnion = Object.create(null); + newType.getTypes().forEach(function (type) { + typeNamesInNewUnion[type.name] = true; + }); + oldType.getTypes().forEach(function (type) { + if (!typeNamesInNewUnion[type.name]) { + typesRemovedFromUnion.push({ + type: BreakingChangeType.TYPE_REMOVED_FROM_UNION, + description: "".concat(type.name, " was removed from union type ").concat(typeName, ".") + }); + } + }); + }); + return typesRemovedFromUnion; +} +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding types to a union type. + */ + +export function findTypesAddedToUnions(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var typesAddedToUnion = []; + Object.keys(newTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isUnionType(oldType) || !isUnionType(newType)) { + return; + } + + var typeNamesInOldUnion = Object.create(null); + oldType.getTypes().forEach(function (type) { + typeNamesInOldUnion[type.name] = true; + }); + newType.getTypes().forEach(function (type) { + if (!typeNamesInOldUnion[type.name]) { + typesAddedToUnion.push({ + type: DangerousChangeType.TYPE_ADDED_TO_UNION, + description: "".concat(type.name, " was added to union type ").concat(typeName, ".") + }); + } + }); + }); + return typesAddedToUnion; +} +/** + * Given two schemas, returns an Array containing descriptions of any breaking + * changes in the newSchema related to removing values from an enum type. + */ + +export function findValuesRemovedFromEnums(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var valuesRemovedFromEnums = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isEnumType(oldType) || !isEnumType(newType)) { + return; + } + + var valuesInNewEnum = Object.create(null); + newType.getValues().forEach(function (value) { + valuesInNewEnum[value.name] = true; + }); + oldType.getValues().forEach(function (value) { + if (!valuesInNewEnum[value.name]) { + valuesRemovedFromEnums.push({ + type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM, + description: "".concat(value.name, " was removed from enum type ").concat(typeName, ".") + }); + } + }); + }); + return valuesRemovedFromEnums; +} +/** + * Given two schemas, returns an Array containing descriptions of any dangerous + * changes in the newSchema related to adding values to an enum type. + */ + +export function findValuesAddedToEnums(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var valuesAddedToEnums = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isEnumType(oldType) || !isEnumType(newType)) { + return; + } + + var valuesInOldEnum = Object.create(null); + oldType.getValues().forEach(function (value) { + valuesInOldEnum[value.name] = true; + }); + newType.getValues().forEach(function (value) { + if (!valuesInOldEnum[value.name]) { + valuesAddedToEnums.push({ + type: DangerousChangeType.VALUE_ADDED_TO_ENUM, + description: "".concat(value.name, " was added to enum type ").concat(typeName, ".") + }); + } + }); + }); + return valuesAddedToEnums; +} +export function findInterfacesRemovedFromObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var breakingChanges = []; + Object.keys(oldTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isObjectType(oldType) || !isObjectType(newType)) { + return; + } + + var oldInterfaces = oldType.getInterfaces(); + var newInterfaces = newType.getInterfaces(); + oldInterfaces.forEach(function (oldInterface) { + if (!newInterfaces.some(function (int) { + return int.name === oldInterface.name; + })) { + breakingChanges.push({ + type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT, + description: "".concat(typeName, " no longer implements interface ") + "".concat(oldInterface.name, ".") + }); + } + }); + }); + return breakingChanges; +} +export function findInterfacesAddedToObjectTypes(oldSchema, newSchema) { + var oldTypeMap = oldSchema.getTypeMap(); + var newTypeMap = newSchema.getTypeMap(); + var interfacesAddedToObjectTypes = []; + Object.keys(newTypeMap).forEach(function (typeName) { + var oldType = oldTypeMap[typeName]; + var newType = newTypeMap[typeName]; + + if (!isObjectType(oldType) || !isObjectType(newType)) { + return; + } + + var oldInterfaces = oldType.getInterfaces(); + var newInterfaces = newType.getInterfaces(); + newInterfaces.forEach(function (newInterface) { + if (!oldInterfaces.some(function (int) { + return int.name === newInterface.name; + })) { + interfacesAddedToObjectTypes.push({ + type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT, + description: "".concat(newInterface.name, " added to interfaces implemented ") + "by ".concat(typeName, ".") + }); + } + }); + }); + return interfacesAddedToObjectTypes; +} +export function findRemovedDirectives(oldSchema, newSchema) { + var removedDirectives = []; + var newSchemaDirectiveMap = getDirectiveMapForSchema(newSchema); + oldSchema.getDirectives().forEach(function (directive) { + if (!newSchemaDirectiveMap[directive.name]) { + removedDirectives.push({ + type: BreakingChangeType.DIRECTIVE_REMOVED, + description: "".concat(directive.name, " was removed") + }); + } + }); + return removedDirectives; +} + +function findRemovedArgsForDirective(oldDirective, newDirective) { + var removedArgs = []; + var newArgMap = getArgumentMapForDirective(newDirective); + oldDirective.args.forEach(function (arg) { + if (!newArgMap[arg.name]) { + removedArgs.push(arg); + } + }); + return removedArgs; +} + +export function findRemovedDirectiveArgs(oldSchema, newSchema) { + var removedDirectiveArgs = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findRemovedArgsForDirective(oldDirective, newDirective).forEach(function (arg) { + removedDirectiveArgs.push({ + type: BreakingChangeType.DIRECTIVE_ARG_REMOVED, + description: "".concat(arg.name, " was removed from ").concat(newDirective.name) + }); + }); + }); + return removedDirectiveArgs; +} + +function findAddedArgsForDirective(oldDirective, newDirective) { + var addedArgs = []; + var oldArgMap = getArgumentMapForDirective(oldDirective); + newDirective.args.forEach(function (arg) { + if (!oldArgMap[arg.name]) { + addedArgs.push(arg); + } + }); + return addedArgs; +} + +export function findAddedNonNullDirectiveArgs(oldSchema, newSchema) { + var addedNonNullableArgs = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findAddedArgsForDirective(oldDirective, newDirective).forEach(function (arg) { + if (!isNonNullType(arg.type)) { + return; + } + + addedNonNullableArgs.push({ + type: BreakingChangeType.NON_NULL_DIRECTIVE_ARG_ADDED, + description: "A non-null arg ".concat(arg.name, " on directive ") + "".concat(newDirective.name, " was added") + }); + }); + }); + return addedNonNullableArgs; +} +export function findRemovedLocationsForDirective(oldDirective, newDirective) { + var removedLocations = []; + var newLocationSet = new Set(newDirective.locations); + oldDirective.locations.forEach(function (oldLocation) { + if (!newLocationSet.has(oldLocation)) { + removedLocations.push(oldLocation); + } + }); + return removedLocations; +} +export function findRemovedDirectiveLocations(oldSchema, newSchema) { + var removedLocations = []; + var oldSchemaDirectiveMap = getDirectiveMapForSchema(oldSchema); + newSchema.getDirectives().forEach(function (newDirective) { + var oldDirective = oldSchemaDirectiveMap[newDirective.name]; + + if (!oldDirective) { + return; + } + + findRemovedLocationsForDirective(oldDirective, newDirective).forEach(function (location) { + removedLocations.push({ + type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED, + description: "".concat(location, " was removed from ").concat(newDirective.name) + }); + }); + }); + return removedLocations; +} + +function getDirectiveMapForSchema(schema) { + return keyMap(schema.getDirectives(), function (dir) { + return dir.name; + }); +} + +function getArgumentMapForDirective(directive) { + return keyMap(directive.args, function (arg) { + return arg.name; + }); +} \ No newline at end of file diff --git a/dist/utilities/findDeprecatedUsages.js b/dist/utilities/findDeprecatedUsages.js new file mode 100644 index 0000000000..28ff7be36d --- /dev/null +++ b/dist/utilities/findDeprecatedUsages.js @@ -0,0 +1,62 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.findDeprecatedUsages = findDeprecatedUsages; + +var _GraphQLError = require("../error/GraphQLError"); + +var _visitor = require("../language/visitor"); + +var _definition = require("../type/definition"); + +var _schema = require("../type/schema"); + +var _TypeInfo = require("./TypeInfo"); + +/** + * 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. + * + * strict + */ + +/** + * A validation rule which reports deprecated usages. + * + * Returns a list of GraphQLError instances describing each deprecated use. + */ +function findDeprecatedUsages(schema, ast) { + var errors = []; + var typeInfo = new _TypeInfo.TypeInfo(schema); + (0, _visitor.visit)(ast, (0, _visitor.visitWithTypeInfo)(typeInfo, { + Field: function Field(node) { + var fieldDef = typeInfo.getFieldDef(); + + if (fieldDef && fieldDef.isDeprecated) { + var parentType = typeInfo.getParentType(); + + if (parentType) { + var reason = fieldDef.deprecationReason; + errors.push(new _GraphQLError.GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node])); + } + } + }, + EnumValue: function EnumValue(node) { + var enumVal = typeInfo.getEnumValue(); + + if (enumVal && enumVal.isDeprecated) { + var type = (0, _definition.getNamedType)(typeInfo.getInputType()); + + if (type) { + var reason = enumVal.deprecationReason; + errors.push(new _GraphQLError.GraphQLError("The enum value ".concat(type.name, ".").concat(enumVal.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node])); + } + } + } + })); + return errors; +} \ No newline at end of file diff --git a/dist/utilities/findDeprecatedUsages.js.flow b/dist/utilities/findDeprecatedUsages.js.flow new file mode 100644 index 0000000000..ae4c1deab5 --- /dev/null +++ b/dist/utilities/findDeprecatedUsages.js.flow @@ -0,0 +1,68 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLError } from '../error/GraphQLError'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import type { DocumentNode } from '../language/ast'; +import { getNamedType } from '../type/definition'; +import { GraphQLSchema } from '../type/schema'; +import { TypeInfo } from './TypeInfo'; + +/** + * A validation rule which reports deprecated usages. + * + * Returns a list of GraphQLError instances describing each deprecated use. + */ +export function findDeprecatedUsages( + schema: GraphQLSchema, + ast: DocumentNode, +): Array { + const errors = []; + const typeInfo = new TypeInfo(schema); + + visit( + ast, + visitWithTypeInfo(typeInfo, { + Field(node) { + const fieldDef = typeInfo.getFieldDef(); + if (fieldDef && fieldDef.isDeprecated) { + const parentType = typeInfo.getParentType(); + if (parentType) { + const reason = fieldDef.deprecationReason; + errors.push( + new GraphQLError( + `The field ${parentType.name}.${fieldDef.name} is deprecated.` + + (reason ? ' ' + reason : ''), + [node], + ), + ); + } + } + }, + EnumValue(node) { + const enumVal = typeInfo.getEnumValue(); + if (enumVal && enumVal.isDeprecated) { + const type = getNamedType(typeInfo.getInputType()); + if (type) { + const reason = enumVal.deprecationReason; + errors.push( + new GraphQLError( + `The enum value ${type.name}.${enumVal.name} is deprecated.` + + (reason ? ' ' + reason : ''), + [node], + ), + ); + } + } + }, + }), + ); + + return errors; +} diff --git a/dist/utilities/findDeprecatedUsages.mjs b/dist/utilities/findDeprecatedUsages.mjs new file mode 100644 index 0000000000..0ea134fd7f --- /dev/null +++ b/dist/utilities/findDeprecatedUsages.mjs @@ -0,0 +1,50 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../error/GraphQLError'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import { getNamedType } from '../type/definition'; +import { GraphQLSchema } from '../type/schema'; +import { TypeInfo } from './TypeInfo'; +/** + * A validation rule which reports deprecated usages. + * + * Returns a list of GraphQLError instances describing each deprecated use. + */ + +export function findDeprecatedUsages(schema, ast) { + var errors = []; + var typeInfo = new TypeInfo(schema); + visit(ast, visitWithTypeInfo(typeInfo, { + Field: function Field(node) { + var fieldDef = typeInfo.getFieldDef(); + + if (fieldDef && fieldDef.isDeprecated) { + var parentType = typeInfo.getParentType(); + + if (parentType) { + var reason = fieldDef.deprecationReason; + errors.push(new GraphQLError("The field ".concat(parentType.name, ".").concat(fieldDef.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node])); + } + } + }, + EnumValue: function EnumValue(node) { + var enumVal = typeInfo.getEnumValue(); + + if (enumVal && enumVal.isDeprecated) { + var type = getNamedType(typeInfo.getInputType()); + + if (type) { + var reason = enumVal.deprecationReason; + errors.push(new GraphQLError("The enum value ".concat(type.name, ".").concat(enumVal.name, " is deprecated.") + (reason ? ' ' + reason : ''), [node])); + } + } + } + })); + return errors; +} \ No newline at end of file diff --git a/dist/utilities/getOperationAST.js b/dist/utilities/getOperationAST.js new file mode 100644 index 0000000000..c3f1c49412 --- /dev/null +++ b/dist/utilities/getOperationAST.js @@ -0,0 +1,47 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getOperationAST = getOperationAST; + +var _kinds = require("../language/kinds"); + +/** + * 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. + * + * strict + */ + +/** + * Returns an operation AST given a document AST and optionally an operation + * name. If a name is not provided, an operation is only returned if only one is + * provided in the document. + */ +function getOperationAST(documentAST, operationName) { + var operation = null; + + for (var i = 0; i < documentAST.definitions.length; i++) { + var definition = documentAST.definitions[i]; + + if (definition.kind === _kinds.Kind.OPERATION_DEFINITION) { + if (!operationName) { + // If no operation name was provided, only return an Operation if there + // is one defined in the document. Upon encountering the second, return + // null. + if (operation) { + return null; + } + + operation = definition; + } else if (definition.name && definition.name.value === operationName) { + return definition; + } + } + } + + return operation; +} \ No newline at end of file diff --git a/dist/utilities/getOperationAST.js.flow b/dist/utilities/getOperationAST.js.flow new file mode 100644 index 0000000000..b4e491a7fa --- /dev/null +++ b/dist/utilities/getOperationAST.js.flow @@ -0,0 +1,40 @@ +/** + * 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. + * + * @flow strict + */ + +import { Kind } from '../language/kinds'; +import type { DocumentNode, OperationDefinitionNode } from '../language/ast'; + +/** + * Returns an operation AST given a document AST and optionally an operation + * name. If a name is not provided, an operation is only returned if only one is + * provided in the document. + */ +export function getOperationAST( + documentAST: DocumentNode, + operationName: ?string, +): ?OperationDefinitionNode { + let operation = null; + for (let i = 0; i < documentAST.definitions.length; i++) { + const definition = documentAST.definitions[i]; + if (definition.kind === Kind.OPERATION_DEFINITION) { + if (!operationName) { + // If no operation name was provided, only return an Operation if there + // is one defined in the document. Upon encountering the second, return + // null. + if (operation) { + return null; + } + operation = definition; + } else if (definition.name && definition.name.value === operationName) { + return definition; + } + } + } + return operation; +} diff --git a/dist/utilities/getOperationAST.mjs b/dist/utilities/getOperationAST.mjs new file mode 100644 index 0000000000..d26da1031a --- /dev/null +++ b/dist/utilities/getOperationAST.mjs @@ -0,0 +1,39 @@ +/** + * 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. + * + * strict + */ +import { Kind } from '../language/kinds'; + +/** + * Returns an operation AST given a document AST and optionally an operation + * name. If a name is not provided, an operation is only returned if only one is + * provided in the document. + */ +export function getOperationAST(documentAST, operationName) { + var operation = null; + + for (var i = 0; i < documentAST.definitions.length; i++) { + var definition = documentAST.definitions[i]; + + if (definition.kind === Kind.OPERATION_DEFINITION) { + if (!operationName) { + // If no operation name was provided, only return an Operation if there + // is one defined in the document. Upon encountering the second, return + // null. + if (operation) { + return null; + } + + operation = definition; + } else if (definition.name && definition.name.value === operationName) { + return definition; + } + } + } + + return operation; +} \ No newline at end of file diff --git a/dist/utilities/getOperationRootType.js b/dist/utilities/getOperationRootType.js new file mode 100644 index 0000000000..53e366e5ab --- /dev/null +++ b/dist/utilities/getOperationRootType.js @@ -0,0 +1,56 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getOperationRootType = getOperationRootType; + +var _GraphQLError = require("../error/GraphQLError"); + +var _schema = require("../type/schema"); + +/** + * 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. + * + * strict + */ + +/** + * Extracts the root type of the operation from the schema. + */ +function getOperationRootType(schema, operation) { + switch (operation.operation) { + case 'query': + var queryType = schema.getQueryType(); + + if (!queryType) { + throw new _GraphQLError.GraphQLError('Schema does not define the required query root type.', [operation]); + } + + return queryType; + + case 'mutation': + var mutationType = schema.getMutationType(); + + if (!mutationType) { + throw new _GraphQLError.GraphQLError('Schema is not configured for mutations.', [operation]); + } + + return mutationType; + + case 'subscription': + var subscriptionType = schema.getSubscriptionType(); + + if (!subscriptionType) { + throw new _GraphQLError.GraphQLError('Schema is not configured for subscriptions.', [operation]); + } + + return subscriptionType; + + default: + throw new _GraphQLError.GraphQLError('Can only have query, mutation and subscription operations.', [operation]); + } +} \ No newline at end of file diff --git a/dist/utilities/getOperationRootType.js.flow b/dist/utilities/getOperationRootType.js.flow new file mode 100644 index 0000000000..72f7b3d8cb --- /dev/null +++ b/dist/utilities/getOperationRootType.js.flow @@ -0,0 +1,57 @@ +/** + * 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. + * + * @flow strict + */ + +import { GraphQLError } from '../error/GraphQLError'; +import type { + OperationDefinitionNode, + OperationTypeDefinitionNode, +} from '../language/ast'; +import { GraphQLSchema } from '../type/schema'; +import type { GraphQLObjectType } from '../type/definition'; + +/** + * Extracts the root type of the operation from the schema. + */ +export function getOperationRootType( + schema: GraphQLSchema, + operation: OperationDefinitionNode | OperationTypeDefinitionNode, +): GraphQLObjectType { + switch (operation.operation) { + case 'query': + const queryType = schema.getQueryType(); + if (!queryType) { + throw new GraphQLError( + 'Schema does not define the required query root type.', + [operation], + ); + } + return queryType; + case 'mutation': + const mutationType = schema.getMutationType(); + if (!mutationType) { + throw new GraphQLError('Schema is not configured for mutations.', [ + operation, + ]); + } + return mutationType; + case 'subscription': + const subscriptionType = schema.getSubscriptionType(); + if (!subscriptionType) { + throw new GraphQLError('Schema is not configured for subscriptions.', [ + operation, + ]); + } + return subscriptionType; + default: + throw new GraphQLError( + 'Can only have query, mutation and subscription operations.', + [operation], + ); + } +} diff --git a/dist/utilities/getOperationRootType.mjs b/dist/utilities/getOperationRootType.mjs new file mode 100644 index 0000000000..e8c214c26c --- /dev/null +++ b/dist/utilities/getOperationRootType.mjs @@ -0,0 +1,47 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../error/GraphQLError'; +import { GraphQLSchema } from '../type/schema'; + +/** + * Extracts the root type of the operation from the schema. + */ +export function getOperationRootType(schema, operation) { + switch (operation.operation) { + case 'query': + var queryType = schema.getQueryType(); + + if (!queryType) { + throw new GraphQLError('Schema does not define the required query root type.', [operation]); + } + + return queryType; + + case 'mutation': + var mutationType = schema.getMutationType(); + + if (!mutationType) { + throw new GraphQLError('Schema is not configured for mutations.', [operation]); + } + + return mutationType; + + case 'subscription': + var subscriptionType = schema.getSubscriptionType(); + + if (!subscriptionType) { + throw new GraphQLError('Schema is not configured for subscriptions.', [operation]); + } + + return subscriptionType; + + default: + throw new GraphQLError('Can only have query, mutation and subscription operations.', [operation]); + } +} \ No newline at end of file diff --git a/dist/utilities/index.js b/dist/utilities/index.js new file mode 100644 index 0000000000..938e66ea12 --- /dev/null +++ b/dist/utilities/index.js @@ -0,0 +1,255 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "getIntrospectionQuery", { + enumerable: true, + get: function get() { + return _introspectionQuery.getIntrospectionQuery; + } +}); +Object.defineProperty(exports, "introspectionQuery", { + enumerable: true, + get: function get() { + return _introspectionQuery.introspectionQuery; + } +}); +Object.defineProperty(exports, "getOperationAST", { + enumerable: true, + get: function get() { + return _getOperationAST.getOperationAST; + } +}); +Object.defineProperty(exports, "getOperationRootType", { + enumerable: true, + get: function get() { + return _getOperationRootType.getOperationRootType; + } +}); +Object.defineProperty(exports, "introspectionFromSchema", { + enumerable: true, + get: function get() { + return _introspectionFromSchema.introspectionFromSchema; + } +}); +Object.defineProperty(exports, "buildClientSchema", { + enumerable: true, + get: function get() { + return _buildClientSchema.buildClientSchema; + } +}); +Object.defineProperty(exports, "buildASTSchema", { + enumerable: true, + get: function get() { + return _buildASTSchema.buildASTSchema; + } +}); +Object.defineProperty(exports, "buildSchema", { + enumerable: true, + get: function get() { + return _buildASTSchema.buildSchema; + } +}); +Object.defineProperty(exports, "getDescription", { + enumerable: true, + get: function get() { + return _buildASTSchema.getDescription; + } +}); +Object.defineProperty(exports, "extendSchema", { + enumerable: true, + get: function get() { + return _extendSchema.extendSchema; + } +}); +Object.defineProperty(exports, "lexicographicSortSchema", { + enumerable: true, + get: function get() { + return _lexicographicSortSchema.lexicographicSortSchema; + } +}); +Object.defineProperty(exports, "printSchema", { + enumerable: true, + get: function get() { + return _schemaPrinter.printSchema; + } +}); +Object.defineProperty(exports, "printType", { + enumerable: true, + get: function get() { + return _schemaPrinter.printType; + } +}); +Object.defineProperty(exports, "printIntrospectionSchema", { + enumerable: true, + get: function get() { + return _schemaPrinter.printIntrospectionSchema; + } +}); +Object.defineProperty(exports, "typeFromAST", { + enumerable: true, + get: function get() { + return _typeFromAST.typeFromAST; + } +}); +Object.defineProperty(exports, "valueFromAST", { + enumerable: true, + get: function get() { + return _valueFromAST.valueFromAST; + } +}); +Object.defineProperty(exports, "valueFromASTUntyped", { + enumerable: true, + get: function get() { + return _valueFromASTUntyped.valueFromASTUntyped; + } +}); +Object.defineProperty(exports, "astFromValue", { + enumerable: true, + get: function get() { + return _astFromValue.astFromValue; + } +}); +Object.defineProperty(exports, "TypeInfo", { + enumerable: true, + get: function get() { + return _TypeInfo.TypeInfo; + } +}); +Object.defineProperty(exports, "coerceValue", { + enumerable: true, + get: function get() { + return _coerceValue.coerceValue; + } +}); +Object.defineProperty(exports, "isValidJSValue", { + enumerable: true, + get: function get() { + return _isValidJSValue.isValidJSValue; + } +}); +Object.defineProperty(exports, "isValidLiteralValue", { + enumerable: true, + get: function get() { + return _isValidLiteralValue.isValidLiteralValue; + } +}); +Object.defineProperty(exports, "concatAST", { + enumerable: true, + get: function get() { + return _concatAST.concatAST; + } +}); +Object.defineProperty(exports, "separateOperations", { + enumerable: true, + get: function get() { + return _separateOperations.separateOperations; + } +}); +Object.defineProperty(exports, "isEqualType", { + enumerable: true, + get: function get() { + return _typeComparators.isEqualType; + } +}); +Object.defineProperty(exports, "isTypeSubTypeOf", { + enumerable: true, + get: function get() { + return _typeComparators.isTypeSubTypeOf; + } +}); +Object.defineProperty(exports, "doTypesOverlap", { + enumerable: true, + get: function get() { + return _typeComparators.doTypesOverlap; + } +}); +Object.defineProperty(exports, "assertValidName", { + enumerable: true, + get: function get() { + return _assertValidName.assertValidName; + } +}); +Object.defineProperty(exports, "isValidNameError", { + enumerable: true, + get: function get() { + return _assertValidName.isValidNameError; + } +}); +Object.defineProperty(exports, "BreakingChangeType", { + enumerable: true, + get: function get() { + return _findBreakingChanges.BreakingChangeType; + } +}); +Object.defineProperty(exports, "DangerousChangeType", { + enumerable: true, + get: function get() { + return _findBreakingChanges.DangerousChangeType; + } +}); +Object.defineProperty(exports, "findBreakingChanges", { + enumerable: true, + get: function get() { + return _findBreakingChanges.findBreakingChanges; + } +}); +Object.defineProperty(exports, "findDangerousChanges", { + enumerable: true, + get: function get() { + return _findBreakingChanges.findDangerousChanges; + } +}); +Object.defineProperty(exports, "findDeprecatedUsages", { + enumerable: true, + get: function get() { + return _findDeprecatedUsages.findDeprecatedUsages; + } +}); + +var _introspectionQuery = require("./introspectionQuery"); + +var _getOperationAST = require("./getOperationAST"); + +var _getOperationRootType = require("./getOperationRootType"); + +var _introspectionFromSchema = require("./introspectionFromSchema"); + +var _buildClientSchema = require("./buildClientSchema"); + +var _buildASTSchema = require("./buildASTSchema"); + +var _extendSchema = require("./extendSchema"); + +var _lexicographicSortSchema = require("./lexicographicSortSchema"); + +var _schemaPrinter = require("./schemaPrinter"); + +var _typeFromAST = require("./typeFromAST"); + +var _valueFromAST = require("./valueFromAST"); + +var _valueFromASTUntyped = require("./valueFromASTUntyped"); + +var _astFromValue = require("./astFromValue"); + +var _TypeInfo = require("./TypeInfo"); + +var _coerceValue = require("./coerceValue"); + +var _isValidJSValue = require("./isValidJSValue"); + +var _isValidLiteralValue = require("./isValidLiteralValue"); + +var _concatAST = require("./concatAST"); + +var _separateOperations = require("./separateOperations"); + +var _typeComparators = require("./typeComparators"); + +var _assertValidName = require("./assertValidName"); + +var _findBreakingChanges = require("./findBreakingChanges"); + +var _findDeprecatedUsages = require("./findDeprecatedUsages"); \ No newline at end of file diff --git a/dist/utilities/index.js.flow b/dist/utilities/index.js.flow new file mode 100644 index 0000000000..3fa3529342 --- /dev/null +++ b/dist/utilities/index.js.flow @@ -0,0 +1,121 @@ +/** + * 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. + * + * @flow strict + */ + +// The GraphQL query recommended for a full schema introspection. +export { + getIntrospectionQuery, + // Deprecated, use getIntrospectionQuery() + introspectionQuery, +} from './introspectionQuery'; +export type { + IntrospectionOptions, + IntrospectionQuery, + IntrospectionSchema, + IntrospectionType, + IntrospectionInputType, + IntrospectionOutputType, + IntrospectionScalarType, + IntrospectionObjectType, + IntrospectionInterfaceType, + IntrospectionUnionType, + IntrospectionEnumType, + IntrospectionInputObjectType, + IntrospectionTypeRef, + IntrospectionInputTypeRef, + IntrospectionOutputTypeRef, + IntrospectionNamedTypeRef, + IntrospectionListTypeRef, + IntrospectionNonNullTypeRef, + IntrospectionField, + IntrospectionInputValue, + IntrospectionEnumValue, + IntrospectionDirective, +} from './introspectionQuery'; + +// Gets the target Operation from a Document +export { getOperationAST } from './getOperationAST'; + +// Gets the Type for the target Operation AST. +export { getOperationRootType } from './getOperationRootType'; + +// Convert a GraphQLSchema to an IntrospectionQuery +export { introspectionFromSchema } from './introspectionFromSchema'; + +// Build a GraphQLSchema from an introspection result. +export { buildClientSchema } from './buildClientSchema'; + +// Build a GraphQLSchema from GraphQL Schema language. +export { buildASTSchema, buildSchema, getDescription } from './buildASTSchema'; +export type { BuildSchemaOptions } from './buildASTSchema'; + +// Extends an existing GraphQLSchema from a parsed GraphQL Schema language AST. +export { extendSchema } from './extendSchema'; + +// Sort a GraphQLSchema. +export { lexicographicSortSchema } from './lexicographicSortSchema'; + +// Print a GraphQLSchema to GraphQL Schema language. +export { + printSchema, + printType, + printIntrospectionSchema, +} from './schemaPrinter'; + +// Create a GraphQLType from a GraphQL language AST. +export { typeFromAST } from './typeFromAST'; + +// Create a JavaScript value from a GraphQL language AST with a type. +export { valueFromAST } from './valueFromAST'; + +// Create a JavaScript value from a GraphQL language AST without a type. +export { valueFromASTUntyped } from './valueFromASTUntyped'; + +// Create a GraphQL language AST from a JavaScript value. +export { astFromValue } from './astFromValue'; + +// A helper to use within recursive-descent visitors which need to be aware of +// the GraphQL type system. +export { TypeInfo } from './TypeInfo'; + +// Coerces a JavaScript value to a GraphQL type, or produces errors. +export { coerceValue } from './coerceValue'; + +// @deprecated use coerceValue +export { isValidJSValue } from './isValidJSValue'; + +// Determine if AST values adhere to a GraphQL type. +export { isValidLiteralValue } from './isValidLiteralValue'; + +// Concatenates multiple AST together. +export { concatAST } from './concatAST'; + +// Separates an AST into an AST per Operation. +export { separateOperations } from './separateOperations'; + +// Comparators for types +export { + isEqualType, + isTypeSubTypeOf, + doTypesOverlap, +} from './typeComparators'; + +// Asserts that a string is a valid GraphQL name +export { assertValidName, isValidNameError } from './assertValidName'; + +// Compares two GraphQLSchemas and detects breaking changes. +export { + BreakingChangeType, + DangerousChangeType, + findBreakingChanges, + findDangerousChanges, +} from './findBreakingChanges'; +export type { BreakingChange, DangerousChange } from './findBreakingChanges'; + +// Report all deprecated usage within a GraphQL document. +export { findDeprecatedUsages } from './findDeprecatedUsages'; diff --git a/dist/utilities/index.mjs b/dist/utilities/index.mjs new file mode 100644 index 0000000000..d5aae3ec90 --- /dev/null +++ b/dist/utilities/index.mjs @@ -0,0 +1,56 @@ +/** + * 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. + * + * strict + */ +// The GraphQL query recommended for a full schema introspection. +export { getIntrospectionQuery, // Deprecated, use getIntrospectionQuery() +introspectionQuery } from './introspectionQuery'; +// Gets the target Operation from a Document +export { getOperationAST } from './getOperationAST'; // Gets the Type for the target Operation AST. + +export { getOperationRootType } from './getOperationRootType'; // Convert a GraphQLSchema to an IntrospectionQuery + +export { introspectionFromSchema } from './introspectionFromSchema'; // Build a GraphQLSchema from an introspection result. + +export { buildClientSchema } from './buildClientSchema'; // Build a GraphQLSchema from GraphQL Schema language. + +export { buildASTSchema, buildSchema, getDescription } from './buildASTSchema'; +// Extends an existing GraphQLSchema from a parsed GraphQL Schema language AST. +export { extendSchema } from './extendSchema'; // Sort a GraphQLSchema. + +export { lexicographicSortSchema } from './lexicographicSortSchema'; // Print a GraphQLSchema to GraphQL Schema language. + +export { printSchema, printType, printIntrospectionSchema } from './schemaPrinter'; // Create a GraphQLType from a GraphQL language AST. + +export { typeFromAST } from './typeFromAST'; // Create a JavaScript value from a GraphQL language AST with a type. + +export { valueFromAST } from './valueFromAST'; // Create a JavaScript value from a GraphQL language AST without a type. + +export { valueFromASTUntyped } from './valueFromASTUntyped'; // Create a GraphQL language AST from a JavaScript value. + +export { astFromValue } from './astFromValue'; // A helper to use within recursive-descent visitors which need to be aware of +// the GraphQL type system. + +export { TypeInfo } from './TypeInfo'; // Coerces a JavaScript value to a GraphQL type, or produces errors. + +export { coerceValue } from './coerceValue'; // @deprecated use coerceValue + +export { isValidJSValue } from './isValidJSValue'; // Determine if AST values adhere to a GraphQL type. + +export { isValidLiteralValue } from './isValidLiteralValue'; // Concatenates multiple AST together. + +export { concatAST } from './concatAST'; // Separates an AST into an AST per Operation. + +export { separateOperations } from './separateOperations'; // Comparators for types + +export { isEqualType, isTypeSubTypeOf, doTypesOverlap } from './typeComparators'; // Asserts that a string is a valid GraphQL name + +export { assertValidName, isValidNameError } from './assertValidName'; // Compares two GraphQLSchemas and detects breaking changes. + +export { BreakingChangeType, DangerousChangeType, findBreakingChanges, findDangerousChanges } from './findBreakingChanges'; +// Report all deprecated usage within a GraphQL document. +export { findDeprecatedUsages } from './findDeprecatedUsages'; \ No newline at end of file diff --git a/dist/utilities/introspectionFromSchema.js b/dist/utilities/introspectionFromSchema.js new file mode 100644 index 0000000000..b92fba24f5 --- /dev/null +++ b/dist/utilities/introspectionFromSchema.js @@ -0,0 +1,43 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.introspectionFromSchema = introspectionFromSchema; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _introspectionQuery = require("./introspectionQuery"); + +var _schema = require("../type/schema"); + +var _execute = require("../execution/execute"); + +var _parser = require("../language/parser"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Build an IntrospectionQuery from a GraphQLSchema + * + * IntrospectionQuery is useful for utilities that care about type and field + * relationships, but do not need to traverse through those relationships. + * + * This is the inverse of buildClientSchema. The primary use case is outside + * of the server context, for instance when doing schema comparisons. + */ +function introspectionFromSchema(schema, options) { + var queryAST = (0, _parser.parse)((0, _introspectionQuery.getIntrospectionQuery)(options)); + var result = (0, _execute.execute)(schema, queryAST); + !(!result.then && !result.errors && result.data) ? (0, _invariant.default)(0) : void 0; + return result.data; +} \ No newline at end of file diff --git a/dist/utilities/introspectionFromSchema.js.flow b/dist/utilities/introspectionFromSchema.js.flow new file mode 100644 index 0000000000..fa81609ff4 --- /dev/null +++ b/dist/utilities/introspectionFromSchema.js.flow @@ -0,0 +1,37 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; +import { getIntrospectionQuery } from './introspectionQuery'; +import { GraphQLSchema } from '../type/schema'; +import { execute } from '../execution/execute'; +import { parse } from '../language/parser'; +import type { + IntrospectionQuery, + IntrospectionOptions, +} from './introspectionQuery'; + +/** + * Build an IntrospectionQuery from a GraphQLSchema + * + * IntrospectionQuery is useful for utilities that care about type and field + * relationships, but do not need to traverse through those relationships. + * + * This is the inverse of buildClientSchema. The primary use case is outside + * of the server context, for instance when doing schema comparisons. + */ +export function introspectionFromSchema( + schema: GraphQLSchema, + options: IntrospectionOptions, +): IntrospectionQuery { + const queryAST = parse(getIntrospectionQuery(options)); + const result = execute(schema, queryAST); + invariant(!result.then && !result.errors && result.data); + return (result.data: any); +} diff --git a/dist/utilities/introspectionFromSchema.mjs b/dist/utilities/introspectionFromSchema.mjs new file mode 100644 index 0000000000..27bc89e125 --- /dev/null +++ b/dist/utilities/introspectionFromSchema.mjs @@ -0,0 +1,29 @@ +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; +import { getIntrospectionQuery } from './introspectionQuery'; +import { GraphQLSchema } from '../type/schema'; +import { execute } from '../execution/execute'; +import { parse } from '../language/parser'; + +/** + * Build an IntrospectionQuery from a GraphQLSchema + * + * IntrospectionQuery is useful for utilities that care about type and field + * relationships, but do not need to traverse through those relationships. + * + * This is the inverse of buildClientSchema. The primary use case is outside + * of the server context, for instance when doing schema comparisons. + */ +export function introspectionFromSchema(schema, options) { + var queryAST = parse(getIntrospectionQuery(options)); + var result = execute(schema, queryAST); + !(!result.then && !result.errors && result.data) ? invariant(0) : void 0; + return result.data; +} \ No newline at end of file diff --git a/dist/utilities/introspectionQuery.js b/dist/utilities/introspectionQuery.js new file mode 100644 index 0000000000..7dadb904ec --- /dev/null +++ b/dist/utilities/introspectionQuery.js @@ -0,0 +1,23 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.getIntrospectionQuery = getIntrospectionQuery; +exports.introspectionQuery = void 0; + +/** + * 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. + * + * strict + */ +function getIntrospectionQuery(options) { + var descriptions = !(options && options.descriptions === false); + return "\n query IntrospectionQuery {\n __schema {\n queryType { name }\n mutationType { name }\n subscriptionType { name }\n types {\n ...FullType\n }\n directives {\n name\n ".concat(descriptions ? 'description' : '', "\n locations\n args {\n ...InputValue\n }\n }\n }\n }\n\n fragment FullType on __Type {\n kind\n name\n ").concat(descriptions ? 'description' : '', "\n fields(includeDeprecated: true) {\n name\n ").concat(descriptions ? 'description' : '', "\n args {\n ...InputValue\n }\n type {\n ...TypeRef\n }\n isDeprecated\n deprecationReason\n }\n inputFields {\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues(includeDeprecated: true) {\n name\n ").concat(descriptions ? 'description' : '', "\n isDeprecated\n deprecationReason\n }\n possibleTypes {\n ...TypeRef\n }\n }\n\n fragment InputValue on __InputValue {\n name\n ").concat(descriptions ? 'description' : '', "\n type { ...TypeRef }\n defaultValue\n }\n\n fragment TypeRef on __Type {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n }\n }\n }\n }\n }\n "); +} + +var introspectionQuery = getIntrospectionQuery(); +exports.introspectionQuery = introspectionQuery; \ No newline at end of file diff --git a/dist/utilities/introspectionQuery.js.flow b/dist/utilities/introspectionQuery.js.flow new file mode 100644 index 0000000000..9c67dbd0e4 --- /dev/null +++ b/dist/utilities/introspectionQuery.js.flow @@ -0,0 +1,271 @@ +/** + * 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. + * + * @flow strict + */ + +import type { DirectiveLocationEnum } from '../language/directiveLocation'; + +export type IntrospectionOptions = {| + // Whether to include descriptions in the introspection result. + // Default: true + descriptions: boolean, +|}; + +export function getIntrospectionQuery(options?: IntrospectionOptions): string { + const descriptions = !(options && options.descriptions === false); + return ` + query IntrospectionQuery { + __schema { + queryType { name } + mutationType { name } + subscriptionType { name } + types { + ...FullType + } + directives { + name + ${descriptions ? 'description' : ''} + locations + args { + ...InputValue + } + } + } + } + + fragment FullType on __Type { + kind + name + ${descriptions ? 'description' : ''} + fields(includeDeprecated: true) { + name + ${descriptions ? 'description' : ''} + args { + ...InputValue + } + type { + ...TypeRef + } + isDeprecated + deprecationReason + } + inputFields { + ...InputValue + } + interfaces { + ...TypeRef + } + enumValues(includeDeprecated: true) { + name + ${descriptions ? 'description' : ''} + isDeprecated + deprecationReason + } + possibleTypes { + ...TypeRef + } + } + + fragment InputValue on __InputValue { + name + ${descriptions ? 'description' : ''} + type { ...TypeRef } + defaultValue + } + + fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } + } + } + } + } + } + `; +} + +export const introspectionQuery = getIntrospectionQuery(); + +export type IntrospectionQuery = {| + +__schema: IntrospectionSchema, +|}; + +export type IntrospectionSchema = {| + +queryType: IntrospectionNamedTypeRef, + +mutationType: ?IntrospectionNamedTypeRef, + +subscriptionType: ?IntrospectionNamedTypeRef, + +types: $ReadOnlyArray, + +directives: $ReadOnlyArray, +|}; + +export type IntrospectionType = + | IntrospectionScalarType + | IntrospectionObjectType + | IntrospectionInterfaceType + | IntrospectionUnionType + | IntrospectionEnumType + | IntrospectionInputObjectType; + +export type IntrospectionOutputType = + | IntrospectionScalarType + | IntrospectionObjectType + | IntrospectionInterfaceType + | IntrospectionUnionType + | IntrospectionEnumType; + +export type IntrospectionInputType = + | IntrospectionScalarType + | IntrospectionEnumType + | IntrospectionInputObjectType; + +export type IntrospectionScalarType = { + +kind: 'SCALAR', + +name: string, + +description?: ?string, +}; + +export type IntrospectionObjectType = { + +kind: 'OBJECT', + +name: string, + +description?: ?string, + +fields: $ReadOnlyArray, + +interfaces: $ReadOnlyArray< + IntrospectionNamedTypeRef, + >, +}; + +export type IntrospectionInterfaceType = { + +kind: 'INTERFACE', + +name: string, + +description?: ?string, + +fields: $ReadOnlyArray, + +possibleTypes: $ReadOnlyArray< + IntrospectionNamedTypeRef, + >, +}; + +export type IntrospectionUnionType = { + +kind: 'UNION', + +name: string, + +description?: ?string, + +possibleTypes: $ReadOnlyArray< + IntrospectionNamedTypeRef, + >, +}; + +export type IntrospectionEnumType = { + +kind: 'ENUM', + +name: string, + +description?: ?string, + +enumValues: $ReadOnlyArray, +}; + +export type IntrospectionInputObjectType = { + +kind: 'INPUT_OBJECT', + +name: string, + +description?: ?string, + +inputFields: $ReadOnlyArray, +}; + +export type IntrospectionListTypeRef< + T: IntrospectionTypeRef = IntrospectionTypeRef, +> = { + +kind: 'LIST', + +ofType: T, +}; + +export type IntrospectionNonNullTypeRef< + T: IntrospectionTypeRef = IntrospectionTypeRef, +> = { + +kind: 'NON_NULL', + +ofType: T, +}; + +export type IntrospectionTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef< + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef, + >; + +export type IntrospectionOutputTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef< + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef, + >; + +export type IntrospectionInputTypeRef = + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef + | IntrospectionNonNullTypeRef< + | IntrospectionNamedTypeRef + | IntrospectionListTypeRef, + >; + +export type IntrospectionNamedTypeRef< + T: IntrospectionType = IntrospectionType, +> = { + +kind: $PropertyType, + +name: string, +}; + +export type IntrospectionField = {| + +name: string, + +description?: ?string, + +args: $ReadOnlyArray, + +type: IntrospectionOutputTypeRef, + +isDeprecated: boolean, + +deprecationReason: ?string, +|}; + +export type IntrospectionInputValue = {| + +name: string, + +description?: ?string, + +type: IntrospectionInputTypeRef, + +defaultValue: ?string, +|}; + +export type IntrospectionEnumValue = {| + +name: string, + +description?: ?string, + +isDeprecated: boolean, + +deprecationReason: ?string, +|}; + +export type IntrospectionDirective = {| + +name: string, + +description?: ?string, + +locations: $ReadOnlyArray, + +args: $ReadOnlyArray, +|}; diff --git a/dist/utilities/introspectionQuery.mjs b/dist/utilities/introspectionQuery.mjs new file mode 100644 index 0000000000..e6dbb91111 --- /dev/null +++ b/dist/utilities/introspectionQuery.mjs @@ -0,0 +1,13 @@ +/** + * 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. + * + * strict + */ +export function getIntrospectionQuery(options) { + var descriptions = !(options && options.descriptions === false); + return "\n query IntrospectionQuery {\n __schema {\n queryType { name }\n mutationType { name }\n subscriptionType { name }\n types {\n ...FullType\n }\n directives {\n name\n ".concat(descriptions ? 'description' : '', "\n locations\n args {\n ...InputValue\n }\n }\n }\n }\n\n fragment FullType on __Type {\n kind\n name\n ").concat(descriptions ? 'description' : '', "\n fields(includeDeprecated: true) {\n name\n ").concat(descriptions ? 'description' : '', "\n args {\n ...InputValue\n }\n type {\n ...TypeRef\n }\n isDeprecated\n deprecationReason\n }\n inputFields {\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues(includeDeprecated: true) {\n name\n ").concat(descriptions ? 'description' : '', "\n isDeprecated\n deprecationReason\n }\n possibleTypes {\n ...TypeRef\n }\n }\n\n fragment InputValue on __InputValue {\n name\n ").concat(descriptions ? 'description' : '', "\n type { ...TypeRef }\n defaultValue\n }\n\n fragment TypeRef on __Type {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n }\n }\n }\n }\n }\n "); +} +export var introspectionQuery = getIntrospectionQuery(); \ No newline at end of file diff --git a/dist/utilities/isValidJSValue.js b/dist/utilities/isValidJSValue.js new file mode 100644 index 0000000000..d94fb51d47 --- /dev/null +++ b/dist/utilities/isValidJSValue.js @@ -0,0 +1,27 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isValidJSValue = isValidJSValue; + +var _coerceValue = require("./coerceValue"); + +/** + * 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. + * + * strict + */ + +/** + * Deprecated. Use coerceValue() directly for richer information. + */ +function isValidJSValue(value, type) { + var errors = (0, _coerceValue.coerceValue)(value, type).errors; + return errors ? errors.map(function (error) { + return error.message; + }) : []; +} \ No newline at end of file diff --git a/dist/utilities/isValidJSValue.js.flow b/dist/utilities/isValidJSValue.js.flow new file mode 100644 index 0000000000..c68eeb056f --- /dev/null +++ b/dist/utilities/isValidJSValue.js.flow @@ -0,0 +1,22 @@ +/** + * 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. + * + * @flow strict + */ + +import { coerceValue } from './coerceValue'; +import type { GraphQLInputType } from '../type/definition'; + +/** + * Deprecated. Use coerceValue() directly for richer information. + */ +export function isValidJSValue( + value: mixed, + type: GraphQLInputType, +): Array { + const errors = coerceValue(value, type).errors; + return errors ? errors.map(error => error.message) : []; +} diff --git a/dist/utilities/isValidJSValue.mjs b/dist/utilities/isValidJSValue.mjs new file mode 100644 index 0000000000..33009516d9 --- /dev/null +++ b/dist/utilities/isValidJSValue.mjs @@ -0,0 +1,19 @@ +/** + * 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. + * + * strict + */ +import { coerceValue } from './coerceValue'; + +/** + * Deprecated. Use coerceValue() directly for richer information. + */ +export function isValidJSValue(value, type) { + var errors = coerceValue(value, type).errors; + return errors ? errors.map(function (error) { + return error.message; + }) : []; +} \ No newline at end of file diff --git a/dist/utilities/isValidLiteralValue.js b/dist/utilities/isValidLiteralValue.js new file mode 100644 index 0000000000..9d99abed32 --- /dev/null +++ b/dist/utilities/isValidLiteralValue.js @@ -0,0 +1,47 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isValidLiteralValue = isValidLiteralValue; + +var _TypeInfo = require("./TypeInfo"); + +var _kinds = require("../language/kinds"); + +var _visitor = require("../language/visitor"); + +var _schema = require("../type/schema"); + +var _ValuesOfCorrectType = require("../validation/rules/ValuesOfCorrectType"); + +var _ValidationContext = _interopRequireDefault(require("../validation/ValidationContext")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Utility which determines if a value literal node is valid for an input type. + * + * Deprecated. Rely on validation for documents containing literal values. + */ +function isValidLiteralValue(type, valueNode) { + var emptySchema = new _schema.GraphQLSchema({}); + var emptyDoc = { + kind: _kinds.Kind.DOCUMENT, + definitions: [] + }; + var typeInfo = new _TypeInfo.TypeInfo(emptySchema, undefined, type); + var context = new _ValidationContext.default(emptySchema, emptyDoc, typeInfo); + var visitor = (0, _ValuesOfCorrectType.ValuesOfCorrectType)(context); + (0, _visitor.visit)(valueNode, (0, _visitor.visitWithTypeInfo)(typeInfo, visitor)); + return context.getErrors(); +} \ No newline at end of file diff --git a/dist/utilities/isValidLiteralValue.js.flow b/dist/utilities/isValidLiteralValue.js.flow new file mode 100644 index 0000000000..719a2c24aa --- /dev/null +++ b/dist/utilities/isValidLiteralValue.js.flow @@ -0,0 +1,36 @@ +/** + * 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. + * + * @flow strict + */ + +import { TypeInfo } from './TypeInfo'; +import type { GraphQLError } from '../error/GraphQLError'; +import type { ValueNode } from '../language/ast'; +import { Kind } from '../language/kinds'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import type { GraphQLInputType } from '../type/definition'; +import { GraphQLSchema } from '../type/schema'; +import { ValuesOfCorrectType } from '../validation/rules/ValuesOfCorrectType'; +import ValidationContext from '../validation/ValidationContext'; + +/** + * Utility which determines if a value literal node is valid for an input type. + * + * Deprecated. Rely on validation for documents containing literal values. + */ +export function isValidLiteralValue( + type: GraphQLInputType, + valueNode: ValueNode, +): $ReadOnlyArray { + const emptySchema = new GraphQLSchema({}); + const emptyDoc = { kind: Kind.DOCUMENT, definitions: [] }; + const typeInfo = new TypeInfo(emptySchema, undefined, type); + const context = new ValidationContext(emptySchema, emptyDoc, typeInfo); + const visitor = ValuesOfCorrectType(context); + visit(valueNode, visitWithTypeInfo(typeInfo, visitor)); + return context.getErrors(); +} diff --git a/dist/utilities/isValidLiteralValue.mjs b/dist/utilities/isValidLiteralValue.mjs new file mode 100644 index 0000000000..2623a70224 --- /dev/null +++ b/dist/utilities/isValidLiteralValue.mjs @@ -0,0 +1,32 @@ +/** + * 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. + * + * strict + */ +import { TypeInfo } from './TypeInfo'; +import { Kind } from '../language/kinds'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import { GraphQLSchema } from '../type/schema'; +import { ValuesOfCorrectType } from '../validation/rules/ValuesOfCorrectType'; +import ValidationContext from '../validation/ValidationContext'; +/** + * Utility which determines if a value literal node is valid for an input type. + * + * Deprecated. Rely on validation for documents containing literal values. + */ + +export function isValidLiteralValue(type, valueNode) { + var emptySchema = new GraphQLSchema({}); + var emptyDoc = { + kind: Kind.DOCUMENT, + definitions: [] + }; + var typeInfo = new TypeInfo(emptySchema, undefined, type); + var context = new ValidationContext(emptySchema, emptyDoc, typeInfo); + var visitor = ValuesOfCorrectType(context); + visit(valueNode, visitWithTypeInfo(typeInfo, visitor)); + return context.getErrors(); +} \ No newline at end of file diff --git a/dist/utilities/lexicographicSortSchema.js b/dist/utilities/lexicographicSortSchema.js new file mode 100644 index 0000000000..09f56d74d9 --- /dev/null +++ b/dist/utilities/lexicographicSortSchema.js @@ -0,0 +1,219 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.lexicographicSortSchema = lexicographicSortSchema; + +var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _schema = require("../type/schema"); + +var _directives = require("../type/directives"); + +var _definition = require("../type/definition"); + +var _scalars = require("../type/scalars"); + +var _introspection = require("../type/introspection"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * Sort GraphQLSchema. + */ +function lexicographicSortSchema(schema) { + var cache = Object.create(null); + + var sortMaybeType = function sortMaybeType(maybeType) { + return maybeType && sortNamedType(maybeType); + }; + + return new _schema.GraphQLSchema({ + types: sortTypes((0, _objectValues.default)(schema.getTypeMap())), + directives: sortByName(schema.getDirectives()).map(sortDirective), + query: sortMaybeType(schema.getQueryType()), + mutation: sortMaybeType(schema.getMutationType()), + subscription: sortMaybeType(schema.getSubscriptionType()), + astNode: schema.astNode + }); + + function sortDirective(directive) { + return new _directives.GraphQLDirective({ + name: directive.name, + description: directive.description, + locations: sortBy(directive.locations, function (x) { + return x; + }), + args: sortArgs(directive.args), + astNode: directive.astNode + }); + } + + function sortArgs(args) { + return (0, _keyValMap.default)(sortByName(args), function (arg) { + return arg.name; + }, function (arg) { + return _objectSpread({}, arg, { + type: sortType(arg.type) + }); + }); + } + + function sortFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return { + type: sortType(field.type), + args: sortArgs(field.args), + resolve: field.resolve, + subscribe: field.subscribe, + deprecationReason: field.deprecationReason, + description: field.description, + astNode: field.astNode + }; + }); + } + + function sortInputFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return { + type: sortType(field.type), + defaultValue: field.defaultValue, + description: field.description, + astNode: field.astNode + }; + }); + } + + function sortType(type) { + if ((0, _definition.isListType)(type)) { + return new _definition.GraphQLList(sortType(type.ofType)); + } else if ((0, _definition.isNonNullType)(type)) { + return new _definition.GraphQLNonNull(sortType(type.ofType)); + } + + return sortNamedType(type); + } + + function sortTypes(arr) { + return sortByName(arr).map(sortNamedType); + } + + function sortNamedType(type) { + if ((0, _scalars.isSpecifiedScalarType)(type) || (0, _introspection.isIntrospectionType)(type)) { + return type; + } + + var sortedType = cache[type.name]; + + if (!sortedType) { + sortedType = sortNamedTypeImpl(type); + cache[type.name] = sortedType; + } + + return sortedType; + } + + function sortNamedTypeImpl(type) { + if ((0, _definition.isScalarType)(type)) { + return type; + } else if ((0, _definition.isObjectType)(type)) { + return new _definition.GraphQLObjectType({ + name: type.name, + interfaces: function interfaces() { + return sortTypes(type.getInterfaces()); + }, + fields: function fields() { + return sortFields(type.getFields()); + }, + isTypeOf: type.isTypeOf, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes + }); + } else if ((0, _definition.isInterfaceType)(type)) { + return new _definition.GraphQLInterfaceType({ + name: type.name, + fields: function fields() { + return sortFields(type.getFields()); + }, + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes + }); + } else if ((0, _definition.isUnionType)(type)) { + return new _definition.GraphQLUnionType({ + name: type.name, + types: function types() { + return sortTypes(type.getTypes()); + }, + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode + }); + } else if ((0, _definition.isEnumType)(type)) { + return new _definition.GraphQLEnumType({ + name: type.name, + values: (0, _keyValMap.default)(sortByName(type.getValues()), function (val) { + return val.name; + }, function (val) { + return { + value: val.value, + deprecationReason: val.deprecationReason, + description: val.description, + astNode: val.astNode + }; + }), + description: type.description, + astNode: type.astNode + }); + } else if ((0, _definition.isInputObjectType)(type)) { + return new _definition.GraphQLInputObjectType({ + name: type.name, + fields: function fields() { + return sortInputFields(type.getFields()); + }, + description: type.description, + astNode: type.astNode + }); + } + + throw new Error("Unknown type: \"".concat(type, "\"")); + } +} + +function sortObjMap(map, sortValueFn) { + var sortedMap = Object.create(null); + var sortedKeys = sortBy(Object.keys(map), function (x) { + return x; + }); + + for (var _i = 0; _i < sortedKeys.length; _i++) { + var key = sortedKeys[_i]; + var value = map[key]; + sortedMap[key] = sortValueFn ? sortValueFn(value) : value; + } + + return sortedMap; +} + +function sortByName(array) { + return sortBy(array, function (obj) { + return obj.name; + }); +} + +function sortBy(array, mapToKey) { + return array.slice().sort(function (obj1, obj2) { + var key1 = mapToKey(obj1); + var key2 = mapToKey(obj2); + return key1.localeCompare(key2); + }); +} \ No newline at end of file diff --git a/dist/utilities/lexicographicSortSchema.js.flow b/dist/utilities/lexicographicSortSchema.js.flow new file mode 100644 index 0000000000..570d3e45f9 --- /dev/null +++ b/dist/utilities/lexicographicSortSchema.js.flow @@ -0,0 +1,198 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from '../jsutils/ObjMap'; +import keyValMap from '../jsutils/keyValMap'; +import objectValues from '../jsutils/objectValues'; +import { GraphQLSchema } from '../type/schema'; +import { GraphQLDirective } from '../type/directives'; +import type { GraphQLNamedType } from '../type/definition'; +import { + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLEnumType, + GraphQLInputObjectType, + GraphQLList, + GraphQLNonNull, + isListType, + isNonNullType, + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, +} from '../type/definition'; +import { isSpecifiedScalarType } from '../type/scalars'; +import { isIntrospectionType } from '../type/introspection'; + +/** + * Sort GraphQLSchema. + */ +export function lexicographicSortSchema(schema: GraphQLSchema): GraphQLSchema { + const cache = Object.create(null); + + const sortMaybeType = maybeType => maybeType && sortNamedType(maybeType); + return new GraphQLSchema({ + types: sortTypes(objectValues(schema.getTypeMap())), + directives: sortByName(schema.getDirectives()).map(sortDirective), + query: sortMaybeType(schema.getQueryType()), + mutation: sortMaybeType(schema.getMutationType()), + subscription: sortMaybeType(schema.getSubscriptionType()), + astNode: schema.astNode, + }); + + function sortDirective(directive) { + return new GraphQLDirective({ + name: directive.name, + description: directive.description, + locations: sortBy(directive.locations, x => x), + args: sortArgs(directive.args), + astNode: directive.astNode, + }); + } + + function sortArgs(args) { + return keyValMap( + sortByName(args), + arg => arg.name, + arg => ({ + ...arg, + type: sortType(arg.type), + }), + ); + } + + function sortFields(fieldsMap) { + return sortObjMap(fieldsMap, field => ({ + type: sortType(field.type), + args: sortArgs(field.args), + resolve: field.resolve, + subscribe: field.subscribe, + deprecationReason: field.deprecationReason, + description: field.description, + astNode: field.astNode, + })); + } + + function sortInputFields(fieldsMap) { + return sortObjMap(fieldsMap, field => ({ + type: sortType(field.type), + defaultValue: field.defaultValue, + description: field.description, + astNode: field.astNode, + })); + } + + function sortType(type) { + if (isListType(type)) { + return new GraphQLList(sortType(type.ofType)); + } else if (isNonNullType(type)) { + return new GraphQLNonNull(sortType(type.ofType)); + } + return sortNamedType(type); + } + + function sortTypes(arr: Array): Array { + return sortByName(arr).map(sortNamedType); + } + + function sortNamedType(type: T): T { + if (isSpecifiedScalarType(type) || isIntrospectionType(type)) { + return type; + } + + let sortedType = cache[type.name]; + if (!sortedType) { + sortedType = sortNamedTypeImpl(type); + cache[type.name] = sortedType; + } + return ((sortedType: any): T); + } + + function sortNamedTypeImpl(type) { + if (isScalarType(type)) { + return type; + } else if (isObjectType(type)) { + return new GraphQLObjectType({ + name: type.name, + interfaces: () => sortTypes(type.getInterfaces()), + fields: () => sortFields(type.getFields()), + isTypeOf: type.isTypeOf, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes, + }); + } else if (isInterfaceType(type)) { + return new GraphQLInterfaceType({ + name: type.name, + fields: () => sortFields(type.getFields()), + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes, + }); + } else if (isUnionType(type)) { + return new GraphQLUnionType({ + name: type.name, + types: () => sortTypes(type.getTypes()), + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode, + }); + } else if (isEnumType(type)) { + return new GraphQLEnumType({ + name: type.name, + values: keyValMap( + sortByName(type.getValues()), + val => val.name, + val => ({ + value: val.value, + deprecationReason: val.deprecationReason, + description: val.description, + astNode: val.astNode, + }), + ), + description: type.description, + astNode: type.astNode, + }); + } else if (isInputObjectType(type)) { + return new GraphQLInputObjectType({ + name: type.name, + fields: () => sortInputFields(type.getFields()), + description: type.description, + astNode: type.astNode, + }); + } + throw new Error(`Unknown type: "${type}"`); + } +} + +function sortObjMap(map: ObjMap, sortValueFn?: T => R): ObjMap { + const sortedMap = Object.create(null); + const sortedKeys = sortBy(Object.keys(map), x => x); + for (const key of sortedKeys) { + const value = map[key]; + sortedMap[key] = sortValueFn ? sortValueFn(value) : value; + } + return sortedMap; +} + +function sortByName(array: $ReadOnlyArray): Array { + return sortBy(array, obj => obj.name); +} + +function sortBy(array: $ReadOnlyArray, mapToKey: T => string): Array { + return array.slice().sort((obj1, obj2) => { + const key1 = mapToKey(obj1); + const key2 = mapToKey(obj2); + return key1.localeCompare(key2); + }); +} diff --git a/dist/utilities/lexicographicSortSchema.mjs b/dist/utilities/lexicographicSortSchema.mjs new file mode 100644 index 0000000000..fc0841a59d --- /dev/null +++ b/dist/utilities/lexicographicSortSchema.mjs @@ -0,0 +1,212 @@ +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import keyValMap from '../jsutils/keyValMap'; +import objectValues from '../jsutils/objectValues'; +import { GraphQLSchema } from '../type/schema'; +import { GraphQLDirective } from '../type/directives'; +import { GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, isListType, isNonNullType, isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from '../type/definition'; +import { isSpecifiedScalarType } from '../type/scalars'; +import { isIntrospectionType } from '../type/introspection'; +/** + * Sort GraphQLSchema. + */ + +export function lexicographicSortSchema(schema) { + var cache = Object.create(null); + + var sortMaybeType = function sortMaybeType(maybeType) { + return maybeType && sortNamedType(maybeType); + }; + + return new GraphQLSchema({ + types: sortTypes(objectValues(schema.getTypeMap())), + directives: sortByName(schema.getDirectives()).map(sortDirective), + query: sortMaybeType(schema.getQueryType()), + mutation: sortMaybeType(schema.getMutationType()), + subscription: sortMaybeType(schema.getSubscriptionType()), + astNode: schema.astNode + }); + + function sortDirective(directive) { + return new GraphQLDirective({ + name: directive.name, + description: directive.description, + locations: sortBy(directive.locations, function (x) { + return x; + }), + args: sortArgs(directive.args), + astNode: directive.astNode + }); + } + + function sortArgs(args) { + return keyValMap(sortByName(args), function (arg) { + return arg.name; + }, function (arg) { + return _objectSpread({}, arg, { + type: sortType(arg.type) + }); + }); + } + + function sortFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return { + type: sortType(field.type), + args: sortArgs(field.args), + resolve: field.resolve, + subscribe: field.subscribe, + deprecationReason: field.deprecationReason, + description: field.description, + astNode: field.astNode + }; + }); + } + + function sortInputFields(fieldsMap) { + return sortObjMap(fieldsMap, function (field) { + return { + type: sortType(field.type), + defaultValue: field.defaultValue, + description: field.description, + astNode: field.astNode + }; + }); + } + + function sortType(type) { + if (isListType(type)) { + return new GraphQLList(sortType(type.ofType)); + } else if (isNonNullType(type)) { + return new GraphQLNonNull(sortType(type.ofType)); + } + + return sortNamedType(type); + } + + function sortTypes(arr) { + return sortByName(arr).map(sortNamedType); + } + + function sortNamedType(type) { + if (isSpecifiedScalarType(type) || isIntrospectionType(type)) { + return type; + } + + var sortedType = cache[type.name]; + + if (!sortedType) { + sortedType = sortNamedTypeImpl(type); + cache[type.name] = sortedType; + } + + return sortedType; + } + + function sortNamedTypeImpl(type) { + if (isScalarType(type)) { + return type; + } else if (isObjectType(type)) { + return new GraphQLObjectType({ + name: type.name, + interfaces: function interfaces() { + return sortTypes(type.getInterfaces()); + }, + fields: function fields() { + return sortFields(type.getFields()); + }, + isTypeOf: type.isTypeOf, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes + }); + } else if (isInterfaceType(type)) { + return new GraphQLInterfaceType({ + name: type.name, + fields: function fields() { + return sortFields(type.getFields()); + }, + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode, + extensionASTNodes: type.extensionASTNodes + }); + } else if (isUnionType(type)) { + return new GraphQLUnionType({ + name: type.name, + types: function types() { + return sortTypes(type.getTypes()); + }, + resolveType: type.resolveType, + description: type.description, + astNode: type.astNode + }); + } else if (isEnumType(type)) { + return new GraphQLEnumType({ + name: type.name, + values: keyValMap(sortByName(type.getValues()), function (val) { + return val.name; + }, function (val) { + return { + value: val.value, + deprecationReason: val.deprecationReason, + description: val.description, + astNode: val.astNode + }; + }), + description: type.description, + astNode: type.astNode + }); + } else if (isInputObjectType(type)) { + return new GraphQLInputObjectType({ + name: type.name, + fields: function fields() { + return sortInputFields(type.getFields()); + }, + description: type.description, + astNode: type.astNode + }); + } + + throw new Error("Unknown type: \"".concat(type, "\"")); + } +} + +function sortObjMap(map, sortValueFn) { + var sortedMap = Object.create(null); + var sortedKeys = sortBy(Object.keys(map), function (x) { + return x; + }); + + for (var _i = 0; _i < sortedKeys.length; _i++) { + var key = sortedKeys[_i]; + var value = map[key]; + sortedMap[key] = sortValueFn ? sortValueFn(value) : value; + } + + return sortedMap; +} + +function sortByName(array) { + return sortBy(array, function (obj) { + return obj.name; + }); +} + +function sortBy(array, mapToKey) { + return array.slice().sort(function (obj1, obj2) { + var key1 = mapToKey(obj1); + var key2 = mapToKey(obj2); + return key1.localeCompare(key2); + }); +} \ No newline at end of file diff --git a/dist/utilities/schemaPrinter.js b/dist/utilities/schemaPrinter.js new file mode 100644 index 0000000000..3355b22fed --- /dev/null +++ b/dist/utilities/schemaPrinter.js @@ -0,0 +1,342 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.printSchema = printSchema; +exports.printIntrospectionSchema = printIntrospectionSchema; +exports.printType = printType; + +var _isNullish = _interopRequireDefault(require("../jsutils/isNullish")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _astFromValue = require("../utilities/astFromValue"); + +var _printer = require("../language/printer"); + +var _definition = require("../type/definition"); + +var _scalars = require("../type/scalars"); + +var _directives = require("../type/directives"); + +var _introspection = require("../type/introspection"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +function printSchema(schema, options) { + return printFilteredSchema(schema, function (n) { + return !(0, _directives.isSpecifiedDirective)(n); + }, isDefinedType, options); +} + +function printIntrospectionSchema(schema, options) { + return printFilteredSchema(schema, _directives.isSpecifiedDirective, _introspection.isIntrospectionType, options); +} + +function isDefinedType(type) { + return !(0, _scalars.isSpecifiedScalarType)(type) && !(0, _introspection.isIntrospectionType)(type); +} + +function printFilteredSchema(schema, directiveFilter, typeFilter, options) { + var directives = schema.getDirectives().filter(directiveFilter); + var typeMap = schema.getTypeMap(); + var types = (0, _objectValues.default)(typeMap).sort(function (type1, type2) { + return type1.name.localeCompare(type2.name); + }).filter(typeFilter); + return [printSchemaDefinition(schema)].concat(directives.map(function (directive) { + return printDirective(directive, options); + }), types.map(function (type) { + return printType(type, options); + })).filter(Boolean).join('\n\n') + '\n'; +} + +function printSchemaDefinition(schema) { + if (isSchemaOfCommonNames(schema)) { + return; + } + + var operationTypes = []; + var queryType = schema.getQueryType(); + + if (queryType) { + operationTypes.push(" query: ".concat(queryType.name)); + } + + var mutationType = schema.getMutationType(); + + if (mutationType) { + operationTypes.push(" mutation: ".concat(mutationType.name)); + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType) { + operationTypes.push(" subscription: ".concat(subscriptionType.name)); + } + + return "schema {\n".concat(operationTypes.join('\n'), "\n}"); +} +/** + * GraphQL schema define root types for each type of operation. These types are + * the same as any other type and can be named in any manner, however there is + * a common naming convention: + * + * schema { + * query: Query + * mutation: Mutation + * } + * + * When using this naming convention, the schema description can be omitted. + */ + + +function isSchemaOfCommonNames(schema) { + var queryType = schema.getQueryType(); + + if (queryType && queryType.name !== 'Query') { + return false; + } + + var mutationType = schema.getMutationType(); + + if (mutationType && mutationType.name !== 'Mutation') { + return false; + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType && subscriptionType.name !== 'Subscription') { + return false; + } + + return true; +} + +function printType(type, options) { + if ((0, _definition.isScalarType)(type)) { + return printScalar(type, options); + } else if ((0, _definition.isObjectType)(type)) { + return printObject(type, options); + } else if ((0, _definition.isInterfaceType)(type)) { + return printInterface(type, options); + } else if ((0, _definition.isUnionType)(type)) { + return printUnion(type, options); + } else if ((0, _definition.isEnumType)(type)) { + return printEnum(type, options); + } else if ((0, _definition.isInputObjectType)(type)) { + return printInputObject(type, options); + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} + +function printScalar(type, options) { + return printDescription(options, type) + "scalar ".concat(type.name); +} + +function printObject(type, options) { + var interfaces = type.getInterfaces(); + var implementedInterfaces = interfaces.length ? ' implements ' + interfaces.map(function (i) { + return i.name; + }).join(' & ') : ''; + return printDescription(options, type) + "type ".concat(type.name).concat(implementedInterfaces, " {\n") + printFields(options, type) + '\n' + '}'; +} + +function printInterface(type, options) { + return printDescription(options, type) + "interface ".concat(type.name, " {\n") + printFields(options, type) + '\n' + '}'; +} + +function printUnion(type, options) { + return printDescription(options, type) + "union ".concat(type.name, " = ").concat(type.getTypes().join(' | ')); +} + +function printEnum(type, options) { + return printDescription(options, type) + "enum ".concat(type.name, " {\n") + printEnumValues(type.getValues(), options) + '\n' + '}'; +} + +function printEnumValues(values, options) { + return values.map(function (value, i) { + return printDescription(options, value, ' ', !i) + ' ' + value.name + printDeprecated(value); + }).join('\n'); +} + +function printInputObject(type, options) { + var fields = (0, _objectValues.default)(type.getFields()); + return printDescription(options, type) + "input ".concat(type.name, " {\n") + fields.map(function (f, i) { + return printDescription(options, f, ' ', !i) + ' ' + printInputValue(f); + }).join('\n') + '\n' + '}'; +} + +function printFields(options, type) { + var fields = (0, _objectValues.default)(type.getFields()); + return fields.map(function (f, i) { + return printDescription(options, f, ' ', !i) + ' ' + f.name + printArgs(options, f.args, ' ') + ': ' + String(f.type) + printDeprecated(f); + }).join('\n'); +} + +function printArgs(options, args) { + var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; + + if (args.length === 0) { + return ''; + } // If every arg does not have a description, print them on one line. + + + if (args.every(function (arg) { + return !arg.description; + })) { + return '(' + args.map(printInputValue).join(', ') + ')'; + } + + return '(\n' + args.map(function (arg, i) { + return printDescription(options, arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg); + }).join('\n') + '\n' + indentation + ')'; +} + +function printInputValue(arg) { + var argDecl = arg.name + ': ' + String(arg.type); + + if (!(0, _isInvalid.default)(arg.defaultValue)) { + argDecl += " = ".concat((0, _printer.print)((0, _astFromValue.astFromValue)(arg.defaultValue, arg.type))); + } + + return argDecl; +} + +function printDirective(directive, options) { + return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + ' on ' + directive.locations.join(' | '); +} + +function printDeprecated(fieldOrEnumVal) { + if (!fieldOrEnumVal.isDeprecated) { + return ''; + } + + var reason = fieldOrEnumVal.deprecationReason; + + if ((0, _isNullish.default)(reason) || reason === '' || reason === _directives.DEFAULT_DEPRECATION_REASON) { + return ' @deprecated'; + } + + return ' @deprecated(reason: ' + (0, _printer.print)((0, _astFromValue.astFromValue)(reason, _scalars.GraphQLString)) + ')'; +} + +function printDescription(options, def) { + var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; + var firstInBlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; + + if (!def.description) { + return ''; + } + + var lines = descriptionLines(def.description, 120 - indentation.length); + + if (options && options.commentDescriptions) { + return printDescriptionWithComments(lines, indentation, firstInBlock); + } + + var description = indentation && !firstInBlock ? '\n' + indentation + '"""' : indentation + '"""'; // In some circumstances, a single line can be used for the description. + + if (lines.length === 1 && lines[0].length < 70 && lines[0][lines[0].length - 1] !== '"') { + return description + escapeQuote(lines[0]) + '"""\n'; + } // Format a multi-line block quote to account for leading space. + + + var hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t'; + + if (!hasLeadingSpace) { + description += '\n'; + } + + for (var i = 0; i < lines.length; i++) { + if (i !== 0 || !hasLeadingSpace) { + description += indentation; + } + + description += escapeQuote(lines[i]) + '\n'; + } + + description += indentation + '"""\n'; + return description; +} + +function escapeQuote(line) { + return line.replace(/"""/g, '\\"""'); +} + +function printDescriptionWithComments(lines, indentation, firstInBlock) { + var description = indentation && !firstInBlock ? '\n' : ''; + + for (var i = 0; i < lines.length; i++) { + if (lines[i] === '') { + description += indentation + '#\n'; + } else { + description += indentation + '# ' + lines[i] + '\n'; + } + } + + return description; +} + +function descriptionLines(description, maxLen) { + var lines = []; + var rawLines = description.split('\n'); + + for (var i = 0; i < rawLines.length; i++) { + if (rawLines[i] === '') { + lines.push(rawLines[i]); + } else { + // For > 120 character long lines, cut at space boundaries into sublines + // of ~80 chars. + var sublines = breakLine(rawLines[i], maxLen); + + for (var j = 0; j < sublines.length; j++) { + lines.push(sublines[j]); + } + } + } + + return lines; +} + +function breakLine(line, maxLen) { + if (line.length < maxLen + 5) { + return [line]; + } + + var parts = line.split(new RegExp("((?: |^).{15,".concat(maxLen - 40, "}(?= |$))"))); + + if (parts.length < 4) { + return [line]; + } + + var sublines = [parts[0] + parts[1] + parts[2]]; + + for (var i = 3; i < parts.length; i += 2) { + sublines.push(parts[i].slice(1) + parts[i + 1]); + } + + return sublines; +} \ No newline at end of file diff --git a/dist/utilities/schemaPrinter.js.flow b/dist/utilities/schemaPrinter.js.flow new file mode 100644 index 0000000000..a630f27d23 --- /dev/null +++ b/dist/utilities/schemaPrinter.js.flow @@ -0,0 +1,415 @@ +/** + * 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. + * + * @flow strict + */ + +import isNullish from '../jsutils/isNullish'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { astFromValue } from '../utilities/astFromValue'; +import { print } from '../language/printer'; +import type { GraphQLSchema } from '../type/schema'; +import { + isScalarType, + isObjectType, + isInterfaceType, + isUnionType, + isEnumType, + isInputObjectType, +} from '../type/definition'; +import type { + GraphQLNamedType, + GraphQLScalarType, + GraphQLEnumType, + GraphQLObjectType, + GraphQLInterfaceType, + GraphQLUnionType, + GraphQLInputObjectType, +} from '../type/definition'; +import { GraphQLString, isSpecifiedScalarType } from '../type/scalars'; +import { + GraphQLDirective, + DEFAULT_DEPRECATION_REASON, + isSpecifiedDirective, +} from '../type/directives'; +import { isIntrospectionType } from '../type/introspection'; + +type Options = {| commentDescriptions?: boolean |}; + +/** + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function printSchema(schema: GraphQLSchema, options?: Options): string { + return printFilteredSchema( + schema, + n => !isSpecifiedDirective(n), + isDefinedType, + options, + ); +} + +export function printIntrospectionSchema( + schema: GraphQLSchema, + options?: Options, +): string { + return printFilteredSchema( + schema, + isSpecifiedDirective, + isIntrospectionType, + options, + ); +} + +function isDefinedType(type: GraphQLNamedType): boolean { + return !isSpecifiedScalarType(type) && !isIntrospectionType(type); +} + +function printFilteredSchema( + schema: GraphQLSchema, + directiveFilter: (type: GraphQLDirective) => boolean, + typeFilter: (type: GraphQLNamedType) => boolean, + options, +): string { + const directives = schema.getDirectives().filter(directiveFilter); + const typeMap = schema.getTypeMap(); + const types = objectValues(typeMap) + .sort((type1, type2) => type1.name.localeCompare(type2.name)) + .filter(typeFilter); + + return ( + [printSchemaDefinition(schema)] + .concat( + directives.map(directive => printDirective(directive, options)), + types.map(type => printType(type, options)), + ) + .filter(Boolean) + .join('\n\n') + '\n' + ); +} + +function printSchemaDefinition(schema: GraphQLSchema): ?string { + if (isSchemaOfCommonNames(schema)) { + return; + } + + const operationTypes = []; + + const queryType = schema.getQueryType(); + if (queryType) { + operationTypes.push(` query: ${queryType.name}`); + } + + const mutationType = schema.getMutationType(); + if (mutationType) { + operationTypes.push(` mutation: ${mutationType.name}`); + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType) { + operationTypes.push(` subscription: ${subscriptionType.name}`); + } + + return `schema {\n${operationTypes.join('\n')}\n}`; +} + +/** + * GraphQL schema define root types for each type of operation. These types are + * the same as any other type and can be named in any manner, however there is + * a common naming convention: + * + * schema { + * query: Query + * mutation: Mutation + * } + * + * When using this naming convention, the schema description can be omitted. + */ +function isSchemaOfCommonNames(schema: GraphQLSchema): boolean { + const queryType = schema.getQueryType(); + if (queryType && queryType.name !== 'Query') { + return false; + } + + const mutationType = schema.getMutationType(); + if (mutationType && mutationType.name !== 'Mutation') { + return false; + } + + const subscriptionType = schema.getSubscriptionType(); + if (subscriptionType && subscriptionType.name !== 'Subscription') { + return false; + } + + return true; +} + +export function printType(type: GraphQLNamedType, options?: Options): string { + if (isScalarType(type)) { + return printScalar(type, options); + } else if (isObjectType(type)) { + return printObject(type, options); + } else if (isInterfaceType(type)) { + return printInterface(type, options); + } else if (isUnionType(type)) { + return printUnion(type, options); + } else if (isEnumType(type)) { + return printEnum(type, options); + } else if (isInputObjectType(type)) { + return printInputObject(type, options); + } + /* istanbul ignore next */ + throw new Error(`Unknown type: ${(type: empty)}.`); +} + +function printScalar(type: GraphQLScalarType, options): string { + return printDescription(options, type) + `scalar ${type.name}`; +} + +function printObject(type: GraphQLObjectType, options): string { + const interfaces = type.getInterfaces(); + const implementedInterfaces = interfaces.length + ? ' implements ' + interfaces.map(i => i.name).join(' & ') + : ''; + return ( + printDescription(options, type) + + `type ${type.name}${implementedInterfaces} {\n` + + printFields(options, type) + + '\n' + + '}' + ); +} + +function printInterface(type: GraphQLInterfaceType, options): string { + return ( + printDescription(options, type) + + `interface ${type.name} {\n` + + printFields(options, type) + + '\n' + + '}' + ); +} + +function printUnion(type: GraphQLUnionType, options): string { + return ( + printDescription(options, type) + + `union ${type.name} = ${type.getTypes().join(' | ')}` + ); +} + +function printEnum(type: GraphQLEnumType, options): string { + return ( + printDescription(options, type) + + `enum ${type.name} {\n` + + printEnumValues(type.getValues(), options) + + '\n' + + '}' + ); +} + +function printEnumValues(values, options): string { + return values + .map( + (value, i) => + printDescription(options, value, ' ', !i) + + ' ' + + value.name + + printDeprecated(value), + ) + .join('\n'); +} + +function printInputObject(type: GraphQLInputObjectType, options): string { + const fields = objectValues(type.getFields()); + return ( + printDescription(options, type) + + `input ${type.name} {\n` + + fields + .map( + (f, i) => + printDescription(options, f, ' ', !i) + ' ' + printInputValue(f), + ) + .join('\n') + + '\n' + + '}' + ); +} + +function printFields(options, type) { + const fields = objectValues(type.getFields()); + return fields + .map( + (f, i) => + printDescription(options, f, ' ', !i) + + ' ' + + f.name + + printArgs(options, f.args, ' ') + + ': ' + + String(f.type) + + printDeprecated(f), + ) + .join('\n'); +} + +function printArgs(options, args, indentation = '') { + if (args.length === 0) { + return ''; + } + + // If every arg does not have a description, print them on one line. + if (args.every(arg => !arg.description)) { + return '(' + args.map(printInputValue).join(', ') + ')'; + } + + return ( + '(\n' + + args + .map( + (arg, i) => + printDescription(options, arg, ' ' + indentation, !i) + + ' ' + + indentation + + printInputValue(arg), + ) + .join('\n') + + '\n' + + indentation + + ')' + ); +} + +function printInputValue(arg) { + let argDecl = arg.name + ': ' + String(arg.type); + if (!isInvalid(arg.defaultValue)) { + argDecl += ` = ${print(astFromValue(arg.defaultValue, arg.type))}`; + } + return argDecl; +} + +function printDirective(directive, options) { + return ( + printDescription(options, directive) + + 'directive @' + + directive.name + + printArgs(options, directive.args) + + ' on ' + + directive.locations.join(' | ') + ); +} + +function printDeprecated(fieldOrEnumVal) { + if (!fieldOrEnumVal.isDeprecated) { + return ''; + } + const reason = fieldOrEnumVal.deprecationReason; + if ( + isNullish(reason) || + reason === '' || + reason === DEFAULT_DEPRECATION_REASON + ) { + return ' @deprecated'; + } + return ( + ' @deprecated(reason: ' + print(astFromValue(reason, GraphQLString)) + ')' + ); +} + +function printDescription( + options, + def, + indentation = '', + firstInBlock = true, +): string { + if (!def.description) { + return ''; + } + + const lines = descriptionLines(def.description, 120 - indentation.length); + if (options && options.commentDescriptions) { + return printDescriptionWithComments(lines, indentation, firstInBlock); + } + + let description = + indentation && !firstInBlock + ? '\n' + indentation + '"""' + : indentation + '"""'; + + // In some circumstances, a single line can be used for the description. + if ( + lines.length === 1 && + lines[0].length < 70 && + lines[0][lines[0].length - 1] !== '"' + ) { + return description + escapeQuote(lines[0]) + '"""\n'; + } + + // Format a multi-line block quote to account for leading space. + const hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t'; + if (!hasLeadingSpace) { + description += '\n'; + } + for (let i = 0; i < lines.length; i++) { + if (i !== 0 || !hasLeadingSpace) { + description += indentation; + } + description += escapeQuote(lines[i]) + '\n'; + } + description += indentation + '"""\n'; + return description; +} + +function escapeQuote(line) { + return line.replace(/"""/g, '\\"""'); +} + +function printDescriptionWithComments(lines, indentation, firstInBlock) { + let description = indentation && !firstInBlock ? '\n' : ''; + for (let i = 0; i < lines.length; i++) { + if (lines[i] === '') { + description += indentation + '#\n'; + } else { + description += indentation + '# ' + lines[i] + '\n'; + } + } + return description; +} + +function descriptionLines(description: string, maxLen: number): Array { + const lines = []; + const rawLines = description.split('\n'); + for (let i = 0; i < rawLines.length; i++) { + if (rawLines[i] === '') { + lines.push(rawLines[i]); + } else { + // For > 120 character long lines, cut at space boundaries into sublines + // of ~80 chars. + const sublines = breakLine(rawLines[i], maxLen); + for (let j = 0; j < sublines.length; j++) { + lines.push(sublines[j]); + } + } + } + return lines; +} + +function breakLine(line: string, maxLen: number): Array { + if (line.length < maxLen + 5) { + return [line]; + } + const parts = line.split(new RegExp(`((?: |^).{15,${maxLen - 40}}(?= |$))`)); + if (parts.length < 4) { + return [line]; + } + const sublines = [parts[0] + parts[1] + parts[2]]; + for (let i = 3; i < parts.length; i += 2) { + sublines.push(parts[i].slice(1) + parts[i + 1]); + } + return sublines; +} diff --git a/dist/utilities/schemaPrinter.mjs b/dist/utilities/schemaPrinter.mjs new file mode 100644 index 0000000000..cfc27d328d --- /dev/null +++ b/dist/utilities/schemaPrinter.mjs @@ -0,0 +1,321 @@ +/** + * 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. + * + * strict + */ +import isNullish from '../jsutils/isNullish'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { astFromValue } from '../utilities/astFromValue'; +import { print } from '../language/printer'; +import { isScalarType, isObjectType, isInterfaceType, isUnionType, isEnumType, isInputObjectType } from '../type/definition'; +import { GraphQLString, isSpecifiedScalarType } from '../type/scalars'; +import { GraphQLDirective, DEFAULT_DEPRECATION_REASON, isSpecifiedDirective } from '../type/directives'; +import { isIntrospectionType } from '../type/introspection'; + +/** + * Accepts options as a second argument: + * + * - commentDescriptions: + * Provide true to use preceding comments as the description. + * + */ +export function printSchema(schema, options) { + return printFilteredSchema(schema, function (n) { + return !isSpecifiedDirective(n); + }, isDefinedType, options); +} +export function printIntrospectionSchema(schema, options) { + return printFilteredSchema(schema, isSpecifiedDirective, isIntrospectionType, options); +} + +function isDefinedType(type) { + return !isSpecifiedScalarType(type) && !isIntrospectionType(type); +} + +function printFilteredSchema(schema, directiveFilter, typeFilter, options) { + var directives = schema.getDirectives().filter(directiveFilter); + var typeMap = schema.getTypeMap(); + var types = objectValues(typeMap).sort(function (type1, type2) { + return type1.name.localeCompare(type2.name); + }).filter(typeFilter); + return [printSchemaDefinition(schema)].concat(directives.map(function (directive) { + return printDirective(directive, options); + }), types.map(function (type) { + return printType(type, options); + })).filter(Boolean).join('\n\n') + '\n'; +} + +function printSchemaDefinition(schema) { + if (isSchemaOfCommonNames(schema)) { + return; + } + + var operationTypes = []; + var queryType = schema.getQueryType(); + + if (queryType) { + operationTypes.push(" query: ".concat(queryType.name)); + } + + var mutationType = schema.getMutationType(); + + if (mutationType) { + operationTypes.push(" mutation: ".concat(mutationType.name)); + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType) { + operationTypes.push(" subscription: ".concat(subscriptionType.name)); + } + + return "schema {\n".concat(operationTypes.join('\n'), "\n}"); +} +/** + * GraphQL schema define root types for each type of operation. These types are + * the same as any other type and can be named in any manner, however there is + * a common naming convention: + * + * schema { + * query: Query + * mutation: Mutation + * } + * + * When using this naming convention, the schema description can be omitted. + */ + + +function isSchemaOfCommonNames(schema) { + var queryType = schema.getQueryType(); + + if (queryType && queryType.name !== 'Query') { + return false; + } + + var mutationType = schema.getMutationType(); + + if (mutationType && mutationType.name !== 'Mutation') { + return false; + } + + var subscriptionType = schema.getSubscriptionType(); + + if (subscriptionType && subscriptionType.name !== 'Subscription') { + return false; + } + + return true; +} + +export function printType(type, options) { + if (isScalarType(type)) { + return printScalar(type, options); + } else if (isObjectType(type)) { + return printObject(type, options); + } else if (isInterfaceType(type)) { + return printInterface(type, options); + } else if (isUnionType(type)) { + return printUnion(type, options); + } else if (isEnumType(type)) { + return printEnum(type, options); + } else if (isInputObjectType(type)) { + return printInputObject(type, options); + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} + +function printScalar(type, options) { + return printDescription(options, type) + "scalar ".concat(type.name); +} + +function printObject(type, options) { + var interfaces = type.getInterfaces(); + var implementedInterfaces = interfaces.length ? ' implements ' + interfaces.map(function (i) { + return i.name; + }).join(' & ') : ''; + return printDescription(options, type) + "type ".concat(type.name).concat(implementedInterfaces, " {\n") + printFields(options, type) + '\n' + '}'; +} + +function printInterface(type, options) { + return printDescription(options, type) + "interface ".concat(type.name, " {\n") + printFields(options, type) + '\n' + '}'; +} + +function printUnion(type, options) { + return printDescription(options, type) + "union ".concat(type.name, " = ").concat(type.getTypes().join(' | ')); +} + +function printEnum(type, options) { + return printDescription(options, type) + "enum ".concat(type.name, " {\n") + printEnumValues(type.getValues(), options) + '\n' + '}'; +} + +function printEnumValues(values, options) { + return values.map(function (value, i) { + return printDescription(options, value, ' ', !i) + ' ' + value.name + printDeprecated(value); + }).join('\n'); +} + +function printInputObject(type, options) { + var fields = objectValues(type.getFields()); + return printDescription(options, type) + "input ".concat(type.name, " {\n") + fields.map(function (f, i) { + return printDescription(options, f, ' ', !i) + ' ' + printInputValue(f); + }).join('\n') + '\n' + '}'; +} + +function printFields(options, type) { + var fields = objectValues(type.getFields()); + return fields.map(function (f, i) { + return printDescription(options, f, ' ', !i) + ' ' + f.name + printArgs(options, f.args, ' ') + ': ' + String(f.type) + printDeprecated(f); + }).join('\n'); +} + +function printArgs(options, args) { + var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; + + if (args.length === 0) { + return ''; + } // If every arg does not have a description, print them on one line. + + + if (args.every(function (arg) { + return !arg.description; + })) { + return '(' + args.map(printInputValue).join(', ') + ')'; + } + + return '(\n' + args.map(function (arg, i) { + return printDescription(options, arg, ' ' + indentation, !i) + ' ' + indentation + printInputValue(arg); + }).join('\n') + '\n' + indentation + ')'; +} + +function printInputValue(arg) { + var argDecl = arg.name + ': ' + String(arg.type); + + if (!isInvalid(arg.defaultValue)) { + argDecl += " = ".concat(print(astFromValue(arg.defaultValue, arg.type))); + } + + return argDecl; +} + +function printDirective(directive, options) { + return printDescription(options, directive) + 'directive @' + directive.name + printArgs(options, directive.args) + ' on ' + directive.locations.join(' | '); +} + +function printDeprecated(fieldOrEnumVal) { + if (!fieldOrEnumVal.isDeprecated) { + return ''; + } + + var reason = fieldOrEnumVal.deprecationReason; + + if (isNullish(reason) || reason === '' || reason === DEFAULT_DEPRECATION_REASON) { + return ' @deprecated'; + } + + return ' @deprecated(reason: ' + print(astFromValue(reason, GraphQLString)) + ')'; +} + +function printDescription(options, def) { + var indentation = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; + var firstInBlock = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; + + if (!def.description) { + return ''; + } + + var lines = descriptionLines(def.description, 120 - indentation.length); + + if (options && options.commentDescriptions) { + return printDescriptionWithComments(lines, indentation, firstInBlock); + } + + var description = indentation && !firstInBlock ? '\n' + indentation + '"""' : indentation + '"""'; // In some circumstances, a single line can be used for the description. + + if (lines.length === 1 && lines[0].length < 70 && lines[0][lines[0].length - 1] !== '"') { + return description + escapeQuote(lines[0]) + '"""\n'; + } // Format a multi-line block quote to account for leading space. + + + var hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t'; + + if (!hasLeadingSpace) { + description += '\n'; + } + + for (var i = 0; i < lines.length; i++) { + if (i !== 0 || !hasLeadingSpace) { + description += indentation; + } + + description += escapeQuote(lines[i]) + '\n'; + } + + description += indentation + '"""\n'; + return description; +} + +function escapeQuote(line) { + return line.replace(/"""/g, '\\"""'); +} + +function printDescriptionWithComments(lines, indentation, firstInBlock) { + var description = indentation && !firstInBlock ? '\n' : ''; + + for (var i = 0; i < lines.length; i++) { + if (lines[i] === '') { + description += indentation + '#\n'; + } else { + description += indentation + '# ' + lines[i] + '\n'; + } + } + + return description; +} + +function descriptionLines(description, maxLen) { + var lines = []; + var rawLines = description.split('\n'); + + for (var i = 0; i < rawLines.length; i++) { + if (rawLines[i] === '') { + lines.push(rawLines[i]); + } else { + // For > 120 character long lines, cut at space boundaries into sublines + // of ~80 chars. + var sublines = breakLine(rawLines[i], maxLen); + + for (var j = 0; j < sublines.length; j++) { + lines.push(sublines[j]); + } + } + } + + return lines; +} + +function breakLine(line, maxLen) { + if (line.length < maxLen + 5) { + return [line]; + } + + var parts = line.split(new RegExp("((?: |^).{15,".concat(maxLen - 40, "}(?= |$))"))); + + if (parts.length < 4) { + return [line]; + } + + var sublines = [parts[0] + parts[1] + parts[2]]; + + for (var i = 3; i < parts.length; i += 2) { + sublines.push(parts[i].slice(1) + parts[i + 1]); + } + + return sublines; +} \ No newline at end of file diff --git a/dist/utilities/separateOperations.js b/dist/utilities/separateOperations.js new file mode 100644 index 0000000000..54d4053228 --- /dev/null +++ b/dist/utilities/separateOperations.js @@ -0,0 +1,91 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.separateOperations = separateOperations; + +var _visitor = require("../language/visitor"); + +/** + * 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. + * + * strict + */ + +/** + * separateOperations accepts a single AST document which may contain many + * operations and fragments and returns a collection of AST documents each of + * which contains a single operation as well the fragment definitions it + * refers to. + */ +function separateOperations(documentAST) { + var operations = []; + var fragments = Object.create(null); + var positions = new Map(); + var depGraph = Object.create(null); + var fromName; + var idx = 0; // Populate metadata and build a dependency graph. + + (0, _visitor.visit)(documentAST, { + OperationDefinition: function OperationDefinition(node) { + fromName = opName(node); + operations.push(node); + positions.set(node, idx++); + }, + FragmentDefinition: function FragmentDefinition(node) { + fromName = node.name.value; + fragments[fromName] = node; + positions.set(node, idx++); + }, + FragmentSpread: function FragmentSpread(node) { + var toName = node.name.value; + (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[toName] = true; + } + }); // For each operation, produce a new synthesized AST which includes only what + // is necessary for completing that operation. + + var separatedDocumentASTs = Object.create(null); + operations.forEach(function (operation) { + var operationName = opName(operation); + var dependencies = Object.create(null); + collectTransitiveDependencies(dependencies, depGraph, operationName); // The list of definition nodes to be included for this operation, sorted + // to retain the same order as the original document. + + var definitions = [operation]; + Object.keys(dependencies).forEach(function (name) { + definitions.push(fragments[name]); + }); + definitions.sort(function (n1, n2) { + return (positions.get(n1) || 0) - (positions.get(n2) || 0); + }); + separatedDocumentASTs[operationName] = { + kind: 'Document', + definitions: definitions + }; + }); + return separatedDocumentASTs; +} + +// Provides the empty string for anonymous operations. +function opName(operation) { + return operation.name ? operation.name.value : ''; +} // From a dependency graph, collects a list of transitive dependencies by +// recursing through a dependency graph. + + +function collectTransitiveDependencies(collected, depGraph, fromName) { + var immediateDeps = depGraph[fromName]; + + if (immediateDeps) { + Object.keys(immediateDeps).forEach(function (toName) { + if (!collected[toName]) { + collected[toName] = true; + collectTransitiveDependencies(collected, depGraph, toName); + } + }); + } +} \ No newline at end of file diff --git a/dist/utilities/separateOperations.js.flow b/dist/utilities/separateOperations.js.flow new file mode 100644 index 0000000000..4087fc1242 --- /dev/null +++ b/dist/utilities/separateOperations.js.flow @@ -0,0 +1,100 @@ +/** + * 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. + * + * @flow strict + */ + +import { visit } from '../language/visitor'; +import type { ObjMap } from '../jsutils/ObjMap'; +import type { DocumentNode, OperationDefinitionNode } from '../language/ast'; + +/** + * separateOperations accepts a single AST document which may contain many + * operations and fragments and returns a collection of AST documents each of + * which contains a single operation as well the fragment definitions it + * refers to. + */ +export function separateOperations( + documentAST: DocumentNode, +): ObjMap { + const operations = []; + const fragments = Object.create(null); + const positions = new Map(); + const depGraph: DepGraph = Object.create(null); + let fromName; + let idx = 0; + + // Populate metadata and build a dependency graph. + visit(documentAST, { + OperationDefinition(node) { + fromName = opName(node); + operations.push(node); + positions.set(node, idx++); + }, + FragmentDefinition(node) { + fromName = node.name.value; + fragments[fromName] = node; + positions.set(node, idx++); + }, + FragmentSpread(node) { + const toName = node.name.value; + (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[ + toName + ] = true; + }, + }); + + // For each operation, produce a new synthesized AST which includes only what + // is necessary for completing that operation. + const separatedDocumentASTs = Object.create(null); + operations.forEach(operation => { + const operationName = opName(operation); + const dependencies = Object.create(null); + collectTransitiveDependencies(dependencies, depGraph, operationName); + + // The list of definition nodes to be included for this operation, sorted + // to retain the same order as the original document. + const definitions = [operation]; + Object.keys(dependencies).forEach(name => { + definitions.push(fragments[name]); + }); + definitions.sort( + (n1, n2) => (positions.get(n1) || 0) - (positions.get(n2) || 0), + ); + + separatedDocumentASTs[operationName] = { + kind: 'Document', + definitions, + }; + }); + + return separatedDocumentASTs; +} + +type DepGraph = ObjMap>; + +// Provides the empty string for anonymous operations. +function opName(operation: OperationDefinitionNode): string { + return operation.name ? operation.name.value : ''; +} + +// From a dependency graph, collects a list of transitive dependencies by +// recursing through a dependency graph. +function collectTransitiveDependencies( + collected: ObjMap, + depGraph: DepGraph, + fromName: string, +): void { + const immediateDeps = depGraph[fromName]; + if (immediateDeps) { + Object.keys(immediateDeps).forEach(toName => { + if (!collected[toName]) { + collected[toName] = true; + collectTransitiveDependencies(collected, depGraph, toName); + } + }); + } +} diff --git a/dist/utilities/separateOperations.mjs b/dist/utilities/separateOperations.mjs new file mode 100644 index 0000000000..4d9b6e0848 --- /dev/null +++ b/dist/utilities/separateOperations.mjs @@ -0,0 +1,83 @@ +/** + * 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. + * + * strict + */ +import { visit } from '../language/visitor'; + +/** + * separateOperations accepts a single AST document which may contain many + * operations and fragments and returns a collection of AST documents each of + * which contains a single operation as well the fragment definitions it + * refers to. + */ +export function separateOperations(documentAST) { + var operations = []; + var fragments = Object.create(null); + var positions = new Map(); + var depGraph = Object.create(null); + var fromName; + var idx = 0; // Populate metadata and build a dependency graph. + + visit(documentAST, { + OperationDefinition: function OperationDefinition(node) { + fromName = opName(node); + operations.push(node); + positions.set(node, idx++); + }, + FragmentDefinition: function FragmentDefinition(node) { + fromName = node.name.value; + fragments[fromName] = node; + positions.set(node, idx++); + }, + FragmentSpread: function FragmentSpread(node) { + var toName = node.name.value; + (depGraph[fromName] || (depGraph[fromName] = Object.create(null)))[toName] = true; + } + }); // For each operation, produce a new synthesized AST which includes only what + // is necessary for completing that operation. + + var separatedDocumentASTs = Object.create(null); + operations.forEach(function (operation) { + var operationName = opName(operation); + var dependencies = Object.create(null); + collectTransitiveDependencies(dependencies, depGraph, operationName); // The list of definition nodes to be included for this operation, sorted + // to retain the same order as the original document. + + var definitions = [operation]; + Object.keys(dependencies).forEach(function (name) { + definitions.push(fragments[name]); + }); + definitions.sort(function (n1, n2) { + return (positions.get(n1) || 0) - (positions.get(n2) || 0); + }); + separatedDocumentASTs[operationName] = { + kind: 'Document', + definitions: definitions + }; + }); + return separatedDocumentASTs; +} + +// Provides the empty string for anonymous operations. +function opName(operation) { + return operation.name ? operation.name.value : ''; +} // From a dependency graph, collects a list of transitive dependencies by +// recursing through a dependency graph. + + +function collectTransitiveDependencies(collected, depGraph, fromName) { + var immediateDeps = depGraph[fromName]; + + if (immediateDeps) { + Object.keys(immediateDeps).forEach(function (toName) { + if (!collected[toName]) { + collected[toName] = true; + collectTransitiveDependencies(collected, depGraph, toName); + } + }); + } +} \ No newline at end of file diff --git a/dist/utilities/typeComparators.js b/dist/utilities/typeComparators.js new file mode 100644 index 0000000000..3a9e9d6671 --- /dev/null +++ b/dist/utilities/typeComparators.js @@ -0,0 +1,129 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.isEqualType = isEqualType; +exports.isTypeSubTypeOf = isTypeSubTypeOf; +exports.doTypesOverlap = doTypesOverlap; + +var _definition = require("../type/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. + * + * strict + */ + +/** + * Provided two types, return true if the types are equal (invariant). + */ +function isEqualType(typeA, typeB) { + // Equivalent types are equal. + if (typeA === typeB) { + return true; + } // If either type is non-null, the other must also be non-null. + + + if ((0, _definition.isNonNullType)(typeA) && (0, _definition.isNonNullType)(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } // If either type is a list, the other must also be a list. + + + if ((0, _definition.isListType)(typeA) && (0, _definition.isListType)(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } // Otherwise the types are not equal. + + + return false; +} +/** + * Provided a type and a super type, return true if the first type is either + * equal or a subset of the second super type (covariant). + */ + + +function isTypeSubTypeOf(schema, maybeSubType, superType) { + // Equivalent type is a valid subtype + if (maybeSubType === superType) { + return true; + } // If superType is non-null, maybeSubType must also be non-null. + + + if ((0, _definition.isNonNullType)(superType)) { + if ((0, _definition.isNonNullType)(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + + return false; + } + + if ((0, _definition.isNonNullType)(maybeSubType)) { + // If superType is nullable, maybeSubType may be non-null or nullable. + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType); + } // If superType type is a list, maybeSubType type must also be a list. + + + if ((0, _definition.isListType)(superType)) { + if ((0, _definition.isListType)(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + + return false; + } + + if ((0, _definition.isListType)(maybeSubType)) { + // If superType is not a list, maybeSubType must also be not a list. + return false; + } // If superType type is an abstract type, maybeSubType type may be a currently + // possible object type. + + + if ((0, _definition.isAbstractType)(superType) && (0, _definition.isObjectType)(maybeSubType) && schema.isPossibleType(superType, maybeSubType)) { + return true; + } // Otherwise, the child type is not a valid subtype of the parent type. + + + return false; +} +/** + * Provided two composite types, determine if they "overlap". Two composite + * types overlap when the Sets of possible concrete types for each intersect. + * + * This is often used to determine if a fragment of a given type could possibly + * be visited in a context of another type. + * + * This function is commutative. + */ + + +function doTypesOverlap(schema, typeA, typeB) { + // Equivalent types overlap + if (typeA === typeB) { + return true; + } + + if ((0, _definition.isAbstractType)(typeA)) { + if ((0, _definition.isAbstractType)(typeB)) { + // If both types are abstract, then determine if there is any intersection + // between possible concrete types of each. + return schema.getPossibleTypes(typeA).some(function (type) { + return schema.isPossibleType(typeB, type); + }); + } // Determine if the latter type is a possible concrete type of the former. + + + return schema.isPossibleType(typeA, typeB); + } + + if ((0, _definition.isAbstractType)(typeB)) { + // Determine if the former type is a possible concrete type of the latter. + return schema.isPossibleType(typeB, typeA); + } // Otherwise the types do not overlap. + + + return false; +} \ No newline at end of file diff --git a/dist/utilities/typeComparators.js.flow b/dist/utilities/typeComparators.js.flow new file mode 100644 index 0000000000..b903ff4f51 --- /dev/null +++ b/dist/utilities/typeComparators.js.flow @@ -0,0 +1,132 @@ +/** + * 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. + * + * @flow strict + */ + +import { + isObjectType, + isListType, + isNonNullType, + isAbstractType, +} from '../type/definition'; +import type { GraphQLType, GraphQLCompositeType } from '../type/definition'; +import type { GraphQLSchema } from '../type/schema'; + +/** + * Provided two types, return true if the types are equal (invariant). + */ +export function isEqualType(typeA: GraphQLType, typeB: GraphQLType): boolean { + // Equivalent types are equal. + if (typeA === typeB) { + return true; + } + + // If either type is non-null, the other must also be non-null. + if (isNonNullType(typeA) && isNonNullType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } + + // If either type is a list, the other must also be a list. + if (isListType(typeA) && isListType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } + + // Otherwise the types are not equal. + return false; +} + +/** + * Provided a type and a super type, return true if the first type is either + * equal or a subset of the second super type (covariant). + */ +export function isTypeSubTypeOf( + schema: GraphQLSchema, + maybeSubType: GraphQLType, + superType: GraphQLType, +): boolean { + // Equivalent type is a valid subtype + if (maybeSubType === superType) { + return true; + } + + // If superType is non-null, maybeSubType must also be non-null. + if (isNonNullType(superType)) { + if (isNonNullType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + return false; + } + if (isNonNullType(maybeSubType)) { + // If superType is nullable, maybeSubType may be non-null or nullable. + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType); + } + + // If superType type is a list, maybeSubType type must also be a list. + if (isListType(superType)) { + if (isListType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + return false; + } + if (isListType(maybeSubType)) { + // If superType is not a list, maybeSubType must also be not a list. + return false; + } + + // If superType type is an abstract type, maybeSubType type may be a currently + // possible object type. + if ( + isAbstractType(superType) && + isObjectType(maybeSubType) && + schema.isPossibleType(superType, maybeSubType) + ) { + return true; + } + + // Otherwise, the child type is not a valid subtype of the parent type. + return false; +} + +/** + * Provided two composite types, determine if they "overlap". Two composite + * types overlap when the Sets of possible concrete types for each intersect. + * + * This is often used to determine if a fragment of a given type could possibly + * be visited in a context of another type. + * + * This function is commutative. + */ +export function doTypesOverlap( + schema: GraphQLSchema, + typeA: GraphQLCompositeType, + typeB: GraphQLCompositeType, +): boolean { + // Equivalent types overlap + if (typeA === typeB) { + return true; + } + + if (isAbstractType(typeA)) { + if (isAbstractType(typeB)) { + // If both types are abstract, then determine if there is any intersection + // between possible concrete types of each. + return schema + .getPossibleTypes(typeA) + .some(type => schema.isPossibleType(typeB, type)); + } + // Determine if the latter type is a possible concrete type of the former. + return schema.isPossibleType(typeA, typeB); + } + + if (isAbstractType(typeB)) { + // Determine if the former type is a possible concrete type of the latter. + return schema.isPossibleType(typeB, typeA); + } + + // Otherwise the types do not overlap. + return false; +} diff --git a/dist/utilities/typeComparators.mjs b/dist/utilities/typeComparators.mjs new file mode 100644 index 0000000000..31a8ac2b2d --- /dev/null +++ b/dist/utilities/typeComparators.mjs @@ -0,0 +1,117 @@ +/** + * 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. + * + * strict + */ +import { isObjectType, isListType, isNonNullType, isAbstractType } from '../type/definition'; + +/** + * Provided two types, return true if the types are equal (invariant). + */ +export function isEqualType(typeA, typeB) { + // Equivalent types are equal. + if (typeA === typeB) { + return true; + } // If either type is non-null, the other must also be non-null. + + + if (isNonNullType(typeA) && isNonNullType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } // If either type is a list, the other must also be a list. + + + if (isListType(typeA) && isListType(typeB)) { + return isEqualType(typeA.ofType, typeB.ofType); + } // Otherwise the types are not equal. + + + return false; +} +/** + * Provided a type and a super type, return true if the first type is either + * equal or a subset of the second super type (covariant). + */ + +export function isTypeSubTypeOf(schema, maybeSubType, superType) { + // Equivalent type is a valid subtype + if (maybeSubType === superType) { + return true; + } // If superType is non-null, maybeSubType must also be non-null. + + + if (isNonNullType(superType)) { + if (isNonNullType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + + return false; + } + + if (isNonNullType(maybeSubType)) { + // If superType is nullable, maybeSubType may be non-null or nullable. + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType); + } // If superType type is a list, maybeSubType type must also be a list. + + + if (isListType(superType)) { + if (isListType(maybeSubType)) { + return isTypeSubTypeOf(schema, maybeSubType.ofType, superType.ofType); + } + + return false; + } + + if (isListType(maybeSubType)) { + // If superType is not a list, maybeSubType must also be not a list. + return false; + } // If superType type is an abstract type, maybeSubType type may be a currently + // possible object type. + + + if (isAbstractType(superType) && isObjectType(maybeSubType) && schema.isPossibleType(superType, maybeSubType)) { + return true; + } // Otherwise, the child type is not a valid subtype of the parent type. + + + return false; +} +/** + * Provided two composite types, determine if they "overlap". Two composite + * types overlap when the Sets of possible concrete types for each intersect. + * + * This is often used to determine if a fragment of a given type could possibly + * be visited in a context of another type. + * + * This function is commutative. + */ + +export function doTypesOverlap(schema, typeA, typeB) { + // Equivalent types overlap + if (typeA === typeB) { + return true; + } + + if (isAbstractType(typeA)) { + if (isAbstractType(typeB)) { + // If both types are abstract, then determine if there is any intersection + // between possible concrete types of each. + return schema.getPossibleTypes(typeA).some(function (type) { + return schema.isPossibleType(typeB, type); + }); + } // Determine if the latter type is a possible concrete type of the former. + + + return schema.isPossibleType(typeA, typeB); + } + + if (isAbstractType(typeB)) { + // Determine if the former type is a possible concrete type of the latter. + return schema.isPossibleType(typeB, typeA); + } // Otherwise the types do not overlap. + + + return false; +} \ No newline at end of file diff --git a/dist/utilities/typeFromAST.js b/dist/utilities/typeFromAST.js new file mode 100644 index 0000000000..a6201ac122 --- /dev/null +++ b/dist/utilities/typeFromAST.js @@ -0,0 +1,41 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.typeFromAST = typeFromAST; + +var _kinds = require("../language/kinds"); + +var _definition = require("../type/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. + * + * strict + */ +function typeFromAST(schema, typeNode) { + /* eslint-enable no-redeclare */ + var innerType; + + if (typeNode.kind === _kinds.Kind.LIST_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && (0, _definition.GraphQLList)(innerType); + } + + if (typeNode.kind === _kinds.Kind.NON_NULL_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && (0, _definition.GraphQLNonNull)(innerType); + } + + if (typeNode.kind === _kinds.Kind.NAMED_TYPE) { + return schema.getType(typeNode.name.value); + } + /* istanbul ignore next */ + + + throw new Error("Unexpected type kind: ".concat(typeNode.kind, ".")); +} \ No newline at end of file diff --git a/dist/utilities/typeFromAST.js.flow b/dist/utilities/typeFromAST.js.flow new file mode 100644 index 0000000000..596e16f9d4 --- /dev/null +++ b/dist/utilities/typeFromAST.js.flow @@ -0,0 +1,56 @@ +/** + * 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. + * + * @flow strict + */ + +import { Kind } from '../language/kinds'; +import type { + NamedTypeNode, + ListTypeNode, + NonNullTypeNode, +} from '../language/ast'; +import { GraphQLList, GraphQLNonNull } from '../type/definition'; +import type { GraphQLNamedType } from '../type/definition'; +import type { GraphQLSchema } from '../type/schema'; + +/** + * Given a Schema and an AST node describing a type, return a GraphQLType + * definition which applies to that type. For example, if provided the parsed + * AST node for `[User]`, a GraphQLList instance will be returned, containing + * the type called "User" found in the schema. If a type called "User" is not + * found in the schema, then undefined will be returned. + */ +/* eslint-disable no-redeclare */ +declare function typeFromAST( + schema: GraphQLSchema, + typeNode: NamedTypeNode, +): GraphQLNamedType | void; +declare function typeFromAST( + schema: GraphQLSchema, + typeNode: ListTypeNode, +): GraphQLList | void; +declare function typeFromAST( + schema: GraphQLSchema, + typeNode: NonNullTypeNode, +): GraphQLNonNull | void; +export function typeFromAST(schema, typeNode) { + /* eslint-enable no-redeclare */ + let innerType; + if (typeNode.kind === Kind.LIST_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && GraphQLList(innerType); + } + if (typeNode.kind === Kind.NON_NULL_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && GraphQLNonNull(innerType); + } + if (typeNode.kind === Kind.NAMED_TYPE) { + return schema.getType(typeNode.name.value); + } + /* istanbul ignore next */ + throw new Error(`Unexpected type kind: ${(typeNode.kind: empty)}.`); +} diff --git a/dist/utilities/typeFromAST.mjs b/dist/utilities/typeFromAST.mjs new file mode 100644 index 0000000000..e1dee0d2e5 --- /dev/null +++ b/dist/utilities/typeFromAST.mjs @@ -0,0 +1,32 @@ +/** + * 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. + * + * strict + */ +import { Kind } from '../language/kinds'; +import { GraphQLList, GraphQLNonNull } from '../type/definition'; +export function typeFromAST(schema, typeNode) { + /* eslint-enable no-redeclare */ + var innerType; + + if (typeNode.kind === Kind.LIST_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && GraphQLList(innerType); + } + + if (typeNode.kind === Kind.NON_NULL_TYPE) { + innerType = typeFromAST(schema, typeNode.type); + return innerType && GraphQLNonNull(innerType); + } + + if (typeNode.kind === Kind.NAMED_TYPE) { + return schema.getType(typeNode.name.value); + } + /* istanbul ignore next */ + + + throw new Error("Unexpected type kind: ".concat(typeNode.kind, ".")); +} \ No newline at end of file diff --git a/dist/utilities/valueFromAST.js b/dist/utilities/valueFromAST.js new file mode 100644 index 0000000000..420999e88c --- /dev/null +++ b/dist/utilities/valueFromAST.js @@ -0,0 +1,207 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.valueFromAST = valueFromAST; + +var _keyMap = _interopRequireDefault(require("../jsutils/keyMap")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _objectValues = _interopRequireDefault(require("../jsutils/objectValues")); + +var _kinds = require("../language/kinds"); + +var _definition = require("../type/definition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * A GraphQL type must be provided, which will be used to interpret different + * GraphQL Value literals. + * + * Returns `undefined` when the value could not be validly coerced according to + * the provided type. + * + * | GraphQL Value | JSON Value | + * | -------------------- | ------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String | String | + * | Int / Float | Number | + * | Enum Value | Mixed | + * | NullValue | null | + * + */ +function valueFromAST(valueNode, type, variables) { + if (!valueNode) { + // When there is no node, then there is also no value. + // Importantly, this is different from returning the value null. + return; + } + + if ((0, _definition.isNonNullType)(type)) { + if (valueNode.kind === _kinds.Kind.NULL) { + return; // Invalid: intentionally return no value. + } + + return valueFromAST(valueNode, type.ofType, variables); + } + + if (valueNode.kind === _kinds.Kind.NULL) { + // This is explicitly returning the value null. + return null; + } + + if (valueNode.kind === _kinds.Kind.VARIABLE) { + var variableName = valueNode.name.value; + + if (!variables || (0, _isInvalid.default)(variables[variableName])) { + // No valid return value. + return; + } + + var variableValue = variables[variableName]; + + if (variableValue === null && (0, _definition.isNonNullType)(type)) { + return; // Invalid: intentionally return no value. + } // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + + + return variableValue; + } + + if ((0, _definition.isListType)(type)) { + var itemType = type.ofType; + + if (valueNode.kind === _kinds.Kind.LIST) { + var coercedValues = []; + var itemNodes = valueNode.values; + + for (var i = 0; i < itemNodes.length; i++) { + if (isMissingVariable(itemNodes[i], variables)) { + // If an array contains a missing variable, it is either coerced to + // null or if the item type is non-null, it considered invalid. + if ((0, _definition.isNonNullType)(itemType)) { + return; // Invalid: intentionally return no value. + } + + coercedValues.push(null); + } else { + var itemValue = valueFromAST(itemNodes[i], itemType, variables); + + if ((0, _isInvalid.default)(itemValue)) { + return; // Invalid: intentionally return no value. + } + + coercedValues.push(itemValue); + } + } + + return coercedValues; + } + + var coercedValue = valueFromAST(valueNode, itemType, variables); + + if ((0, _isInvalid.default)(coercedValue)) { + return; // Invalid: intentionally return no value. + } + + return [coercedValue]; + } + + if ((0, _definition.isInputObjectType)(type)) { + if (valueNode.kind !== _kinds.Kind.OBJECT) { + return; // Invalid: intentionally return no value. + } + + var coercedObj = Object.create(null); + var fieldNodes = (0, _keyMap.default)(valueNode.fields, function (field) { + return field.name.value; + }); + var fields = (0, _objectValues.default)(type.getFields()); + + for (var _i = 0; _i < fields.length; _i++) { + var field = fields[_i]; + var fieldNode = fieldNodes[field.name]; + + if (!fieldNode || isMissingVariable(fieldNode.value, variables)) { + if (field.defaultValue !== undefined) { + coercedObj[field.name] = field.defaultValue; + } else if ((0, _definition.isNonNullType)(field.type)) { + return; // Invalid: intentionally return no value. + } + + continue; + } + + var fieldValue = valueFromAST(fieldNode.value, field.type, variables); + + if ((0, _isInvalid.default)(fieldValue)) { + return; // Invalid: intentionally return no value. + } + + coercedObj[field.name] = fieldValue; + } + + return coercedObj; + } + + if ((0, _definition.isEnumType)(type)) { + if (valueNode.kind !== _kinds.Kind.ENUM) { + return; // Invalid: intentionally return no value. + } + + var enumValue = type.getValue(valueNode.value); + + if (!enumValue) { + return; // Invalid: intentionally return no value. + } + + return enumValue.value; + } + + if ((0, _definition.isScalarType)(type)) { + // Scalars fulfill parsing a literal value via parseLiteral(). + // Invalid values represent a failure to parse correctly, in which case + // no value is returned. + var result; + + try { + result = type.parseLiteral(valueNode, variables); + } catch (_error) { + return; // Invalid: intentionally return no value. + } + + if ((0, _isInvalid.default)(result)) { + return; // Invalid: intentionally return no value. + } + + return result; + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} // Returns true if the provided valueNode is a variable which is not defined +// in the set of variables. + + +function isMissingVariable(valueNode, variables) { + return valueNode.kind === _kinds.Kind.VARIABLE && (!variables || (0, _isInvalid.default)(variables[valueNode.name.value])); +} \ No newline at end of file diff --git a/dist/utilities/valueFromAST.js.flow b/dist/utilities/valueFromAST.js.flow new file mode 100644 index 0000000000..f198d1fcca --- /dev/null +++ b/dist/utilities/valueFromAST.js.flow @@ -0,0 +1,179 @@ +/** + * 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. + * + * @flow strict + */ + +import keyMap from '../jsutils/keyMap'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import type { ObjMap } from '../jsutils/ObjMap'; +import { Kind } from '../language/kinds'; +import { + isScalarType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, +} from '../type/definition'; +import type { GraphQLInputType } from '../type/definition'; +import type { ValueNode } from '../language/ast'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * A GraphQL type must be provided, which will be used to interpret different + * GraphQL Value literals. + * + * Returns `undefined` when the value could not be validly coerced according to + * the provided type. + * + * | GraphQL Value | JSON Value | + * | -------------------- | ------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String | String | + * | Int / Float | Number | + * | Enum Value | Mixed | + * | NullValue | null | + * + */ +export function valueFromAST( + valueNode: ?ValueNode, + type: GraphQLInputType, + variables?: ?ObjMap, +): mixed | void { + if (!valueNode) { + // When there is no node, then there is also no value. + // Importantly, this is different from returning the value null. + return; + } + + if (isNonNullType(type)) { + if (valueNode.kind === Kind.NULL) { + return; // Invalid: intentionally return no value. + } + return valueFromAST(valueNode, type.ofType, variables); + } + + if (valueNode.kind === Kind.NULL) { + // This is explicitly returning the value null. + return null; + } + + if (valueNode.kind === Kind.VARIABLE) { + const variableName = valueNode.name.value; + if (!variables || isInvalid(variables[variableName])) { + // No valid return value. + return; + } + const variableValue = variables[variableName]; + if (variableValue === null && isNonNullType(type)) { + return; // Invalid: intentionally return no value. + } + // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + return variableValue; + } + + if (isListType(type)) { + const itemType = type.ofType; + if (valueNode.kind === Kind.LIST) { + const coercedValues = []; + const itemNodes = valueNode.values; + for (let i = 0; i < itemNodes.length; i++) { + if (isMissingVariable(itemNodes[i], variables)) { + // If an array contains a missing variable, it is either coerced to + // null or if the item type is non-null, it considered invalid. + if (isNonNullType(itemType)) { + return; // Invalid: intentionally return no value. + } + coercedValues.push(null); + } else { + const itemValue = valueFromAST(itemNodes[i], itemType, variables); + if (isInvalid(itemValue)) { + return; // Invalid: intentionally return no value. + } + coercedValues.push(itemValue); + } + } + return coercedValues; + } + const coercedValue = valueFromAST(valueNode, itemType, variables); + if (isInvalid(coercedValue)) { + return; // Invalid: intentionally return no value. + } + return [coercedValue]; + } + + if (isInputObjectType(type)) { + if (valueNode.kind !== Kind.OBJECT) { + return; // Invalid: intentionally return no value. + } + const coercedObj = Object.create(null); + const fieldNodes = keyMap(valueNode.fields, field => field.name.value); + const fields = objectValues(type.getFields()); + for (let i = 0; i < fields.length; i++) { + const field = fields[i]; + const fieldNode = fieldNodes[field.name]; + if (!fieldNode || isMissingVariable(fieldNode.value, variables)) { + if (field.defaultValue !== undefined) { + coercedObj[field.name] = field.defaultValue; + } else if (isNonNullType(field.type)) { + return; // Invalid: intentionally return no value. + } + continue; + } + const fieldValue = valueFromAST(fieldNode.value, field.type, variables); + if (isInvalid(fieldValue)) { + return; // Invalid: intentionally return no value. + } + coercedObj[field.name] = fieldValue; + } + return coercedObj; + } + + if (isEnumType(type)) { + if (valueNode.kind !== Kind.ENUM) { + return; // Invalid: intentionally return no value. + } + const enumValue = type.getValue(valueNode.value); + if (!enumValue) { + return; // Invalid: intentionally return no value. + } + return enumValue.value; + } + + if (isScalarType(type)) { + // Scalars fulfill parsing a literal value via parseLiteral(). + // Invalid values represent a failure to parse correctly, in which case + // no value is returned. + let result; + try { + result = type.parseLiteral(valueNode, variables); + } catch (_error) { + return; // Invalid: intentionally return no value. + } + if (isInvalid(result)) { + return; // Invalid: intentionally return no value. + } + return result; + } + + /* istanbul ignore next */ + throw new Error(`Unknown type: ${(type: empty)}.`); +} + +// Returns true if the provided valueNode is a variable which is not defined +// in the set of variables. +function isMissingVariable(valueNode, variables) { + return ( + valueNode.kind === Kind.VARIABLE && + (!variables || isInvalid(variables[valueNode.name.value])) + ); +} diff --git a/dist/utilities/valueFromAST.mjs b/dist/utilities/valueFromAST.mjs new file mode 100644 index 0000000000..5ac56952fd --- /dev/null +++ b/dist/utilities/valueFromAST.mjs @@ -0,0 +1,192 @@ +/** + * 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. + * + * strict + */ +import keyMap from '../jsutils/keyMap'; +import isInvalid from '../jsutils/isInvalid'; +import objectValues from '../jsutils/objectValues'; +import { Kind } from '../language/kinds'; +import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * A GraphQL type must be provided, which will be used to interpret different + * GraphQL Value literals. + * + * Returns `undefined` when the value could not be validly coerced according to + * the provided type. + * + * | GraphQL Value | JSON Value | + * | -------------------- | ------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String | String | + * | Int / Float | Number | + * | Enum Value | Mixed | + * | NullValue | null | + * + */ +export function valueFromAST(valueNode, type, variables) { + if (!valueNode) { + // When there is no node, then there is also no value. + // Importantly, this is different from returning the value null. + return; + } + + if (isNonNullType(type)) { + if (valueNode.kind === Kind.NULL) { + return; // Invalid: intentionally return no value. + } + + return valueFromAST(valueNode, type.ofType, variables); + } + + if (valueNode.kind === Kind.NULL) { + // This is explicitly returning the value null. + return null; + } + + if (valueNode.kind === Kind.VARIABLE) { + var variableName = valueNode.name.value; + + if (!variables || isInvalid(variables[variableName])) { + // No valid return value. + return; + } + + var variableValue = variables[variableName]; + + if (variableValue === null && isNonNullType(type)) { + return; // Invalid: intentionally return no value. + } // Note: This does no further checking that this variable is correct. + // This assumes that this query has been validated and the variable + // usage here is of the correct type. + + + return variableValue; + } + + if (isListType(type)) { + var itemType = type.ofType; + + if (valueNode.kind === Kind.LIST) { + var coercedValues = []; + var itemNodes = valueNode.values; + + for (var i = 0; i < itemNodes.length; i++) { + if (isMissingVariable(itemNodes[i], variables)) { + // If an array contains a missing variable, it is either coerced to + // null or if the item type is non-null, it considered invalid. + if (isNonNullType(itemType)) { + return; // Invalid: intentionally return no value. + } + + coercedValues.push(null); + } else { + var itemValue = valueFromAST(itemNodes[i], itemType, variables); + + if (isInvalid(itemValue)) { + return; // Invalid: intentionally return no value. + } + + coercedValues.push(itemValue); + } + } + + return coercedValues; + } + + var coercedValue = valueFromAST(valueNode, itemType, variables); + + if (isInvalid(coercedValue)) { + return; // Invalid: intentionally return no value. + } + + return [coercedValue]; + } + + if (isInputObjectType(type)) { + if (valueNode.kind !== Kind.OBJECT) { + return; // Invalid: intentionally return no value. + } + + var coercedObj = Object.create(null); + var fieldNodes = keyMap(valueNode.fields, function (field) { + return field.name.value; + }); + var fields = objectValues(type.getFields()); + + for (var _i = 0; _i < fields.length; _i++) { + var field = fields[_i]; + var fieldNode = fieldNodes[field.name]; + + if (!fieldNode || isMissingVariable(fieldNode.value, variables)) { + if (field.defaultValue !== undefined) { + coercedObj[field.name] = field.defaultValue; + } else if (isNonNullType(field.type)) { + return; // Invalid: intentionally return no value. + } + + continue; + } + + var fieldValue = valueFromAST(fieldNode.value, field.type, variables); + + if (isInvalid(fieldValue)) { + return; // Invalid: intentionally return no value. + } + + coercedObj[field.name] = fieldValue; + } + + return coercedObj; + } + + if (isEnumType(type)) { + if (valueNode.kind !== Kind.ENUM) { + return; // Invalid: intentionally return no value. + } + + var enumValue = type.getValue(valueNode.value); + + if (!enumValue) { + return; // Invalid: intentionally return no value. + } + + return enumValue.value; + } + + if (isScalarType(type)) { + // Scalars fulfill parsing a literal value via parseLiteral(). + // Invalid values represent a failure to parse correctly, in which case + // no value is returned. + var result; + + try { + result = type.parseLiteral(valueNode, variables); + } catch (_error) { + return; // Invalid: intentionally return no value. + } + + if (isInvalid(result)) { + return; // Invalid: intentionally return no value. + } + + return result; + } + /* istanbul ignore next */ + + + throw new Error("Unknown type: ".concat(type, ".")); +} // Returns true if the provided valueNode is a variable which is not defined +// in the set of variables. + +function isMissingVariable(valueNode, variables) { + return valueNode.kind === Kind.VARIABLE && (!variables || isInvalid(variables[valueNode.name.value])); +} \ No newline at end of file diff --git a/dist/utilities/valueFromASTUntyped.js b/dist/utilities/valueFromASTUntyped.js new file mode 100644 index 0000000000..8c289b5635 --- /dev/null +++ b/dist/utilities/valueFromASTUntyped.js @@ -0,0 +1,77 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.valueFromASTUntyped = valueFromASTUntyped; + +var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap")); + +var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid")); + +var _kinds = require("../language/kinds"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value + * will reflect the provided GraphQL value AST. + * + * | GraphQL Value | JavaScript Value | + * | -------------------- | ---------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String / Enum | String | + * | Int / Float | Number | + * | Null | null | + * + */ +function valueFromASTUntyped(valueNode, variables) { + switch (valueNode.kind) { + case _kinds.Kind.NULL: + return null; + + case _kinds.Kind.INT: + return parseInt(valueNode.value, 10); + + case _kinds.Kind.FLOAT: + return parseFloat(valueNode.value); + + case _kinds.Kind.STRING: + case _kinds.Kind.ENUM: + case _kinds.Kind.BOOLEAN: + return valueNode.value; + + case _kinds.Kind.LIST: + return valueNode.values.map(function (node) { + return valueFromASTUntyped(node, variables); + }); + + case _kinds.Kind.OBJECT: + return (0, _keyValMap.default)(valueNode.fields, function (field) { + return field.name.value; + }, function (field) { + return valueFromASTUntyped(field.value, variables); + }); + + case _kinds.Kind.VARIABLE: + var variableName = valueNode.name.value; + return variables && !(0, _isInvalid.default)(variables[variableName]) ? variables[variableName] : undefined; + } + /* istanbul ignore next */ + + + throw new Error('Unexpected value kind: ' + valueNode.kind); +} \ No newline at end of file diff --git a/dist/utilities/valueFromASTUntyped.js.flow b/dist/utilities/valueFromASTUntyped.js.flow new file mode 100644 index 0000000000..3ed6558d97 --- /dev/null +++ b/dist/utilities/valueFromASTUntyped.js.flow @@ -0,0 +1,63 @@ +/** + * 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. + * + * @flow strict + */ + +import keyValMap from '../jsutils/keyValMap'; +import isInvalid from '../jsutils/isInvalid'; +import type { ObjMap } from '../jsutils/ObjMap'; +import { Kind } from '../language/kinds'; +import type { ValueNode } from '../language/ast'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value + * will reflect the provided GraphQL value AST. + * + * | GraphQL Value | JavaScript Value | + * | -------------------- | ---------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String / Enum | String | + * | Int / Float | Number | + * | Null | null | + * + */ +export function valueFromASTUntyped( + valueNode: ValueNode, + variables?: ?ObjMap, +): mixed { + switch (valueNode.kind) { + case Kind.NULL: + return null; + case Kind.INT: + return parseInt(valueNode.value, 10); + case Kind.FLOAT: + return parseFloat(valueNode.value); + case Kind.STRING: + case Kind.ENUM: + case Kind.BOOLEAN: + return valueNode.value; + case Kind.LIST: + return valueNode.values.map(node => valueFromASTUntyped(node, variables)); + case Kind.OBJECT: + return keyValMap( + valueNode.fields, + field => field.name.value, + field => valueFromASTUntyped(field.value, variables), + ); + case Kind.VARIABLE: + const variableName = valueNode.name.value; + return variables && !isInvalid(variables[variableName]) + ? variables[variableName] + : undefined; + } + /* istanbul ignore next */ + throw new Error('Unexpected value kind: ' + (valueNode.kind: empty)); +} diff --git a/dist/utilities/valueFromASTUntyped.mjs b/dist/utilities/valueFromASTUntyped.mjs new file mode 100644 index 0000000000..2f56d73ee2 --- /dev/null +++ b/dist/utilities/valueFromASTUntyped.mjs @@ -0,0 +1,65 @@ +/** + * 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. + * + * strict + */ +import keyValMap from '../jsutils/keyValMap'; +import isInvalid from '../jsutils/isInvalid'; +import { Kind } from '../language/kinds'; + +/** + * Produces a JavaScript value given a GraphQL Value AST. + * + * Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value + * will reflect the provided GraphQL value AST. + * + * | GraphQL Value | JavaScript Value | + * | -------------------- | ---------------- | + * | Input Object | Object | + * | List | Array | + * | Boolean | Boolean | + * | String / Enum | String | + * | Int / Float | Number | + * | Null | null | + * + */ +export function valueFromASTUntyped(valueNode, variables) { + switch (valueNode.kind) { + case Kind.NULL: + return null; + + case Kind.INT: + return parseInt(valueNode.value, 10); + + case Kind.FLOAT: + return parseFloat(valueNode.value); + + case Kind.STRING: + case Kind.ENUM: + case Kind.BOOLEAN: + return valueNode.value; + + case Kind.LIST: + return valueNode.values.map(function (node) { + return valueFromASTUntyped(node, variables); + }); + + case Kind.OBJECT: + return keyValMap(valueNode.fields, function (field) { + return field.name.value; + }, function (field) { + return valueFromASTUntyped(field.value, variables); + }); + + case Kind.VARIABLE: + var variableName = valueNode.name.value; + return variables && !isInvalid(variables[variableName]) ? variables[variableName] : undefined; + } + /* istanbul ignore next */ + + + throw new Error('Unexpected value kind: ' + valueNode.kind); +} \ No newline at end of file diff --git a/dist/validation/ValidationContext.js b/dist/validation/ValidationContext.js new file mode 100644 index 0000000000..deda38be19 --- /dev/null +++ b/dist/validation/ValidationContext.js @@ -0,0 +1,226 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +var _error = require("../error"); + +var _visitor = require("../language/visitor"); + +var _kinds = require("../language/kinds"); + +var _schema = require("../type/schema"); + +var _TypeInfo = require("../utilities/TypeInfo"); + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * An instance of this class is passed as the "this" context to all validators, + * allowing access to commonly useful contextual information from within a + * validation rule. + */ +var ValidationContext = +/*#__PURE__*/ +function () { + function ValidationContext(schema, ast, typeInfo) { + _defineProperty(this, "_schema", void 0); + + _defineProperty(this, "_ast", void 0); + + _defineProperty(this, "_typeInfo", void 0); + + _defineProperty(this, "_errors", void 0); + + _defineProperty(this, "_fragments", void 0); + + _defineProperty(this, "_fragmentSpreads", void 0); + + _defineProperty(this, "_recursivelyReferencedFragments", void 0); + + _defineProperty(this, "_variableUsages", void 0); + + _defineProperty(this, "_recursiveVariableUsages", void 0); + + this._schema = schema; + this._ast = ast; + this._typeInfo = typeInfo; + this._errors = []; + this._fragmentSpreads = new Map(); + this._recursivelyReferencedFragments = new Map(); + this._variableUsages = new Map(); + this._recursiveVariableUsages = new Map(); + } + + var _proto = ValidationContext.prototype; + + _proto.reportError = function reportError(error) { + this._errors.push(error); + }; + + _proto.getErrors = function getErrors() { + return this._errors; + }; + + _proto.getSchema = function getSchema() { + return this._schema; + }; + + _proto.getDocument = function getDocument() { + return this._ast; + }; + + _proto.getFragment = function getFragment(name) { + var fragments = this._fragments; + + if (!fragments) { + this._fragments = fragments = this.getDocument().definitions.reduce(function (frags, statement) { + if (statement.kind === _kinds.Kind.FRAGMENT_DEFINITION) { + frags[statement.name.value] = statement; + } + + return frags; + }, Object.create(null)); + } + + return fragments[name]; + }; + + _proto.getFragmentSpreads = function getFragmentSpreads(node) { + var spreads = this._fragmentSpreads.get(node); + + if (!spreads) { + spreads = []; + var setsToVisit = [node]; + + while (setsToVisit.length !== 0) { + var set = setsToVisit.pop(); + + for (var i = 0; i < set.selections.length; i++) { + var selection = set.selections[i]; + + if (selection.kind === _kinds.Kind.FRAGMENT_SPREAD) { + spreads.push(selection); + } else if (selection.selectionSet) { + setsToVisit.push(selection.selectionSet); + } + } + } + + this._fragmentSpreads.set(node, spreads); + } + + return spreads; + }; + + _proto.getRecursivelyReferencedFragments = function getRecursivelyReferencedFragments(operation) { + var fragments = this._recursivelyReferencedFragments.get(operation); + + if (!fragments) { + fragments = []; + var collectedNames = Object.create(null); + var nodesToVisit = [operation.selectionSet]; + + while (nodesToVisit.length !== 0) { + var _node = nodesToVisit.pop(); + + var spreads = this.getFragmentSpreads(_node); + + for (var i = 0; i < spreads.length; i++) { + var fragName = spreads[i].name.value; + + if (collectedNames[fragName] !== true) { + collectedNames[fragName] = true; + var fragment = this.getFragment(fragName); + + if (fragment) { + fragments.push(fragment); + nodesToVisit.push(fragment.selectionSet); + } + } + } + } + + this._recursivelyReferencedFragments.set(operation, fragments); + } + + return fragments; + }; + + _proto.getVariableUsages = function getVariableUsages(node) { + var usages = this._variableUsages.get(node); + + if (!usages) { + var newUsages = []; + var typeInfo = new _TypeInfo.TypeInfo(this._schema); + (0, _visitor.visit)(node, (0, _visitor.visitWithTypeInfo)(typeInfo, { + VariableDefinition: function VariableDefinition() { + return false; + }, + Variable: function Variable(variable) { + newUsages.push({ + node: variable, + type: typeInfo.getInputType(), + defaultValue: typeInfo.getDefaultValue() + }); + } + })); + usages = newUsages; + + this._variableUsages.set(node, usages); + } + + return usages; + }; + + _proto.getRecursiveVariableUsages = function getRecursiveVariableUsages(operation) { + var usages = this._recursiveVariableUsages.get(operation); + + if (!usages) { + usages = this.getVariableUsages(operation); + var fragments = this.getRecursivelyReferencedFragments(operation); + + for (var i = 0; i < fragments.length; i++) { + Array.prototype.push.apply(usages, this.getVariableUsages(fragments[i])); + } + + this._recursiveVariableUsages.set(operation, usages); + } + + return usages; + }; + + _proto.getType = function getType() { + return this._typeInfo.getType(); + }; + + _proto.getParentType = function getParentType() { + return this._typeInfo.getParentType(); + }; + + _proto.getInputType = function getInputType() { + return this._typeInfo.getInputType(); + }; + + _proto.getParentInputType = function getParentInputType() { + return this._typeInfo.getParentInputType(); + }; + + _proto.getFieldDef = function getFieldDef() { + return this._typeInfo.getFieldDef(); + }; + + _proto.getDirective = function getDirective() { + return this._typeInfo.getDirective(); + }; + + _proto.getArgument = function getArgument() { + return this._typeInfo.getArgument(); + }; + + return ValidationContext; +}(); + +exports.default = ValidationContext; \ No newline at end of file diff --git a/dist/validation/ValidationContext.js.flow b/dist/validation/ValidationContext.js.flow new file mode 100644 index 0000000000..1d6ef75741 --- /dev/null +++ b/dist/validation/ValidationContext.js.flow @@ -0,0 +1,229 @@ +/** + * 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. + * + * @flow strict + */ + +import type { ObjMap } from '../jsutils/ObjMap'; +import { GraphQLError } from '../error'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import { Kind } from '../language/kinds'; +import type { + DocumentNode, + OperationDefinitionNode, + VariableNode, + SelectionSetNode, + FragmentSpreadNode, + FragmentDefinitionNode, +} from '../language/ast'; +import { GraphQLSchema } from '../type/schema'; +import type { + GraphQLInputType, + GraphQLOutputType, + GraphQLCompositeType, + GraphQLField, + GraphQLArgument, +} from '../type/definition'; +import type { GraphQLDirective } from '../type/directives'; +import { TypeInfo } from '../utilities/TypeInfo'; + +type NodeWithSelectionSet = OperationDefinitionNode | FragmentDefinitionNode; +type VariableUsage = {| + +node: VariableNode, + +type: ?GraphQLInputType, + +defaultValue: ?mixed, +|}; + +/** + * An instance of this class is passed as the "this" context to all validators, + * allowing access to commonly useful contextual information from within a + * validation rule. + */ +export default class ValidationContext { + _schema: GraphQLSchema; + _ast: DocumentNode; + _typeInfo: TypeInfo; + _errors: Array; + _fragments: ObjMap; + _fragmentSpreads: Map>; + _recursivelyReferencedFragments: Map< + OperationDefinitionNode, + $ReadOnlyArray, + >; + _variableUsages: Map>; + _recursiveVariableUsages: Map< + OperationDefinitionNode, + $ReadOnlyArray, + >; + + constructor( + schema: GraphQLSchema, + ast: DocumentNode, + typeInfo: TypeInfo, + ): void { + this._schema = schema; + this._ast = ast; + this._typeInfo = typeInfo; + this._errors = []; + this._fragmentSpreads = new Map(); + this._recursivelyReferencedFragments = new Map(); + this._variableUsages = new Map(); + this._recursiveVariableUsages = new Map(); + } + + reportError(error: GraphQLError): void { + this._errors.push(error); + } + + getErrors(): $ReadOnlyArray { + return this._errors; + } + + getSchema(): GraphQLSchema { + return this._schema; + } + + getDocument(): DocumentNode { + return this._ast; + } + + getFragment(name: string): ?FragmentDefinitionNode { + let fragments = this._fragments; + if (!fragments) { + this._fragments = fragments = this.getDocument().definitions.reduce( + (frags, statement) => { + if (statement.kind === Kind.FRAGMENT_DEFINITION) { + frags[statement.name.value] = statement; + } + return frags; + }, + Object.create(null), + ); + } + return fragments[name]; + } + + getFragmentSpreads( + node: SelectionSetNode, + ): $ReadOnlyArray { + let spreads = this._fragmentSpreads.get(node); + if (!spreads) { + spreads = []; + const setsToVisit: Array = [node]; + while (setsToVisit.length !== 0) { + const set = setsToVisit.pop(); + for (let i = 0; i < set.selections.length; i++) { + const selection = set.selections[i]; + if (selection.kind === Kind.FRAGMENT_SPREAD) { + spreads.push(selection); + } else if (selection.selectionSet) { + setsToVisit.push(selection.selectionSet); + } + } + } + this._fragmentSpreads.set(node, spreads); + } + return spreads; + } + + getRecursivelyReferencedFragments( + operation: OperationDefinitionNode, + ): $ReadOnlyArray { + let fragments = this._recursivelyReferencedFragments.get(operation); + if (!fragments) { + fragments = []; + const collectedNames = Object.create(null); + const nodesToVisit: Array = [operation.selectionSet]; + while (nodesToVisit.length !== 0) { + const node = nodesToVisit.pop(); + const spreads = this.getFragmentSpreads(node); + for (let i = 0; i < spreads.length; i++) { + const fragName = spreads[i].name.value; + if (collectedNames[fragName] !== true) { + collectedNames[fragName] = true; + const fragment = this.getFragment(fragName); + if (fragment) { + fragments.push(fragment); + nodesToVisit.push(fragment.selectionSet); + } + } + } + } + this._recursivelyReferencedFragments.set(operation, fragments); + } + return fragments; + } + + getVariableUsages(node: NodeWithSelectionSet): $ReadOnlyArray { + let usages = this._variableUsages.get(node); + if (!usages) { + const newUsages = []; + const typeInfo = new TypeInfo(this._schema); + visit( + node, + visitWithTypeInfo(typeInfo, { + VariableDefinition: () => false, + Variable(variable) { + newUsages.push({ + node: variable, + type: typeInfo.getInputType(), + defaultValue: typeInfo.getDefaultValue(), + }); + }, + }), + ); + usages = newUsages; + this._variableUsages.set(node, usages); + } + return usages; + } + + getRecursiveVariableUsages( + operation: OperationDefinitionNode, + ): $ReadOnlyArray { + let usages = this._recursiveVariableUsages.get(operation); + if (!usages) { + usages = this.getVariableUsages(operation); + const fragments = this.getRecursivelyReferencedFragments(operation); + for (let i = 0; i < fragments.length; i++) { + Array.prototype.push.apply( + usages, + this.getVariableUsages(fragments[i]), + ); + } + this._recursiveVariableUsages.set(operation, usages); + } + return usages; + } + + getType(): ?GraphQLOutputType { + return this._typeInfo.getType(); + } + + getParentType(): ?GraphQLCompositeType { + return this._typeInfo.getParentType(); + } + + getInputType(): ?GraphQLInputType { + return this._typeInfo.getInputType(); + } + + getParentInputType(): ?GraphQLInputType { + return this._typeInfo.getParentInputType(); + } + + getFieldDef(): ?GraphQLField<*, *> { + return this._typeInfo.getFieldDef(); + } + + getDirective(): ?GraphQLDirective { + return this._typeInfo.getDirective(); + } + + getArgument(): ?GraphQLArgument { + return this._typeInfo.getArgument(); + } +} diff --git a/dist/validation/ValidationContext.mjs b/dist/validation/ValidationContext.mjs new file mode 100644 index 0000000000..33d97576d1 --- /dev/null +++ b/dist/validation/ValidationContext.mjs @@ -0,0 +1,223 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../error'; +import { visit, visitWithTypeInfo } from '../language/visitor'; +import { Kind } from '../language/kinds'; +import { GraphQLSchema } from '../type/schema'; +import { TypeInfo } from '../utilities/TypeInfo'; + +/** + * An instance of this class is passed as the "this" context to all validators, + * allowing access to commonly useful contextual information from within a + * validation rule. + */ +var ValidationContext = +/*#__PURE__*/ +function () { + function ValidationContext(schema, ast, typeInfo) { + _defineProperty(this, "_schema", void 0); + + _defineProperty(this, "_ast", void 0); + + _defineProperty(this, "_typeInfo", void 0); + + _defineProperty(this, "_errors", void 0); + + _defineProperty(this, "_fragments", void 0); + + _defineProperty(this, "_fragmentSpreads", void 0); + + _defineProperty(this, "_recursivelyReferencedFragments", void 0); + + _defineProperty(this, "_variableUsages", void 0); + + _defineProperty(this, "_recursiveVariableUsages", void 0); + + this._schema = schema; + this._ast = ast; + this._typeInfo = typeInfo; + this._errors = []; + this._fragmentSpreads = new Map(); + this._recursivelyReferencedFragments = new Map(); + this._variableUsages = new Map(); + this._recursiveVariableUsages = new Map(); + } + + var _proto = ValidationContext.prototype; + + _proto.reportError = function reportError(error) { + this._errors.push(error); + }; + + _proto.getErrors = function getErrors() { + return this._errors; + }; + + _proto.getSchema = function getSchema() { + return this._schema; + }; + + _proto.getDocument = function getDocument() { + return this._ast; + }; + + _proto.getFragment = function getFragment(name) { + var fragments = this._fragments; + + if (!fragments) { + this._fragments = fragments = this.getDocument().definitions.reduce(function (frags, statement) { + if (statement.kind === Kind.FRAGMENT_DEFINITION) { + frags[statement.name.value] = statement; + } + + return frags; + }, Object.create(null)); + } + + return fragments[name]; + }; + + _proto.getFragmentSpreads = function getFragmentSpreads(node) { + var spreads = this._fragmentSpreads.get(node); + + if (!spreads) { + spreads = []; + var setsToVisit = [node]; + + while (setsToVisit.length !== 0) { + var set = setsToVisit.pop(); + + for (var i = 0; i < set.selections.length; i++) { + var selection = set.selections[i]; + + if (selection.kind === Kind.FRAGMENT_SPREAD) { + spreads.push(selection); + } else if (selection.selectionSet) { + setsToVisit.push(selection.selectionSet); + } + } + } + + this._fragmentSpreads.set(node, spreads); + } + + return spreads; + }; + + _proto.getRecursivelyReferencedFragments = function getRecursivelyReferencedFragments(operation) { + var fragments = this._recursivelyReferencedFragments.get(operation); + + if (!fragments) { + fragments = []; + var collectedNames = Object.create(null); + var nodesToVisit = [operation.selectionSet]; + + while (nodesToVisit.length !== 0) { + var _node = nodesToVisit.pop(); + + var spreads = this.getFragmentSpreads(_node); + + for (var i = 0; i < spreads.length; i++) { + var fragName = spreads[i].name.value; + + if (collectedNames[fragName] !== true) { + collectedNames[fragName] = true; + var fragment = this.getFragment(fragName); + + if (fragment) { + fragments.push(fragment); + nodesToVisit.push(fragment.selectionSet); + } + } + } + } + + this._recursivelyReferencedFragments.set(operation, fragments); + } + + return fragments; + }; + + _proto.getVariableUsages = function getVariableUsages(node) { + var usages = this._variableUsages.get(node); + + if (!usages) { + var newUsages = []; + var typeInfo = new TypeInfo(this._schema); + visit(node, visitWithTypeInfo(typeInfo, { + VariableDefinition: function VariableDefinition() { + return false; + }, + Variable: function Variable(variable) { + newUsages.push({ + node: variable, + type: typeInfo.getInputType(), + defaultValue: typeInfo.getDefaultValue() + }); + } + })); + usages = newUsages; + + this._variableUsages.set(node, usages); + } + + return usages; + }; + + _proto.getRecursiveVariableUsages = function getRecursiveVariableUsages(operation) { + var usages = this._recursiveVariableUsages.get(operation); + + if (!usages) { + usages = this.getVariableUsages(operation); + var fragments = this.getRecursivelyReferencedFragments(operation); + + for (var i = 0; i < fragments.length; i++) { + Array.prototype.push.apply(usages, this.getVariableUsages(fragments[i])); + } + + this._recursiveVariableUsages.set(operation, usages); + } + + return usages; + }; + + _proto.getType = function getType() { + return this._typeInfo.getType(); + }; + + _proto.getParentType = function getParentType() { + return this._typeInfo.getParentType(); + }; + + _proto.getInputType = function getInputType() { + return this._typeInfo.getInputType(); + }; + + _proto.getParentInputType = function getParentInputType() { + return this._typeInfo.getParentInputType(); + }; + + _proto.getFieldDef = function getFieldDef() { + return this._typeInfo.getFieldDef(); + }; + + _proto.getDirective = function getDirective() { + return this._typeInfo.getDirective(); + }; + + _proto.getArgument = function getArgument() { + return this._typeInfo.getArgument(); + }; + + return ValidationContext; +}(); + +export { ValidationContext as default }; \ No newline at end of file diff --git a/dist/validation/index.js b/dist/validation/index.js new file mode 100644 index 0000000000..96de5f8a16 --- /dev/null +++ b/dist/validation/index.js @@ -0,0 +1,231 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "validate", { + enumerable: true, + get: function get() { + return _validate.validate; + } +}); +Object.defineProperty(exports, "ValidationContext", { + enumerable: true, + get: function get() { + return _ValidationContext.default; + } +}); +Object.defineProperty(exports, "specifiedRules", { + enumerable: true, + get: function get() { + return _specifiedRules.specifiedRules; + } +}); +Object.defineProperty(exports, "FieldsOnCorrectTypeRule", { + enumerable: true, + get: function get() { + return _FieldsOnCorrectType.FieldsOnCorrectType; + } +}); +Object.defineProperty(exports, "FragmentsOnCompositeTypesRule", { + enumerable: true, + get: function get() { + return _FragmentsOnCompositeTypes.FragmentsOnCompositeTypes; + } +}); +Object.defineProperty(exports, "KnownArgumentNamesRule", { + enumerable: true, + get: function get() { + return _KnownArgumentNames.KnownArgumentNames; + } +}); +Object.defineProperty(exports, "KnownDirectivesRule", { + enumerable: true, + get: function get() { + return _KnownDirectives.KnownDirectives; + } +}); +Object.defineProperty(exports, "KnownFragmentNamesRule", { + enumerable: true, + get: function get() { + return _KnownFragmentNames.KnownFragmentNames; + } +}); +Object.defineProperty(exports, "KnownTypeNamesRule", { + enumerable: true, + get: function get() { + return _KnownTypeNames.KnownTypeNames; + } +}); +Object.defineProperty(exports, "LoneAnonymousOperationRule", { + enumerable: true, + get: function get() { + return _LoneAnonymousOperation.LoneAnonymousOperation; + } +}); +Object.defineProperty(exports, "NoFragmentCyclesRule", { + enumerable: true, + get: function get() { + return _NoFragmentCycles.NoFragmentCycles; + } +}); +Object.defineProperty(exports, "NoUndefinedVariablesRule", { + enumerable: true, + get: function get() { + return _NoUndefinedVariables.NoUndefinedVariables; + } +}); +Object.defineProperty(exports, "NoUnusedFragmentsRule", { + enumerable: true, + get: function get() { + return _NoUnusedFragments.NoUnusedFragments; + } +}); +Object.defineProperty(exports, "NoUnusedVariablesRule", { + enumerable: true, + get: function get() { + return _NoUnusedVariables.NoUnusedVariables; + } +}); +Object.defineProperty(exports, "OverlappingFieldsCanBeMergedRule", { + enumerable: true, + get: function get() { + return _OverlappingFieldsCanBeMerged.OverlappingFieldsCanBeMerged; + } +}); +Object.defineProperty(exports, "PossibleFragmentSpreadsRule", { + enumerable: true, + get: function get() { + return _PossibleFragmentSpreads.PossibleFragmentSpreads; + } +}); +Object.defineProperty(exports, "ProvidedRequiredArgumentsRule", { + enumerable: true, + get: function get() { + return _ProvidedRequiredArguments.ProvidedRequiredArguments; + } +}); +Object.defineProperty(exports, "ScalarLeafsRule", { + enumerable: true, + get: function get() { + return _ScalarLeafs.ScalarLeafs; + } +}); +Object.defineProperty(exports, "SingleFieldSubscriptionsRule", { + enumerable: true, + get: function get() { + return _SingleFieldSubscriptions.SingleFieldSubscriptions; + } +}); +Object.defineProperty(exports, "UniqueArgumentNamesRule", { + enumerable: true, + get: function get() { + return _UniqueArgumentNames.UniqueArgumentNames; + } +}); +Object.defineProperty(exports, "UniqueDirectivesPerLocationRule", { + enumerable: true, + get: function get() { + return _UniqueDirectivesPerLocation.UniqueDirectivesPerLocation; + } +}); +Object.defineProperty(exports, "UniqueFragmentNamesRule", { + enumerable: true, + get: function get() { + return _UniqueFragmentNames.UniqueFragmentNames; + } +}); +Object.defineProperty(exports, "UniqueInputFieldNamesRule", { + enumerable: true, + get: function get() { + return _UniqueInputFieldNames.UniqueInputFieldNames; + } +}); +Object.defineProperty(exports, "UniqueOperationNamesRule", { + enumerable: true, + get: function get() { + return _UniqueOperationNames.UniqueOperationNames; + } +}); +Object.defineProperty(exports, "UniqueVariableNamesRule", { + enumerable: true, + get: function get() { + return _UniqueVariableNames.UniqueVariableNames; + } +}); +Object.defineProperty(exports, "ValuesOfCorrectTypeRule", { + enumerable: true, + get: function get() { + return _ValuesOfCorrectType.ValuesOfCorrectType; + } +}); +Object.defineProperty(exports, "VariablesAreInputTypesRule", { + enumerable: true, + get: function get() { + return _VariablesAreInputTypes.VariablesAreInputTypes; + } +}); +Object.defineProperty(exports, "VariablesInAllowedPositionRule", { + enumerable: true, + get: function get() { + return _VariablesInAllowedPosition.VariablesInAllowedPosition; + } +}); + +var _validate = require("./validate"); + +var _ValidationContext = _interopRequireDefault(require("./ValidationContext")); + +var _specifiedRules = require("./specifiedRules"); + +var _FieldsOnCorrectType = require("./rules/FieldsOnCorrectType"); + +var _FragmentsOnCompositeTypes = require("./rules/FragmentsOnCompositeTypes"); + +var _KnownArgumentNames = require("./rules/KnownArgumentNames"); + +var _KnownDirectives = require("./rules/KnownDirectives"); + +var _KnownFragmentNames = require("./rules/KnownFragmentNames"); + +var _KnownTypeNames = require("./rules/KnownTypeNames"); + +var _LoneAnonymousOperation = require("./rules/LoneAnonymousOperation"); + +var _NoFragmentCycles = require("./rules/NoFragmentCycles"); + +var _NoUndefinedVariables = require("./rules/NoUndefinedVariables"); + +var _NoUnusedFragments = require("./rules/NoUnusedFragments"); + +var _NoUnusedVariables = require("./rules/NoUnusedVariables"); + +var _OverlappingFieldsCanBeMerged = require("./rules/OverlappingFieldsCanBeMerged"); + +var _PossibleFragmentSpreads = require("./rules/PossibleFragmentSpreads"); + +var _ProvidedRequiredArguments = require("./rules/ProvidedRequiredArguments"); + +var _ScalarLeafs = require("./rules/ScalarLeafs"); + +var _SingleFieldSubscriptions = require("./rules/SingleFieldSubscriptions"); + +var _UniqueArgumentNames = require("./rules/UniqueArgumentNames"); + +var _UniqueDirectivesPerLocation = require("./rules/UniqueDirectivesPerLocation"); + +var _UniqueFragmentNames = require("./rules/UniqueFragmentNames"); + +var _UniqueInputFieldNames = require("./rules/UniqueInputFieldNames"); + +var _UniqueOperationNames = require("./rules/UniqueOperationNames"); + +var _UniqueVariableNames = require("./rules/UniqueVariableNames"); + +var _ValuesOfCorrectType = require("./rules/ValuesOfCorrectType"); + +var _VariablesAreInputTypes = require("./rules/VariablesAreInputTypes"); + +var _VariablesInAllowedPosition = require("./rules/VariablesInAllowedPosition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } \ No newline at end of file diff --git a/dist/validation/index.js.flow b/dist/validation/index.js.flow new file mode 100644 index 0000000000..afcff6994b --- /dev/null +++ b/dist/validation/index.js.flow @@ -0,0 +1,137 @@ +/** + * 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. + * + * @flow strict + */ + +export { validate } from './validate'; + +// https://github.com/tc39/proposal-export-default-from +import ValidationContext from './ValidationContext'; +export { ValidationContext }; + +export { specifiedRules } from './specifiedRules'; + +// Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" +export { + FieldsOnCorrectType as FieldsOnCorrectTypeRule, +} from './rules/FieldsOnCorrectType'; + +// Spec Section: "Fragments on Composite Types" +export { + FragmentsOnCompositeTypes as FragmentsOnCompositeTypesRule, +} from './rules/FragmentsOnCompositeTypes'; + +// Spec Section: "Argument Names" +export { + KnownArgumentNames as KnownArgumentNamesRule, +} from './rules/KnownArgumentNames'; + +// Spec Section: "Directives Are Defined" +export { + KnownDirectives as KnownDirectivesRule, +} from './rules/KnownDirectives'; + +// Spec Section: "Fragment spread target defined" +export { + KnownFragmentNames as KnownFragmentNamesRule, +} from './rules/KnownFragmentNames'; + +// Spec Section: "Fragment Spread Type Existence" +export { KnownTypeNames as KnownTypeNamesRule } from './rules/KnownTypeNames'; + +// Spec Section: "Lone Anonymous Operation" +export { + LoneAnonymousOperation as LoneAnonymousOperationRule, +} from './rules/LoneAnonymousOperation'; + +// Spec Section: "Fragments must not form cycles" +export { + NoFragmentCycles as NoFragmentCyclesRule, +} from './rules/NoFragmentCycles'; + +// Spec Section: "All Variable Used Defined" +export { + NoUndefinedVariables as NoUndefinedVariablesRule, +} from './rules/NoUndefinedVariables'; + +// Spec Section: "Fragments must be used" +export { + NoUnusedFragments as NoUnusedFragmentsRule, +} from './rules/NoUnusedFragments'; + +// Spec Section: "All Variables Used" +export { + NoUnusedVariables as NoUnusedVariablesRule, +} from './rules/NoUnusedVariables'; + +// Spec Section: "Field Selection Merging" +export { + OverlappingFieldsCanBeMerged as OverlappingFieldsCanBeMergedRule, +} from './rules/OverlappingFieldsCanBeMerged'; + +// Spec Section: "Fragment spread is possible" +export { + PossibleFragmentSpreads as PossibleFragmentSpreadsRule, +} from './rules/PossibleFragmentSpreads'; + +// Spec Section: "Argument Optionality" +export { + ProvidedRequiredArguments as ProvidedRequiredArgumentsRule, +} from './rules/ProvidedRequiredArguments'; + +// Spec Section: "Leaf Field Selections" +export { ScalarLeafs as ScalarLeafsRule } from './rules/ScalarLeafs'; + +// Spec Section: "Subscriptions with Single Root Field" +export { + SingleFieldSubscriptions as SingleFieldSubscriptionsRule, +} from './rules/SingleFieldSubscriptions'; + +// Spec Section: "Argument Uniqueness" +export { + UniqueArgumentNames as UniqueArgumentNamesRule, +} from './rules/UniqueArgumentNames'; + +// Spec Section: "Directives Are Unique Per Location" +export { + UniqueDirectivesPerLocation as UniqueDirectivesPerLocationRule, +} from './rules/UniqueDirectivesPerLocation'; + +// Spec Section: "Fragment Name Uniqueness" +export { + UniqueFragmentNames as UniqueFragmentNamesRule, +} from './rules/UniqueFragmentNames'; + +// Spec Section: "Input Object Field Uniqueness" +export { + UniqueInputFieldNames as UniqueInputFieldNamesRule, +} from './rules/UniqueInputFieldNames'; + +// Spec Section: "Operation Name Uniqueness" +export { + UniqueOperationNames as UniqueOperationNamesRule, +} from './rules/UniqueOperationNames'; + +// Spec Section: "Variable Uniqueness" +export { + UniqueVariableNames as UniqueVariableNamesRule, +} from './rules/UniqueVariableNames'; + +// Spec Section: "Values Type Correctness" +export { + ValuesOfCorrectType as ValuesOfCorrectTypeRule, +} from './rules/ValuesOfCorrectType'; + +// Spec Section: "Variables are Input Types" +export { + VariablesAreInputTypes as VariablesAreInputTypesRule, +} from './rules/VariablesAreInputTypes'; + +// Spec Section: "All Variable Usages Are Allowed" +export { + VariablesInAllowedPosition as VariablesInAllowedPositionRule, +} from './rules/VariablesInAllowedPosition'; diff --git a/dist/validation/index.mjs b/dist/validation/index.mjs new file mode 100644 index 0000000000..a9879620b4 --- /dev/null +++ b/dist/validation/index.mjs @@ -0,0 +1,63 @@ +/** + * 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. + * + * strict + */ +export { validate } from './validate'; // https://github.com/tc39/proposal-export-default-from + +import ValidationContext from './ValidationContext'; +export { ValidationContext }; +export { specifiedRules } from './specifiedRules'; // Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" + +export { FieldsOnCorrectType as FieldsOnCorrectTypeRule } from './rules/FieldsOnCorrectType'; // Spec Section: "Fragments on Composite Types" + +export { FragmentsOnCompositeTypes as FragmentsOnCompositeTypesRule } from './rules/FragmentsOnCompositeTypes'; // Spec Section: "Argument Names" + +export { KnownArgumentNames as KnownArgumentNamesRule } from './rules/KnownArgumentNames'; // Spec Section: "Directives Are Defined" + +export { KnownDirectives as KnownDirectivesRule } from './rules/KnownDirectives'; // Spec Section: "Fragment spread target defined" + +export { KnownFragmentNames as KnownFragmentNamesRule } from './rules/KnownFragmentNames'; // Spec Section: "Fragment Spread Type Existence" + +export { KnownTypeNames as KnownTypeNamesRule } from './rules/KnownTypeNames'; // Spec Section: "Lone Anonymous Operation" + +export { LoneAnonymousOperation as LoneAnonymousOperationRule } from './rules/LoneAnonymousOperation'; // Spec Section: "Fragments must not form cycles" + +export { NoFragmentCycles as NoFragmentCyclesRule } from './rules/NoFragmentCycles'; // Spec Section: "All Variable Used Defined" + +export { NoUndefinedVariables as NoUndefinedVariablesRule } from './rules/NoUndefinedVariables'; // Spec Section: "Fragments must be used" + +export { NoUnusedFragments as NoUnusedFragmentsRule } from './rules/NoUnusedFragments'; // Spec Section: "All Variables Used" + +export { NoUnusedVariables as NoUnusedVariablesRule } from './rules/NoUnusedVariables'; // Spec Section: "Field Selection Merging" + +export { OverlappingFieldsCanBeMerged as OverlappingFieldsCanBeMergedRule } from './rules/OverlappingFieldsCanBeMerged'; // Spec Section: "Fragment spread is possible" + +export { PossibleFragmentSpreads as PossibleFragmentSpreadsRule } from './rules/PossibleFragmentSpreads'; // Spec Section: "Argument Optionality" + +export { ProvidedRequiredArguments as ProvidedRequiredArgumentsRule } from './rules/ProvidedRequiredArguments'; // Spec Section: "Leaf Field Selections" + +export { ScalarLeafs as ScalarLeafsRule } from './rules/ScalarLeafs'; // Spec Section: "Subscriptions with Single Root Field" + +export { SingleFieldSubscriptions as SingleFieldSubscriptionsRule } from './rules/SingleFieldSubscriptions'; // Spec Section: "Argument Uniqueness" + +export { UniqueArgumentNames as UniqueArgumentNamesRule } from './rules/UniqueArgumentNames'; // Spec Section: "Directives Are Unique Per Location" + +export { UniqueDirectivesPerLocation as UniqueDirectivesPerLocationRule } from './rules/UniqueDirectivesPerLocation'; // Spec Section: "Fragment Name Uniqueness" + +export { UniqueFragmentNames as UniqueFragmentNamesRule } from './rules/UniqueFragmentNames'; // Spec Section: "Input Object Field Uniqueness" + +export { UniqueInputFieldNames as UniqueInputFieldNamesRule } from './rules/UniqueInputFieldNames'; // Spec Section: "Operation Name Uniqueness" + +export { UniqueOperationNames as UniqueOperationNamesRule } from './rules/UniqueOperationNames'; // Spec Section: "Variable Uniqueness" + +export { UniqueVariableNames as UniqueVariableNamesRule } from './rules/UniqueVariableNames'; // Spec Section: "Values Type Correctness" + +export { ValuesOfCorrectType as ValuesOfCorrectTypeRule } from './rules/ValuesOfCorrectType'; // Spec Section: "Variables are Input Types" + +export { VariablesAreInputTypes as VariablesAreInputTypesRule } from './rules/VariablesAreInputTypes'; // Spec Section: "All Variable Usages Are Allowed" + +export { VariablesInAllowedPosition as VariablesInAllowedPositionRule } from './rules/VariablesInAllowedPosition'; \ No newline at end of file diff --git a/dist/validation/rules/ExecutableDefinitions.js b/dist/validation/rules/ExecutableDefinitions.js new file mode 100644 index 0000000000..277267c6e8 --- /dev/null +++ b/dist/validation/rules/ExecutableDefinitions.js @@ -0,0 +1,43 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.nonExecutableDefinitionMessage = nonExecutableDefinitionMessage; +exports.ExecutableDefinitions = ExecutableDefinitions; + +var _error = require("../../error"); + +var _kinds = require("../../language/kinds"); + +/** + * 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. + * + * strict + */ +function nonExecutableDefinitionMessage(defName) { + return "The ".concat(defName, " definition is not executable."); +} +/** + * Executable definitions + * + * A GraphQL document is only valid for execution if all definitions are either + * operation or fragment definitions. + */ + + +function ExecutableDefinitions(context) { + return { + Document: function Document(node) { + node.definitions.forEach(function (definition) { + if (definition.kind !== _kinds.Kind.OPERATION_DEFINITION && definition.kind !== _kinds.Kind.FRAGMENT_DEFINITION) { + context.reportError(new _error.GraphQLError(nonExecutableDefinitionMessage(definition.kind === _kinds.Kind.SCHEMA_DEFINITION || definition.kind === _kinds.Kind.SCHEMA_EXTENSION ? 'schema' : definition.name.value), [definition])); + } + }); + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/ExecutableDefinitions.js.flow b/dist/validation/rules/ExecutableDefinitions.js.flow new file mode 100644 index 0000000000..ac6a5a4bb9 --- /dev/null +++ b/dist/validation/rules/ExecutableDefinitions.js.flow @@ -0,0 +1,49 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +import type { ASTVisitor } from '../../language/visitor'; + +export function nonExecutableDefinitionMessage(defName: string): string { + return `The ${defName} definition is not executable.`; +} + +/** + * Executable definitions + * + * A GraphQL document is only valid for execution if all definitions are either + * operation or fragment definitions. + */ +export function ExecutableDefinitions(context: ValidationContext): ASTVisitor { + return { + Document(node) { + node.definitions.forEach(definition => { + if ( + definition.kind !== Kind.OPERATION_DEFINITION && + definition.kind !== Kind.FRAGMENT_DEFINITION + ) { + context.reportError( + new GraphQLError( + nonExecutableDefinitionMessage( + definition.kind === Kind.SCHEMA_DEFINITION || + definition.kind === Kind.SCHEMA_EXTENSION + ? 'schema' + : definition.name.value, + ), + [definition], + ), + ); + } + }); + return false; + }, + }; +} diff --git a/dist/validation/rules/ExecutableDefinitions.mjs b/dist/validation/rules/ExecutableDefinitions.mjs new file mode 100644 index 0000000000..0204c003aa --- /dev/null +++ b/dist/validation/rules/ExecutableDefinitions.mjs @@ -0,0 +1,32 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +export function nonExecutableDefinitionMessage(defName) { + return "The ".concat(defName, " definition is not executable."); +} +/** + * Executable definitions + * + * A GraphQL document is only valid for execution if all definitions are either + * operation or fragment definitions. + */ + +export function ExecutableDefinitions(context) { + return { + Document: function Document(node) { + node.definitions.forEach(function (definition) { + if (definition.kind !== Kind.OPERATION_DEFINITION && definition.kind !== Kind.FRAGMENT_DEFINITION) { + context.reportError(new GraphQLError(nonExecutableDefinitionMessage(definition.kind === Kind.SCHEMA_DEFINITION || definition.kind === Kind.SCHEMA_EXTENSION ? 'schema' : definition.name.value), [definition])); + } + }); + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/FieldsOnCorrectType.js b/dist/validation/rules/FieldsOnCorrectType.js new file mode 100644 index 0000000000..42836b8380 --- /dev/null +++ b/dist/validation/rules/FieldsOnCorrectType.js @@ -0,0 +1,123 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.undefinedFieldMessage = undefinedFieldMessage; +exports.FieldsOnCorrectType = FieldsOnCorrectType; + +var _error = require("../../error"); + +var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList")); + +var _quotedOrList = _interopRequireDefault(require("../../jsutils/quotedOrList")); + +var _definition = require("../../type/definition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function undefinedFieldMessage(fieldName, type, suggestedTypeNames, suggestedFieldNames) { + var message = "Cannot query field \"".concat(fieldName, "\" on type \"").concat(type, "\"."); + + if (suggestedTypeNames.length !== 0) { + var suggestions = (0, _quotedOrList.default)(suggestedTypeNames); + message += " Did you mean to use an inline fragment on ".concat(suggestions, "?"); + } else if (suggestedFieldNames.length !== 0) { + message += " Did you mean ".concat((0, _quotedOrList.default)(suggestedFieldNames), "?"); + } + + return message; +} +/** + * Fields on correct type + * + * A GraphQL document is only valid if all fields selected are defined by the + * parent type, or are an allowed meta field such as __typename. + */ + + +function FieldsOnCorrectType(context) { + return { + Field: function Field(node) { + var type = context.getParentType(); + + if (type) { + var fieldDef = context.getFieldDef(); + + if (!fieldDef) { + // This field doesn't exist, lets look for suggestions. + var schema = context.getSchema(); + var fieldName = node.name.value; // First determine if there are any suggested types to condition on. + + var suggestedTypeNames = getSuggestedTypeNames(schema, type, fieldName); // If there are no suggested types, then perhaps this was a typo? + + var suggestedFieldNames = suggestedTypeNames.length !== 0 ? [] : getSuggestedFieldNames(schema, type, fieldName); // Report an error, including helpful suggestions. + + context.reportError(new _error.GraphQLError(undefinedFieldMessage(fieldName, type.name, suggestedTypeNames, suggestedFieldNames), [node])); + } + } + } + }; +} +/** + * Go through all of the implementations of type, as well as the interfaces + * that they implement. If any of those types include the provided field, + * suggest them, sorted by how often the type is referenced, starting + * with Interfaces. + */ + + +function getSuggestedTypeNames(schema, type, fieldName) { + if ((0, _definition.isAbstractType)(type)) { + var suggestedObjectTypes = []; + var interfaceUsageCount = Object.create(null); + schema.getPossibleTypes(type).forEach(function (possibleType) { + if (!possibleType.getFields()[fieldName]) { + return; + } // This object type defines this field. + + + suggestedObjectTypes.push(possibleType.name); + possibleType.getInterfaces().forEach(function (possibleInterface) { + if (!possibleInterface.getFields()[fieldName]) { + return; + } // This interface type defines this field. + + + interfaceUsageCount[possibleInterface.name] = (interfaceUsageCount[possibleInterface.name] || 0) + 1; + }); + }); // Suggest interface types based on how common they are. + + var suggestedInterfaceTypes = Object.keys(interfaceUsageCount).sort(function (a, b) { + return interfaceUsageCount[b] - interfaceUsageCount[a]; + }); // Suggest both interface and object types. + + return suggestedInterfaceTypes.concat(suggestedObjectTypes); + } // Otherwise, must be an Object type, which does not have possible fields. + + + return []; +} +/** + * For the field name provided, determine if there are any similar field names + * that may be the result of a typo. + */ + + +function getSuggestedFieldNames(schema, type, fieldName) { + if ((0, _definition.isObjectType)(type) || (0, _definition.isInterfaceType)(type)) { + var possibleFieldNames = Object.keys(type.getFields()); + return (0, _suggestionList.default)(fieldName, possibleFieldNames); + } // Otherwise, must be a Union type, which does not define fields. + + + return []; +} \ No newline at end of file diff --git a/dist/validation/rules/FieldsOnCorrectType.js.flow b/dist/validation/rules/FieldsOnCorrectType.js.flow new file mode 100644 index 0000000000..168fc1bca7 --- /dev/null +++ b/dist/validation/rules/FieldsOnCorrectType.js.flow @@ -0,0 +1,144 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +import type { FieldNode } from '../../language/ast'; +import type { ASTVisitor } from '../../language/visitor'; +import type { GraphQLSchema } from '../../type/schema'; +import type { GraphQLOutputType } from '../../type/definition'; +import { + isObjectType, + isInterfaceType, + isAbstractType, +} from '../../type/definition'; + +export function undefinedFieldMessage( + fieldName: string, + type: string, + suggestedTypeNames: Array, + suggestedFieldNames: Array, +): string { + let message = `Cannot query field "${fieldName}" on type "${type}".`; + if (suggestedTypeNames.length !== 0) { + const suggestions = quotedOrList(suggestedTypeNames); + message += ` Did you mean to use an inline fragment on ${suggestions}?`; + } else if (suggestedFieldNames.length !== 0) { + message += ` Did you mean ${quotedOrList(suggestedFieldNames)}?`; + } + return message; +} + +/** + * Fields on correct type + * + * A GraphQL document is only valid if all fields selected are defined by the + * parent type, or are an allowed meta field such as __typename. + */ +export function FieldsOnCorrectType(context: ValidationContext): ASTVisitor { + return { + Field(node: FieldNode) { + const type = context.getParentType(); + if (type) { + const fieldDef = context.getFieldDef(); + if (!fieldDef) { + // This field doesn't exist, lets look for suggestions. + const schema = context.getSchema(); + const fieldName = node.name.value; + // First determine if there are any suggested types to condition on. + const suggestedTypeNames = getSuggestedTypeNames( + schema, + type, + fieldName, + ); + // If there are no suggested types, then perhaps this was a typo? + const suggestedFieldNames = + suggestedTypeNames.length !== 0 + ? [] + : getSuggestedFieldNames(schema, type, fieldName); + + // Report an error, including helpful suggestions. + context.reportError( + new GraphQLError( + undefinedFieldMessage( + fieldName, + type.name, + suggestedTypeNames, + suggestedFieldNames, + ), + [node], + ), + ); + } + } + }, + }; +} + +/** + * Go through all of the implementations of type, as well as the interfaces + * that they implement. If any of those types include the provided field, + * suggest them, sorted by how often the type is referenced, starting + * with Interfaces. + */ +function getSuggestedTypeNames( + schema: GraphQLSchema, + type: GraphQLOutputType, + fieldName: string, +): Array { + if (isAbstractType(type)) { + const suggestedObjectTypes = []; + const interfaceUsageCount = Object.create(null); + schema.getPossibleTypes(type).forEach(possibleType => { + if (!possibleType.getFields()[fieldName]) { + return; + } + // This object type defines this field. + suggestedObjectTypes.push(possibleType.name); + possibleType.getInterfaces().forEach(possibleInterface => { + if (!possibleInterface.getFields()[fieldName]) { + return; + } + // This interface type defines this field. + interfaceUsageCount[possibleInterface.name] = + (interfaceUsageCount[possibleInterface.name] || 0) + 1; + }); + }); + + // Suggest interface types based on how common they are. + const suggestedInterfaceTypes = Object.keys(interfaceUsageCount).sort( + (a, b) => interfaceUsageCount[b] - interfaceUsageCount[a], + ); + + // Suggest both interface and object types. + return suggestedInterfaceTypes.concat(suggestedObjectTypes); + } + + // Otherwise, must be an Object type, which does not have possible fields. + return []; +} + +/** + * For the field name provided, determine if there are any similar field names + * that may be the result of a typo. + */ +function getSuggestedFieldNames( + schema: GraphQLSchema, + type: GraphQLOutputType, + fieldName: string, +): Array { + if (isObjectType(type) || isInterfaceType(type)) { + const possibleFieldNames = Object.keys(type.getFields()); + return suggestionList(fieldName, possibleFieldNames); + } + // Otherwise, must be a Union type, which does not define fields. + return []; +} diff --git a/dist/validation/rules/FieldsOnCorrectType.mjs b/dist/validation/rules/FieldsOnCorrectType.mjs new file mode 100644 index 0000000000..ce6ab28af5 --- /dev/null +++ b/dist/validation/rules/FieldsOnCorrectType.mjs @@ -0,0 +1,107 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +import { isObjectType, isInterfaceType, isAbstractType } from '../../type/definition'; +export function undefinedFieldMessage(fieldName, type, suggestedTypeNames, suggestedFieldNames) { + var message = "Cannot query field \"".concat(fieldName, "\" on type \"").concat(type, "\"."); + + if (suggestedTypeNames.length !== 0) { + var suggestions = quotedOrList(suggestedTypeNames); + message += " Did you mean to use an inline fragment on ".concat(suggestions, "?"); + } else if (suggestedFieldNames.length !== 0) { + message += " Did you mean ".concat(quotedOrList(suggestedFieldNames), "?"); + } + + return message; +} +/** + * Fields on correct type + * + * A GraphQL document is only valid if all fields selected are defined by the + * parent type, or are an allowed meta field such as __typename. + */ + +export function FieldsOnCorrectType(context) { + return { + Field: function Field(node) { + var type = context.getParentType(); + + if (type) { + var fieldDef = context.getFieldDef(); + + if (!fieldDef) { + // This field doesn't exist, lets look for suggestions. + var schema = context.getSchema(); + var fieldName = node.name.value; // First determine if there are any suggested types to condition on. + + var suggestedTypeNames = getSuggestedTypeNames(schema, type, fieldName); // If there are no suggested types, then perhaps this was a typo? + + var suggestedFieldNames = suggestedTypeNames.length !== 0 ? [] : getSuggestedFieldNames(schema, type, fieldName); // Report an error, including helpful suggestions. + + context.reportError(new GraphQLError(undefinedFieldMessage(fieldName, type.name, suggestedTypeNames, suggestedFieldNames), [node])); + } + } + } + }; +} +/** + * Go through all of the implementations of type, as well as the interfaces + * that they implement. If any of those types include the provided field, + * suggest them, sorted by how often the type is referenced, starting + * with Interfaces. + */ + +function getSuggestedTypeNames(schema, type, fieldName) { + if (isAbstractType(type)) { + var suggestedObjectTypes = []; + var interfaceUsageCount = Object.create(null); + schema.getPossibleTypes(type).forEach(function (possibleType) { + if (!possibleType.getFields()[fieldName]) { + return; + } // This object type defines this field. + + + suggestedObjectTypes.push(possibleType.name); + possibleType.getInterfaces().forEach(function (possibleInterface) { + if (!possibleInterface.getFields()[fieldName]) { + return; + } // This interface type defines this field. + + + interfaceUsageCount[possibleInterface.name] = (interfaceUsageCount[possibleInterface.name] || 0) + 1; + }); + }); // Suggest interface types based on how common they are. + + var suggestedInterfaceTypes = Object.keys(interfaceUsageCount).sort(function (a, b) { + return interfaceUsageCount[b] - interfaceUsageCount[a]; + }); // Suggest both interface and object types. + + return suggestedInterfaceTypes.concat(suggestedObjectTypes); + } // Otherwise, must be an Object type, which does not have possible fields. + + + return []; +} +/** + * For the field name provided, determine if there are any similar field names + * that may be the result of a typo. + */ + + +function getSuggestedFieldNames(schema, type, fieldName) { + if (isObjectType(type) || isInterfaceType(type)) { + var possibleFieldNames = Object.keys(type.getFields()); + return suggestionList(fieldName, possibleFieldNames); + } // Otherwise, must be a Union type, which does not define fields. + + + return []; +} \ No newline at end of file diff --git a/dist/validation/rules/FragmentsOnCompositeTypes.js b/dist/validation/rules/FragmentsOnCompositeTypes.js new file mode 100644 index 0000000000..21cdc885ad --- /dev/null +++ b/dist/validation/rules/FragmentsOnCompositeTypes.js @@ -0,0 +1,63 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.inlineFragmentOnNonCompositeErrorMessage = inlineFragmentOnNonCompositeErrorMessage; +exports.fragmentOnNonCompositeErrorMessage = fragmentOnNonCompositeErrorMessage; +exports.FragmentsOnCompositeTypes = FragmentsOnCompositeTypes; + +var _error = require("../../error"); + +var _printer = require("../../language/printer"); + +var _definition = require("../../type/definition"); + +var _typeFromAST = require("../../utilities/typeFromAST"); + +/** + * 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. + * + * strict + */ +function inlineFragmentOnNonCompositeErrorMessage(type) { + return "Fragment cannot condition on non composite type \"".concat(String(type), "\"."); +} + +function fragmentOnNonCompositeErrorMessage(fragName, type) { + return "Fragment \"".concat(fragName, "\" cannot condition on non composite ") + "type \"".concat(String(type), "\"."); +} +/** + * 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 FragmentsOnCompositeTypes(context) { + return { + InlineFragment: function InlineFragment(node) { + var typeCondition = node.typeCondition; + + if (typeCondition) { + var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition); + + if (type && !(0, _definition.isCompositeType)(type)) { + context.reportError(new _error.GraphQLError(inlineFragmentOnNonCompositeErrorMessage((0, _printer.print)(typeCondition)), [typeCondition])); + } + } + }, + FragmentDefinition: function FragmentDefinition(node) { + var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), node.typeCondition); + + if (type && !(0, _definition.isCompositeType)(type)) { + context.reportError(new _error.GraphQLError(fragmentOnNonCompositeErrorMessage(node.name.value, (0, _printer.print)(node.typeCondition)), [node.typeCondition])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/FragmentsOnCompositeTypes.js.flow b/dist/validation/rules/FragmentsOnCompositeTypes.js.flow new file mode 100644 index 0000000000..ea03734d85 --- /dev/null +++ b/dist/validation/rules/FragmentsOnCompositeTypes.js.flow @@ -0,0 +1,74 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import { print } from '../../language/printer'; +import type { ASTVisitor } from '../../language/visitor'; +import { isCompositeType } from '../../type/definition'; +import type { GraphQLType } from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; + +export function inlineFragmentOnNonCompositeErrorMessage( + type: GraphQLType, +): string { + return `Fragment cannot condition on non composite type "${String(type)}".`; +} + +export function fragmentOnNonCompositeErrorMessage( + fragName: string, + type: GraphQLType, +): string { + return ( + `Fragment "${fragName}" cannot condition on non composite ` + + `type "${String(type)}".` + ); +} + +/** + * 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. + */ +export function FragmentsOnCompositeTypes( + context: ValidationContext, +): ASTVisitor { + return { + InlineFragment(node) { + const typeCondition = node.typeCondition; + if (typeCondition) { + const type = typeFromAST(context.getSchema(), typeCondition); + if (type && !isCompositeType(type)) { + context.reportError( + new GraphQLError( + inlineFragmentOnNonCompositeErrorMessage(print(typeCondition)), + [typeCondition], + ), + ); + } + } + }, + FragmentDefinition(node) { + const type = typeFromAST(context.getSchema(), node.typeCondition); + if (type && !isCompositeType(type)) { + context.reportError( + new GraphQLError( + fragmentOnNonCompositeErrorMessage( + node.name.value, + print(node.typeCondition), + ), + [node.typeCondition], + ), + ); + } + }, + }; +} diff --git a/dist/validation/rules/FragmentsOnCompositeTypes.mjs b/dist/validation/rules/FragmentsOnCompositeTypes.mjs new file mode 100644 index 0000000000..a7a73f42c7 --- /dev/null +++ b/dist/validation/rules/FragmentsOnCompositeTypes.mjs @@ -0,0 +1,48 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { print } from '../../language/printer'; +import { isCompositeType } from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; +export function inlineFragmentOnNonCompositeErrorMessage(type) { + return "Fragment cannot condition on non composite type \"".concat(String(type), "\"."); +} +export function fragmentOnNonCompositeErrorMessage(fragName, type) { + return "Fragment \"".concat(fragName, "\" cannot condition on non composite ") + "type \"".concat(String(type), "\"."); +} +/** + * 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. + */ + +export function FragmentsOnCompositeTypes(context) { + return { + InlineFragment: function InlineFragment(node) { + var typeCondition = node.typeCondition; + + if (typeCondition) { + var type = typeFromAST(context.getSchema(), typeCondition); + + if (type && !isCompositeType(type)) { + context.reportError(new GraphQLError(inlineFragmentOnNonCompositeErrorMessage(print(typeCondition)), [typeCondition])); + } + } + }, + FragmentDefinition: function FragmentDefinition(node) { + var type = typeFromAST(context.getSchema(), node.typeCondition); + + if (type && !isCompositeType(type)) { + context.reportError(new GraphQLError(fragmentOnNonCompositeErrorMessage(node.name.value, print(node.typeCondition)), [node.typeCondition])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownArgumentNames.js b/dist/validation/rules/KnownArgumentNames.js new file mode 100644 index 0000000000..d319ac1daf --- /dev/null +++ b/dist/validation/rules/KnownArgumentNames.js @@ -0,0 +1,84 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unknownArgMessage = unknownArgMessage; +exports.unknownDirectiveArgMessage = unknownDirectiveArgMessage; +exports.KnownArgumentNames = KnownArgumentNames; + +var _error = require("../../error"); + +var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList")); + +var _quotedOrList = _interopRequireDefault(require("../../jsutils/quotedOrList")); + +var _kinds = require("../../language/kinds"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function unknownArgMessage(argName, fieldName, typeName, suggestedArgs) { + var message = "Unknown argument \"".concat(argName, "\" on field \"").concat(fieldName, "\" of ") + "type \"".concat(typeName, "\"."); + + if (suggestedArgs.length) { + message += " Did you mean ".concat((0, _quotedOrList.default)(suggestedArgs), "?"); + } + + return message; +} + +function unknownDirectiveArgMessage(argName, directiveName, suggestedArgs) { + var message = "Unknown argument \"".concat(argName, "\" on directive \"@").concat(directiveName, "\"."); + + if (suggestedArgs.length) { + message += " Did you mean ".concat((0, _quotedOrList.default)(suggestedArgs), "?"); + } + + return message; +} +/** + * Known argument names + * + * A GraphQL field is only valid if all supplied arguments are defined by + * that field. + */ + + +function KnownArgumentNames(context) { + return { + Argument: function Argument(node, key, parent, path, ancestors) { + var argDef = context.getArgument(); + + if (!argDef) { + var argumentOf = ancestors[ancestors.length - 1]; + + if (argumentOf.kind === _kinds.Kind.FIELD) { + var fieldDef = context.getFieldDef(); + var parentType = context.getParentType(); + + if (fieldDef && parentType) { + context.reportError(new _error.GraphQLError(unknownArgMessage(node.name.value, fieldDef.name, parentType.name, (0, _suggestionList.default)(node.name.value, fieldDef.args.map(function (arg) { + return arg.name; + }))), [node])); + } + } else if (argumentOf.kind === _kinds.Kind.DIRECTIVE) { + var directive = context.getDirective(); + + if (directive) { + context.reportError(new _error.GraphQLError(unknownDirectiveArgMessage(node.name.value, directive.name, (0, _suggestionList.default)(node.name.value, directive.args.map(function (arg) { + return arg.name; + }))), [node])); + } + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownArgumentNames.js.flow b/dist/validation/rules/KnownArgumentNames.js.flow new file mode 100644 index 0000000000..a775880c21 --- /dev/null +++ b/dist/validation/rules/KnownArgumentNames.js.flow @@ -0,0 +1,96 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +import { Kind } from '../../language/kinds'; + +export function unknownArgMessage( + argName: string, + fieldName: string, + typeName: string, + suggestedArgs: Array, +): string { + let message = + `Unknown argument "${argName}" on field "${fieldName}" of ` + + `type "${typeName}".`; + if (suggestedArgs.length) { + message += ` Did you mean ${quotedOrList(suggestedArgs)}?`; + } + return message; +} + +export function unknownDirectiveArgMessage( + argName: string, + directiveName: string, + suggestedArgs: Array, +): string { + let message = `Unknown argument "${argName}" on directive "@${directiveName}".`; + if (suggestedArgs.length) { + message += ` Did you mean ${quotedOrList(suggestedArgs)}?`; + } + return message; +} + +/** + * Known argument names + * + * A GraphQL field is only valid if all supplied arguments are defined by + * that field. + */ +export function KnownArgumentNames(context: ValidationContext): ASTVisitor { + return { + Argument(node, key, parent, path, ancestors) { + const argDef = context.getArgument(); + if (!argDef) { + const argumentOf = ancestors[ancestors.length - 1]; + if (argumentOf.kind === Kind.FIELD) { + const fieldDef = context.getFieldDef(); + const parentType = context.getParentType(); + if (fieldDef && parentType) { + context.reportError( + new GraphQLError( + unknownArgMessage( + node.name.value, + fieldDef.name, + parentType.name, + suggestionList( + node.name.value, + fieldDef.args.map(arg => arg.name), + ), + ), + [node], + ), + ); + } + } else if (argumentOf.kind === Kind.DIRECTIVE) { + const directive = context.getDirective(); + if (directive) { + context.reportError( + new GraphQLError( + unknownDirectiveArgMessage( + node.name.value, + directive.name, + suggestionList( + node.name.value, + directive.args.map(arg => arg.name), + ), + ), + [node], + ), + ); + } + } + } + }, + }; +} diff --git a/dist/validation/rules/KnownArgumentNames.mjs b/dist/validation/rules/KnownArgumentNames.mjs new file mode 100644 index 0000000000..be2ee0eba8 --- /dev/null +++ b/dist/validation/rules/KnownArgumentNames.mjs @@ -0,0 +1,67 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +import { Kind } from '../../language/kinds'; +export function unknownArgMessage(argName, fieldName, typeName, suggestedArgs) { + var message = "Unknown argument \"".concat(argName, "\" on field \"").concat(fieldName, "\" of ") + "type \"".concat(typeName, "\"."); + + if (suggestedArgs.length) { + message += " Did you mean ".concat(quotedOrList(suggestedArgs), "?"); + } + + return message; +} +export function unknownDirectiveArgMessage(argName, directiveName, suggestedArgs) { + var message = "Unknown argument \"".concat(argName, "\" on directive \"@").concat(directiveName, "\"."); + + if (suggestedArgs.length) { + message += " Did you mean ".concat(quotedOrList(suggestedArgs), "?"); + } + + return message; +} +/** + * Known argument names + * + * A GraphQL field is only valid if all supplied arguments are defined by + * that field. + */ + +export function KnownArgumentNames(context) { + return { + Argument: function Argument(node, key, parent, path, ancestors) { + var argDef = context.getArgument(); + + if (!argDef) { + var argumentOf = ancestors[ancestors.length - 1]; + + if (argumentOf.kind === Kind.FIELD) { + var fieldDef = context.getFieldDef(); + var parentType = context.getParentType(); + + if (fieldDef && parentType) { + context.reportError(new GraphQLError(unknownArgMessage(node.name.value, fieldDef.name, parentType.name, suggestionList(node.name.value, fieldDef.args.map(function (arg) { + return arg.name; + }))), [node])); + } + } else if (argumentOf.kind === Kind.DIRECTIVE) { + var directive = context.getDirective(); + + if (directive) { + context.reportError(new GraphQLError(unknownDirectiveArgMessage(node.name.value, directive.name, suggestionList(node.name.value, directive.args.map(function (arg) { + return arg.name; + }))), [node])); + } + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownDirectives.js b/dist/validation/rules/KnownDirectives.js new file mode 100644 index 0000000000..6f9bd6910a --- /dev/null +++ b/dist/validation/rules/KnownDirectives.js @@ -0,0 +1,134 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unknownDirectiveMessage = unknownDirectiveMessage; +exports.misplacedDirectiveMessage = misplacedDirectiveMessage; +exports.KnownDirectives = KnownDirectives; + +var _error = require("../../error"); + +var _find = _interopRequireDefault(require("../../jsutils/find")); + +var _kinds = require("../../language/kinds"); + +var _directiveLocation = require("../../language/directiveLocation"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function unknownDirectiveMessage(directiveName) { + return "Unknown directive \"".concat(directiveName, "\"."); +} + +function misplacedDirectiveMessage(directiveName, location) { + return "Directive \"".concat(directiveName, "\" may not be used on ").concat(location, "."); +} +/** + * Known directives + * + * A GraphQL document is only valid if all `@directives` are known by the + * schema and legally positioned. + */ + + +function KnownDirectives(context) { + return { + Directive: function Directive(node, key, parent, path, ancestors) { + var directiveDef = (0, _find.default)(context.getSchema().getDirectives(), function (def) { + return def.name === node.name.value; + }); + + if (!directiveDef) { + context.reportError(new _error.GraphQLError(unknownDirectiveMessage(node.name.value), [node])); + return; + } + + var candidateLocation = getDirectiveLocationForASTPath(ancestors); + + if (candidateLocation && directiveDef.locations.indexOf(candidateLocation) === -1) { + context.reportError(new _error.GraphQLError(misplacedDirectiveMessage(node.name.value, candidateLocation), [node])); + } + } + }; +} + +function getDirectiveLocationForASTPath(ancestors) { + var appliedTo = ancestors[ancestors.length - 1]; + + if (!Array.isArray(appliedTo)) { + switch (appliedTo.kind) { + case _kinds.Kind.OPERATION_DEFINITION: + switch (appliedTo.operation) { + case 'query': + return _directiveLocation.DirectiveLocation.QUERY; + + case 'mutation': + return _directiveLocation.DirectiveLocation.MUTATION; + + case 'subscription': + return _directiveLocation.DirectiveLocation.SUBSCRIPTION; + } + + break; + + case _kinds.Kind.FIELD: + return _directiveLocation.DirectiveLocation.FIELD; + + case _kinds.Kind.FRAGMENT_SPREAD: + return _directiveLocation.DirectiveLocation.FRAGMENT_SPREAD; + + case _kinds.Kind.INLINE_FRAGMENT: + return _directiveLocation.DirectiveLocation.INLINE_FRAGMENT; + + case _kinds.Kind.FRAGMENT_DEFINITION: + return _directiveLocation.DirectiveLocation.FRAGMENT_DEFINITION; + + case _kinds.Kind.SCHEMA_DEFINITION: + case _kinds.Kind.SCHEMA_EXTENSION: + return _directiveLocation.DirectiveLocation.SCHEMA; + + case _kinds.Kind.SCALAR_TYPE_DEFINITION: + case _kinds.Kind.SCALAR_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.SCALAR; + + case _kinds.Kind.OBJECT_TYPE_DEFINITION: + case _kinds.Kind.OBJECT_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.OBJECT; + + case _kinds.Kind.FIELD_DEFINITION: + return _directiveLocation.DirectiveLocation.FIELD_DEFINITION; + + case _kinds.Kind.INTERFACE_TYPE_DEFINITION: + case _kinds.Kind.INTERFACE_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.INTERFACE; + + case _kinds.Kind.UNION_TYPE_DEFINITION: + case _kinds.Kind.UNION_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.UNION; + + case _kinds.Kind.ENUM_TYPE_DEFINITION: + case _kinds.Kind.ENUM_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.ENUM; + + case _kinds.Kind.ENUM_VALUE_DEFINITION: + return _directiveLocation.DirectiveLocation.ENUM_VALUE; + + case _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION: + case _kinds.Kind.INPUT_OBJECT_TYPE_EXTENSION: + return _directiveLocation.DirectiveLocation.INPUT_OBJECT; + + case _kinds.Kind.INPUT_VALUE_DEFINITION: + var parentNode = ancestors[ancestors.length - 3]; + return parentNode.kind === _kinds.Kind.INPUT_OBJECT_TYPE_DEFINITION ? _directiveLocation.DirectiveLocation.INPUT_FIELD_DEFINITION : _directiveLocation.DirectiveLocation.ARGUMENT_DEFINITION; + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/KnownDirectives.js.flow b/dist/validation/rules/KnownDirectives.js.flow new file mode 100644 index 0000000000..05c12f39f6 --- /dev/null +++ b/dist/validation/rules/KnownDirectives.js.flow @@ -0,0 +1,117 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import find from '../../jsutils/find'; +import { Kind } from '../../language/kinds'; +import { DirectiveLocation } from '../../language/directiveLocation'; +import type { ASTVisitor } from '../../language/visitor'; + +export function unknownDirectiveMessage(directiveName: string): string { + return `Unknown directive "${directiveName}".`; +} + +export function misplacedDirectiveMessage( + directiveName: string, + location: string, +): string { + return `Directive "${directiveName}" may not be used on ${location}.`; +} + +/** + * Known directives + * + * A GraphQL document is only valid if all `@directives` are known by the + * schema and legally positioned. + */ +export function KnownDirectives(context: ValidationContext): ASTVisitor { + return { + Directive(node, key, parent, path, ancestors) { + const directiveDef = find( + context.getSchema().getDirectives(), + def => def.name === node.name.value, + ); + if (!directiveDef) { + context.reportError( + new GraphQLError(unknownDirectiveMessage(node.name.value), [node]), + ); + return; + } + const candidateLocation = getDirectiveLocationForASTPath(ancestors); + if ( + candidateLocation && + directiveDef.locations.indexOf(candidateLocation) === -1 + ) { + context.reportError( + new GraphQLError( + misplacedDirectiveMessage(node.name.value, candidateLocation), + [node], + ), + ); + } + }, + }; +} + +function getDirectiveLocationForASTPath(ancestors) { + const appliedTo = ancestors[ancestors.length - 1]; + if (!Array.isArray(appliedTo)) { + switch (appliedTo.kind) { + case Kind.OPERATION_DEFINITION: + switch (appliedTo.operation) { + case 'query': + return DirectiveLocation.QUERY; + case 'mutation': + return DirectiveLocation.MUTATION; + case 'subscription': + return DirectiveLocation.SUBSCRIPTION; + } + break; + case Kind.FIELD: + return DirectiveLocation.FIELD; + case Kind.FRAGMENT_SPREAD: + return DirectiveLocation.FRAGMENT_SPREAD; + case Kind.INLINE_FRAGMENT: + return DirectiveLocation.INLINE_FRAGMENT; + case Kind.FRAGMENT_DEFINITION: + return DirectiveLocation.FRAGMENT_DEFINITION; + case Kind.SCHEMA_DEFINITION: + case Kind.SCHEMA_EXTENSION: + return DirectiveLocation.SCHEMA; + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.SCALAR_TYPE_EXTENSION: + return DirectiveLocation.SCALAR; + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_EXTENSION: + return DirectiveLocation.OBJECT; + case Kind.FIELD_DEFINITION: + return DirectiveLocation.FIELD_DEFINITION; + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_EXTENSION: + return DirectiveLocation.INTERFACE; + case Kind.UNION_TYPE_DEFINITION: + case Kind.UNION_TYPE_EXTENSION: + return DirectiveLocation.UNION; + case Kind.ENUM_TYPE_DEFINITION: + case Kind.ENUM_TYPE_EXTENSION: + return DirectiveLocation.ENUM; + case Kind.ENUM_VALUE_DEFINITION: + return DirectiveLocation.ENUM_VALUE; + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + return DirectiveLocation.INPUT_OBJECT; + case Kind.INPUT_VALUE_DEFINITION: + const parentNode = ancestors[ancestors.length - 3]; + return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION + ? DirectiveLocation.INPUT_FIELD_DEFINITION + : DirectiveLocation.ARGUMENT_DEFINITION; + } + } +} diff --git a/dist/validation/rules/KnownDirectives.mjs b/dist/validation/rules/KnownDirectives.mjs new file mode 100644 index 0000000000..e3b6d85e05 --- /dev/null +++ b/dist/validation/rules/KnownDirectives.mjs @@ -0,0 +1,117 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import find from '../../jsutils/find'; +import { Kind } from '../../language/kinds'; +import { DirectiveLocation } from '../../language/directiveLocation'; +export function unknownDirectiveMessage(directiveName) { + return "Unknown directive \"".concat(directiveName, "\"."); +} +export function misplacedDirectiveMessage(directiveName, location) { + return "Directive \"".concat(directiveName, "\" may not be used on ").concat(location, "."); +} +/** + * Known directives + * + * A GraphQL document is only valid if all `@directives` are known by the + * schema and legally positioned. + */ + +export function KnownDirectives(context) { + return { + Directive: function Directive(node, key, parent, path, ancestors) { + var directiveDef = find(context.getSchema().getDirectives(), function (def) { + return def.name === node.name.value; + }); + + if (!directiveDef) { + context.reportError(new GraphQLError(unknownDirectiveMessage(node.name.value), [node])); + return; + } + + var candidateLocation = getDirectiveLocationForASTPath(ancestors); + + if (candidateLocation && directiveDef.locations.indexOf(candidateLocation) === -1) { + context.reportError(new GraphQLError(misplacedDirectiveMessage(node.name.value, candidateLocation), [node])); + } + } + }; +} + +function getDirectiveLocationForASTPath(ancestors) { + var appliedTo = ancestors[ancestors.length - 1]; + + if (!Array.isArray(appliedTo)) { + switch (appliedTo.kind) { + case Kind.OPERATION_DEFINITION: + switch (appliedTo.operation) { + case 'query': + return DirectiveLocation.QUERY; + + case 'mutation': + return DirectiveLocation.MUTATION; + + case 'subscription': + return DirectiveLocation.SUBSCRIPTION; + } + + break; + + case Kind.FIELD: + return DirectiveLocation.FIELD; + + case Kind.FRAGMENT_SPREAD: + return DirectiveLocation.FRAGMENT_SPREAD; + + case Kind.INLINE_FRAGMENT: + return DirectiveLocation.INLINE_FRAGMENT; + + case Kind.FRAGMENT_DEFINITION: + return DirectiveLocation.FRAGMENT_DEFINITION; + + case Kind.SCHEMA_DEFINITION: + case Kind.SCHEMA_EXTENSION: + return DirectiveLocation.SCHEMA; + + case Kind.SCALAR_TYPE_DEFINITION: + case Kind.SCALAR_TYPE_EXTENSION: + return DirectiveLocation.SCALAR; + + case Kind.OBJECT_TYPE_DEFINITION: + case Kind.OBJECT_TYPE_EXTENSION: + return DirectiveLocation.OBJECT; + + case Kind.FIELD_DEFINITION: + return DirectiveLocation.FIELD_DEFINITION; + + case Kind.INTERFACE_TYPE_DEFINITION: + case Kind.INTERFACE_TYPE_EXTENSION: + return DirectiveLocation.INTERFACE; + + case Kind.UNION_TYPE_DEFINITION: + case Kind.UNION_TYPE_EXTENSION: + return DirectiveLocation.UNION; + + case Kind.ENUM_TYPE_DEFINITION: + case Kind.ENUM_TYPE_EXTENSION: + return DirectiveLocation.ENUM; + + case Kind.ENUM_VALUE_DEFINITION: + return DirectiveLocation.ENUM_VALUE; + + case Kind.INPUT_OBJECT_TYPE_DEFINITION: + case Kind.INPUT_OBJECT_TYPE_EXTENSION: + return DirectiveLocation.INPUT_OBJECT; + + case Kind.INPUT_VALUE_DEFINITION: + var parentNode = ancestors[ancestors.length - 3]; + return parentNode.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ? DirectiveLocation.INPUT_FIELD_DEFINITION : DirectiveLocation.ARGUMENT_DEFINITION; + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/KnownFragmentNames.js b/dist/validation/rules/KnownFragmentNames.js new file mode 100644 index 0000000000..50dfbd62bc --- /dev/null +++ b/dist/validation/rules/KnownFragmentNames.js @@ -0,0 +1,41 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unknownFragmentMessage = unknownFragmentMessage; +exports.KnownFragmentNames = KnownFragmentNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function unknownFragmentMessage(fragName) { + return "Unknown fragment \"".concat(fragName, "\"."); +} +/** + * Known fragment names + * + * A GraphQL document is only valid if all `...Fragment` fragment spreads refer + * to fragments defined in the same document. + */ + + +function KnownFragmentNames(context) { + return { + FragmentSpread: function FragmentSpread(node) { + var fragmentName = node.name.value; + var fragment = context.getFragment(fragmentName); + + if (!fragment) { + context.reportError(new _error.GraphQLError(unknownFragmentMessage(fragmentName), [node.name])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownFragmentNames.js.flow b/dist/validation/rules/KnownFragmentNames.js.flow new file mode 100644 index 0000000000..c3893415f0 --- /dev/null +++ b/dist/validation/rules/KnownFragmentNames.js.flow @@ -0,0 +1,36 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function unknownFragmentMessage(fragName: string): string { + return `Unknown fragment "${fragName}".`; +} + +/** + * Known fragment names + * + * A GraphQL document is only valid if all `...Fragment` fragment spreads refer + * to fragments defined in the same document. + */ +export function KnownFragmentNames(context: ValidationContext): ASTVisitor { + return { + FragmentSpread(node) { + const fragmentName = node.name.value; + const fragment = context.getFragment(fragmentName); + if (!fragment) { + context.reportError( + new GraphQLError(unknownFragmentMessage(fragmentName), [node.name]), + ); + } + }, + }; +} diff --git a/dist/validation/rules/KnownFragmentNames.mjs b/dist/validation/rules/KnownFragmentNames.mjs new file mode 100644 index 0000000000..59d64da3cf --- /dev/null +++ b/dist/validation/rules/KnownFragmentNames.mjs @@ -0,0 +1,31 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function unknownFragmentMessage(fragName) { + return "Unknown fragment \"".concat(fragName, "\"."); +} +/** + * Known fragment names + * + * A GraphQL document is only valid if all `...Fragment` fragment spreads refer + * to fragments defined in the same document. + */ + +export function KnownFragmentNames(context) { + return { + FragmentSpread: function FragmentSpread(node) { + var fragmentName = node.name.value; + var fragment = context.getFragment(fragmentName); + + if (!fragment) { + context.reportError(new GraphQLError(unknownFragmentMessage(fragmentName), [node.name])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownTypeNames.js b/dist/validation/rules/KnownTypeNames.js new file mode 100644 index 0000000000..430a7dcddd --- /dev/null +++ b/dist/validation/rules/KnownTypeNames.js @@ -0,0 +1,69 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unknownTypeMessage = unknownTypeMessage; +exports.KnownTypeNames = KnownTypeNames; + +var _error = require("../../error"); + +var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList")); + +var _quotedOrList = _interopRequireDefault(require("../../jsutils/quotedOrList")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function unknownTypeMessage(typeName, suggestedTypes) { + var message = "Unknown type \"".concat(typeName, "\"."); + + if (suggestedTypes.length) { + message += " Did you mean ".concat((0, _quotedOrList.default)(suggestedTypes), "?"); + } + + return message; +} +/** + * Known type names + * + * A GraphQL document is only valid if referenced types (specifically + * variable definitions and fragment conditions) are defined by the type schema. + */ + + +function KnownTypeNames(context) { + return { + // TODO: when validating IDL, re-enable these. Experimental version does not + // add unreferenced types, resulting in false-positive errors. Squelched + // errors for now. + ObjectTypeDefinition: function ObjectTypeDefinition() { + return false; + }, + InterfaceTypeDefinition: function InterfaceTypeDefinition() { + return false; + }, + UnionTypeDefinition: function UnionTypeDefinition() { + return false; + }, + InputObjectTypeDefinition: function InputObjectTypeDefinition() { + return false; + }, + NamedType: function NamedType(node) { + var schema = context.getSchema(); + var typeName = node.name.value; + var type = schema.getType(typeName); + + if (!type) { + context.reportError(new _error.GraphQLError(unknownTypeMessage(typeName, (0, _suggestionList.default)(typeName, Object.keys(schema.getTypeMap()))), [node])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/KnownTypeNames.js.flow b/dist/validation/rules/KnownTypeNames.js.flow new file mode 100644 index 0000000000..8ad1b7775f --- /dev/null +++ b/dist/validation/rules/KnownTypeNames.js.flow @@ -0,0 +1,59 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +import type { ASTVisitor } from '../../language/visitor'; + +export function unknownTypeMessage( + typeName: string, + suggestedTypes: Array, +): string { + let message = `Unknown type "${typeName}".`; + if (suggestedTypes.length) { + message += ` Did you mean ${quotedOrList(suggestedTypes)}?`; + } + return message; +} + +/** + * Known type names + * + * A GraphQL document is only valid if referenced types (specifically + * variable definitions and fragment conditions) are defined by the type schema. + */ +export function KnownTypeNames(context: ValidationContext): ASTVisitor { + return { + // TODO: when validating IDL, re-enable these. Experimental version does not + // add unreferenced types, resulting in false-positive errors. Squelched + // errors for now. + ObjectTypeDefinition: () => false, + InterfaceTypeDefinition: () => false, + UnionTypeDefinition: () => false, + InputObjectTypeDefinition: () => false, + NamedType(node) { + const schema = context.getSchema(); + const typeName = node.name.value; + const type = schema.getType(typeName); + if (!type) { + context.reportError( + new GraphQLError( + unknownTypeMessage( + typeName, + suggestionList(typeName, Object.keys(schema.getTypeMap())), + ), + [node], + ), + ); + } + }, + }; +} diff --git a/dist/validation/rules/KnownTypeNames.mjs b/dist/validation/rules/KnownTypeNames.mjs new file mode 100644 index 0000000000..cd12f8ee21 --- /dev/null +++ b/dist/validation/rules/KnownTypeNames.mjs @@ -0,0 +1,55 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import suggestionList from '../../jsutils/suggestionList'; +import quotedOrList from '../../jsutils/quotedOrList'; +export function unknownTypeMessage(typeName, suggestedTypes) { + var message = "Unknown type \"".concat(typeName, "\"."); + + if (suggestedTypes.length) { + message += " Did you mean ".concat(quotedOrList(suggestedTypes), "?"); + } + + return message; +} +/** + * Known type names + * + * A GraphQL document is only valid if referenced types (specifically + * variable definitions and fragment conditions) are defined by the type schema. + */ + +export function KnownTypeNames(context) { + return { + // TODO: when validating IDL, re-enable these. Experimental version does not + // add unreferenced types, resulting in false-positive errors. Squelched + // errors for now. + ObjectTypeDefinition: function ObjectTypeDefinition() { + return false; + }, + InterfaceTypeDefinition: function InterfaceTypeDefinition() { + return false; + }, + UnionTypeDefinition: function UnionTypeDefinition() { + return false; + }, + InputObjectTypeDefinition: function InputObjectTypeDefinition() { + return false; + }, + NamedType: function NamedType(node) { + var schema = context.getSchema(); + var typeName = node.name.value; + var type = schema.getType(typeName); + + if (!type) { + context.reportError(new GraphQLError(unknownTypeMessage(typeName, suggestionList(typeName, Object.keys(schema.getTypeMap()))), [node])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/LoneAnonymousOperation.js b/dist/validation/rules/LoneAnonymousOperation.js new file mode 100644 index 0000000000..530cea0f1c --- /dev/null +++ b/dist/validation/rules/LoneAnonymousOperation.js @@ -0,0 +1,46 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.anonOperationNotAloneMessage = anonOperationNotAloneMessage; +exports.LoneAnonymousOperation = LoneAnonymousOperation; + +var _error = require("../../error"); + +var _kinds = require("../../language/kinds"); + +/** + * 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. + * + * strict + */ +function anonOperationNotAloneMessage() { + return 'This anonymous operation must be the only defined operation.'; +} +/** + * 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. + */ + + +function LoneAnonymousOperation(context) { + var operationCount = 0; + return { + Document: function Document(node) { + operationCount = node.definitions.filter(function (definition) { + return definition.kind === _kinds.Kind.OPERATION_DEFINITION; + }).length; + }, + OperationDefinition: function OperationDefinition(node) { + if (!node.name && operationCount > 1) { + context.reportError(new _error.GraphQLError(anonOperationNotAloneMessage(), [node])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/LoneAnonymousOperation.js.flow b/dist/validation/rules/LoneAnonymousOperation.js.flow new file mode 100644 index 0000000000..745de201cb --- /dev/null +++ b/dist/validation/rules/LoneAnonymousOperation.js.flow @@ -0,0 +1,41 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +import type { ASTVisitor } from '../../language/visitor'; + +export function anonOperationNotAloneMessage(): string { + return 'This anonymous operation must be the only defined operation.'; +} + +/** + * 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. + */ +export function LoneAnonymousOperation(context: ValidationContext): ASTVisitor { + let operationCount = 0; + return { + Document(node) { + operationCount = node.definitions.filter( + definition => definition.kind === Kind.OPERATION_DEFINITION, + ).length; + }, + OperationDefinition(node) { + if (!node.name && operationCount > 1) { + context.reportError( + new GraphQLError(anonOperationNotAloneMessage(), [node]), + ); + } + }, + }; +} diff --git a/dist/validation/rules/LoneAnonymousOperation.mjs b/dist/validation/rules/LoneAnonymousOperation.mjs new file mode 100644 index 0000000000..9b54948cd0 --- /dev/null +++ b/dist/validation/rules/LoneAnonymousOperation.mjs @@ -0,0 +1,35 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +export function anonOperationNotAloneMessage() { + return 'This anonymous operation must be the only defined operation.'; +} +/** + * 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. + */ + +export function LoneAnonymousOperation(context) { + var operationCount = 0; + return { + Document: function Document(node) { + operationCount = node.definitions.filter(function (definition) { + return definition.kind === Kind.OPERATION_DEFINITION; + }).length; + }, + OperationDefinition: function OperationDefinition(node) { + if (!node.name && operationCount > 1) { + context.reportError(new GraphQLError(anonOperationNotAloneMessage(), [node])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoFragmentCycles.js b/dist/validation/rules/NoFragmentCycles.js new file mode 100644 index 0000000000..022ca766bb --- /dev/null +++ b/dist/validation/rules/NoFragmentCycles.js @@ -0,0 +1,85 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.cycleErrorMessage = cycleErrorMessage; +exports.NoFragmentCycles = NoFragmentCycles; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function cycleErrorMessage(fragName, spreadNames) { + var via = spreadNames.length ? ' via ' + spreadNames.join(', ') : ''; + return "Cannot spread fragment \"".concat(fragName, "\" within itself").concat(via, "."); +} + +function NoFragmentCycles(context) { + // Tracks already visited fragments to maintain O(N) and to ensure that cycles + // are not redundantly reported. + var visitedFrags = Object.create(null); // Array of AST nodes used to produce meaningful errors + + var spreadPath = []; // Position in the spread path + + var spreadPathIndexByName = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + if (!visitedFrags[node.name.value]) { + detectCycleRecursive(node); + } + + return false; + } + }; // This does a straight-forward DFS to find cycles. + // It does not terminate when a cycle was found but continues to explore + // the graph to find all possible cycles. + + function detectCycleRecursive(fragment) { + var fragmentName = fragment.name.value; + visitedFrags[fragmentName] = true; + var spreadNodes = context.getFragmentSpreads(fragment.selectionSet); + + if (spreadNodes.length === 0) { + return; + } + + spreadPathIndexByName[fragmentName] = spreadPath.length; + + for (var i = 0; i < spreadNodes.length; i++) { + var spreadNode = spreadNodes[i]; + var spreadName = spreadNode.name.value; + var cycleIndex = spreadPathIndexByName[spreadName]; + + if (cycleIndex === undefined) { + spreadPath.push(spreadNode); + + if (!visitedFrags[spreadName]) { + var spreadFragment = context.getFragment(spreadName); + + if (spreadFragment) { + detectCycleRecursive(spreadFragment); + } + } + + spreadPath.pop(); + } else { + var cyclePath = spreadPath.slice(cycleIndex); + context.reportError(new _error.GraphQLError(cycleErrorMessage(spreadName, cyclePath.map(function (s) { + return s.name.value; + })), cyclePath.concat(spreadNode))); + } + } + + spreadPathIndexByName[fragmentName] = undefined; + } +} \ No newline at end of file diff --git a/dist/validation/rules/NoFragmentCycles.js.flow b/dist/validation/rules/NoFragmentCycles.js.flow new file mode 100644 index 0000000000..0a51122759 --- /dev/null +++ b/dist/validation/rules/NoFragmentCycles.js.flow @@ -0,0 +1,85 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { FragmentDefinitionNode } from '../../language/ast'; +import type { ASTVisitor } from '../../language/visitor'; + +export function cycleErrorMessage( + fragName: string, + spreadNames: Array, +): string { + const via = spreadNames.length ? ' via ' + spreadNames.join(', ') : ''; + return `Cannot spread fragment "${fragName}" within itself${via}.`; +} + +export function NoFragmentCycles(context: ValidationContext): ASTVisitor { + // Tracks already visited fragments to maintain O(N) and to ensure that cycles + // are not redundantly reported. + const visitedFrags = Object.create(null); + + // Array of AST nodes used to produce meaningful errors + const spreadPath = []; + + // Position in the spread path + const spreadPathIndexByName = Object.create(null); + + return { + OperationDefinition: () => false, + FragmentDefinition(node) { + if (!visitedFrags[node.name.value]) { + detectCycleRecursive(node); + } + return false; + }, + }; + + // This does a straight-forward DFS to find cycles. + // It does not terminate when a cycle was found but continues to explore + // the graph to find all possible cycles. + function detectCycleRecursive(fragment: FragmentDefinitionNode) { + const fragmentName = fragment.name.value; + visitedFrags[fragmentName] = true; + + const spreadNodes = context.getFragmentSpreads(fragment.selectionSet); + if (spreadNodes.length === 0) { + return; + } + + spreadPathIndexByName[fragmentName] = spreadPath.length; + + for (let i = 0; i < spreadNodes.length; i++) { + const spreadNode = spreadNodes[i]; + const spreadName = spreadNode.name.value; + const cycleIndex = spreadPathIndexByName[spreadName]; + + if (cycleIndex === undefined) { + spreadPath.push(spreadNode); + if (!visitedFrags[spreadName]) { + const spreadFragment = context.getFragment(spreadName); + if (spreadFragment) { + detectCycleRecursive(spreadFragment); + } + } + spreadPath.pop(); + } else { + const cyclePath = spreadPath.slice(cycleIndex); + context.reportError( + new GraphQLError( + cycleErrorMessage(spreadName, cyclePath.map(s => s.name.value)), + cyclePath.concat(spreadNode), + ), + ); + } + } + + spreadPathIndexByName[fragmentName] = undefined; + } +} diff --git a/dist/validation/rules/NoFragmentCycles.mjs b/dist/validation/rules/NoFragmentCycles.mjs new file mode 100644 index 0000000000..5d18024008 --- /dev/null +++ b/dist/validation/rules/NoFragmentCycles.mjs @@ -0,0 +1,75 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function cycleErrorMessage(fragName, spreadNames) { + var via = spreadNames.length ? ' via ' + spreadNames.join(', ') : ''; + return "Cannot spread fragment \"".concat(fragName, "\" within itself").concat(via, "."); +} +export function NoFragmentCycles(context) { + // Tracks already visited fragments to maintain O(N) and to ensure that cycles + // are not redundantly reported. + var visitedFrags = Object.create(null); // Array of AST nodes used to produce meaningful errors + + var spreadPath = []; // Position in the spread path + + var spreadPathIndexByName = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + if (!visitedFrags[node.name.value]) { + detectCycleRecursive(node); + } + + return false; + } + }; // This does a straight-forward DFS to find cycles. + // It does not terminate when a cycle was found but continues to explore + // the graph to find all possible cycles. + + function detectCycleRecursive(fragment) { + var fragmentName = fragment.name.value; + visitedFrags[fragmentName] = true; + var spreadNodes = context.getFragmentSpreads(fragment.selectionSet); + + if (spreadNodes.length === 0) { + return; + } + + spreadPathIndexByName[fragmentName] = spreadPath.length; + + for (var i = 0; i < spreadNodes.length; i++) { + var spreadNode = spreadNodes[i]; + var spreadName = spreadNode.name.value; + var cycleIndex = spreadPathIndexByName[spreadName]; + + if (cycleIndex === undefined) { + spreadPath.push(spreadNode); + + if (!visitedFrags[spreadName]) { + var spreadFragment = context.getFragment(spreadName); + + if (spreadFragment) { + detectCycleRecursive(spreadFragment); + } + } + + spreadPath.pop(); + } else { + var cyclePath = spreadPath.slice(cycleIndex); + context.reportError(new GraphQLError(cycleErrorMessage(spreadName, cyclePath.map(function (s) { + return s.name.value; + })), cyclePath.concat(spreadNode))); + } + } + + spreadPathIndexByName[fragmentName] = undefined; + } +} \ No newline at end of file diff --git a/dist/validation/rules/NoUndefinedVariables.js b/dist/validation/rules/NoUndefinedVariables.js new file mode 100644 index 0000000000..0400cfca18 --- /dev/null +++ b/dist/validation/rules/NoUndefinedVariables.js @@ -0,0 +1,53 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.undefinedVarMessage = undefinedVarMessage; +exports.NoUndefinedVariables = NoUndefinedVariables; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function undefinedVarMessage(varName, opName) { + return opName ? "Variable \"$".concat(varName, "\" is not defined by operation \"").concat(opName, "\".") : "Variable \"$".concat(varName, "\" is not defined."); +} +/** + * No undefined variables + * + * A GraphQL operation is only valid if all variables encountered, both directly + * and via fragment spreads, are defined by that operation. + */ + + +function NoUndefinedVariables(context) { + var variableNameDefined = Object.create(null); + return { + OperationDefinition: { + enter: function enter() { + variableNameDefined = Object.create(null); + }, + leave: function leave(operation) { + var usages = context.getRecursiveVariableUsages(operation); + usages.forEach(function (_ref) { + var node = _ref.node; + var varName = node.name.value; + + if (variableNameDefined[varName] !== true) { + context.reportError(new _error.GraphQLError(undefinedVarMessage(varName, operation.name && operation.name.value), [node, operation])); + } + }); + } + }, + VariableDefinition: function VariableDefinition(node) { + variableNameDefined[node.variable.name.value] = true; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoUndefinedVariables.js.flow b/dist/validation/rules/NoUndefinedVariables.js.flow new file mode 100644 index 0000000000..d74a49265b --- /dev/null +++ b/dist/validation/rules/NoUndefinedVariables.js.flow @@ -0,0 +1,57 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function undefinedVarMessage(varName: string, opName: ?string): string { + return opName + ? `Variable "$${varName}" is not defined by operation "${opName}".` + : `Variable "$${varName}" is not defined.`; +} + +/** + * No undefined variables + * + * A GraphQL operation is only valid if all variables encountered, both directly + * and via fragment spreads, are defined by that operation. + */ +export function NoUndefinedVariables(context: ValidationContext): ASTVisitor { + let variableNameDefined = Object.create(null); + + return { + OperationDefinition: { + enter() { + variableNameDefined = Object.create(null); + }, + leave(operation) { + const usages = context.getRecursiveVariableUsages(operation); + + usages.forEach(({ node }) => { + const varName = node.name.value; + if (variableNameDefined[varName] !== true) { + context.reportError( + new GraphQLError( + undefinedVarMessage( + varName, + operation.name && operation.name.value, + ), + [node, operation], + ), + ); + } + }); + }, + }, + VariableDefinition(node) { + variableNameDefined[node.variable.name.value] = true; + }, + }; +} diff --git a/dist/validation/rules/NoUndefinedVariables.mjs b/dist/validation/rules/NoUndefinedVariables.mjs new file mode 100644 index 0000000000..14011d385b --- /dev/null +++ b/dist/validation/rules/NoUndefinedVariables.mjs @@ -0,0 +1,43 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function undefinedVarMessage(varName, opName) { + return opName ? "Variable \"$".concat(varName, "\" is not defined by operation \"").concat(opName, "\".") : "Variable \"$".concat(varName, "\" is not defined."); +} +/** + * No undefined variables + * + * A GraphQL operation is only valid if all variables encountered, both directly + * and via fragment spreads, are defined by that operation. + */ + +export function NoUndefinedVariables(context) { + var variableNameDefined = Object.create(null); + return { + OperationDefinition: { + enter: function enter() { + variableNameDefined = Object.create(null); + }, + leave: function leave(operation) { + var usages = context.getRecursiveVariableUsages(operation); + usages.forEach(function (_ref) { + var node = _ref.node; + var varName = node.name.value; + + if (variableNameDefined[varName] !== true) { + context.reportError(new GraphQLError(undefinedVarMessage(varName, operation.name && operation.name.value), [node, operation])); + } + }); + } + }, + VariableDefinition: function VariableDefinition(node) { + variableNameDefined[node.variable.name.value] = true; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoUnusedFragments.js b/dist/validation/rules/NoUnusedFragments.js new file mode 100644 index 0000000000..db193c0acd --- /dev/null +++ b/dist/validation/rules/NoUnusedFragments.js @@ -0,0 +1,60 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unusedFragMessage = unusedFragMessage; +exports.NoUnusedFragments = NoUnusedFragments; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function unusedFragMessage(fragName) { + return "Fragment \"".concat(fragName, "\" is never used."); +} +/** + * No unused fragments + * + * A GraphQL document is only valid if all fragment definitions are spread + * within operations, or spread within other fragments spread within operations. + */ + + +function NoUnusedFragments(context) { + var operationDefs = []; + var fragmentDefs = []; + return { + OperationDefinition: function OperationDefinition(node) { + operationDefs.push(node); + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + fragmentDefs.push(node); + return false; + }, + Document: { + leave: function leave() { + var fragmentNameUsed = Object.create(null); + operationDefs.forEach(function (operation) { + context.getRecursivelyReferencedFragments(operation).forEach(function (fragment) { + fragmentNameUsed[fragment.name.value] = true; + }); + }); + fragmentDefs.forEach(function (fragmentDef) { + var fragName = fragmentDef.name.value; + + if (fragmentNameUsed[fragName] !== true) { + context.reportError(new _error.GraphQLError(unusedFragMessage(fragName), [fragmentDef])); + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoUnusedFragments.js.flow b/dist/validation/rules/NoUnusedFragments.js.flow new file mode 100644 index 0000000000..4e2b6e2b7f --- /dev/null +++ b/dist/validation/rules/NoUnusedFragments.js.flow @@ -0,0 +1,59 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function unusedFragMessage(fragName: string): string { + return `Fragment "${fragName}" is never used.`; +} + +/** + * No unused fragments + * + * A GraphQL document is only valid if all fragment definitions are spread + * within operations, or spread within other fragments spread within operations. + */ +export function NoUnusedFragments(context: ValidationContext): ASTVisitor { + const operationDefs = []; + const fragmentDefs = []; + + return { + OperationDefinition(node) { + operationDefs.push(node); + return false; + }, + FragmentDefinition(node) { + fragmentDefs.push(node); + return false; + }, + Document: { + leave() { + const fragmentNameUsed = Object.create(null); + operationDefs.forEach(operation => { + context + .getRecursivelyReferencedFragments(operation) + .forEach(fragment => { + fragmentNameUsed[fragment.name.value] = true; + }); + }); + + fragmentDefs.forEach(fragmentDef => { + const fragName = fragmentDef.name.value; + if (fragmentNameUsed[fragName] !== true) { + context.reportError( + new GraphQLError(unusedFragMessage(fragName), [fragmentDef]), + ); + } + }); + }, + }, + }; +} diff --git a/dist/validation/rules/NoUnusedFragments.mjs b/dist/validation/rules/NoUnusedFragments.mjs new file mode 100644 index 0000000000..2585f5abb9 --- /dev/null +++ b/dist/validation/rules/NoUnusedFragments.mjs @@ -0,0 +1,50 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function unusedFragMessage(fragName) { + return "Fragment \"".concat(fragName, "\" is never used."); +} +/** + * No unused fragments + * + * A GraphQL document is only valid if all fragment definitions are spread + * within operations, or spread within other fragments spread within operations. + */ + +export function NoUnusedFragments(context) { + var operationDefs = []; + var fragmentDefs = []; + return { + OperationDefinition: function OperationDefinition(node) { + operationDefs.push(node); + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + fragmentDefs.push(node); + return false; + }, + Document: { + leave: function leave() { + var fragmentNameUsed = Object.create(null); + operationDefs.forEach(function (operation) { + context.getRecursivelyReferencedFragments(operation).forEach(function (fragment) { + fragmentNameUsed[fragment.name.value] = true; + }); + }); + fragmentDefs.forEach(function (fragmentDef) { + var fragName = fragmentDef.name.value; + + if (fragmentNameUsed[fragName] !== true) { + context.reportError(new GraphQLError(unusedFragMessage(fragName), [fragmentDef])); + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoUnusedVariables.js b/dist/validation/rules/NoUnusedVariables.js new file mode 100644 index 0000000000..a7baa1f63c --- /dev/null +++ b/dist/validation/rules/NoUnusedVariables.js @@ -0,0 +1,58 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.unusedVariableMessage = unusedVariableMessage; +exports.NoUnusedVariables = NoUnusedVariables; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function unusedVariableMessage(varName, opName) { + return opName ? "Variable \"$".concat(varName, "\" is never used in operation \"").concat(opName, "\".") : "Variable \"$".concat(varName, "\" is never used."); +} +/** + * 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 variableDefs = []; + return { + OperationDefinition: { + enter: function enter() { + variableDefs = []; + }, + leave: function leave(operation) { + var variableNameUsed = Object.create(null); + var usages = context.getRecursiveVariableUsages(operation); + var opName = operation.name ? operation.name.value : null; + usages.forEach(function (_ref) { + var node = _ref.node; + variableNameUsed[node.name.value] = true; + }); + variableDefs.forEach(function (variableDef) { + var variableName = variableDef.variable.name.value; + + if (variableNameUsed[variableName] !== true) { + context.reportError(new _error.GraphQLError(unusedVariableMessage(variableName, opName), [variableDef])); + } + }); + } + }, + VariableDefinition: function VariableDefinition(def) { + variableDefs.push(def); + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/NoUnusedVariables.js.flow b/dist/validation/rules/NoUnusedVariables.js.flow new file mode 100644 index 0000000000..c5637e22db --- /dev/null +++ b/dist/validation/rules/NoUnusedVariables.js.flow @@ -0,0 +1,62 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function unusedVariableMessage( + varName: string, + opName: ?string, +): string { + return opName + ? `Variable "$${varName}" is never used in operation "${opName}".` + : `Variable "$${varName}" is never used.`; +} + +/** + * 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. + */ +export function NoUnusedVariables(context: ValidationContext): ASTVisitor { + let variableDefs = []; + + return { + OperationDefinition: { + enter() { + variableDefs = []; + }, + leave(operation) { + const variableNameUsed = Object.create(null); + const usages = context.getRecursiveVariableUsages(operation); + const opName = operation.name ? operation.name.value : null; + + usages.forEach(({ node }) => { + variableNameUsed[node.name.value] = true; + }); + + variableDefs.forEach(variableDef => { + const variableName = variableDef.variable.name.value; + if (variableNameUsed[variableName] !== true) { + context.reportError( + new GraphQLError(unusedVariableMessage(variableName, opName), [ + variableDef, + ]), + ); + } + }); + }, + }, + VariableDefinition(def) { + variableDefs.push(def); + }, + }; +} diff --git a/dist/validation/rules/NoUnusedVariables.mjs b/dist/validation/rules/NoUnusedVariables.mjs new file mode 100644 index 0000000000..bfbbf5bbe2 --- /dev/null +++ b/dist/validation/rules/NoUnusedVariables.mjs @@ -0,0 +1,48 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function unusedVariableMessage(varName, opName) { + return opName ? "Variable \"$".concat(varName, "\" is never used in operation \"").concat(opName, "\".") : "Variable \"$".concat(varName, "\" is never used."); +} +/** + * 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. + */ + +export function NoUnusedVariables(context) { + var variableDefs = []; + return { + OperationDefinition: { + enter: function enter() { + variableDefs = []; + }, + leave: function leave(operation) { + var variableNameUsed = Object.create(null); + var usages = context.getRecursiveVariableUsages(operation); + var opName = operation.name ? operation.name.value : null; + usages.forEach(function (_ref) { + var node = _ref.node; + variableNameUsed[node.name.value] = true; + }); + variableDefs.forEach(function (variableDef) { + var variableName = variableDef.variable.name.value; + + if (variableNameUsed[variableName] !== true) { + context.reportError(new GraphQLError(unusedVariableMessage(variableName, opName), [variableDef])); + } + }); + } + }, + VariableDefinition: function VariableDefinition(def) { + variableDefs.push(def); + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/OverlappingFieldsCanBeMerged.js b/dist/validation/rules/OverlappingFieldsCanBeMerged.js new file mode 100644 index 0000000000..57f05f129d --- /dev/null +++ b/dist/validation/rules/OverlappingFieldsCanBeMerged.js @@ -0,0 +1,585 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.fieldsConflictMessage = fieldsConflictMessage; +exports.OverlappingFieldsCanBeMerged = OverlappingFieldsCanBeMerged; + +var _error = require("../../error"); + +var _find = _interopRequireDefault(require("../../jsutils/find")); + +var _kinds = require("../../language/kinds"); + +var _printer = require("../../language/printer"); + +var _definition = require("../../type/definition"); + +var _typeFromAST = require("../../utilities/typeFromAST"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function fieldsConflictMessage(responseName, reason) { + return "Fields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(reason)) + '. Use different aliases on the fields to fetch both if this was ' + 'intentional.'; +} + +function reasonMessage(reason) { + if (Array.isArray(reason)) { + return reason.map(function (_ref) { + var responseName = _ref[0], + subreason = _ref[1]; + return "subfields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(subreason)); + }).join(' and '); + } + + return reason; +} +/** + * Overlapping fields can be merged + * + * A selection set is only valid if all fields (including spreading any + * fragments) either correspond to distinct response names or can be merged + * without ambiguity. + */ + + +function OverlappingFieldsCanBeMerged(context) { + // A memoization for when two fragments are compared "between" each other for + // conflicts. Two fragments may be compared many times, so memoizing this can + // dramatically improve the performance of this validator. + var comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given + // selection set. Selection sets may be asked for this information multiple + // times, so this improves the performance of this validator. + + var cachedFieldsAndFragmentNames = new Map(); + return { + SelectionSet: function SelectionSet(selectionSet) { + var conflicts = findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, context.getParentType(), selectionSet); + conflicts.forEach(function (_ref2) { + var _ref2$ = _ref2[0], + responseName = _ref2$[0], + reason = _ref2$[1], + fields1 = _ref2[1], + fields2 = _ref2[2]; + return context.reportError(new _error.GraphQLError(fieldsConflictMessage(responseName, reason), fields1.concat(fields2))); + }); + } + }; +} + +/** + * Algorithm: + * + * Conflicts occur when two fields exist in a query which will produce the same + * response name, but represent differing values, thus creating a conflict. + * The algorithm below finds all conflicts via making a series of comparisons + * between fields. In order to compare as few fields as possible, this makes + * a series of comparisons "within" sets of fields and "between" sets of fields. + * + * Given any selection set, a collection produces both a set of fields by + * also including all inline fragments, as well as a list of fragments + * referenced by fragment spreads. + * + * A) Each selection set represented in the document first compares "within" its + * collected set of fields, finding any conflicts between every pair of + * overlapping fields. + * Note: This is the *only time* that a the fields "within" a set are compared + * to each other. After this only fields "between" sets are compared. + * + * B) Also, if any fragment is referenced in a selection set, then a + * comparison is made "between" the original set of fields and the + * referenced fragment. + * + * C) Also, if multiple fragments are referenced, then comparisons + * are made "between" each referenced fragment. + * + * D) When comparing "between" a set of fields and a referenced fragment, first + * a comparison is made between each field in the original set of fields and + * each field in the the referenced set of fields. + * + * E) Also, if any fragment is referenced in the referenced selection set, + * then a comparison is made "between" the original set of fields and the + * referenced fragment (recursively referring to step D). + * + * F) When comparing "between" two fragments, first a comparison is made between + * each field in the first referenced set of fields and each field in the the + * second referenced set of fields. + * + * G) Also, any fragments referenced by the first must be compared to the + * second, and any fragments referenced by the second must be compared to the + * first (recursively referring to step F). + * + * H) When comparing two fields, if both have selection sets, then a comparison + * is made "between" both selection sets, first comparing the set of fields in + * the first selection set with the set of fields in the second. + * + * I) Also, if any fragment is referenced in either selection set, then a + * comparison is made "between" the other set of fields and the + * referenced fragment. + * + * J) Also, if two fragments are referenced in both selection sets, then a + * comparison is made "between" the two fragments. + * + */ +// Find all conflicts found "within" a selection set, including those found +// via spreading in fragments. Called when visiting each SelectionSet in the +// GraphQL Document. +function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentType, selectionSet) { + var conflicts = []; + + var _getFieldsAndFragment = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet), + fieldMap = _getFieldsAndFragment[0], + fragmentNames = _getFieldsAndFragment[1]; // (A) Find find all conflicts "within" the fields of this selection set. + // Note: this is the *only place* `collectConflictsWithin` is called. + + + collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap); + + if (fragmentNames.length !== 0) { + // (B) Then collect conflicts between these fields and those represented by + // each spread fragment name found. + var comparedFragments = Object.create(null); + + for (var i = 0; i < fragmentNames.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, false, fieldMap, fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this + // selection set to collect conflicts between fragments spread together. + // This compares each item in the list of fragment names to every other + // item in that same list (except for itself). + + for (var j = i + 1; j < fragmentNames.length; j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fragmentNames[i], fragmentNames[j]); + } + } + } + + return conflicts; +} // Collect all conflicts found between a set of fields and a fragment reference +// including via spreading in any nested fragments. + + +function collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentName) { + // Memoize so a fragment is not compared for conflicts more than once. + if (comparedFragments[fragmentName]) { + return; + } + + comparedFragments[fragmentName] = true; + var fragment = context.getFragment(fragmentName); + + if (!fragment) { + return; + } + + var _getReferencedFieldsA = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment), + fieldMap2 = _getReferencedFieldsA[0], + fragmentNames2 = _getReferencedFieldsA[1]; // Do not compare a fragment's fieldMap to itself. + + + if (fieldMap === fieldMap2) { + return; + } // (D) First collect any conflicts between the provided collection of fields + // and the collection of fields represented by the given fragment. + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fieldMap2); // (E) Then collect any conflicts between the provided collection of fields + // and any fragment names found in the given fragment. + + for (var i = 0; i < fragmentNames2.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentNames2[i]); + } +} // Collect all conflicts found between two fragments, including via spreading in +// any nested fragments. + + +function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentName2) { + // No need to compare a fragment to itself. + if (fragmentName1 === fragmentName2) { + return; + } // Memoize so two fragments are not compared for conflicts more than once. + + + if (comparedFragmentPairs.has(fragmentName1, fragmentName2, areMutuallyExclusive)) { + return; + } + + comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive); + var fragment1 = context.getFragment(fragmentName1); + var fragment2 = context.getFragment(fragmentName2); + + if (!fragment1 || !fragment2) { + return; + } + + var _getReferencedFieldsA2 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment1), + fieldMap1 = _getReferencedFieldsA2[0], + fragmentNames1 = _getReferencedFieldsA2[1]; + + var _getReferencedFieldsA3 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment2), + fieldMap2 = _getReferencedFieldsA3[0], + fragmentNames2 = _getReferencedFieldsA3[1]; // (F) First, collect all conflicts between these two collections of fields + // (not including any nested fragments). + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (G) Then collect conflicts between the first fragment and any nested + // fragments spread in the second fragment. + + for (var j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentNames2[j]); + } // (G) Then collect conflicts between the second fragment and any nested + // fragments spread in the first fragment. + + + for (var i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[i], fragmentName2); + } +} // Find all conflicts found between two selection sets, including those found +// via spreading in fragments. Called when determining if conflicts exist +// between the sub-fields of two overlapping fields. + + +function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) { + var conflicts = []; + + var _getFieldsAndFragment2 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType1, selectionSet1), + fieldMap1 = _getFieldsAndFragment2[0], + fragmentNames1 = _getFieldsAndFragment2[1]; + + var _getFieldsAndFragment3 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType2, selectionSet2), + fieldMap2 = _getFieldsAndFragment3[0], + fragmentNames2 = _getFieldsAndFragment3[1]; // (H) First, collect all conflicts between these two collections of field. + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (I) Then collect conflicts between the first collection of fields and + // those referenced by each fragment name associated with the second. + + if (fragmentNames2.length !== 0) { + var comparedFragments = Object.create(null); + + for (var j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fragmentNames2[j]); + } + } // (I) Then collect conflicts between the second collection of fields and + // those referenced by each fragment name associated with the first. + + + if (fragmentNames1.length !== 0) { + var _comparedFragments = Object.create(null); + + for (var i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, _comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap2, fragmentNames1[i]); + } + } // (J) Also collect conflicts between any fragment names by the first and + // fragment names by the second. This compares each item in the first set of + // names to each item in the second set of names. + + + for (var _i = 0; _i < fragmentNames1.length; _i++) { + for (var _j = 0; _j < fragmentNames2.length; _j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[_i], fragmentNames2[_j]); + } + } + + return conflicts; +} // Collect all Conflicts "within" one collection of fields. + + +function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap) { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For every response name, if there are multiple fields, they + // must be compared to find a potential conflict. + Object.keys(fieldMap).forEach(function (responseName) { + var fields = fieldMap[responseName]; // This compares every field in the list to every other field in this list + // (except to itself). If the list only has one item, nothing needs to + // be compared. + + if (fields.length > 1) { + for (var i = 0; i < fields.length; i++) { + for (var j = i + 1; j < fields.length; j++) { + var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, // within one collection is never mutually exclusive + responseName, fields[i], fields[j]); + + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} // Collect all Conflicts between two collections of fields. This is similar to, +// but different from the `collectConflictsWithin` function above. This check +// assumes that `collectConflictsWithin` has already been called on each +// provided collection of fields. This is true because this validator traverses +// each individual selection set. + + +function collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, fieldMap1, fieldMap2) { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For any response name which appears in both provided field + // maps, each field from the first field map must be compared to every field + // in the second field map to find potential conflicts. + Object.keys(fieldMap1).forEach(function (responseName) { + var fields2 = fieldMap2[responseName]; + + if (fields2) { + var fields1 = fieldMap1[responseName]; + + for (var i = 0; i < fields1.length; i++) { + for (var j = 0; j < fields2.length; j++) { + var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]); + + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} // Determines if there is a conflict between two particular fields, including +// comparing their sub-fields. + + +function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, field1, field2) { + var parentType1 = field1[0], + node1 = field1[1], + def1 = field1[2]; + var parentType2 = field2[0], + node2 = field2[1], + def2 = field2[2]; // If it is known that two fields could not possibly apply at the same + // time, due to the parent types, then it is safe to permit them to diverge + // in aliased field or arguments used as they will not present any ambiguity + // by differing. + // It is known that two parent types could never overlap if they are + // different Object types. Interface or Union types might overlap - if not + // in the current state of the schema, then perhaps in some future version, + // thus may not safely diverge. + + var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && (0, _definition.isObjectType)(parentType1) && (0, _definition.isObjectType)(parentType2); // The return type for each field. + + var type1 = def1 && def1.type; + var type2 = def2 && def2.type; + + if (!areMutuallyExclusive) { + // Two aliases must refer to the same field. + var name1 = node1.name.value; + var name2 = node2.name.value; + + if (name1 !== name2) { + return [[responseName, "".concat(name1, " and ").concat(name2, " are different fields")], [node1], [node2]]; + } // Two field calls must have the same arguments. + + + if (!sameArguments(node1.arguments || [], node2.arguments || [])) { + return [[responseName, 'they have differing arguments'], [node1], [node2]]; + } + } + + if (type1 && type2 && doTypesConflict(type1, type2)) { + return [[responseName, "they return conflicting types ".concat(String(type1), " and ").concat(String(type2))], [node1], [node2]]; + } // Collect and compare sub-fields. Use the same "visited fragment names" list + // for both collections so fields in a fragment reference are never + // compared to themselves. + + + var selectionSet1 = node1.selectionSet; + var selectionSet2 = node2.selectionSet; + + if (selectionSet1 && selectionSet2) { + var conflicts = findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, (0, _definition.getNamedType)(type1), selectionSet1, (0, _definition.getNamedType)(type2), selectionSet2); + return subfieldConflicts(conflicts, responseName, node1, node2); + } +} + +function sameArguments(arguments1, arguments2) { + if (arguments1.length !== arguments2.length) { + return false; + } + + return arguments1.every(function (argument1) { + var argument2 = (0, _find.default)(arguments2, function (argument) { + return argument.name.value === argument1.name.value; + }); + + if (!argument2) { + return false; + } + + return sameValue(argument1.value, argument2.value); + }); +} + +function sameValue(value1, value2) { + return !value1 && !value2 || (0, _printer.print)(value1) === (0, _printer.print)(value2); +} // Two types conflict if both types could not apply to a value simultaneously. +// Composite types are ignored as their individual field types will be compared +// later recursively. However List and Non-Null types must match. + + +function doTypesConflict(type1, type2) { + if ((0, _definition.isListType)(type1)) { + return (0, _definition.isListType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + + if ((0, _definition.isListType)(type2)) { + return true; + } + + if ((0, _definition.isNonNullType)(type1)) { + return (0, _definition.isNonNullType)(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + + if ((0, _definition.isNonNullType)(type2)) { + return true; + } + + if ((0, _definition.isLeafType)(type1) || (0, _definition.isLeafType)(type2)) { + return type1 !== type2; + } + + return false; +} // Given a selection set, return the collection of fields (a mapping of response +// name to field nodes and definitions) as well as a list of fragment names +// referenced via fragment spreads. + + +function getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet) { + var cached = cachedFieldsAndFragmentNames.get(selectionSet); + + if (!cached) { + var nodeAndDefs = Object.create(null); + var fragmentNames = Object.create(null); + + _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames); + + cached = [nodeAndDefs, Object.keys(fragmentNames)]; + cachedFieldsAndFragmentNames.set(selectionSet, cached); + } + + return cached; +} // Given a reference to a fragment, return the represented collection of fields +// as well as a list of nested fragment names referenced via fragment spreads. + + +function getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment) { + // Short-circuit building a type from the node if possible. + var cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet); + + if (cached) { + return cached; + } + + var fragmentType = (0, _typeFromAST.typeFromAST)(context.getSchema(), fragment.typeCondition); + return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet); +} + +function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames) { + for (var i = 0; i < selectionSet.selections.length; i++) { + var selection = selectionSet.selections[i]; + + switch (selection.kind) { + case _kinds.Kind.FIELD: + var fieldName = selection.name.value; + var fieldDef = void 0; + + if ((0, _definition.isObjectType)(parentType) || (0, _definition.isInterfaceType)(parentType)) { + fieldDef = parentType.getFields()[fieldName]; + } + + var responseName = selection.alias ? selection.alias.value : fieldName; + + if (!nodeAndDefs[responseName]) { + nodeAndDefs[responseName] = []; + } + + nodeAndDefs[responseName].push([parentType, selection, fieldDef]); + break; + + case _kinds.Kind.FRAGMENT_SPREAD: + fragmentNames[selection.name.value] = true; + break; + + case _kinds.Kind.INLINE_FRAGMENT: + var typeCondition = selection.typeCondition; + var inlineFragmentType = typeCondition ? (0, _typeFromAST.typeFromAST)(context.getSchema(), typeCondition) : parentType; + + _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames); + + break; + } + } +} // Given a series of Conflicts which occurred between two sub-fields, generate +// a single Conflict. + + +function subfieldConflicts(conflicts, responseName, node1, node2) { + if (conflicts.length > 0) { + return [[responseName, conflicts.map(function (_ref3) { + var reason = _ref3[0]; + return reason; + })], conflicts.reduce(function (allFields, _ref4) { + var fields1 = _ref4[1]; + return allFields.concat(fields1); + }, [node1]), conflicts.reduce(function (allFields, _ref5) { + var fields2 = _ref5[2]; + return allFields.concat(fields2); + }, [node2])]; + } +} +/** + * A way to keep track of pairs of things when the ordering of the pair does + * not matter. We do this by maintaining a sort of double adjacency sets. + */ + + +var PairSet = +/*#__PURE__*/ +function () { + function PairSet() { + _defineProperty(this, "_data", void 0); + + this._data = Object.create(null); + } + + var _proto = PairSet.prototype; + + _proto.has = function has(a, b, areMutuallyExclusive) { + var first = this._data[a]; + var result = first && first[b]; + + if (result === undefined) { + return false; + } // areMutuallyExclusive being false is a superset of being true, + // hence if we want to know if this PairSet "has" these two with no + // exclusivity, we have to ensure it was added as such. + + + if (areMutuallyExclusive === false) { + return result === false; + } + + return true; + }; + + _proto.add = function add(a, b, areMutuallyExclusive) { + _pairSetAdd(this._data, a, b, areMutuallyExclusive); + + _pairSetAdd(this._data, b, a, areMutuallyExclusive); + }; + + return PairSet; +}(); + +function _pairSetAdd(data, a, b, areMutuallyExclusive) { + var map = data[a]; + + if (!map) { + map = Object.create(null); + data[a] = map; + } + + map[b] = areMutuallyExclusive; +} \ No newline at end of file diff --git a/dist/validation/rules/OverlappingFieldsCanBeMerged.js.flow b/dist/validation/rules/OverlappingFieldsCanBeMerged.js.flow new file mode 100644 index 0000000000..6dea9df741 --- /dev/null +++ b/dist/validation/rules/OverlappingFieldsCanBeMerged.js.flow @@ -0,0 +1,847 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import find from '../../jsutils/find'; +import type { ObjMap } from '../../jsutils/ObjMap'; +import type { + SelectionSetNode, + FieldNode, + ArgumentNode, + FragmentDefinitionNode, +} from '../../language/ast'; +import { Kind } from '../../language/kinds'; +import { print } from '../../language/printer'; +import type { ASTVisitor } from '../../language/visitor'; +import { + getNamedType, + isNonNullType, + isLeafType, + isObjectType, + isListType, + isInterfaceType, +} from '../../type/definition'; +import type { + GraphQLNamedType, + GraphQLOutputType, + GraphQLCompositeType, + GraphQLField, +} from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; + +export function fieldsConflictMessage( + responseName: string, + reason: ConflictReasonMessage, +): string { + return ( + `Fields "${responseName}" conflict because ${reasonMessage(reason)}` + + '. Use different aliases on the fields to fetch both if this was ' + + 'intentional.' + ); +} + +function reasonMessage(reason: ConflictReasonMessage): string { + if (Array.isArray(reason)) { + return reason + .map( + ([responseName, subreason]) => + `subfields "${responseName}" conflict because ${reasonMessage( + subreason, + )}`, + ) + .join(' and '); + } + return reason; +} + +/** + * Overlapping fields can be merged + * + * A selection set is only valid if all fields (including spreading any + * fragments) either correspond to distinct response names or can be merged + * without ambiguity. + */ +export function OverlappingFieldsCanBeMerged( + context: ValidationContext, +): ASTVisitor { + // A memoization for when two fragments are compared "between" each other for + // conflicts. Two fragments may be compared many times, so memoizing this can + // dramatically improve the performance of this validator. + const comparedFragmentPairs = new PairSet(); + + // A cache for the "field map" and list of fragment names found in any given + // selection set. Selection sets may be asked for this information multiple + // times, so this improves the performance of this validator. + const cachedFieldsAndFragmentNames = new Map(); + + return { + SelectionSet(selectionSet) { + const conflicts = findConflictsWithinSelectionSet( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + context.getParentType(), + selectionSet, + ); + conflicts.forEach(([[responseName, reason], fields1, fields2]) => + context.reportError( + new GraphQLError( + fieldsConflictMessage(responseName, reason), + fields1.concat(fields2), + ), + ), + ); + }, + }; +} + +type Conflict = [ConflictReason, Array, Array]; +// Field name and reason. +type ConflictReason = [string, ConflictReasonMessage]; +// Reason is a string, or a nested list of conflicts. +type ConflictReasonMessage = string | Array; +// Tuple defining a field node in a context. +type NodeAndDef = [GraphQLCompositeType, FieldNode, ?GraphQLField<*, *>]; +// Map of array of those. +type NodeAndDefCollection = ObjMap>; + +/** + * Algorithm: + * + * Conflicts occur when two fields exist in a query which will produce the same + * response name, but represent differing values, thus creating a conflict. + * The algorithm below finds all conflicts via making a series of comparisons + * between fields. In order to compare as few fields as possible, this makes + * a series of comparisons "within" sets of fields and "between" sets of fields. + * + * Given any selection set, a collection produces both a set of fields by + * also including all inline fragments, as well as a list of fragments + * referenced by fragment spreads. + * + * A) Each selection set represented in the document first compares "within" its + * collected set of fields, finding any conflicts between every pair of + * overlapping fields. + * Note: This is the *only time* that a the fields "within" a set are compared + * to each other. After this only fields "between" sets are compared. + * + * B) Also, if any fragment is referenced in a selection set, then a + * comparison is made "between" the original set of fields and the + * referenced fragment. + * + * C) Also, if multiple fragments are referenced, then comparisons + * are made "between" each referenced fragment. + * + * D) When comparing "between" a set of fields and a referenced fragment, first + * a comparison is made between each field in the original set of fields and + * each field in the the referenced set of fields. + * + * E) Also, if any fragment is referenced in the referenced selection set, + * then a comparison is made "between" the original set of fields and the + * referenced fragment (recursively referring to step D). + * + * F) When comparing "between" two fragments, first a comparison is made between + * each field in the first referenced set of fields and each field in the the + * second referenced set of fields. + * + * G) Also, any fragments referenced by the first must be compared to the + * second, and any fragments referenced by the second must be compared to the + * first (recursively referring to step F). + * + * H) When comparing two fields, if both have selection sets, then a comparison + * is made "between" both selection sets, first comparing the set of fields in + * the first selection set with the set of fields in the second. + * + * I) Also, if any fragment is referenced in either selection set, then a + * comparison is made "between" the other set of fields and the + * referenced fragment. + * + * J) Also, if two fragments are referenced in both selection sets, then a + * comparison is made "between" the two fragments. + * + */ + +// Find all conflicts found "within" a selection set, including those found +// via spreading in fragments. Called when visiting each SelectionSet in the +// GraphQL Document. +function findConflictsWithinSelectionSet( + context: ValidationContext, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + parentType: ?GraphQLNamedType, + selectionSet: SelectionSetNode, +): Array { + const conflicts = []; + + const [fieldMap, fragmentNames] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType, + selectionSet, + ); + + // (A) Find find all conflicts "within" the fields of this selection set. + // Note: this is the *only place* `collectConflictsWithin` is called. + collectConflictsWithin( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + fieldMap, + ); + + if (fragmentNames.length !== 0) { + // (B) Then collect conflicts between these fields and those represented by + // each spread fragment name found. + const comparedFragments = Object.create(null); + for (let i = 0; i < fragmentNames.length; i++) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragments, + comparedFragmentPairs, + false, + fieldMap, + fragmentNames[i], + ); + // (C) Then compare this fragment with all other fragments found in this + // selection set to collect conflicts between fragments spread together. + // This compares each item in the list of fragment names to every other + // item in that same list (except for itself). + for (let j = i + 1; j < fragmentNames.length; j++) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + false, + fragmentNames[i], + fragmentNames[j], + ); + } + } + } + return conflicts; +} + +// Collect all conflicts found between a set of fields and a fragment reference +// including via spreading in any nested fragments. +function collectConflictsBetweenFieldsAndFragment( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames, + comparedFragments: ObjMap, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + fieldMap: NodeAndDefCollection, + fragmentName: string, +): void { + // Memoize so a fragment is not compared for conflicts more than once. + if (comparedFragments[fragmentName]) { + return; + } + comparedFragments[fragmentName] = true; + + const fragment = context.getFragment(fragmentName); + if (!fragment) { + return; + } + + const [fieldMap2, fragmentNames2] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment, + ); + + // Do not compare a fragment's fieldMap to itself. + if (fieldMap === fieldMap2) { + return; + } + + // (D) First collect any conflicts between the provided collection of fields + // and the collection of fields represented by the given fragment. + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap, + fieldMap2, + ); + + // (E) Then collect any conflicts between the provided collection of fields + // and any fragment names found in the given fragment. + for (let i = 0; i < fragmentNames2.length; i++) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragments, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap, + fragmentNames2[i], + ); + } +} + +// Collect all conflicts found between two fragments, including via spreading in +// any nested fragments. +function collectConflictsBetweenFragments( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + fragmentName1: string, + fragmentName2: string, +): void { + // No need to compare a fragment to itself. + if (fragmentName1 === fragmentName2) { + return; + } + + // Memoize so two fragments are not compared for conflicts more than once. + if ( + comparedFragmentPairs.has( + fragmentName1, + fragmentName2, + areMutuallyExclusive, + ) + ) { + return; + } + comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive); + + const fragment1 = context.getFragment(fragmentName1); + const fragment2 = context.getFragment(fragmentName2); + if (!fragment1 || !fragment2) { + return; + } + + const [fieldMap1, fragmentNames1] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment1, + ); + const [fieldMap2, fragmentNames2] = getReferencedFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragment2, + ); + + // (F) First, collect all conflicts between these two collections of fields + // (not including any nested fragments). + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fieldMap2, + ); + + // (G) Then collect conflicts between the first fragment and any nested + // fragments spread in the second fragment. + for (let j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fragmentName1, + fragmentNames2[j], + ); + } + + // (G) Then collect conflicts between the second fragment and any nested + // fragments spread in the first fragment. + for (let i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fragmentNames1[i], + fragmentName2, + ); + } +} + +// Find all conflicts found between two selection sets, including those found +// via spreading in fragments. Called when determining if conflicts exist +// between the sub-fields of two overlapping fields. +function findConflictsBetweenSubSelectionSets( + context: ValidationContext, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + areMutuallyExclusive: boolean, + parentType1: ?GraphQLNamedType, + selectionSet1: SelectionSetNode, + parentType2: ?GraphQLNamedType, + selectionSet2: SelectionSetNode, +): Array { + const conflicts = []; + + const [fieldMap1, fragmentNames1] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType1, + selectionSet1, + ); + const [fieldMap2, fragmentNames2] = getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + parentType2, + selectionSet2, + ); + + // (H) First, collect all conflicts between these two collections of field. + collectConflictsBetween( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fieldMap2, + ); + + // (I) Then collect conflicts between the first collection of fields and + // those referenced by each fragment name associated with the second. + if (fragmentNames2.length !== 0) { + const comparedFragments = Object.create(null); + for (let j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragments, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap1, + fragmentNames2[j], + ); + } + } + + // (I) Then collect conflicts between the second collection of fields and + // those referenced by each fragment name associated with the first. + if (fragmentNames1.length !== 0) { + const comparedFragments = Object.create(null); + for (let i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFieldsAndFragment( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragments, + comparedFragmentPairs, + areMutuallyExclusive, + fieldMap2, + fragmentNames1[i], + ); + } + } + + // (J) Also collect conflicts between any fragment names by the first and + // fragment names by the second. This compares each item in the first set of + // names to each item in the second set of names. + for (let i = 0; i < fragmentNames1.length; i++) { + for (let j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFragments( + context, + conflicts, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + fragmentNames1[i], + fragmentNames2[j], + ); + } + } + return conflicts; +} + +// Collect all Conflicts "within" one collection of fields. +function collectConflictsWithin( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + fieldMap: NodeAndDefCollection, +): void { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For every response name, if there are multiple fields, they + // must be compared to find a potential conflict. + Object.keys(fieldMap).forEach(responseName => { + const fields = fieldMap[responseName]; + // This compares every field in the list to every other field in this list + // (except to itself). If the list only has one item, nothing needs to + // be compared. + if (fields.length > 1) { + for (let i = 0; i < fields.length; i++) { + for (let j = i + 1; j < fields.length; j++) { + const conflict = findConflict( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + false, // within one collection is never mutually exclusive + responseName, + fields[i], + fields[j], + ); + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} + +// Collect all Conflicts between two collections of fields. This is similar to, +// but different from the `collectConflictsWithin` function above. This check +// assumes that `collectConflictsWithin` has already been called on each +// provided collection of fields. This is true because this validator traverses +// each individual selection set. +function collectConflictsBetween( + context: ValidationContext, + conflicts: Array, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + parentFieldsAreMutuallyExclusive: boolean, + fieldMap1: NodeAndDefCollection, + fieldMap2: NodeAndDefCollection, +): void { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For any response name which appears in both provided field + // maps, each field from the first field map must be compared to every field + // in the second field map to find potential conflicts. + Object.keys(fieldMap1).forEach(responseName => { + const fields2 = fieldMap2[responseName]; + if (fields2) { + const fields1 = fieldMap1[responseName]; + for (let i = 0; i < fields1.length; i++) { + for (let j = 0; j < fields2.length; j++) { + const conflict = findConflict( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + parentFieldsAreMutuallyExclusive, + responseName, + fields1[i], + fields2[j], + ); + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} + +// Determines if there is a conflict between two particular fields, including +// comparing their sub-fields. +function findConflict( + context: ValidationContext, + cachedFieldsAndFragmentNames, + comparedFragmentPairs: PairSet, + parentFieldsAreMutuallyExclusive: boolean, + responseName: string, + field1: NodeAndDef, + field2: NodeAndDef, +): ?Conflict { + const [parentType1, node1, def1] = field1; + const [parentType2, node2, def2] = field2; + + // If it is known that two fields could not possibly apply at the same + // time, due to the parent types, then it is safe to permit them to diverge + // in aliased field or arguments used as they will not present any ambiguity + // by differing. + // It is known that two parent types could never overlap if they are + // different Object types. Interface or Union types might overlap - if not + // in the current state of the schema, then perhaps in some future version, + // thus may not safely diverge. + const areMutuallyExclusive = + parentFieldsAreMutuallyExclusive || + (parentType1 !== parentType2 && + isObjectType(parentType1) && + isObjectType(parentType2)); + + // The return type for each field. + const type1 = def1 && def1.type; + const type2 = def2 && def2.type; + + if (!areMutuallyExclusive) { + // Two aliases must refer to the same field. + const name1 = node1.name.value; + const name2 = node2.name.value; + if (name1 !== name2) { + return [ + [responseName, `${name1} and ${name2} are different fields`], + [node1], + [node2], + ]; + } + + // Two field calls must have the same arguments. + if (!sameArguments(node1.arguments || [], node2.arguments || [])) { + return [ + [responseName, 'they have differing arguments'], + [node1], + [node2], + ]; + } + } + + if (type1 && type2 && doTypesConflict(type1, type2)) { + return [ + [ + responseName, + `they return conflicting types ${String(type1)} and ${String(type2)}`, + ], + [node1], + [node2], + ]; + } + + // Collect and compare sub-fields. Use the same "visited fragment names" list + // for both collections so fields in a fragment reference are never + // compared to themselves. + const selectionSet1 = node1.selectionSet; + const selectionSet2 = node2.selectionSet; + if (selectionSet1 && selectionSet2) { + const conflicts = findConflictsBetweenSubSelectionSets( + context, + cachedFieldsAndFragmentNames, + comparedFragmentPairs, + areMutuallyExclusive, + getNamedType(type1), + selectionSet1, + getNamedType(type2), + selectionSet2, + ); + return subfieldConflicts(conflicts, responseName, node1, node2); + } +} + +function sameArguments( + arguments1: $ReadOnlyArray, + arguments2: $ReadOnlyArray, +): boolean { + if (arguments1.length !== arguments2.length) { + return false; + } + return arguments1.every(argument1 => { + const argument2 = find( + arguments2, + argument => argument.name.value === argument1.name.value, + ); + if (!argument2) { + return false; + } + return sameValue(argument1.value, argument2.value); + }); +} + +function sameValue(value1, value2) { + return (!value1 && !value2) || print(value1) === print(value2); +} + +// Two types conflict if both types could not apply to a value simultaneously. +// Composite types are ignored as their individual field types will be compared +// later recursively. However List and Non-Null types must match. +function doTypesConflict( + type1: GraphQLOutputType, + type2: GraphQLOutputType, +): boolean { + if (isListType(type1)) { + return isListType(type2) + ? doTypesConflict(type1.ofType, type2.ofType) + : true; + } + if (isListType(type2)) { + return true; + } + if (isNonNullType(type1)) { + return isNonNullType(type2) + ? doTypesConflict(type1.ofType, type2.ofType) + : true; + } + if (isNonNullType(type2)) { + return true; + } + if (isLeafType(type1) || isLeafType(type2)) { + return type1 !== type2; + } + return false; +} + +// Given a selection set, return the collection of fields (a mapping of response +// name to field nodes and definitions) as well as a list of fragment names +// referenced via fragment spreads. +function getFieldsAndFragmentNames( + context: ValidationContext, + cachedFieldsAndFragmentNames, + parentType: ?GraphQLNamedType, + selectionSet: SelectionSetNode, +): [NodeAndDefCollection, Array] { + let cached = cachedFieldsAndFragmentNames.get(selectionSet); + if (!cached) { + const nodeAndDefs = Object.create(null); + const fragmentNames = Object.create(null); + _collectFieldsAndFragmentNames( + context, + parentType, + selectionSet, + nodeAndDefs, + fragmentNames, + ); + cached = [nodeAndDefs, Object.keys(fragmentNames)]; + cachedFieldsAndFragmentNames.set(selectionSet, cached); + } + return cached; +} + +// Given a reference to a fragment, return the represented collection of fields +// as well as a list of nested fragment names referenced via fragment spreads. +function getReferencedFieldsAndFragmentNames( + context: ValidationContext, + cachedFieldsAndFragmentNames, + fragment: FragmentDefinitionNode, +) { + // Short-circuit building a type from the node if possible. + const cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet); + if (cached) { + return cached; + } + + const fragmentType = typeFromAST(context.getSchema(), fragment.typeCondition); + return getFieldsAndFragmentNames( + context, + cachedFieldsAndFragmentNames, + fragmentType, + fragment.selectionSet, + ); +} + +function _collectFieldsAndFragmentNames( + context: ValidationContext, + parentType: ?GraphQLNamedType, + selectionSet: SelectionSetNode, + nodeAndDefs, + fragmentNames, +): void { + for (let i = 0; i < selectionSet.selections.length; i++) { + const selection = selectionSet.selections[i]; + switch (selection.kind) { + case Kind.FIELD: + const fieldName = selection.name.value; + let fieldDef; + if (isObjectType(parentType) || isInterfaceType(parentType)) { + fieldDef = parentType.getFields()[fieldName]; + } + const responseName = selection.alias + ? selection.alias.value + : fieldName; + if (!nodeAndDefs[responseName]) { + nodeAndDefs[responseName] = []; + } + nodeAndDefs[responseName].push([parentType, selection, fieldDef]); + break; + case Kind.FRAGMENT_SPREAD: + fragmentNames[selection.name.value] = true; + break; + case Kind.INLINE_FRAGMENT: + const typeCondition = selection.typeCondition; + const inlineFragmentType = typeCondition + ? typeFromAST(context.getSchema(), typeCondition) + : parentType; + _collectFieldsAndFragmentNames( + context, + inlineFragmentType, + selection.selectionSet, + nodeAndDefs, + fragmentNames, + ); + break; + } + } +} + +// Given a series of Conflicts which occurred between two sub-fields, generate +// a single Conflict. +function subfieldConflicts( + conflicts: Array, + responseName: string, + node1: FieldNode, + node2: FieldNode, +): ?Conflict { + if (conflicts.length > 0) { + return [ + [responseName, conflicts.map(([reason]) => reason)], + conflicts.reduce((allFields, [, fields1]) => allFields.concat(fields1), [ + node1, + ]), + conflicts.reduce( + (allFields, [, , fields2]) => allFields.concat(fields2), + [node2], + ), + ]; + } +} + +/** + * A way to keep track of pairs of things when the ordering of the pair does + * not matter. We do this by maintaining a sort of double adjacency sets. + */ +class PairSet { + _data: ObjMap>; + + constructor(): void { + this._data = Object.create(null); + } + + has(a: string, b: string, areMutuallyExclusive: boolean) { + const first = this._data[a]; + const result = first && first[b]; + if (result === undefined) { + return false; + } + // areMutuallyExclusive being false is a superset of being true, + // hence if we want to know if this PairSet "has" these two with no + // exclusivity, we have to ensure it was added as such. + if (areMutuallyExclusive === false) { + return result === false; + } + return true; + } + + add(a: string, b: string, areMutuallyExclusive: boolean) { + _pairSetAdd(this._data, a, b, areMutuallyExclusive); + _pairSetAdd(this._data, b, a, areMutuallyExclusive); + } +} + +function _pairSetAdd(data, a, b, areMutuallyExclusive) { + let map = data[a]; + if (!map) { + map = Object.create(null); + data[a] = map; + } + map[b] = areMutuallyExclusive; +} diff --git a/dist/validation/rules/OverlappingFieldsCanBeMerged.mjs b/dist/validation/rules/OverlappingFieldsCanBeMerged.mjs new file mode 100644 index 0000000000..7da330d7a8 --- /dev/null +++ b/dist/validation/rules/OverlappingFieldsCanBeMerged.mjs @@ -0,0 +1,577 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import find from '../../jsutils/find'; +import { Kind } from '../../language/kinds'; +import { print } from '../../language/printer'; +import { getNamedType, isNonNullType, isLeafType, isObjectType, isListType, isInterfaceType } from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; +export function fieldsConflictMessage(responseName, reason) { + return "Fields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(reason)) + '. Use different aliases on the fields to fetch both if this was ' + 'intentional.'; +} + +function reasonMessage(reason) { + if (Array.isArray(reason)) { + return reason.map(function (_ref) { + var responseName = _ref[0], + subreason = _ref[1]; + return "subfields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(subreason)); + }).join(' and '); + } + + return reason; +} +/** + * Overlapping fields can be merged + * + * A selection set is only valid if all fields (including spreading any + * fragments) either correspond to distinct response names or can be merged + * without ambiguity. + */ + + +export function OverlappingFieldsCanBeMerged(context) { + // A memoization for when two fragments are compared "between" each other for + // conflicts. Two fragments may be compared many times, so memoizing this can + // dramatically improve the performance of this validator. + var comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given + // selection set. Selection sets may be asked for this information multiple + // times, so this improves the performance of this validator. + + var cachedFieldsAndFragmentNames = new Map(); + return { + SelectionSet: function SelectionSet(selectionSet) { + var conflicts = findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, context.getParentType(), selectionSet); + conflicts.forEach(function (_ref2) { + var _ref2$ = _ref2[0], + responseName = _ref2$[0], + reason = _ref2$[1], + fields1 = _ref2[1], + fields2 = _ref2[2]; + return context.reportError(new GraphQLError(fieldsConflictMessage(responseName, reason), fields1.concat(fields2))); + }); + } + }; +} + +/** + * Algorithm: + * + * Conflicts occur when two fields exist in a query which will produce the same + * response name, but represent differing values, thus creating a conflict. + * The algorithm below finds all conflicts via making a series of comparisons + * between fields. In order to compare as few fields as possible, this makes + * a series of comparisons "within" sets of fields and "between" sets of fields. + * + * Given any selection set, a collection produces both a set of fields by + * also including all inline fragments, as well as a list of fragments + * referenced by fragment spreads. + * + * A) Each selection set represented in the document first compares "within" its + * collected set of fields, finding any conflicts between every pair of + * overlapping fields. + * Note: This is the *only time* that a the fields "within" a set are compared + * to each other. After this only fields "between" sets are compared. + * + * B) Also, if any fragment is referenced in a selection set, then a + * comparison is made "between" the original set of fields and the + * referenced fragment. + * + * C) Also, if multiple fragments are referenced, then comparisons + * are made "between" each referenced fragment. + * + * D) When comparing "between" a set of fields and a referenced fragment, first + * a comparison is made between each field in the original set of fields and + * each field in the the referenced set of fields. + * + * E) Also, if any fragment is referenced in the referenced selection set, + * then a comparison is made "between" the original set of fields and the + * referenced fragment (recursively referring to step D). + * + * F) When comparing "between" two fragments, first a comparison is made between + * each field in the first referenced set of fields and each field in the the + * second referenced set of fields. + * + * G) Also, any fragments referenced by the first must be compared to the + * second, and any fragments referenced by the second must be compared to the + * first (recursively referring to step F). + * + * H) When comparing two fields, if both have selection sets, then a comparison + * is made "between" both selection sets, first comparing the set of fields in + * the first selection set with the set of fields in the second. + * + * I) Also, if any fragment is referenced in either selection set, then a + * comparison is made "between" the other set of fields and the + * referenced fragment. + * + * J) Also, if two fragments are referenced in both selection sets, then a + * comparison is made "between" the two fragments. + * + */ +// Find all conflicts found "within" a selection set, including those found +// via spreading in fragments. Called when visiting each SelectionSet in the +// GraphQL Document. +function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentType, selectionSet) { + var conflicts = []; + + var _getFieldsAndFragment = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet), + fieldMap = _getFieldsAndFragment[0], + fragmentNames = _getFieldsAndFragment[1]; // (A) Find find all conflicts "within" the fields of this selection set. + // Note: this is the *only place* `collectConflictsWithin` is called. + + + collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap); + + if (fragmentNames.length !== 0) { + // (B) Then collect conflicts between these fields and those represented by + // each spread fragment name found. + var comparedFragments = Object.create(null); + + for (var i = 0; i < fragmentNames.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, false, fieldMap, fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this + // selection set to collect conflicts between fragments spread together. + // This compares each item in the list of fragment names to every other + // item in that same list (except for itself). + + for (var j = i + 1; j < fragmentNames.length; j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fragmentNames[i], fragmentNames[j]); + } + } + } + + return conflicts; +} // Collect all conflicts found between a set of fields and a fragment reference +// including via spreading in any nested fragments. + + +function collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentName) { + // Memoize so a fragment is not compared for conflicts more than once. + if (comparedFragments[fragmentName]) { + return; + } + + comparedFragments[fragmentName] = true; + var fragment = context.getFragment(fragmentName); + + if (!fragment) { + return; + } + + var _getReferencedFieldsA = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment), + fieldMap2 = _getReferencedFieldsA[0], + fragmentNames2 = _getReferencedFieldsA[1]; // Do not compare a fragment's fieldMap to itself. + + + if (fieldMap === fieldMap2) { + return; + } // (D) First collect any conflicts between the provided collection of fields + // and the collection of fields represented by the given fragment. + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fieldMap2); // (E) Then collect any conflicts between the provided collection of fields + // and any fragment names found in the given fragment. + + for (var i = 0; i < fragmentNames2.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentNames2[i]); + } +} // Collect all conflicts found between two fragments, including via spreading in +// any nested fragments. + + +function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentName2) { + // No need to compare a fragment to itself. + if (fragmentName1 === fragmentName2) { + return; + } // Memoize so two fragments are not compared for conflicts more than once. + + + if (comparedFragmentPairs.has(fragmentName1, fragmentName2, areMutuallyExclusive)) { + return; + } + + comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive); + var fragment1 = context.getFragment(fragmentName1); + var fragment2 = context.getFragment(fragmentName2); + + if (!fragment1 || !fragment2) { + return; + } + + var _getReferencedFieldsA2 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment1), + fieldMap1 = _getReferencedFieldsA2[0], + fragmentNames1 = _getReferencedFieldsA2[1]; + + var _getReferencedFieldsA3 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment2), + fieldMap2 = _getReferencedFieldsA3[0], + fragmentNames2 = _getReferencedFieldsA3[1]; // (F) First, collect all conflicts between these two collections of fields + // (not including any nested fragments). + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (G) Then collect conflicts between the first fragment and any nested + // fragments spread in the second fragment. + + for (var j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentNames2[j]); + } // (G) Then collect conflicts between the second fragment and any nested + // fragments spread in the first fragment. + + + for (var i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[i], fragmentName2); + } +} // Find all conflicts found between two selection sets, including those found +// via spreading in fragments. Called when determining if conflicts exist +// between the sub-fields of two overlapping fields. + + +function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) { + var conflicts = []; + + var _getFieldsAndFragment2 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType1, selectionSet1), + fieldMap1 = _getFieldsAndFragment2[0], + fragmentNames1 = _getFieldsAndFragment2[1]; + + var _getFieldsAndFragment3 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType2, selectionSet2), + fieldMap2 = _getFieldsAndFragment3[0], + fragmentNames2 = _getFieldsAndFragment3[1]; // (H) First, collect all conflicts between these two collections of field. + + + collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (I) Then collect conflicts between the first collection of fields and + // those referenced by each fragment name associated with the second. + + if (fragmentNames2.length !== 0) { + var comparedFragments = Object.create(null); + + for (var j = 0; j < fragmentNames2.length; j++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fragmentNames2[j]); + } + } // (I) Then collect conflicts between the second collection of fields and + // those referenced by each fragment name associated with the first. + + + if (fragmentNames1.length !== 0) { + var _comparedFragments = Object.create(null); + + for (var i = 0; i < fragmentNames1.length; i++) { + collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, _comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap2, fragmentNames1[i]); + } + } // (J) Also collect conflicts between any fragment names by the first and + // fragment names by the second. This compares each item in the first set of + // names to each item in the second set of names. + + + for (var _i = 0; _i < fragmentNames1.length; _i++) { + for (var _j = 0; _j < fragmentNames2.length; _j++) { + collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[_i], fragmentNames2[_j]); + } + } + + return conflicts; +} // Collect all Conflicts "within" one collection of fields. + + +function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap) { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For every response name, if there are multiple fields, they + // must be compared to find a potential conflict. + Object.keys(fieldMap).forEach(function (responseName) { + var fields = fieldMap[responseName]; // This compares every field in the list to every other field in this list + // (except to itself). If the list only has one item, nothing needs to + // be compared. + + if (fields.length > 1) { + for (var i = 0; i < fields.length; i++) { + for (var j = i + 1; j < fields.length; j++) { + var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, // within one collection is never mutually exclusive + responseName, fields[i], fields[j]); + + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} // Collect all Conflicts between two collections of fields. This is similar to, +// but different from the `collectConflictsWithin` function above. This check +// assumes that `collectConflictsWithin` has already been called on each +// provided collection of fields. This is true because this validator traverses +// each individual selection set. + + +function collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, fieldMap1, fieldMap2) { + // A field map is a keyed collection, where each key represents a response + // name and the value at that key is a list of all fields which provide that + // response name. For any response name which appears in both provided field + // maps, each field from the first field map must be compared to every field + // in the second field map to find potential conflicts. + Object.keys(fieldMap1).forEach(function (responseName) { + var fields2 = fieldMap2[responseName]; + + if (fields2) { + var fields1 = fieldMap1[responseName]; + + for (var i = 0; i < fields1.length; i++) { + for (var j = 0; j < fields2.length; j++) { + var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]); + + if (conflict) { + conflicts.push(conflict); + } + } + } + } + }); +} // Determines if there is a conflict between two particular fields, including +// comparing their sub-fields. + + +function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, field1, field2) { + var parentType1 = field1[0], + node1 = field1[1], + def1 = field1[2]; + var parentType2 = field2[0], + node2 = field2[1], + def2 = field2[2]; // If it is known that two fields could not possibly apply at the same + // time, due to the parent types, then it is safe to permit them to diverge + // in aliased field or arguments used as they will not present any ambiguity + // by differing. + // It is known that two parent types could never overlap if they are + // different Object types. Interface or Union types might overlap - if not + // in the current state of the schema, then perhaps in some future version, + // thus may not safely diverge. + + var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && isObjectType(parentType1) && isObjectType(parentType2); // The return type for each field. + + var type1 = def1 && def1.type; + var type2 = def2 && def2.type; + + if (!areMutuallyExclusive) { + // Two aliases must refer to the same field. + var name1 = node1.name.value; + var name2 = node2.name.value; + + if (name1 !== name2) { + return [[responseName, "".concat(name1, " and ").concat(name2, " are different fields")], [node1], [node2]]; + } // Two field calls must have the same arguments. + + + if (!sameArguments(node1.arguments || [], node2.arguments || [])) { + return [[responseName, 'they have differing arguments'], [node1], [node2]]; + } + } + + if (type1 && type2 && doTypesConflict(type1, type2)) { + return [[responseName, "they return conflicting types ".concat(String(type1), " and ").concat(String(type2))], [node1], [node2]]; + } // Collect and compare sub-fields. Use the same "visited fragment names" list + // for both collections so fields in a fragment reference are never + // compared to themselves. + + + var selectionSet1 = node1.selectionSet; + var selectionSet2 = node2.selectionSet; + + if (selectionSet1 && selectionSet2) { + var conflicts = findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, getNamedType(type1), selectionSet1, getNamedType(type2), selectionSet2); + return subfieldConflicts(conflicts, responseName, node1, node2); + } +} + +function sameArguments(arguments1, arguments2) { + if (arguments1.length !== arguments2.length) { + return false; + } + + return arguments1.every(function (argument1) { + var argument2 = find(arguments2, function (argument) { + return argument.name.value === argument1.name.value; + }); + + if (!argument2) { + return false; + } + + return sameValue(argument1.value, argument2.value); + }); +} + +function sameValue(value1, value2) { + return !value1 && !value2 || print(value1) === print(value2); +} // Two types conflict if both types could not apply to a value simultaneously. +// Composite types are ignored as their individual field types will be compared +// later recursively. However List and Non-Null types must match. + + +function doTypesConflict(type1, type2) { + if (isListType(type1)) { + return isListType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + + if (isListType(type2)) { + return true; + } + + if (isNonNullType(type1)) { + return isNonNullType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true; + } + + if (isNonNullType(type2)) { + return true; + } + + if (isLeafType(type1) || isLeafType(type2)) { + return type1 !== type2; + } + + return false; +} // Given a selection set, return the collection of fields (a mapping of response +// name to field nodes and definitions) as well as a list of fragment names +// referenced via fragment spreads. + + +function getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet) { + var cached = cachedFieldsAndFragmentNames.get(selectionSet); + + if (!cached) { + var nodeAndDefs = Object.create(null); + var fragmentNames = Object.create(null); + + _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames); + + cached = [nodeAndDefs, Object.keys(fragmentNames)]; + cachedFieldsAndFragmentNames.set(selectionSet, cached); + } + + return cached; +} // Given a reference to a fragment, return the represented collection of fields +// as well as a list of nested fragment names referenced via fragment spreads. + + +function getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment) { + // Short-circuit building a type from the node if possible. + var cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet); + + if (cached) { + return cached; + } + + var fragmentType = typeFromAST(context.getSchema(), fragment.typeCondition); + return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet); +} + +function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames) { + for (var i = 0; i < selectionSet.selections.length; i++) { + var selection = selectionSet.selections[i]; + + switch (selection.kind) { + case Kind.FIELD: + var fieldName = selection.name.value; + var fieldDef = void 0; + + if (isObjectType(parentType) || isInterfaceType(parentType)) { + fieldDef = parentType.getFields()[fieldName]; + } + + var responseName = selection.alias ? selection.alias.value : fieldName; + + if (!nodeAndDefs[responseName]) { + nodeAndDefs[responseName] = []; + } + + nodeAndDefs[responseName].push([parentType, selection, fieldDef]); + break; + + case Kind.FRAGMENT_SPREAD: + fragmentNames[selection.name.value] = true; + break; + + case Kind.INLINE_FRAGMENT: + var typeCondition = selection.typeCondition; + var inlineFragmentType = typeCondition ? typeFromAST(context.getSchema(), typeCondition) : parentType; + + _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames); + + break; + } + } +} // Given a series of Conflicts which occurred between two sub-fields, generate +// a single Conflict. + + +function subfieldConflicts(conflicts, responseName, node1, node2) { + if (conflicts.length > 0) { + return [[responseName, conflicts.map(function (_ref3) { + var reason = _ref3[0]; + return reason; + })], conflicts.reduce(function (allFields, _ref4) { + var fields1 = _ref4[1]; + return allFields.concat(fields1); + }, [node1]), conflicts.reduce(function (allFields, _ref5) { + var fields2 = _ref5[2]; + return allFields.concat(fields2); + }, [node2])]; + } +} +/** + * A way to keep track of pairs of things when the ordering of the pair does + * not matter. We do this by maintaining a sort of double adjacency sets. + */ + + +var PairSet = +/*#__PURE__*/ +function () { + function PairSet() { + _defineProperty(this, "_data", void 0); + + this._data = Object.create(null); + } + + var _proto = PairSet.prototype; + + _proto.has = function has(a, b, areMutuallyExclusive) { + var first = this._data[a]; + var result = first && first[b]; + + if (result === undefined) { + return false; + } // areMutuallyExclusive being false is a superset of being true, + // hence if we want to know if this PairSet "has" these two with no + // exclusivity, we have to ensure it was added as such. + + + if (areMutuallyExclusive === false) { + return result === false; + } + + return true; + }; + + _proto.add = function add(a, b, areMutuallyExclusive) { + _pairSetAdd(this._data, a, b, areMutuallyExclusive); + + _pairSetAdd(this._data, b, a, areMutuallyExclusive); + }; + + return PairSet; +}(); + +function _pairSetAdd(data, a, b, areMutuallyExclusive) { + var map = data[a]; + + if (!map) { + map = Object.create(null); + data[a] = map; + } + + map[b] = areMutuallyExclusive; +} \ No newline at end of file diff --git a/dist/validation/rules/PossibleFragmentSpreads.js b/dist/validation/rules/PossibleFragmentSpreads.js new file mode 100644 index 0000000000..ca98d40086 --- /dev/null +++ b/dist/validation/rules/PossibleFragmentSpreads.js @@ -0,0 +1,74 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.typeIncompatibleSpreadMessage = typeIncompatibleSpreadMessage; +exports.typeIncompatibleAnonSpreadMessage = typeIncompatibleAnonSpreadMessage; +exports.PossibleFragmentSpreads = PossibleFragmentSpreads; + +var _error = require("../../error"); + +var _typeComparators = require("../../utilities/typeComparators"); + +var _typeFromAST = require("../../utilities/typeFromAST"); + +var _definition = require("../../type/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. + * + * strict + */ +function typeIncompatibleSpreadMessage(fragName, parentType, fragType) { + return "Fragment \"".concat(fragName, "\" cannot be spread here as objects of ") + "type \"".concat(String(parentType), "\" can never be of type \"").concat(String(fragType), "\"."); +} + +function typeIncompatibleAnonSpreadMessage(parentType, fragType) { + return 'Fragment cannot be spread here as objects of ' + "type \"".concat(String(parentType), "\" can never be of type \"").concat(String(fragType), "\"."); +} +/** + * Possible fragment spread + * + * A fragment spread is only valid if the type condition could ever possibly + * be true: if there is a non-empty intersection of the possible parent types, + * and possible types which pass the type condition. + */ + + +function PossibleFragmentSpreads(context) { + return { + InlineFragment: function InlineFragment(node) { + var fragType = context.getType(); + var parentType = context.getParentType(); + + if ((0, _definition.isCompositeType)(fragType) && (0, _definition.isCompositeType)(parentType) && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) { + context.reportError(new _error.GraphQLError(typeIncompatibleAnonSpreadMessage(parentType, fragType), [node])); + } + }, + FragmentSpread: function FragmentSpread(node) { + var fragName = node.name.value; + var fragType = getFragmentType(context, fragName); + var parentType = context.getParentType(); + + if (fragType && parentType && !(0, _typeComparators.doTypesOverlap)(context.getSchema(), fragType, parentType)) { + context.reportError(new _error.GraphQLError(typeIncompatibleSpreadMessage(fragName, parentType, fragType), [node])); + } + } + }; +} + +function getFragmentType(context, name) { + var frag = context.getFragment(name); + + if (frag) { + var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), frag.typeCondition); + + if ((0, _definition.isCompositeType)(type)) { + return type; + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/PossibleFragmentSpreads.js.flow b/dist/validation/rules/PossibleFragmentSpreads.js.flow new file mode 100644 index 0000000000..31bca238d8 --- /dev/null +++ b/dist/validation/rules/PossibleFragmentSpreads.js.flow @@ -0,0 +1,94 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; +import { doTypesOverlap } from '../../utilities/typeComparators'; +import { typeFromAST } from '../../utilities/typeFromAST'; +import { isCompositeType } from '../../type/definition'; +import type { GraphQLType } from '../../type/definition'; + +export function typeIncompatibleSpreadMessage( + fragName: string, + parentType: GraphQLType, + fragType: GraphQLType, +): string { + return ( + `Fragment "${fragName}" cannot be spread here as objects of ` + + `type "${String(parentType)}" can never be of type "${String(fragType)}".` + ); +} + +export function typeIncompatibleAnonSpreadMessage( + parentType: GraphQLType, + fragType: GraphQLType, +): string { + return ( + 'Fragment cannot be spread here as objects of ' + + `type "${String(parentType)}" can never be of type "${String(fragType)}".` + ); +} + +/** + * Possible fragment spread + * + * A fragment spread is only valid if the type condition could ever possibly + * be true: if there is a non-empty intersection of the possible parent types, + * and possible types which pass the type condition. + */ +export function PossibleFragmentSpreads( + context: ValidationContext, +): ASTVisitor { + return { + InlineFragment(node) { + const fragType = context.getType(); + const parentType = context.getParentType(); + if ( + isCompositeType(fragType) && + isCompositeType(parentType) && + !doTypesOverlap(context.getSchema(), fragType, parentType) + ) { + context.reportError( + new GraphQLError( + typeIncompatibleAnonSpreadMessage(parentType, fragType), + [node], + ), + ); + } + }, + FragmentSpread(node) { + const fragName = node.name.value; + const fragType = getFragmentType(context, fragName); + const parentType = context.getParentType(); + if ( + fragType && + parentType && + !doTypesOverlap(context.getSchema(), fragType, parentType) + ) { + context.reportError( + new GraphQLError( + typeIncompatibleSpreadMessage(fragName, parentType, fragType), + [node], + ), + ); + } + }, + }; +} + +function getFragmentType(context, name) { + const frag = context.getFragment(name); + if (frag) { + const type = typeFromAST(context.getSchema(), frag.typeCondition); + if (isCompositeType(type)) { + return type; + } + } +} diff --git a/dist/validation/rules/PossibleFragmentSpreads.mjs b/dist/validation/rules/PossibleFragmentSpreads.mjs new file mode 100644 index 0000000000..3f340e3339 --- /dev/null +++ b/dist/validation/rules/PossibleFragmentSpreads.mjs @@ -0,0 +1,59 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { doTypesOverlap } from '../../utilities/typeComparators'; +import { typeFromAST } from '../../utilities/typeFromAST'; +import { isCompositeType } from '../../type/definition'; +export function typeIncompatibleSpreadMessage(fragName, parentType, fragType) { + return "Fragment \"".concat(fragName, "\" cannot be spread here as objects of ") + "type \"".concat(String(parentType), "\" can never be of type \"").concat(String(fragType), "\"."); +} +export function typeIncompatibleAnonSpreadMessage(parentType, fragType) { + return 'Fragment cannot be spread here as objects of ' + "type \"".concat(String(parentType), "\" can never be of type \"").concat(String(fragType), "\"."); +} +/** + * Possible fragment spread + * + * A fragment spread is only valid if the type condition could ever possibly + * be true: if there is a non-empty intersection of the possible parent types, + * and possible types which pass the type condition. + */ + +export function PossibleFragmentSpreads(context) { + return { + InlineFragment: function InlineFragment(node) { + var fragType = context.getType(); + var parentType = context.getParentType(); + + if (isCompositeType(fragType) && isCompositeType(parentType) && !doTypesOverlap(context.getSchema(), fragType, parentType)) { + context.reportError(new GraphQLError(typeIncompatibleAnonSpreadMessage(parentType, fragType), [node])); + } + }, + FragmentSpread: function FragmentSpread(node) { + var fragName = node.name.value; + var fragType = getFragmentType(context, fragName); + var parentType = context.getParentType(); + + if (fragType && parentType && !doTypesOverlap(context.getSchema(), fragType, parentType)) { + context.reportError(new GraphQLError(typeIncompatibleSpreadMessage(fragName, parentType, fragType), [node])); + } + } + }; +} + +function getFragmentType(context, name) { + var frag = context.getFragment(name); + + if (frag) { + var type = typeFromAST(context.getSchema(), frag.typeCondition); + + if (isCompositeType(type)) { + return type; + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/ProvidedRequiredArguments.js b/dist/validation/rules/ProvidedRequiredArguments.js new file mode 100644 index 0000000000..f1f3ec5016 --- /dev/null +++ b/dist/validation/rules/ProvidedRequiredArguments.js @@ -0,0 +1,88 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.missingFieldArgMessage = missingFieldArgMessage; +exports.missingDirectiveArgMessage = missingDirectiveArgMessage; +exports.ProvidedRequiredArguments = ProvidedRequiredArguments; + +var _error = require("../../error"); + +var _keyMap = _interopRequireDefault(require("../../jsutils/keyMap")); + +var _definition = require("../../type/definition"); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function missingFieldArgMessage(fieldName, argName, type) { + return "Field \"".concat(fieldName, "\" argument \"").concat(argName, "\" of type ") + "\"".concat(String(type), "\" is required but not provided."); +} + +function missingDirectiveArgMessage(directiveName, argName, type) { + return "Directive \"@".concat(directiveName, "\" argument \"").concat(argName, "\" of type ") + "\"".concat(String(type), "\" is required but not provided."); +} +/** + * Provided required arguments + * + * A field or directive is only valid if all required (non-null without a + * default value) field arguments have been provided. + */ + + +function ProvidedRequiredArguments(context) { + return { + Field: { + // Validate on leave to allow for deeper errors to appear first. + leave: function leave(node) { + var fieldDef = context.getFieldDef(); + + if (!fieldDef) { + return false; + } + + var argNodes = node.arguments || []; + var argNodeMap = (0, _keyMap.default)(argNodes, function (arg) { + return arg.name.value; + }); + fieldDef.args.forEach(function (argDef) { + var argNode = argNodeMap[argDef.name]; + + if (!argNode && (0, _definition.isNonNullType)(argDef.type) && argDef.defaultValue === undefined) { + context.reportError(new _error.GraphQLError(missingFieldArgMessage(node.name.value, argDef.name, argDef.type), [node])); + } + }); + } + }, + Directive: { + // Validate on leave to allow for deeper errors to appear first. + leave: function leave(node) { + var directiveDef = context.getDirective(); + + if (!directiveDef) { + return false; + } + + var argNodes = node.arguments || []; + var argNodeMap = (0, _keyMap.default)(argNodes, function (arg) { + return arg.name.value; + }); + directiveDef.args.forEach(function (argDef) { + var argNode = argNodeMap[argDef.name]; + + if (!argNode && (0, _definition.isNonNullType)(argDef.type) && argDef.defaultValue === undefined) { + context.reportError(new _error.GraphQLError(missingDirectiveArgMessage(node.name.value, argDef.name, argDef.type), [node])); + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/ProvidedRequiredArguments.js.flow b/dist/validation/rules/ProvidedRequiredArguments.js.flow new file mode 100644 index 0000000000..4819e54592 --- /dev/null +++ b/dist/validation/rules/ProvidedRequiredArguments.js.flow @@ -0,0 +1,113 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import keyMap from '../../jsutils/keyMap'; +import { isNonNullType } from '../../type/definition'; +import type { GraphQLType } from '../../type/definition'; +import type { ASTVisitor } from '../../language/visitor'; + +export function missingFieldArgMessage( + fieldName: string, + argName: string, + type: GraphQLType, +): string { + return ( + `Field "${fieldName}" argument "${argName}" of type ` + + `"${String(type)}" is required but not provided.` + ); +} + +export function missingDirectiveArgMessage( + directiveName: string, + argName: string, + type: GraphQLType, +): string { + return ( + `Directive "@${directiveName}" argument "${argName}" of type ` + + `"${String(type)}" is required but not provided.` + ); +} + +/** + * Provided required arguments + * + * A field or directive is only valid if all required (non-null without a + * default value) field arguments have been provided. + */ +export function ProvidedRequiredArguments( + context: ValidationContext, +): ASTVisitor { + return { + Field: { + // Validate on leave to allow for deeper errors to appear first. + leave(node) { + const fieldDef = context.getFieldDef(); + if (!fieldDef) { + return false; + } + const argNodes = node.arguments || []; + + const argNodeMap = keyMap(argNodes, arg => arg.name.value); + fieldDef.args.forEach(argDef => { + const argNode = argNodeMap[argDef.name]; + if ( + !argNode && + isNonNullType(argDef.type) && + argDef.defaultValue === undefined + ) { + context.reportError( + new GraphQLError( + missingFieldArgMessage( + node.name.value, + argDef.name, + argDef.type, + ), + [node], + ), + ); + } + }); + }, + }, + + Directive: { + // Validate on leave to allow for deeper errors to appear first. + leave(node) { + const directiveDef = context.getDirective(); + if (!directiveDef) { + return false; + } + const argNodes = node.arguments || []; + + const argNodeMap = keyMap(argNodes, arg => arg.name.value); + directiveDef.args.forEach(argDef => { + const argNode = argNodeMap[argDef.name]; + if ( + !argNode && + isNonNullType(argDef.type) && + argDef.defaultValue === undefined + ) { + context.reportError( + new GraphQLError( + missingDirectiveArgMessage( + node.name.value, + argDef.name, + argDef.type, + ), + [node], + ), + ); + } + }); + }, + }, + }; +} diff --git a/dist/validation/rules/ProvidedRequiredArguments.mjs b/dist/validation/rules/ProvidedRequiredArguments.mjs new file mode 100644 index 0000000000..490a945f64 --- /dev/null +++ b/dist/validation/rules/ProvidedRequiredArguments.mjs @@ -0,0 +1,72 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import keyMap from '../../jsutils/keyMap'; +import { isNonNullType } from '../../type/definition'; +export function missingFieldArgMessage(fieldName, argName, type) { + return "Field \"".concat(fieldName, "\" argument \"").concat(argName, "\" of type ") + "\"".concat(String(type), "\" is required but not provided."); +} +export function missingDirectiveArgMessage(directiveName, argName, type) { + return "Directive \"@".concat(directiveName, "\" argument \"").concat(argName, "\" of type ") + "\"".concat(String(type), "\" is required but not provided."); +} +/** + * Provided required arguments + * + * A field or directive is only valid if all required (non-null without a + * default value) field arguments have been provided. + */ + +export function ProvidedRequiredArguments(context) { + return { + Field: { + // Validate on leave to allow for deeper errors to appear first. + leave: function leave(node) { + var fieldDef = context.getFieldDef(); + + if (!fieldDef) { + return false; + } + + var argNodes = node.arguments || []; + var argNodeMap = keyMap(argNodes, function (arg) { + return arg.name.value; + }); + fieldDef.args.forEach(function (argDef) { + var argNode = argNodeMap[argDef.name]; + + if (!argNode && isNonNullType(argDef.type) && argDef.defaultValue === undefined) { + context.reportError(new GraphQLError(missingFieldArgMessage(node.name.value, argDef.name, argDef.type), [node])); + } + }); + } + }, + Directive: { + // Validate on leave to allow for deeper errors to appear first. + leave: function leave(node) { + var directiveDef = context.getDirective(); + + if (!directiveDef) { + return false; + } + + var argNodes = node.arguments || []; + var argNodeMap = keyMap(argNodes, function (arg) { + return arg.name.value; + }); + directiveDef.args.forEach(function (argDef) { + var argNode = argNodeMap[argDef.name]; + + if (!argNode && isNonNullType(argDef.type) && argDef.defaultValue === undefined) { + context.reportError(new GraphQLError(missingDirectiveArgMessage(node.name.value, argDef.name, argDef.type), [node])); + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/ScalarLeafs.js b/dist/validation/rules/ScalarLeafs.js new file mode 100644 index 0000000000..9fcb021260 --- /dev/null +++ b/dist/validation/rules/ScalarLeafs.js @@ -0,0 +1,54 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.noSubselectionAllowedMessage = noSubselectionAllowedMessage; +exports.requiredSubselectionMessage = requiredSubselectionMessage; +exports.ScalarLeafs = ScalarLeafs; + +var _error = require("../../error"); + +var _definition = require("../../type/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. + * + * strict + */ +function noSubselectionAllowedMessage(fieldName, type) { + return "Field \"".concat(fieldName, "\" must not have a selection since ") + "type \"".concat(String(type), "\" has no subfields."); +} + +function requiredSubselectionMessage(fieldName, type) { + return "Field \"".concat(fieldName, "\" of type \"").concat(String(type), "\" must have a ") + "selection of subfields. Did you mean \"".concat(fieldName, " { ... }\"?"); +} +/** + * Scalar leafs + * + * A GraphQL document is valid only if all leaf fields (fields without + * sub selections) are of scalar or enum types. + */ + + +function ScalarLeafs(context) { + return { + Field: function Field(node) { + var type = context.getType(); + var selectionSet = node.selectionSet; + + if (type) { + if ((0, _definition.isLeafType)((0, _definition.getNamedType)(type))) { + if (selectionSet) { + context.reportError(new _error.GraphQLError(noSubselectionAllowedMessage(node.name.value, type), [selectionSet])); + } + } else if (!selectionSet) { + context.reportError(new _error.GraphQLError(requiredSubselectionMessage(node.name.value, type), [node])); + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/ScalarLeafs.js.flow b/dist/validation/rules/ScalarLeafs.js.flow new file mode 100644 index 0000000000..8f2291c3c3 --- /dev/null +++ b/dist/validation/rules/ScalarLeafs.js.flow @@ -0,0 +1,69 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { FieldNode } from '../../language/ast'; +import { getNamedType, isLeafType } from '../../type/definition'; +import type { GraphQLType } from '../../type/definition'; +import type { ASTVisitor } from '../../language/visitor'; + +export function noSubselectionAllowedMessage( + fieldName: string, + type: GraphQLType, +): string { + return ( + `Field "${fieldName}" must not have a selection since ` + + `type "${String(type)}" has no subfields.` + ); +} + +export function requiredSubselectionMessage( + fieldName: string, + type: GraphQLType, +): string { + return ( + `Field "${fieldName}" of type "${String(type)}" must have a ` + + `selection of subfields. Did you mean "${fieldName} { ... }"?` + ); +} + +/** + * Scalar leafs + * + * A GraphQL document is valid only if all leaf fields (fields without + * sub selections) are of scalar or enum types. + */ +export function ScalarLeafs(context: ValidationContext): ASTVisitor { + return { + Field(node: FieldNode) { + const type = context.getType(); + const selectionSet = node.selectionSet; + if (type) { + if (isLeafType(getNamedType(type))) { + if (selectionSet) { + context.reportError( + new GraphQLError( + noSubselectionAllowedMessage(node.name.value, type), + [selectionSet], + ), + ); + } + } else if (!selectionSet) { + context.reportError( + new GraphQLError( + requiredSubselectionMessage(node.name.value, type), + [node], + ), + ); + } + } + }, + }; +} diff --git a/dist/validation/rules/ScalarLeafs.mjs b/dist/validation/rules/ScalarLeafs.mjs new file mode 100644 index 0000000000..20e9622806 --- /dev/null +++ b/dist/validation/rules/ScalarLeafs.mjs @@ -0,0 +1,41 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { getNamedType, isLeafType } from '../../type/definition'; +export function noSubselectionAllowedMessage(fieldName, type) { + return "Field \"".concat(fieldName, "\" must not have a selection since ") + "type \"".concat(String(type), "\" has no subfields."); +} +export function requiredSubselectionMessage(fieldName, type) { + return "Field \"".concat(fieldName, "\" of type \"").concat(String(type), "\" must have a ") + "selection of subfields. Did you mean \"".concat(fieldName, " { ... }\"?"); +} +/** + * Scalar leafs + * + * A GraphQL document is valid only if all leaf fields (fields without + * sub selections) are of scalar or enum types. + */ + +export function ScalarLeafs(context) { + return { + Field: function Field(node) { + var type = context.getType(); + var selectionSet = node.selectionSet; + + if (type) { + if (isLeafType(getNamedType(type))) { + if (selectionSet) { + context.reportError(new GraphQLError(noSubselectionAllowedMessage(node.name.value, type), [selectionSet])); + } + } else if (!selectionSet) { + context.reportError(new GraphQLError(requiredSubselectionMessage(node.name.value, type), [node])); + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/SingleFieldSubscriptions.js b/dist/validation/rules/SingleFieldSubscriptions.js new file mode 100644 index 0000000000..5c903c2b05 --- /dev/null +++ b/dist/validation/rules/SingleFieldSubscriptions.js @@ -0,0 +1,39 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.singleFieldOnlyMessage = singleFieldOnlyMessage; +exports.SingleFieldSubscriptions = SingleFieldSubscriptions; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function singleFieldOnlyMessage(name) { + return (name ? "Subscription \"".concat(name, "\" ") : 'Anonymous Subscription ') + 'must select only one top level field.'; +} +/** + * Subscriptions must only include one field. + * + * A GraphQL subscription is valid only if it contains a single root field. + */ + + +function SingleFieldSubscriptions(context) { + return { + OperationDefinition: function OperationDefinition(node) { + if (node.operation === 'subscription') { + if (node.selectionSet.selections.length !== 1) { + context.reportError(new _error.GraphQLError(singleFieldOnlyMessage(node.name && node.name.value), node.selectionSet.selections.slice(1))); + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/SingleFieldSubscriptions.js.flow b/dist/validation/rules/SingleFieldSubscriptions.js.flow new file mode 100644 index 0000000000..622e6fc5f8 --- /dev/null +++ b/dist/validation/rules/SingleFieldSubscriptions.js.flow @@ -0,0 +1,44 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { OperationDefinitionNode } from '../../language/ast'; +import type { ASTVisitor } from '../../language/visitor'; + +export function singleFieldOnlyMessage(name: ?string): string { + return ( + (name ? `Subscription "${name}" ` : 'Anonymous Subscription ') + + 'must select only one top level field.' + ); +} + +/** + * Subscriptions must only include one field. + * + * A GraphQL subscription is valid only if it contains a single root field. + */ +export function SingleFieldSubscriptions( + context: ValidationContext, +): ASTVisitor { + return { + OperationDefinition(node: OperationDefinitionNode) { + if (node.operation === 'subscription') { + if (node.selectionSet.selections.length !== 1) { + context.reportError( + new GraphQLError( + singleFieldOnlyMessage(node.name && node.name.value), + node.selectionSet.selections.slice(1), + ), + ); + } + } + }, + }; +} diff --git a/dist/validation/rules/SingleFieldSubscriptions.mjs b/dist/validation/rules/SingleFieldSubscriptions.mjs new file mode 100644 index 0000000000..d7e02fac85 --- /dev/null +++ b/dist/validation/rules/SingleFieldSubscriptions.mjs @@ -0,0 +1,29 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function singleFieldOnlyMessage(name) { + return (name ? "Subscription \"".concat(name, "\" ") : 'Anonymous Subscription ') + 'must select only one top level field.'; +} +/** + * Subscriptions must only include one field. + * + * A GraphQL subscription is valid only if it contains a single root field. + */ + +export function SingleFieldSubscriptions(context) { + return { + OperationDefinition: function OperationDefinition(node) { + if (node.operation === 'subscription') { + if (node.selectionSet.selections.length !== 1) { + context.reportError(new GraphQLError(singleFieldOnlyMessage(node.name && node.name.value), node.selectionSet.selections.slice(1))); + } + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueArgumentNames.js b/dist/validation/rules/UniqueArgumentNames.js new file mode 100644 index 0000000000..ef70d2a51b --- /dev/null +++ b/dist/validation/rules/UniqueArgumentNames.js @@ -0,0 +1,51 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateArgMessage = duplicateArgMessage; +exports.UniqueArgumentNames = UniqueArgumentNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateArgMessage(argName) { + return "There can be only one argument named \"".concat(argName, "\"."); +} +/** + * Unique argument names + * + * A GraphQL field or directive is only valid if all supplied arguments are + * uniquely named. + */ + + +function UniqueArgumentNames(context) { + var knownArgNames = Object.create(null); + return { + Field: function Field() { + knownArgNames = Object.create(null); + }, + Directive: function Directive() { + knownArgNames = Object.create(null); + }, + Argument: function Argument(node) { + var argName = node.name.value; + + if (knownArgNames[argName]) { + context.reportError(new _error.GraphQLError(duplicateArgMessage(argName), [knownArgNames[argName], node.name])); + } else { + knownArgNames[argName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueArgumentNames.js.flow b/dist/validation/rules/UniqueArgumentNames.js.flow new file mode 100644 index 0000000000..0516401079 --- /dev/null +++ b/dist/validation/rules/UniqueArgumentNames.js.flow @@ -0,0 +1,48 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateArgMessage(argName: string): string { + return `There can be only one argument named "${argName}".`; +} + +/** + * Unique argument names + * + * A GraphQL field or directive is only valid if all supplied arguments are + * uniquely named. + */ +export function UniqueArgumentNames(context: ValidationContext): ASTVisitor { + let knownArgNames = Object.create(null); + return { + Field() { + knownArgNames = Object.create(null); + }, + Directive() { + knownArgNames = Object.create(null); + }, + Argument(node) { + const argName = node.name.value; + if (knownArgNames[argName]) { + context.reportError( + new GraphQLError(duplicateArgMessage(argName), [ + knownArgNames[argName], + node.name, + ]), + ); + } else { + knownArgNames[argName] = node.name; + } + return false; + }, + }; +} diff --git a/dist/validation/rules/UniqueArgumentNames.mjs b/dist/validation/rules/UniqueArgumentNames.mjs new file mode 100644 index 0000000000..a872bfde86 --- /dev/null +++ b/dist/validation/rules/UniqueArgumentNames.mjs @@ -0,0 +1,41 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateArgMessage(argName) { + return "There can be only one argument named \"".concat(argName, "\"."); +} +/** + * Unique argument names + * + * A GraphQL field or directive is only valid if all supplied arguments are + * uniquely named. + */ + +export function UniqueArgumentNames(context) { + var knownArgNames = Object.create(null); + return { + Field: function Field() { + knownArgNames = Object.create(null); + }, + Directive: function Directive() { + knownArgNames = Object.create(null); + }, + Argument: function Argument(node) { + var argName = node.name.value; + + if (knownArgNames[argName]) { + context.reportError(new GraphQLError(duplicateArgMessage(argName), [knownArgNames[argName], node.name])); + } else { + knownArgNames[argName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueDirectivesPerLocation.js b/dist/validation/rules/UniqueDirectivesPerLocation.js new file mode 100644 index 0000000000..8647b4a9ce --- /dev/null +++ b/dist/validation/rules/UniqueDirectivesPerLocation.js @@ -0,0 +1,53 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateDirectiveMessage = duplicateDirectiveMessage; +exports.UniqueDirectivesPerLocation = UniqueDirectivesPerLocation; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateDirectiveMessage(directiveName) { + return "The directive \"".concat(directiveName, "\" can only be used once at ") + 'this location.'; +} +/** + * Unique directive names per location + * + * A GraphQL document is only valid if all directives at a given location + * are uniquely named. + */ + + +function UniqueDirectivesPerLocation(context) { + return { + // Many different AST nodes may contain directives. Rather than listing + // them all, just listen for entering any node, and check to see if it + // defines any directives. + enter: function enter(node) { + // Flow can't refine that node.directives will only contain directives, + var directives = node.directives; + + if (directives) { + var knownDirectives = Object.create(null); + directives.forEach(function (directive) { + var directiveName = directive.name.value; + + if (knownDirectives[directiveName]) { + context.reportError(new _error.GraphQLError(duplicateDirectiveMessage(directiveName), [knownDirectives[directiveName], directive])); + } else { + knownDirectives[directiveName] = directive; + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueDirectivesPerLocation.js.flow b/dist/validation/rules/UniqueDirectivesPerLocation.js.flow new file mode 100644 index 0000000000..fb143d2a15 --- /dev/null +++ b/dist/validation/rules/UniqueDirectivesPerLocation.js.flow @@ -0,0 +1,57 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { DirectiveNode } from '../../language/ast'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateDirectiveMessage(directiveName: string): string { + return ( + `The directive "${directiveName}" can only be used once at ` + + 'this location.' + ); +} + +/** + * Unique directive names per location + * + * A GraphQL document is only valid if all directives at a given location + * are uniquely named. + */ +export function UniqueDirectivesPerLocation( + context: ValidationContext, +): ASTVisitor { + return { + // Many different AST nodes may contain directives. Rather than listing + // them all, just listen for entering any node, and check to see if it + // defines any directives. + enter(node) { + // Flow can't refine that node.directives will only contain directives, + // so we cast so the rest of the code is well typed. + const directives: ?$ReadOnlyArray = (node: any).directives; + if (directives) { + const knownDirectives = Object.create(null); + directives.forEach(directive => { + const directiveName = directive.name.value; + if (knownDirectives[directiveName]) { + context.reportError( + new GraphQLError(duplicateDirectiveMessage(directiveName), [ + knownDirectives[directiveName], + directive, + ]), + ); + } else { + knownDirectives[directiveName] = directive; + } + }); + } + }, + }; +} diff --git a/dist/validation/rules/UniqueDirectivesPerLocation.mjs b/dist/validation/rules/UniqueDirectivesPerLocation.mjs new file mode 100644 index 0000000000..0f451d3932 --- /dev/null +++ b/dist/validation/rules/UniqueDirectivesPerLocation.mjs @@ -0,0 +1,43 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateDirectiveMessage(directiveName) { + return "The directive \"".concat(directiveName, "\" can only be used once at ") + 'this location.'; +} +/** + * Unique directive names per location + * + * A GraphQL document is only valid if all directives at a given location + * are uniquely named. + */ + +export function UniqueDirectivesPerLocation(context) { + return { + // Many different AST nodes may contain directives. Rather than listing + // them all, just listen for entering any node, and check to see if it + // defines any directives. + enter: function enter(node) { + // Flow can't refine that node.directives will only contain directives, + var directives = node.directives; + + if (directives) { + var knownDirectives = Object.create(null); + directives.forEach(function (directive) { + var directiveName = directive.name.value; + + if (knownDirectives[directiveName]) { + context.reportError(new GraphQLError(duplicateDirectiveMessage(directiveName), [knownDirectives[directiveName], directive])); + } else { + knownDirectives[directiveName] = directive; + } + }); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueFragmentNames.js b/dist/validation/rules/UniqueFragmentNames.js new file mode 100644 index 0000000000..adda7d35ab --- /dev/null +++ b/dist/validation/rules/UniqueFragmentNames.js @@ -0,0 +1,47 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateFragmentNameMessage = duplicateFragmentNameMessage; +exports.UniqueFragmentNames = UniqueFragmentNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateFragmentNameMessage(fragName) { + return "There can be only one fragment named \"".concat(fragName, "\"."); +} +/** + * Unique fragment names + * + * A GraphQL document is only valid if all defined fragments have unique names. + */ + + +function UniqueFragmentNames(context) { + var knownFragmentNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + var fragmentName = node.name.value; + + if (knownFragmentNames[fragmentName]) { + context.reportError(new _error.GraphQLError(duplicateFragmentNameMessage(fragmentName), [knownFragmentNames[fragmentName], node.name])); + } else { + knownFragmentNames[fragmentName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueFragmentNames.js.flow b/dist/validation/rules/UniqueFragmentNames.js.flow new file mode 100644 index 0000000000..1a55cd7967 --- /dev/null +++ b/dist/validation/rules/UniqueFragmentNames.js.flow @@ -0,0 +1,42 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateFragmentNameMessage(fragName: string): string { + return `There can be only one fragment named "${fragName}".`; +} + +/** + * Unique fragment names + * + * A GraphQL document is only valid if all defined fragments have unique names. + */ +export function UniqueFragmentNames(context: ValidationContext): ASTVisitor { + const knownFragmentNames = Object.create(null); + return { + OperationDefinition: () => false, + FragmentDefinition(node) { + const fragmentName = node.name.value; + if (knownFragmentNames[fragmentName]) { + context.reportError( + new GraphQLError(duplicateFragmentNameMessage(fragmentName), [ + knownFragmentNames[fragmentName], + node.name, + ]), + ); + } else { + knownFragmentNames[fragmentName] = node.name; + } + return false; + }, + }; +} diff --git a/dist/validation/rules/UniqueFragmentNames.mjs b/dist/validation/rules/UniqueFragmentNames.mjs new file mode 100644 index 0000000000..9b766db244 --- /dev/null +++ b/dist/validation/rules/UniqueFragmentNames.mjs @@ -0,0 +1,37 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateFragmentNameMessage(fragName) { + return "There can be only one fragment named \"".concat(fragName, "\"."); +} +/** + * Unique fragment names + * + * A GraphQL document is only valid if all defined fragments have unique names. + */ + +export function UniqueFragmentNames(context) { + var knownFragmentNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + return false; + }, + FragmentDefinition: function FragmentDefinition(node) { + var fragmentName = node.name.value; + + if (knownFragmentNames[fragmentName]) { + context.reportError(new GraphQLError(duplicateFragmentNameMessage(fragmentName), [knownFragmentNames[fragmentName], node.name])); + } else { + knownFragmentNames[fragmentName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueInputFieldNames.js b/dist/validation/rules/UniqueInputFieldNames.js new file mode 100644 index 0000000000..aa0d0fa949 --- /dev/null +++ b/dist/validation/rules/UniqueInputFieldNames.js @@ -0,0 +1,55 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateInputFieldMessage = duplicateInputFieldMessage; +exports.UniqueInputFieldNames = UniqueInputFieldNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateInputFieldMessage(fieldName) { + return "There can be only one input field named \"".concat(fieldName, "\"."); +} +/** + * Unique input field names + * + * A GraphQL input object value is only valid if all supplied fields are + * uniquely named. + */ + + +function UniqueInputFieldNames(context) { + var knownNameStack = []; + var knownNames = Object.create(null); + return { + ObjectValue: { + enter: function enter() { + knownNameStack.push(knownNames); + knownNames = Object.create(null); + }, + leave: function leave() { + knownNames = knownNameStack.pop(); + } + }, + ObjectField: function ObjectField(node) { + var fieldName = node.name.value; + + if (knownNames[fieldName]) { + context.reportError(new _error.GraphQLError(duplicateInputFieldMessage(fieldName), [knownNames[fieldName], node.name])); + } else { + knownNames[fieldName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueInputFieldNames.js.flow b/dist/validation/rules/UniqueInputFieldNames.js.flow new file mode 100644 index 0000000000..9290b60305 --- /dev/null +++ b/dist/validation/rules/UniqueInputFieldNames.js.flow @@ -0,0 +1,53 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateInputFieldMessage(fieldName: string): string { + return `There can be only one input field named "${fieldName}".`; +} + +/** + * Unique input field names + * + * A GraphQL input object value is only valid if all supplied fields are + * uniquely named. + */ +export function UniqueInputFieldNames(context: ValidationContext): ASTVisitor { + const knownNameStack = []; + let knownNames = Object.create(null); + + return { + ObjectValue: { + enter() { + knownNameStack.push(knownNames); + knownNames = Object.create(null); + }, + leave() { + knownNames = knownNameStack.pop(); + }, + }, + ObjectField(node) { + const fieldName = node.name.value; + if (knownNames[fieldName]) { + context.reportError( + new GraphQLError(duplicateInputFieldMessage(fieldName), [ + knownNames[fieldName], + node.name, + ]), + ); + } else { + knownNames[fieldName] = node.name; + } + return false; + }, + }; +} diff --git a/dist/validation/rules/UniqueInputFieldNames.mjs b/dist/validation/rules/UniqueInputFieldNames.mjs new file mode 100644 index 0000000000..d4f69029a5 --- /dev/null +++ b/dist/validation/rules/UniqueInputFieldNames.mjs @@ -0,0 +1,45 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateInputFieldMessage(fieldName) { + return "There can be only one input field named \"".concat(fieldName, "\"."); +} +/** + * Unique input field names + * + * A GraphQL input object value is only valid if all supplied fields are + * uniquely named. + */ + +export function UniqueInputFieldNames(context) { + var knownNameStack = []; + var knownNames = Object.create(null); + return { + ObjectValue: { + enter: function enter() { + knownNameStack.push(knownNames); + knownNames = Object.create(null); + }, + leave: function leave() { + knownNames = knownNameStack.pop(); + } + }, + ObjectField: function ObjectField(node) { + var fieldName = node.name.value; + + if (knownNames[fieldName]) { + context.reportError(new GraphQLError(duplicateInputFieldMessage(fieldName), [knownNames[fieldName], node.name])); + } else { + knownNames[fieldName] = node.name; + } + + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueOperationNames.js b/dist/validation/rules/UniqueOperationNames.js new file mode 100644 index 0000000000..63f0389aec --- /dev/null +++ b/dist/validation/rules/UniqueOperationNames.js @@ -0,0 +1,49 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateOperationNameMessage = duplicateOperationNameMessage; +exports.UniqueOperationNames = UniqueOperationNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateOperationNameMessage(operationName) { + return "There can be only one operation named \"".concat(operationName, "\"."); +} +/** + * Unique operation names + * + * A GraphQL document is only valid if all defined operations have unique names. + */ + + +function UniqueOperationNames(context) { + var knownOperationNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition(node) { + var operationName = node.name; + + if (operationName) { + if (knownOperationNames[operationName.value]) { + context.reportError(new _error.GraphQLError(duplicateOperationNameMessage(operationName.value), [knownOperationNames[operationName.value], operationName])); + } else { + knownOperationNames[operationName.value] = operationName; + } + } + + return false; + }, + FragmentDefinition: function FragmentDefinition() { + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueOperationNames.js.flow b/dist/validation/rules/UniqueOperationNames.js.flow new file mode 100644 index 0000000000..765b0b80f4 --- /dev/null +++ b/dist/validation/rules/UniqueOperationNames.js.flow @@ -0,0 +1,44 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateOperationNameMessage(operationName: string): string { + return `There can be only one operation named "${operationName}".`; +} + +/** + * Unique operation names + * + * A GraphQL document is only valid if all defined operations have unique names. + */ +export function UniqueOperationNames(context: ValidationContext): ASTVisitor { + const knownOperationNames = Object.create(null); + return { + OperationDefinition(node) { + const operationName = node.name; + if (operationName) { + if (knownOperationNames[operationName.value]) { + context.reportError( + new GraphQLError( + duplicateOperationNameMessage(operationName.value), + [knownOperationNames[operationName.value], operationName], + ), + ); + } else { + knownOperationNames[operationName.value] = operationName; + } + } + return false; + }, + FragmentDefinition: () => false, + }; +} diff --git a/dist/validation/rules/UniqueOperationNames.mjs b/dist/validation/rules/UniqueOperationNames.mjs new file mode 100644 index 0000000000..ddaa6ad32e --- /dev/null +++ b/dist/validation/rules/UniqueOperationNames.mjs @@ -0,0 +1,39 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateOperationNameMessage(operationName) { + return "There can be only one operation named \"".concat(operationName, "\"."); +} +/** + * Unique operation names + * + * A GraphQL document is only valid if all defined operations have unique names. + */ + +export function UniqueOperationNames(context) { + var knownOperationNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition(node) { + var operationName = node.name; + + if (operationName) { + if (knownOperationNames[operationName.value]) { + context.reportError(new GraphQLError(duplicateOperationNameMessage(operationName.value), [knownOperationNames[operationName.value], operationName])); + } else { + knownOperationNames[operationName.value] = operationName; + } + } + + return false; + }, + FragmentDefinition: function FragmentDefinition() { + return false; + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueVariableNames.js b/dist/validation/rules/UniqueVariableNames.js new file mode 100644 index 0000000000..0935b15e6c --- /dev/null +++ b/dist/validation/rules/UniqueVariableNames.js @@ -0,0 +1,45 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.duplicateVariableMessage = duplicateVariableMessage; +exports.UniqueVariableNames = UniqueVariableNames; + +var _error = require("../../error"); + +/** + * 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. + * + * strict + */ +function duplicateVariableMessage(variableName) { + return "There can be only one variable named \"".concat(variableName, "\"."); +} +/** + * Unique variable names + * + * A GraphQL operation is only valid if all its variables are uniquely named. + */ + + +function UniqueVariableNames(context) { + var knownVariableNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + knownVariableNames = Object.create(null); + }, + VariableDefinition: function VariableDefinition(node) { + var variableName = node.variable.name.value; + + if (knownVariableNames[variableName]) { + context.reportError(new _error.GraphQLError(duplicateVariableMessage(variableName), [knownVariableNames[variableName], node.variable.name])); + } else { + knownVariableNames[variableName] = node.variable.name; + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/UniqueVariableNames.js.flow b/dist/validation/rules/UniqueVariableNames.js.flow new file mode 100644 index 0000000000..90f84483ec --- /dev/null +++ b/dist/validation/rules/UniqueVariableNames.js.flow @@ -0,0 +1,44 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import type { VariableDefinitionNode } from '../../language/ast'; +import { GraphQLError } from '../../error'; +import type { ASTVisitor } from '../../language/visitor'; + +export function duplicateVariableMessage(variableName: string): string { + return `There can be only one variable named "${variableName}".`; +} + +/** + * Unique variable names + * + * A GraphQL operation is only valid if all its variables are uniquely named. + */ +export function UniqueVariableNames(context: ValidationContext): ASTVisitor { + let knownVariableNames = Object.create(null); + return { + OperationDefinition() { + knownVariableNames = Object.create(null); + }, + VariableDefinition(node: VariableDefinitionNode) { + const variableName = node.variable.name.value; + if (knownVariableNames[variableName]) { + context.reportError( + new GraphQLError(duplicateVariableMessage(variableName), [ + knownVariableNames[variableName], + node.variable.name, + ]), + ); + } else { + knownVariableNames[variableName] = node.variable.name; + } + }, + }; +} diff --git a/dist/validation/rules/UniqueVariableNames.mjs b/dist/validation/rules/UniqueVariableNames.mjs new file mode 100644 index 0000000000..14c9f1f386 --- /dev/null +++ b/dist/validation/rules/UniqueVariableNames.mjs @@ -0,0 +1,35 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +export function duplicateVariableMessage(variableName) { + return "There can be only one variable named \"".concat(variableName, "\"."); +} +/** + * Unique variable names + * + * A GraphQL operation is only valid if all its variables are uniquely named. + */ + +export function UniqueVariableNames(context) { + var knownVariableNames = Object.create(null); + return { + OperationDefinition: function OperationDefinition() { + knownVariableNames = Object.create(null); + }, + VariableDefinition: function VariableDefinition(node) { + var variableName = node.variable.name.value; + + if (knownVariableNames[variableName]) { + context.reportError(new GraphQLError(duplicateVariableMessage(variableName), [knownVariableNames[variableName], node.variable.name])); + } else { + knownVariableNames[variableName] = node.variable.name; + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/ValuesOfCorrectType.js b/dist/validation/rules/ValuesOfCorrectType.js new file mode 100644 index 0000000000..c0e2386295 --- /dev/null +++ b/dist/validation/rules/ValuesOfCorrectType.js @@ -0,0 +1,176 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.badValueMessage = badValueMessage; +exports.requiredFieldMessage = requiredFieldMessage; +exports.unknownFieldMessage = unknownFieldMessage; +exports.ValuesOfCorrectType = ValuesOfCorrectType; + +var _error = require("../../error"); + +var _printer = require("../../language/printer"); + +var _definition = require("../../type/definition"); + +var _isInvalid = _interopRequireDefault(require("../../jsutils/isInvalid")); + +var _keyMap = _interopRequireDefault(require("../../jsutils/keyMap")); + +var _orList = _interopRequireDefault(require("../../jsutils/orList")); + +var _suggestionList = _interopRequireDefault(require("../../jsutils/suggestionList")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ +function badValueMessage(typeName, valueName, message) { + return "Expected type ".concat(typeName, ", found ").concat(valueName) + (message ? "; ".concat(message) : '.'); +} + +function requiredFieldMessage(typeName, fieldName, fieldTypeName) { + return "Field ".concat(typeName, ".").concat(fieldName, " of required type ") + "".concat(fieldTypeName, " was not provided."); +} + +function unknownFieldMessage(typeName, fieldName, message) { + return "Field \"".concat(fieldName, "\" is not defined by type ").concat(typeName) + (message ? "; ".concat(message) : '.'); +} +/** + * Value literals of correct type + * + * A GraphQL document is only valid if all value literals are of the type + * expected at their position. + */ + + +function ValuesOfCorrectType(context) { + return { + NullValue: function NullValue(node) { + var type = context.getInputType(); + + if ((0, _definition.isNonNullType)(type)) { + context.reportError(new _error.GraphQLError(badValueMessage(String(type), (0, _printer.print)(node)), node)); + } + }, + ListValue: function ListValue(node) { + // Note: TypeInfo will traverse into a list's item type, so look to the + // parent input type to check if it is a list. + var type = (0, _definition.getNullableType)(context.getParentInputType()); + + if (!(0, _definition.isListType)(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } + }, + ObjectValue: function ObjectValue(node) { + var type = (0, _definition.getNamedType)(context.getInputType()); + + if (!(0, _definition.isInputObjectType)(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } // Ensure every required field exists. + + + var inputFields = type.getFields(); + var fieldNodeMap = (0, _keyMap.default)(node.fields, function (field) { + return field.name.value; + }); + Object.keys(inputFields).forEach(function (fieldName) { + var fieldDef = inputFields[fieldName]; + var fieldType = fieldDef.type; + var fieldNode = fieldNodeMap[fieldName]; + + if (!fieldNode && (0, _definition.isNonNullType)(fieldType) && fieldDef.defaultValue === undefined) { + context.reportError(new _error.GraphQLError(requiredFieldMessage(type.name, fieldName, String(fieldType)), node)); + } + }); + }, + ObjectField: function ObjectField(node) { + var parentType = (0, _definition.getNamedType)(context.getParentInputType()); + var fieldType = context.getInputType(); + + if (!fieldType && (0, _definition.isInputObjectType)(parentType)) { + var suggestions = (0, _suggestionList.default)(node.name.value, Object.keys(parentType.getFields())); + var didYouMean = suggestions.length !== 0 ? "Did you mean ".concat((0, _orList.default)(suggestions), "?") : undefined; + context.reportError(new _error.GraphQLError(unknownFieldMessage(parentType.name, node.name.value, didYouMean), node)); + } + }, + EnumValue: function EnumValue(node) { + var type = (0, _definition.getNamedType)(context.getInputType()); + + if (!(0, _definition.isEnumType)(type)) { + isValidScalar(context, node); + } else if (!type.getValue(node.value)) { + context.reportError(new _error.GraphQLError(badValueMessage(type.name, (0, _printer.print)(node), enumTypeSuggestion(type, node)), node)); + } + }, + IntValue: function IntValue(node) { + return isValidScalar(context, node); + }, + FloatValue: function FloatValue(node) { + return isValidScalar(context, node); + }, + StringValue: function StringValue(node) { + return isValidScalar(context, node); + }, + BooleanValue: function BooleanValue(node) { + return isValidScalar(context, node); + } + }; +} +/** + * Any value literal may be a valid representation of a Scalar, depending on + * that scalar type. + */ + + +function isValidScalar(context, node) { + // Report any error at the full type expected by the location. + var locationType = context.getInputType(); + + if (!locationType) { + return; + } + + var type = (0, _definition.getNamedType)(locationType); + + if (!(0, _definition.isScalarType)(type)) { + context.reportError(new _error.GraphQLError(badValueMessage(String(locationType), (0, _printer.print)(node), enumTypeSuggestion(type, node)), node)); + return; + } // Scalars determine if a literal value is valid via parseLiteral() which + // may throw or return an invalid value to indicate failure. + + + try { + var parseResult = type.parseLiteral(node, undefined + /* variables */ + ); + + if ((0, _isInvalid.default)(parseResult)) { + context.reportError(new _error.GraphQLError(badValueMessage(String(locationType), (0, _printer.print)(node)), node)); + } + } catch (error) { + // Ensure a reference to the original error is maintained. + context.reportError(new _error.GraphQLError(badValueMessage(String(locationType), (0, _printer.print)(node), error.message), node, undefined, undefined, undefined, error)); + } +} + +function enumTypeSuggestion(type, node) { + if ((0, _definition.isEnumType)(type)) { + var suggestions = (0, _suggestionList.default)((0, _printer.print)(node), type.getValues().map(function (value) { + return value.name; + })); + + if (suggestions.length !== 0) { + return "Did you mean the enum value ".concat((0, _orList.default)(suggestions), "?"); + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/ValuesOfCorrectType.js.flow b/dist/validation/rules/ValuesOfCorrectType.js.flow new file mode 100644 index 0000000000..d17a7a80c0 --- /dev/null +++ b/dist/validation/rules/ValuesOfCorrectType.js.flow @@ -0,0 +1,223 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { ValueNode } from '../../language/ast'; +import { print } from '../../language/printer'; +import type { ASTVisitor } from '../../language/visitor'; +import { + isScalarType, + isEnumType, + isInputObjectType, + isListType, + isNonNullType, + getNullableType, + getNamedType, +} from '../../type/definition'; +import type { GraphQLType } from '../../type/definition'; +import isInvalid from '../../jsutils/isInvalid'; +import keyMap from '../../jsutils/keyMap'; +import orList from '../../jsutils/orList'; +import suggestionList from '../../jsutils/suggestionList'; + +export function badValueMessage( + typeName: string, + valueName: string, + message?: string, +): string { + return ( + `Expected type ${typeName}, found ${valueName}` + + (message ? `; ${message}` : '.') + ); +} + +export function requiredFieldMessage( + typeName: string, + fieldName: string, + fieldTypeName: string, +): string { + return ( + `Field ${typeName}.${fieldName} of required type ` + + `${fieldTypeName} was not provided.` + ); +} + +export function unknownFieldMessage( + typeName: string, + fieldName: string, + message?: string, +): string { + return ( + `Field "${fieldName}" is not defined by type ${typeName}` + + (message ? `; ${message}` : '.') + ); +} + +/** + * Value literals of correct type + * + * A GraphQL document is only valid if all value literals are of the type + * expected at their position. + */ +export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor { + return { + NullValue(node) { + const type = context.getInputType(); + if (isNonNullType(type)) { + context.reportError( + new GraphQLError(badValueMessage(String(type), print(node)), node), + ); + } + }, + ListValue(node) { + // Note: TypeInfo will traverse into a list's item type, so look to the + // parent input type to check if it is a list. + const type = getNullableType(context.getParentInputType()); + if (!isListType(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } + }, + ObjectValue(node) { + const type = getNamedType(context.getInputType()); + if (!isInputObjectType(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } + // Ensure every required field exists. + const inputFields = type.getFields(); + const fieldNodeMap = keyMap(node.fields, field => field.name.value); + Object.keys(inputFields).forEach(fieldName => { + const fieldDef = inputFields[fieldName]; + const fieldType = fieldDef.type; + const fieldNode = fieldNodeMap[fieldName]; + if ( + !fieldNode && + isNonNullType(fieldType) && + fieldDef.defaultValue === undefined + ) { + context.reportError( + new GraphQLError( + requiredFieldMessage(type.name, fieldName, String(fieldType)), + node, + ), + ); + } + }); + }, + ObjectField(node) { + const parentType = getNamedType(context.getParentInputType()); + const fieldType = context.getInputType(); + if (!fieldType && isInputObjectType(parentType)) { + const suggestions = suggestionList( + node.name.value, + Object.keys(parentType.getFields()), + ); + const didYouMean = + suggestions.length !== 0 + ? `Did you mean ${orList(suggestions)}?` + : undefined; + context.reportError( + new GraphQLError( + unknownFieldMessage(parentType.name, node.name.value, didYouMean), + node, + ), + ); + } + }, + EnumValue(node) { + const type = getNamedType(context.getInputType()); + if (!isEnumType(type)) { + isValidScalar(context, node); + } else if (!type.getValue(node.value)) { + context.reportError( + new GraphQLError( + badValueMessage( + type.name, + print(node), + enumTypeSuggestion(type, node), + ), + node, + ), + ); + } + }, + IntValue: node => isValidScalar(context, node), + FloatValue: node => isValidScalar(context, node), + StringValue: node => isValidScalar(context, node), + BooleanValue: node => isValidScalar(context, node), + }; +} + +/** + * Any value literal may be a valid representation of a Scalar, depending on + * that scalar type. + */ +function isValidScalar(context: ValidationContext, node: ValueNode): void { + // Report any error at the full type expected by the location. + const locationType = context.getInputType(); + if (!locationType) { + return; + } + + const type = getNamedType(locationType); + + if (!isScalarType(type)) { + context.reportError( + new GraphQLError( + badValueMessage( + String(locationType), + print(node), + enumTypeSuggestion(type, node), + ), + node, + ), + ); + return; + } + + // Scalars determine if a literal value is valid via parseLiteral() which + // may throw or return an invalid value to indicate failure. + try { + const parseResult = type.parseLiteral(node, undefined /* variables */); + if (isInvalid(parseResult)) { + context.reportError( + new GraphQLError( + badValueMessage(String(locationType), print(node)), + node, + ), + ); + } + } catch (error) { + // Ensure a reference to the original error is maintained. + context.reportError( + new GraphQLError( + badValueMessage(String(locationType), print(node), error.message), + node, + undefined, + undefined, + undefined, + error, + ), + ); + } +} + +function enumTypeSuggestion(type: GraphQLType, node: ValueNode): string | void { + if (isEnumType(type)) { + const suggestions = suggestionList( + print(node), + type.getValues().map(value => value.name), + ); + if (suggestions.length !== 0) { + return `Did you mean the enum value ${orList(suggestions)}?`; + } + } +} diff --git a/dist/validation/rules/ValuesOfCorrectType.mjs b/dist/validation/rules/ValuesOfCorrectType.mjs new file mode 100644 index 0000000000..a8503064fd --- /dev/null +++ b/dist/validation/rules/ValuesOfCorrectType.mjs @@ -0,0 +1,153 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { print } from '../../language/printer'; +import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType, getNullableType, getNamedType } from '../../type/definition'; +import isInvalid from '../../jsutils/isInvalid'; +import keyMap from '../../jsutils/keyMap'; +import orList from '../../jsutils/orList'; +import suggestionList from '../../jsutils/suggestionList'; +export function badValueMessage(typeName, valueName, message) { + return "Expected type ".concat(typeName, ", found ").concat(valueName) + (message ? "; ".concat(message) : '.'); +} +export function requiredFieldMessage(typeName, fieldName, fieldTypeName) { + return "Field ".concat(typeName, ".").concat(fieldName, " of required type ") + "".concat(fieldTypeName, " was not provided."); +} +export function unknownFieldMessage(typeName, fieldName, message) { + return "Field \"".concat(fieldName, "\" is not defined by type ").concat(typeName) + (message ? "; ".concat(message) : '.'); +} +/** + * Value literals of correct type + * + * A GraphQL document is only valid if all value literals are of the type + * expected at their position. + */ + +export function ValuesOfCorrectType(context) { + return { + NullValue: function NullValue(node) { + var type = context.getInputType(); + + if (isNonNullType(type)) { + context.reportError(new GraphQLError(badValueMessage(String(type), print(node)), node)); + } + }, + ListValue: function ListValue(node) { + // Note: TypeInfo will traverse into a list's item type, so look to the + // parent input type to check if it is a list. + var type = getNullableType(context.getParentInputType()); + + if (!isListType(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } + }, + ObjectValue: function ObjectValue(node) { + var type = getNamedType(context.getInputType()); + + if (!isInputObjectType(type)) { + isValidScalar(context, node); + return false; // Don't traverse further. + } // Ensure every required field exists. + + + var inputFields = type.getFields(); + var fieldNodeMap = keyMap(node.fields, function (field) { + return field.name.value; + }); + Object.keys(inputFields).forEach(function (fieldName) { + var fieldDef = inputFields[fieldName]; + var fieldType = fieldDef.type; + var fieldNode = fieldNodeMap[fieldName]; + + if (!fieldNode && isNonNullType(fieldType) && fieldDef.defaultValue === undefined) { + context.reportError(new GraphQLError(requiredFieldMessage(type.name, fieldName, String(fieldType)), node)); + } + }); + }, + ObjectField: function ObjectField(node) { + var parentType = getNamedType(context.getParentInputType()); + var fieldType = context.getInputType(); + + if (!fieldType && isInputObjectType(parentType)) { + var suggestions = suggestionList(node.name.value, Object.keys(parentType.getFields())); + var didYouMean = suggestions.length !== 0 ? "Did you mean ".concat(orList(suggestions), "?") : undefined; + context.reportError(new GraphQLError(unknownFieldMessage(parentType.name, node.name.value, didYouMean), node)); + } + }, + EnumValue: function EnumValue(node) { + var type = getNamedType(context.getInputType()); + + if (!isEnumType(type)) { + isValidScalar(context, node); + } else if (!type.getValue(node.value)) { + context.reportError(new GraphQLError(badValueMessage(type.name, print(node), enumTypeSuggestion(type, node)), node)); + } + }, + IntValue: function IntValue(node) { + return isValidScalar(context, node); + }, + FloatValue: function FloatValue(node) { + return isValidScalar(context, node); + }, + StringValue: function StringValue(node) { + return isValidScalar(context, node); + }, + BooleanValue: function BooleanValue(node) { + return isValidScalar(context, node); + } + }; +} +/** + * Any value literal may be a valid representation of a Scalar, depending on + * that scalar type. + */ + +function isValidScalar(context, node) { + // Report any error at the full type expected by the location. + var locationType = context.getInputType(); + + if (!locationType) { + return; + } + + var type = getNamedType(locationType); + + if (!isScalarType(type)) { + context.reportError(new GraphQLError(badValueMessage(String(locationType), print(node), enumTypeSuggestion(type, node)), node)); + return; + } // Scalars determine if a literal value is valid via parseLiteral() which + // may throw or return an invalid value to indicate failure. + + + try { + var parseResult = type.parseLiteral(node, undefined + /* variables */ + ); + + if (isInvalid(parseResult)) { + context.reportError(new GraphQLError(badValueMessage(String(locationType), print(node)), node)); + } + } catch (error) { + // Ensure a reference to the original error is maintained. + context.reportError(new GraphQLError(badValueMessage(String(locationType), print(node), error.message), node, undefined, undefined, undefined, error)); + } +} + +function enumTypeSuggestion(type, node) { + if (isEnumType(type)) { + var suggestions = suggestionList(print(node), type.getValues().map(function (value) { + return value.name; + })); + + if (suggestions.length !== 0) { + return "Did you mean the enum value ".concat(orList(suggestions), "?"); + } + } +} \ No newline at end of file diff --git a/dist/validation/rules/VariablesAreInputTypes.js b/dist/validation/rules/VariablesAreInputTypes.js new file mode 100644 index 0000000000..d4fb7c7260 --- /dev/null +++ b/dist/validation/rules/VariablesAreInputTypes.js @@ -0,0 +1,47 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.nonInputTypeOnVarMessage = nonInputTypeOnVarMessage; +exports.VariablesAreInputTypes = VariablesAreInputTypes; + +var _error = require("../../error"); + +var _printer = require("../../language/printer"); + +var _definition = require("../../type/definition"); + +var _typeFromAST = require("../../utilities/typeFromAST"); + +/** + * 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. + * + * strict + */ +function nonInputTypeOnVarMessage(variableName, typeName) { + return "Variable \"$".concat(variableName, "\" cannot be non-input type \"").concat(typeName, "\"."); +} +/** + * Variables are input types + * + * A GraphQL operation is only valid if all the variables it defines are of + * input types (scalar, enum, or input object). + */ + + +function VariablesAreInputTypes(context) { + return { + VariableDefinition: function VariableDefinition(node) { + var type = (0, _typeFromAST.typeFromAST)(context.getSchema(), node.type); // If the variable type is not an input type, return an error. + + if (type && !(0, _definition.isInputType)(type)) { + var variableName = node.variable.name.value; + context.reportError(new _error.GraphQLError(nonInputTypeOnVarMessage(variableName, (0, _printer.print)(node.type)), [node.type])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/VariablesAreInputTypes.js.flow b/dist/validation/rules/VariablesAreInputTypes.js.flow new file mode 100644 index 0000000000..b152025c02 --- /dev/null +++ b/dist/validation/rules/VariablesAreInputTypes.js.flow @@ -0,0 +1,48 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import type { VariableDefinitionNode } from '../../language/ast'; +import { print } from '../../language/printer'; +import type { ASTVisitor } from '../../language/visitor'; +import { isInputType } from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; + +export function nonInputTypeOnVarMessage( + variableName: string, + typeName: string, +): string { + return `Variable "$${variableName}" cannot be non-input type "${typeName}".`; +} + +/** + * Variables are input types + * + * A GraphQL operation is only valid if all the variables it defines are of + * input types (scalar, enum, or input object). + */ +export function VariablesAreInputTypes(context: ValidationContext): ASTVisitor { + return { + VariableDefinition(node: VariableDefinitionNode): ?GraphQLError { + const type = typeFromAST(context.getSchema(), node.type); + + // If the variable type is not an input type, return an error. + if (type && !isInputType(type)) { + const variableName = node.variable.name.value; + context.reportError( + new GraphQLError( + nonInputTypeOnVarMessage(variableName, print(node.type)), + [node.type], + ), + ); + } + }, + }; +} diff --git a/dist/validation/rules/VariablesAreInputTypes.mjs b/dist/validation/rules/VariablesAreInputTypes.mjs new file mode 100644 index 0000000000..bc24177c30 --- /dev/null +++ b/dist/validation/rules/VariablesAreInputTypes.mjs @@ -0,0 +1,34 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { print } from '../../language/printer'; +import { isInputType } from '../../type/definition'; +import { typeFromAST } from '../../utilities/typeFromAST'; +export function nonInputTypeOnVarMessage(variableName, typeName) { + return "Variable \"$".concat(variableName, "\" cannot be non-input type \"").concat(typeName, "\"."); +} +/** + * Variables are input types + * + * A GraphQL operation is only valid if all the variables it defines are of + * input types (scalar, enum, or input object). + */ + +export function VariablesAreInputTypes(context) { + return { + VariableDefinition: function VariableDefinition(node) { + var type = typeFromAST(context.getSchema(), node.type); // If the variable type is not an input type, return an error. + + if (type && !isInputType(type)) { + var variableName = node.variable.name.value; + context.reportError(new GraphQLError(nonInputTypeOnVarMessage(variableName, print(node.type)), [node.type])); + } + } + }; +} \ No newline at end of file diff --git a/dist/validation/rules/VariablesInAllowedPosition.js b/dist/validation/rules/VariablesInAllowedPosition.js new file mode 100644 index 0000000000..23ba2af0fd --- /dev/null +++ b/dist/validation/rules/VariablesInAllowedPosition.js @@ -0,0 +1,93 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.badVarPosMessage = badVarPosMessage; +exports.VariablesInAllowedPosition = VariablesInAllowedPosition; + +var _error = require("../../error"); + +var _kinds = require("../../language/kinds"); + +var _definition = require("../../type/definition"); + +var _typeComparators = require("../../utilities/typeComparators"); + +var _typeFromAST = require("../../utilities/typeFromAST"); + +/** + * 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. + * + * strict + */ +function badVarPosMessage(varName, varType, expectedType) { + return "Variable \"$".concat(varName, "\" of type \"").concat(String(varType), "\" used in ") + "position expecting type \"".concat(String(expectedType), "\"."); +} +/** + * Variables passed to field arguments conform to type + */ + + +function VariablesInAllowedPosition(context) { + var varDefMap = Object.create(null); + return { + OperationDefinition: { + enter: function enter() { + varDefMap = Object.create(null); + }, + leave: function leave(operation) { + var usages = context.getRecursiveVariableUsages(operation); + usages.forEach(function (_ref) { + var node = _ref.node, + type = _ref.type, + defaultValue = _ref.defaultValue; + var varName = node.name.value; + var varDef = varDefMap[varName]; + + if (varDef && type) { + // A var type is allowed if it is the same or more strict (e.g. is + // a subtype of) than the expected type. It can be more strict if + // the variable type is non-null when the expected type is nullable. + // If both are list types, the variable item type can be more strict + // than the expected item type (contravariant). + var schema = context.getSchema(); + var varType = (0, _typeFromAST.typeFromAST)(schema, varDef.type); + + if (varType && !allowedVariableUsage(schema, varType, varDef.defaultValue, type, defaultValue)) { + context.reportError(new _error.GraphQLError(badVarPosMessage(varName, varType, type), [varDef, node])); + } + } + }); + } + }, + VariableDefinition: function VariableDefinition(node) { + varDefMap[node.variable.name.value] = node; + } + }; +} +/** + * Returns true if the variable is allowed in the location it was found, + * which includes considering if default values exist for either the variable + * or the location at which it is located. + */ + + +function allowedVariableUsage(schema, varType, varDefaultValue, locationType, locationDefaultValue) { + if ((0, _definition.isNonNullType)(locationType) && !(0, _definition.isNonNullType)(varType)) { + var hasNonNullVariableDefaultValue = varDefaultValue && varDefaultValue.kind !== _kinds.Kind.NULL; + var hasLocationDefaultValue = locationDefaultValue !== undefined; + + if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) { + return false; + } + + var nullableLocationType = locationType.ofType; + return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, nullableLocationType); + } + + return (0, _typeComparators.isTypeSubTypeOf)(schema, varType, locationType); +} \ No newline at end of file diff --git a/dist/validation/rules/VariablesInAllowedPosition.js.flow b/dist/validation/rules/VariablesInAllowedPosition.js.flow new file mode 100644 index 0000000000..20805feb49 --- /dev/null +++ b/dist/validation/rules/VariablesInAllowedPosition.js.flow @@ -0,0 +1,109 @@ +/** + * 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. + * + * @flow strict + */ + +import type ValidationContext from '../ValidationContext'; +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +import type { ValueNode } from '../../language/ast'; +import type { ASTVisitor } from '../../language/visitor'; +import { isNonNullType } from '../../type/definition'; +import { isTypeSubTypeOf } from '../../utilities/typeComparators'; +import { typeFromAST } from '../../utilities/typeFromAST'; +import type { GraphQLType } from '../../type/definition'; +import type { GraphQLSchema } from '../../type/schema'; + +export function badVarPosMessage( + varName: string, + varType: GraphQLType, + expectedType: GraphQLType, +): string { + return ( + `Variable "$${varName}" of type "${String(varType)}" used in ` + + `position expecting type "${String(expectedType)}".` + ); +} + +/** + * Variables passed to field arguments conform to type + */ +export function VariablesInAllowedPosition( + context: ValidationContext, +): ASTVisitor { + let varDefMap = Object.create(null); + + return { + OperationDefinition: { + enter() { + varDefMap = Object.create(null); + }, + leave(operation) { + const usages = context.getRecursiveVariableUsages(operation); + + usages.forEach(({ node, type, defaultValue }) => { + const varName = node.name.value; + const varDef = varDefMap[varName]; + if (varDef && type) { + // A var type is allowed if it is the same or more strict (e.g. is + // a subtype of) than the expected type. It can be more strict if + // the variable type is non-null when the expected type is nullable. + // If both are list types, the variable item type can be more strict + // than the expected item type (contravariant). + const schema = context.getSchema(); + const varType = typeFromAST(schema, varDef.type); + if ( + varType && + !allowedVariableUsage( + schema, + varType, + varDef.defaultValue, + type, + defaultValue, + ) + ) { + context.reportError( + new GraphQLError(badVarPosMessage(varName, varType, type), [ + varDef, + node, + ]), + ); + } + } + }); + }, + }, + VariableDefinition(node) { + varDefMap[node.variable.name.value] = node; + }, + }; +} + +/** + * Returns true if the variable is allowed in the location it was found, + * which includes considering if default values exist for either the variable + * or the location at which it is located. + */ +function allowedVariableUsage( + schema: GraphQLSchema, + varType: GraphQLType, + varDefaultValue: ?ValueNode, + locationType: GraphQLType, + locationDefaultValue: ?mixed, +): boolean { + if (isNonNullType(locationType) && !isNonNullType(varType)) { + const hasNonNullVariableDefaultValue = + varDefaultValue && varDefaultValue.kind !== Kind.NULL; + const hasLocationDefaultValue = locationDefaultValue !== undefined; + if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) { + return false; + } + const nullableLocationType = locationType.ofType; + return isTypeSubTypeOf(schema, varType, nullableLocationType); + } + return isTypeSubTypeOf(schema, varType, locationType); +} diff --git a/dist/validation/rules/VariablesInAllowedPosition.mjs b/dist/validation/rules/VariablesInAllowedPosition.mjs new file mode 100644 index 0000000000..89dd32ea66 --- /dev/null +++ b/dist/validation/rules/VariablesInAllowedPosition.mjs @@ -0,0 +1,78 @@ +/** + * 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. + * + * strict + */ +import { GraphQLError } from '../../error'; +import { Kind } from '../../language/kinds'; +import { isNonNullType } from '../../type/definition'; +import { isTypeSubTypeOf } from '../../utilities/typeComparators'; +import { typeFromAST } from '../../utilities/typeFromAST'; +export function badVarPosMessage(varName, varType, expectedType) { + return "Variable \"$".concat(varName, "\" of type \"").concat(String(varType), "\" used in ") + "position expecting type \"".concat(String(expectedType), "\"."); +} +/** + * Variables passed to field arguments conform to type + */ + +export function VariablesInAllowedPosition(context) { + var varDefMap = Object.create(null); + return { + OperationDefinition: { + enter: function enter() { + varDefMap = Object.create(null); + }, + leave: function leave(operation) { + var usages = context.getRecursiveVariableUsages(operation); + usages.forEach(function (_ref) { + var node = _ref.node, + type = _ref.type, + defaultValue = _ref.defaultValue; + var varName = node.name.value; + var varDef = varDefMap[varName]; + + if (varDef && type) { + // A var type is allowed if it is the same or more strict (e.g. is + // a subtype of) than the expected type. It can be more strict if + // the variable type is non-null when the expected type is nullable. + // If both are list types, the variable item type can be more strict + // than the expected item type (contravariant). + var schema = context.getSchema(); + var varType = typeFromAST(schema, varDef.type); + + if (varType && !allowedVariableUsage(schema, varType, varDef.defaultValue, type, defaultValue)) { + context.reportError(new GraphQLError(badVarPosMessage(varName, varType, type), [varDef, node])); + } + } + }); + } + }, + VariableDefinition: function VariableDefinition(node) { + varDefMap[node.variable.name.value] = node; + } + }; +} +/** + * Returns true if the variable is allowed in the location it was found, + * which includes considering if default values exist for either the variable + * or the location at which it is located. + */ + +function allowedVariableUsage(schema, varType, varDefaultValue, locationType, locationDefaultValue) { + if (isNonNullType(locationType) && !isNonNullType(varType)) { + var hasNonNullVariableDefaultValue = varDefaultValue && varDefaultValue.kind !== Kind.NULL; + var hasLocationDefaultValue = locationDefaultValue !== undefined; + + if (!hasNonNullVariableDefaultValue && !hasLocationDefaultValue) { + return false; + } + + var nullableLocationType = locationType.ofType; + return isTypeSubTypeOf(schema, varType, nullableLocationType); + } + + return isTypeSubTypeOf(schema, varType, locationType); +} \ No newline at end of file diff --git a/dist/validation/specifiedRules.js b/dist/validation/specifiedRules.js new file mode 100644 index 0000000000..df31734211 --- /dev/null +++ b/dist/validation/specifiedRules.js @@ -0,0 +1,102 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.specifiedRules = void 0; + +var _ExecutableDefinitions = require("./rules/ExecutableDefinitions"); + +var _UniqueOperationNames = require("./rules/UniqueOperationNames"); + +var _LoneAnonymousOperation = require("./rules/LoneAnonymousOperation"); + +var _SingleFieldSubscriptions = require("./rules/SingleFieldSubscriptions"); + +var _KnownTypeNames = require("./rules/KnownTypeNames"); + +var _FragmentsOnCompositeTypes = require("./rules/FragmentsOnCompositeTypes"); + +var _VariablesAreInputTypes = require("./rules/VariablesAreInputTypes"); + +var _ScalarLeafs = require("./rules/ScalarLeafs"); + +var _FieldsOnCorrectType = require("./rules/FieldsOnCorrectType"); + +var _UniqueFragmentNames = require("./rules/UniqueFragmentNames"); + +var _KnownFragmentNames = require("./rules/KnownFragmentNames"); + +var _NoUnusedFragments = require("./rules/NoUnusedFragments"); + +var _PossibleFragmentSpreads = require("./rules/PossibleFragmentSpreads"); + +var _NoFragmentCycles = require("./rules/NoFragmentCycles"); + +var _UniqueVariableNames = require("./rules/UniqueVariableNames"); + +var _NoUndefinedVariables = require("./rules/NoUndefinedVariables"); + +var _NoUnusedVariables = require("./rules/NoUnusedVariables"); + +var _KnownDirectives = require("./rules/KnownDirectives"); + +var _UniqueDirectivesPerLocation = require("./rules/UniqueDirectivesPerLocation"); + +var _KnownArgumentNames = require("./rules/KnownArgumentNames"); + +var _UniqueArgumentNames = require("./rules/UniqueArgumentNames"); + +var _ValuesOfCorrectType = require("./rules/ValuesOfCorrectType"); + +var _ProvidedRequiredArguments = require("./rules/ProvidedRequiredArguments"); + +var _VariablesInAllowedPosition = require("./rules/VariablesInAllowedPosition"); + +var _OverlappingFieldsCanBeMerged = require("./rules/OverlappingFieldsCanBeMerged"); + +var _UniqueInputFieldNames = require("./rules/UniqueInputFieldNames"); + +/** + * 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. + * + * strict + */ +// Spec Section: "Executable Definitions" +// Spec Section: "Operation Name Uniqueness" +// Spec Section: "Lone Anonymous Operation" +// Spec Section: "Subscriptions with Single Root Field" +// Spec Section: "Fragment Spread Type Existence" +// Spec Section: "Fragments on Composite Types" +// Spec Section: "Variables are Input Types" +// Spec Section: "Leaf Field Selections" +// Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" +// Spec Section: "Fragment Name Uniqueness" +// Spec Section: "Fragment spread target defined" +// Spec Section: "Fragments must be used" +// Spec Section: "Fragment spread is possible" +// Spec Section: "Fragments must not form cycles" +// Spec Section: "Variable Uniqueness" +// Spec Section: "All Variable Used Defined" +// Spec Section: "All Variables Used" +// Spec Section: "Directives Are Defined" +// Spec Section: "Directives Are Unique Per Location" +// Spec Section: "Argument Names" +// Spec Section: "Argument Uniqueness" +// Spec Section: "Value Type Correctness" +// Spec Section: "Argument Optionality" +// Spec Section: "All Variable Usages Are Allowed" +// Spec Section: "Field Selection Merging" +// Spec Section: "Input Object Field Uniqueness" + +/** + * This set includes all validation rules defined by the GraphQL spec. + * + * The order of the rules in this list has been adjusted to lead to the + * most clear output when encountering multiple validation errors. + */ +var specifiedRules = [_ExecutableDefinitions.ExecutableDefinitions, _UniqueOperationNames.UniqueOperationNames, _LoneAnonymousOperation.LoneAnonymousOperation, _SingleFieldSubscriptions.SingleFieldSubscriptions, _KnownTypeNames.KnownTypeNames, _FragmentsOnCompositeTypes.FragmentsOnCompositeTypes, _VariablesAreInputTypes.VariablesAreInputTypes, _ScalarLeafs.ScalarLeafs, _FieldsOnCorrectType.FieldsOnCorrectType, _UniqueFragmentNames.UniqueFragmentNames, _KnownFragmentNames.KnownFragmentNames, _NoUnusedFragments.NoUnusedFragments, _PossibleFragmentSpreads.PossibleFragmentSpreads, _NoFragmentCycles.NoFragmentCycles, _UniqueVariableNames.UniqueVariableNames, _NoUndefinedVariables.NoUndefinedVariables, _NoUnusedVariables.NoUnusedVariables, _KnownDirectives.KnownDirectives, _UniqueDirectivesPerLocation.UniqueDirectivesPerLocation, _KnownArgumentNames.KnownArgumentNames, _UniqueArgumentNames.UniqueArgumentNames, _ValuesOfCorrectType.ValuesOfCorrectType, _ProvidedRequiredArguments.ProvidedRequiredArguments, _VariablesInAllowedPosition.VariablesInAllowedPosition, _OverlappingFieldsCanBeMerged.OverlappingFieldsCanBeMerged, _UniqueInputFieldNames.UniqueInputFieldNames]; +exports.specifiedRules = specifiedRules; \ No newline at end of file diff --git a/dist/validation/specifiedRules.js.flow b/dist/validation/specifiedRules.js.flow new file mode 100644 index 0000000000..77792d6a05 --- /dev/null +++ b/dist/validation/specifiedRules.js.flow @@ -0,0 +1,123 @@ +/** + * 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. + * + * @flow strict + */ + +// Spec Section: "Executable Definitions" +import { ExecutableDefinitions } from './rules/ExecutableDefinitions'; + +// Spec Section: "Operation Name Uniqueness" +import { UniqueOperationNames } from './rules/UniqueOperationNames'; + +// Spec Section: "Lone Anonymous Operation" +import { LoneAnonymousOperation } from './rules/LoneAnonymousOperation'; + +// Spec Section: "Subscriptions with Single Root Field" +import { SingleFieldSubscriptions } from './rules/SingleFieldSubscriptions'; + +// Spec Section: "Fragment Spread Type Existence" +import { KnownTypeNames } from './rules/KnownTypeNames'; + +// Spec Section: "Fragments on Composite Types" +import { FragmentsOnCompositeTypes } from './rules/FragmentsOnCompositeTypes'; + +// Spec Section: "Variables are Input Types" +import { VariablesAreInputTypes } from './rules/VariablesAreInputTypes'; + +// Spec Section: "Leaf Field Selections" +import { ScalarLeafs } from './rules/ScalarLeafs'; + +// Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" +import { FieldsOnCorrectType } from './rules/FieldsOnCorrectType'; + +// Spec Section: "Fragment Name Uniqueness" +import { UniqueFragmentNames } from './rules/UniqueFragmentNames'; + +// Spec Section: "Fragment spread target defined" +import { KnownFragmentNames } from './rules/KnownFragmentNames'; + +// Spec Section: "Fragments must be used" +import { NoUnusedFragments } from './rules/NoUnusedFragments'; + +// Spec Section: "Fragment spread is possible" +import { PossibleFragmentSpreads } from './rules/PossibleFragmentSpreads'; + +// Spec Section: "Fragments must not form cycles" +import { NoFragmentCycles } from './rules/NoFragmentCycles'; + +// Spec Section: "Variable Uniqueness" +import { UniqueVariableNames } from './rules/UniqueVariableNames'; + +// Spec Section: "All Variable Used Defined" +import { NoUndefinedVariables } from './rules/NoUndefinedVariables'; + +// Spec Section: "All Variables Used" +import { NoUnusedVariables } from './rules/NoUnusedVariables'; + +// Spec Section: "Directives Are Defined" +import { KnownDirectives } from './rules/KnownDirectives'; + +// Spec Section: "Directives Are Unique Per Location" +import { UniqueDirectivesPerLocation } from './rules/UniqueDirectivesPerLocation'; + +// Spec Section: "Argument Names" +import { KnownArgumentNames } from './rules/KnownArgumentNames'; + +// Spec Section: "Argument Uniqueness" +import { UniqueArgumentNames } from './rules/UniqueArgumentNames'; + +// Spec Section: "Value Type Correctness" +import { ValuesOfCorrectType } from './rules/ValuesOfCorrectType'; + +// Spec Section: "Argument Optionality" +import { ProvidedRequiredArguments } from './rules/ProvidedRequiredArguments'; + +// Spec Section: "All Variable Usages Are Allowed" +import { VariablesInAllowedPosition } from './rules/VariablesInAllowedPosition'; + +// Spec Section: "Field Selection Merging" +import { OverlappingFieldsCanBeMerged } from './rules/OverlappingFieldsCanBeMerged'; + +// Spec Section: "Input Object Field Uniqueness" +import { UniqueInputFieldNames } from './rules/UniqueInputFieldNames'; + +import type ValidationContext from './ValidationContext'; + +/** + * This set includes all validation rules defined by the GraphQL spec. + * + * The order of the rules in this list has been adjusted to lead to the + * most clear output when encountering multiple validation errors. + */ +export const specifiedRules: Array<(context: ValidationContext) => any> = [ + ExecutableDefinitions, + UniqueOperationNames, + LoneAnonymousOperation, + SingleFieldSubscriptions, + KnownTypeNames, + FragmentsOnCompositeTypes, + VariablesAreInputTypes, + ScalarLeafs, + FieldsOnCorrectType, + UniqueFragmentNames, + KnownFragmentNames, + NoUnusedFragments, + PossibleFragmentSpreads, + NoFragmentCycles, + UniqueVariableNames, + NoUndefinedVariables, + NoUnusedVariables, + KnownDirectives, + UniqueDirectivesPerLocation, + KnownArgumentNames, + UniqueArgumentNames, + ValuesOfCorrectType, + ProvidedRequiredArguments, + VariablesInAllowedPosition, + OverlappingFieldsCanBeMerged, + UniqueInputFieldNames, +]; diff --git a/dist/validation/specifiedRules.mjs b/dist/validation/specifiedRules.mjs new file mode 100644 index 0000000000..97eacda322 --- /dev/null +++ b/dist/validation/specifiedRules.mjs @@ -0,0 +1,68 @@ +/** + * 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. + * + * strict + */ +// Spec Section: "Executable Definitions" +import { ExecutableDefinitions } from './rules/ExecutableDefinitions'; // Spec Section: "Operation Name Uniqueness" + +import { UniqueOperationNames } from './rules/UniqueOperationNames'; // Spec Section: "Lone Anonymous Operation" + +import { LoneAnonymousOperation } from './rules/LoneAnonymousOperation'; // Spec Section: "Subscriptions with Single Root Field" + +import { SingleFieldSubscriptions } from './rules/SingleFieldSubscriptions'; // Spec Section: "Fragment Spread Type Existence" + +import { KnownTypeNames } from './rules/KnownTypeNames'; // Spec Section: "Fragments on Composite Types" + +import { FragmentsOnCompositeTypes } from './rules/FragmentsOnCompositeTypes'; // Spec Section: "Variables are Input Types" + +import { VariablesAreInputTypes } from './rules/VariablesAreInputTypes'; // Spec Section: "Leaf Field Selections" + +import { ScalarLeafs } from './rules/ScalarLeafs'; // Spec Section: "Field Selections on Objects, Interfaces, and Unions Types" + +import { FieldsOnCorrectType } from './rules/FieldsOnCorrectType'; // Spec Section: "Fragment Name Uniqueness" + +import { UniqueFragmentNames } from './rules/UniqueFragmentNames'; // Spec Section: "Fragment spread target defined" + +import { KnownFragmentNames } from './rules/KnownFragmentNames'; // Spec Section: "Fragments must be used" + +import { NoUnusedFragments } from './rules/NoUnusedFragments'; // Spec Section: "Fragment spread is possible" + +import { PossibleFragmentSpreads } from './rules/PossibleFragmentSpreads'; // Spec Section: "Fragments must not form cycles" + +import { NoFragmentCycles } from './rules/NoFragmentCycles'; // Spec Section: "Variable Uniqueness" + +import { UniqueVariableNames } from './rules/UniqueVariableNames'; // Spec Section: "All Variable Used Defined" + +import { NoUndefinedVariables } from './rules/NoUndefinedVariables'; // Spec Section: "All Variables Used" + +import { NoUnusedVariables } from './rules/NoUnusedVariables'; // Spec Section: "Directives Are Defined" + +import { KnownDirectives } from './rules/KnownDirectives'; // Spec Section: "Directives Are Unique Per Location" + +import { UniqueDirectivesPerLocation } from './rules/UniqueDirectivesPerLocation'; // Spec Section: "Argument Names" + +import { KnownArgumentNames } from './rules/KnownArgumentNames'; // Spec Section: "Argument Uniqueness" + +import { UniqueArgumentNames } from './rules/UniqueArgumentNames'; // Spec Section: "Value Type Correctness" + +import { ValuesOfCorrectType } from './rules/ValuesOfCorrectType'; // Spec Section: "Argument Optionality" + +import { ProvidedRequiredArguments } from './rules/ProvidedRequiredArguments'; // Spec Section: "All Variable Usages Are Allowed" + +import { VariablesInAllowedPosition } from './rules/VariablesInAllowedPosition'; // Spec Section: "Field Selection Merging" + +import { OverlappingFieldsCanBeMerged } from './rules/OverlappingFieldsCanBeMerged'; // Spec Section: "Input Object Field Uniqueness" + +import { UniqueInputFieldNames } from './rules/UniqueInputFieldNames'; + +/** + * This set includes all validation rules defined by the GraphQL spec. + * + * The order of the rules in this list has been adjusted to lead to the + * most clear output when encountering multiple validation errors. + */ +export var specifiedRules = [ExecutableDefinitions, UniqueOperationNames, LoneAnonymousOperation, SingleFieldSubscriptions, KnownTypeNames, FragmentsOnCompositeTypes, VariablesAreInputTypes, ScalarLeafs, FieldsOnCorrectType, UniqueFragmentNames, KnownFragmentNames, NoUnusedFragments, PossibleFragmentSpreads, NoFragmentCycles, UniqueVariableNames, NoUndefinedVariables, NoUnusedVariables, KnownDirectives, UniqueDirectivesPerLocation, KnownArgumentNames, UniqueArgumentNames, ValuesOfCorrectType, ProvidedRequiredArguments, VariablesInAllowedPosition, OverlappingFieldsCanBeMerged, UniqueInputFieldNames]; \ No newline at end of file diff --git a/dist/validation/validate.js b/dist/validation/validate.js new file mode 100644 index 0000000000..45c65a8beb --- /dev/null +++ b/dist/validation/validate.js @@ -0,0 +1,73 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.validate = validate; + +var _invariant = _interopRequireDefault(require("../jsutils/invariant")); + +var _error = require("../error"); + +var _visitor = require("../language/visitor"); + +var _schema = require("../type/schema"); + +var _validate = require("../type/validate"); + +var _TypeInfo = require("../utilities/TypeInfo"); + +var _specifiedRules = require("./specifiedRules"); + +var _ValidationContext = _interopRequireDefault(require("./ValidationContext")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +/** + * 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. + * + * strict + */ + +/** + * Implements the "Validation" section of the spec. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the document is valid. + * + * A list of specific validation rules may be provided. If not provided, the + * default list of rules defined by the GraphQL specification will be used. + * + * Each validation rules is a function which returns a visitor + * (see the language/visitor API). Visitor methods are expected to return + * GraphQLErrors, or Arrays of GraphQLErrors when invalid. + * + * Optionally a custom TypeInfo instance may be provided. If not provided, one + * will be created from the provided schema. + */ +function validate(schema, ast, rules, typeInfo) { + !ast ? (0, _invariant.default)(0, 'Must provide document') : void 0; // If the schema used for validation is invalid, throw an error. + + (0, _validate.assertValidSchema)(schema); + return visitUsingRules(schema, typeInfo || new _TypeInfo.TypeInfo(schema), ast, rules || _specifiedRules.specifiedRules); +} +/** + * This uses a specialized visitor which runs multiple visitors in parallel, + * while maintaining the visitor skip and break API. + * + * @internal + */ + + +function visitUsingRules(schema, typeInfo, documentAST, rules) { + var context = new _ValidationContext.default(schema, documentAST, typeInfo); + var visitors = rules.map(function (rule) { + return rule(context); + }); // Visit the whole document with each instance of all provided rules. + + (0, _visitor.visit)(documentAST, (0, _visitor.visitWithTypeInfo)(typeInfo, (0, _visitor.visitInParallel)(visitors))); + return context.getErrors(); +} \ No newline at end of file diff --git a/dist/validation/validate.js.flow b/dist/validation/validate.js.flow new file mode 100644 index 0000000000..61213f8b86 --- /dev/null +++ b/dist/validation/validate.js.flow @@ -0,0 +1,71 @@ +/** + * 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. + * + * @flow strict + */ + +import invariant from '../jsutils/invariant'; +import { GraphQLError } from '../error'; +import { visit, visitInParallel, visitWithTypeInfo } from '../language/visitor'; +import type { DocumentNode } from '../language/ast'; +import type { ASTVisitor } from '../language/visitor'; +import { GraphQLSchema } from '../type/schema'; +import { assertValidSchema } from '../type/validate'; +import { TypeInfo } from '../utilities/TypeInfo'; +import { specifiedRules } from './specifiedRules'; +import ValidationContext from './ValidationContext'; + +/** + * Implements the "Validation" section of the spec. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the document is valid. + * + * A list of specific validation rules may be provided. If not provided, the + * default list of rules defined by the GraphQL specification will be used. + * + * Each validation rules is a function which returns a visitor + * (see the language/visitor API). Visitor methods are expected to return + * GraphQLErrors, or Arrays of GraphQLErrors when invalid. + * + * Optionally a custom TypeInfo instance may be provided. If not provided, one + * will be created from the provided schema. + */ +export function validate( + schema: GraphQLSchema, + ast: DocumentNode, + rules?: $ReadOnlyArray, + typeInfo?: TypeInfo, +): $ReadOnlyArray { + invariant(ast, 'Must provide document'); + // If the schema used for validation is invalid, throw an error. + assertValidSchema(schema); + return visitUsingRules( + schema, + typeInfo || new TypeInfo(schema), + ast, + rules || specifiedRules, + ); +} + +/** + * This uses a specialized visitor which runs multiple visitors in parallel, + * while maintaining the visitor skip and break API. + * + * @internal + */ +function visitUsingRules( + schema: GraphQLSchema, + typeInfo: TypeInfo, + documentAST: DocumentNode, + rules: $ReadOnlyArray<(ValidationContext) => ASTVisitor>, +): $ReadOnlyArray { + const context = new ValidationContext(schema, documentAST, typeInfo); + const visitors = rules.map(rule => rule(context)); + // Visit the whole document with each instance of all provided rules. + visit(documentAST, visitWithTypeInfo(typeInfo, visitInParallel(visitors))); + return context.getErrors(); +} diff --git a/dist/validation/validate.mjs b/dist/validation/validate.mjs new file mode 100644 index 0000000000..91d47567b8 --- /dev/null +++ b/dist/validation/validate.mjs @@ -0,0 +1,55 @@ +/** + * 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. + * + * strict + */ +import invariant from '../jsutils/invariant'; +import { GraphQLError } from '../error'; +import { visit, visitInParallel, visitWithTypeInfo } from '../language/visitor'; +import { GraphQLSchema } from '../type/schema'; +import { assertValidSchema } from '../type/validate'; +import { TypeInfo } from '../utilities/TypeInfo'; +import { specifiedRules } from './specifiedRules'; +import ValidationContext from './ValidationContext'; +/** + * Implements the "Validation" section of the spec. + * + * Validation runs synchronously, returning an array of encountered errors, or + * an empty array if no errors were encountered and the document is valid. + * + * A list of specific validation rules may be provided. If not provided, the + * default list of rules defined by the GraphQL specification will be used. + * + * Each validation rules is a function which returns a visitor + * (see the language/visitor API). Visitor methods are expected to return + * GraphQLErrors, or Arrays of GraphQLErrors when invalid. + * + * Optionally a custom TypeInfo instance may be provided. If not provided, one + * will be created from the provided schema. + */ + +export function validate(schema, ast, rules, typeInfo) { + !ast ? invariant(0, 'Must provide document') : void 0; // If the schema used for validation is invalid, throw an error. + + assertValidSchema(schema); + return visitUsingRules(schema, typeInfo || new TypeInfo(schema), ast, rules || specifiedRules); +} +/** + * This uses a specialized visitor which runs multiple visitors in parallel, + * while maintaining the visitor skip and break API. + * + * @internal + */ + +function visitUsingRules(schema, typeInfo, documentAST, rules) { + var context = new ValidationContext(schema, documentAST, typeInfo); + var visitors = rules.map(function (rule) { + return rule(context); + }); // Visit the whole document with each instance of all provided rules. + + visit(documentAST, visitWithTypeInfo(typeInfo, visitInParallel(visitors))); + return context.getErrors(); +} \ No newline at end of file diff --git a/package.json b/package.json index 52064fdd7d..1edc7788ec 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ "watch": "babel-node ./resources/watch.js", "test": "npm run lint && npm run check && npm run testonly", "test:ci": "npm run lint && npm run check && npm run testonly:coveralls", - "t": "mocha --require babel-register --require babel-polyfill", - "testonly": "mocha --require babel-register --require babel-polyfill --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js", + "t": "mocha --require @babel/register --require @babel/polyfill", + "testonly": "mocha --require @babel/register --require @babel/polyfill --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js", "testonly:cover": "nyc --reporter html --reporter text-summary -- npm run testonly", "testonly:coveralls": "nyc --reporter text-lcov npm run testonly | coveralls", "lint": "eslint --rulesdir ./resources/lint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)", @@ -31,8 +31,8 @@ "build:clean": "rm -rf ./dist && mkdir ./dist", "build:cp": "cp README.md LICENSE ./dist", "build:package-json": "node ./resources/copy-package-json.js", - "build:cjs": "babel src --optional runtime --ignore __tests__ --out-dir dist/", - "build:mjs": "BABEL_MODULES=1 babel src --optional runtime --ignore __tests__ --out-dir dist/module/ && for file in $(find dist/module -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/module/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/module", + "build:cjs": "babel src --optional runtime --ignore '**/__tests__' --out-dir dist/", + "build:mjs": "BABEL_MODULES=1 babel src --optional runtime --ignore '**/__tests__' --out-dir dist/module/ && for file in $(find dist/module -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/module/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/module", "build:flow": "for file in $(find ./src -name '*.js' -not -path '*/__tests__*'); do cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/dist\\//g'`.flow; done", "preversion": ". ./resources/checkgit.sh && npm test", "prepublishOnly": ". ./resources/prepublish.sh", @@ -42,17 +42,22 @@ "iterall": "^1.2.2" }, "devDependencies": { - "babel-cli": "6.26.0", + "@babel/cli": "7.0.0-beta.47", + "@babel/core": "7.0.0-beta.47", + "@babel/node": "7.0.0-beta.47", + "@babel/plugin-proposal-class-properties": "7.0.0-beta.47", + "@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.47", + "@babel/plugin-syntax-async-generators": "7.0.0-beta.47", + "@babel/plugin-transform-classes": "7.0.0-beta.47", + "@babel/plugin-transform-destructuring": "7.0.0-beta.47", + "@babel/plugin-transform-flow-strip-types": "7.0.0-beta.47", + "@babel/plugin-transform-modules-commonjs": "7.0.0-beta.47", + "@babel/plugin-transform-spread": "7.0.0-beta.47", + "@babel/polyfill": "7.0.0-beta.47", + "@babel/preset-env": "7.0.0-beta.47", + "@babel/register": "7.0.0-beta.47", "babel-eslint": "8.2.3", "babel-plugin-syntax-async-functions": "6.13.0", - "babel-plugin-syntax-async-generators": "6.13.0", - "babel-plugin-transform-class-properties": "6.24.1", - "babel-plugin-transform-es2015-modules-commonjs": "6.26.0", - "babel-plugin-transform-flow-strip-types": "6.22.0", - "babel-plugin-transform-object-rest-spread": "6.26.0", - "babel-polyfill": "^6.26.0", - "babel-preset-env": "^1.5.2", - "babel-register": "^6.26.0", "beautify-benchmark": "0.2.4", "benchmark": "2.1.4", "chai": "4.1.2", diff --git a/resources/common-js-modules.js b/resources/common-js-modules.js index d9838b3123..c60a5ed300 100644 --- a/resources/common-js-modules.js +++ b/resources/common-js-modules.js @@ -14,4 +14,4 @@ */ module.exports = process.env.BABEL_MODULES ? () => ({}) : - require('babel-plugin-transform-es2015-modules-commonjs'); + require('@babel/plugin-transform-modules-commonjs'); diff --git a/yarn-error.log b/yarn-error.log new file mode 100644 index 0000000000..8448173a98 --- /dev/null +++ b/yarn-error.log @@ -0,0 +1,4093 @@ +Arguments: + /usr/local/bin/node /usr/share/yarn/bin/yarn.js build + +PATH: + /usr/local/cuda-8.0/bin:/usr/local/cuda-8.0/bin:/home/aaron/bin:/home/aaron/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin + +Yarn version: + 1.5.1 + +Node version: + 9.4.0 + +Platform: + linux x64 + +npm manifest: + { + "name": "graphql", + "version": "0.13.2", + "description": "A Query Language and Runtime which can target any service.", + "license": "MIT", + "main": "index", + "module": "index.mjs", + "sideEffects": false, + "homepage": "https://github.com/graphql/graphql-js", + "bugs": { + "url": "https://github.com/graphql/graphql-js/issues" + }, + "repository": { + "type": "git", + "url": "http://github.com/graphql/graphql-js.git" + }, + "scripts": { + "watch": "babel-node ./resources/watch.js", + "test": "npm run lint && npm run check && npm run testonly", + "test:ci": "npm run lint && npm run check && npm run testonly:coveralls", + "t": "mocha --require @babel/register --require @babel/polyfill", + "testonly": "mocha --require @babel/register --require @babel/polyfill --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js", + "testonly:cover": "nyc --reporter html --reporter text-summary -- npm run testonly", + "testonly:coveralls": "nyc --reporter text-lcov npm run testonly | coveralls", + "lint": "eslint --rulesdir ./resources/lint src || (printf '\\033[33mTry: \\033[7m npm run lint -- --fix \\033[0m\\n' && exit 1)", + "benchmark": "node ./resources/benchmark.js", + "prettier": "prettier --write 'src/**/*.js'", + "check": "flow check", + "check-cover": "for file in {src/*.js,src/**/*.js}; do echo $file; flow coverage $file; done", + "build": "npm run build:clean && npm run build:cp && npm run build:package-json && npm run build:cjs && npm run build:mjs && npm run build:flow", + "build:clean": "rm -rf ./dist && mkdir ./dist", + "build:cp": "cp README.md LICENSE ./dist", + "build:package-json": "node ./resources/copy-package-json.js", + "build:cjs": "babel src --optional runtime --ignore __tests__ --out-dir dist/", + "build:mjs": "BABEL_MODULES=1 babel src --optional runtime --ignore __tests__ --out-dir dist/module/ && for file in $(find dist/module -name '*.js'); do mv \"$file\" `echo \"$file\" | sed 's/dist\\/module/dist/g; s/.js$/.mjs/g'`; done && rm -rf dist/module", + "build:flow": "for file in $(find ./src -name '*.js' -not -path '*/__tests__*'); do cp \"$file\" `echo \"$file\" | sed 's/\\/src\\//\\/dist\\//g'`.flow; done", + "preversion": ". ./resources/checkgit.sh && npm test", + "prepublishOnly": ". ./resources/prepublish.sh", + "gitpublish": ". ./resources/gitpublish.sh" + }, + "dependencies": { + "iterall": "^1.2.2" + }, + "devDependencies": { + "@babel/cli": "^7.0.0-beta.46", + "@babel/core": "^7.0.0-beta.46", + "@babel/node": "^7.0.0-beta.46", + "@babel/plugin-proposal-class-properties": "^7.0.0-beta.46", + "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.46", + "@babel/plugin-syntax-async-generators": "^7.0.0-beta.46", + "@babel/plugin-transform-classes": "^7.0.0-beta.46", + "@babel/plugin-transform-destructuring": "^7.0.0-beta.46", + "@babel/plugin-transform-flow-strip-types": "^7.0.0-beta.46", + "@babel/plugin-transform-modules-commonjs": "^7.0.0-beta.46", + "@babel/plugin-transform-spread": "^7.0.0-beta.46", + "@babel/polyfill": "^7.0.0-beta.46", + "@babel/preset-env": "^7.0.0-beta.46", + "@babel/register": "^7.0.0-beta.46", + "babel-eslint": "8.2.3", + "babel-plugin-syntax-async-functions": "6.13.0", + "beautify-benchmark": "0.2.4", + "benchmark": "2.1.4", + "chai": "4.1.2", + "coveralls": "3.0.0", + "eslint": "4.19.1", + "eslint-plugin-babel": "5.1.0", + "eslint-plugin-flowtype": "2.46.3", + "eslint-plugin-prettier": "2.6.0", + "flow-bin": "0.71.0", + "mocha": "5.1.1", + "nyc": "^11.7.1", + "prettier": "1.12.1", + "sane": "2.5.0" + } + } + +yarn manifest: + No manifest + +Lockfile: + # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. + # yarn lockfile v1 + + + "@babel/cli@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.0.0-beta.46.tgz#4fcede56d20659bf063b2c30c444267aa23f1bd3" + dependencies: + commander "^2.8.1" + convert-source-map "^1.1.0" + fs-readdir-recursive "^1.0.0" + glob "^7.0.0" + lodash "^4.2.0" + output-file-sync "^2.0.0" + slash "^1.0.0" + source-map "^0.5.0" + optionalDependencies: + chokidar "^1.6.1" + + "@babel/code-frame@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz#2a02643368de80916162be70865c97774f3adbd9" + dependencies: + "@babel/highlight" "7.0.0-beta.44" + + "@babel/code-frame@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.46.tgz#e0d002100805daab1461c0fcb32a07e304f3a4f4" + dependencies: + "@babel/highlight" "7.0.0-beta.46" + + "@babel/core@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.0.0-beta.46.tgz#dbe2189bcdef9a2c84becb1ec624878d31a95689" + dependencies: + "@babel/code-frame" "7.0.0-beta.46" + "@babel/generator" "7.0.0-beta.46" + "@babel/helpers" "7.0.0-beta.46" + "@babel/template" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + babylon "7.0.0-beta.46" + convert-source-map "^1.1.0" + debug "^3.1.0" + json5 "^0.5.0" + lodash "^4.2.0" + micromatch "^2.3.11" + resolve "^1.3.2" + semver "^5.4.1" + source-map "^0.5.0" + + "@babel/generator@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.44.tgz#c7e67b9b5284afcf69b309b50d7d37f3e5033d42" + dependencies: + "@babel/types" "7.0.0-beta.44" + jsesc "^2.5.1" + lodash "^4.2.0" + source-map "^0.5.0" + trim-right "^1.0.1" + + "@babel/generator@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.46.tgz#6f57159bcc28bf8c3ed6b549789355cebfa3faa7" + dependencies: + "@babel/types" "7.0.0-beta.46" + jsesc "^2.5.1" + lodash "^4.2.0" + source-map "^0.5.0" + trim-right "^1.0.1" + + "@babel/helper-annotate-as-pure@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0-beta.46.tgz#4cd76d5c93409ea01d31be66395a3b98a372792e" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-builder-binary-assignment-operator-visitor@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-builder-binary-assignment-operator-visitor/-/helper-builder-binary-assignment-operator-visitor-7.0.0-beta.46.tgz#b6c8de48693b66bf90239e99856be4c2257e43ba" + dependencies: + "@babel/helper-explode-assignable-expression" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-call-delegate@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-call-delegate/-/helper-call-delegate-7.0.0-beta.46.tgz#a9e8b46cece47726308f015ce979293ef3d36ab7" + dependencies: + "@babel/helper-hoist-variables" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-define-map@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-define-map/-/helper-define-map-7.0.0-beta.46.tgz#994219751ef48bf1ec32604b43935f2b24d617fa" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/helper-explode-assignable-expression@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.0.0-beta.46.tgz#6a34a7533761b97ce4f7bf6fc586dcfb204ffa11" + dependencies: + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-function-name@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz#e18552aaae2231100a6e485e03854bc3532d44dd" + dependencies: + "@babel/helper-get-function-arity" "7.0.0-beta.44" + "@babel/template" "7.0.0-beta.44" + "@babel/types" "7.0.0-beta.44" + + "@babel/helper-function-name@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.46.tgz#d0c4eed2e220e180f91b02e008dcc4594afe1d39" + dependencies: + "@babel/helper-get-function-arity" "7.0.0-beta.46" + "@babel/template" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-get-function-arity@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz#d03ca6dd2b9f7b0b1e6b32c56c72836140db3a15" + dependencies: + "@babel/types" "7.0.0-beta.44" + + "@babel/helper-get-function-arity@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.46.tgz#7161bfe449b4183dbe25d1fe5579338b7429e5f2" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-hoist-variables@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.0.0-beta.46.tgz#2d656215bea3f044ff1ee391fc51d55fce46ddf5" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-member-expression-to-functions@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.0.0-beta.46.tgz#736344c1d68fb2c4b75cbe62370eb610c0578427" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-module-imports@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.0.0-beta.46.tgz#8bd2e1fcfae883d28149a350e31ce606aa24eda6" + dependencies: + "@babel/types" "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/helper-module-transforms@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.0.0-beta.46.tgz#90ad981f3a0020d9a8e526296555a5dd7e87cf5e" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.46" + "@babel/helper-simple-access" "7.0.0-beta.46" + "@babel/helper-split-export-declaration" "7.0.0-beta.46" + "@babel/template" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/helper-optimise-call-expression@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.0.0-beta.46.tgz#50f060b4e4af01c73b40986fa593ae7958422e89" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-plugin-utils@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0-beta.46.tgz#f630adbd9d645d0ba2e43f4955b4ad61f44ccdf4" + + "@babel/helper-regex@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-regex/-/helper-regex-7.0.0-beta.46.tgz#df3675cec700e062d823225c52830e012f32308f" + dependencies: + lodash "^4.2.0" + + "@babel/helper-remap-async-to-generator@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-remap-async-to-generator/-/helper-remap-async-to-generator-7.0.0-beta.46.tgz#275d455dbced4c807543f001302a40303a3f0914" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.46" + "@babel/helper-wrap-function" "7.0.0-beta.46" + "@babel/template" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-replace-supers@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.0.0-beta.46.tgz#921c0f25d875026a8fb12feda1b72323595ea156" + dependencies: + "@babel/helper-member-expression-to-functions" "7.0.0-beta.46" + "@babel/helper-optimise-call-expression" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-simple-access@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.0.0-beta.46.tgz#8eb0edf978c85915d11b6a7aa8591434e158170d" + dependencies: + "@babel/template" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/helper-split-export-declaration@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz#c0b351735e0fbcb3822c8ad8db4e583b05ebd9dc" + dependencies: + "@babel/types" "7.0.0-beta.44" + + "@babel/helper-split-export-declaration@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.46.tgz#6903893c72bb2a3d54ed20b5ff2aa8a28e8d2ea1" + dependencies: + "@babel/types" "7.0.0-beta.46" + + "@babel/helper-wrap-function@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helper-wrap-function/-/helper-wrap-function-7.0.0-beta.46.tgz#d0fb836516d8a38ab80df1b434e4b76015be9035" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/template" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/helpers@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.0.0-beta.46.tgz#b5f988dfd77f4f713792cf7818b687050736ee52" + dependencies: + "@babel/template" "7.0.0-beta.46" + "@babel/traverse" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + + "@babel/highlight@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.44.tgz#18c94ce543916a80553edcdcf681890b200747d5" + dependencies: + chalk "^2.0.0" + esutils "^2.0.2" + js-tokens "^3.0.0" + + "@babel/highlight@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.46.tgz#c553c51e65f572bdedd6eff66fc0bb563016645e" + dependencies: + chalk "^2.0.0" + esutils "^2.0.2" + js-tokens "^3.0.0" + + "@babel/node@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/node/-/node-7.0.0-beta.46.tgz#88ec3ca630b93829fa9422af4bd8ba964c2ddd76" + dependencies: + "@babel/polyfill" "7.0.0-beta.46" + "@babel/register" "7.0.0-beta.46" + commander "^2.8.1" + fs-readdir-recursive "^1.0.0" + lodash "^4.2.0" + output-file-sync "^2.0.0" + v8flags "^3.0.0" + + "@babel/plugin-proposal-async-generator-functions@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.0.0-beta.46.tgz#395330d1d5d7fb76c33b7bd99750adeafc37c68c" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-remap-async-to-generator" "7.0.0-beta.46" + "@babel/plugin-syntax-async-generators" "7.0.0-beta.46" + + "@babel/plugin-proposal-class-properties@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.0.0-beta.46.tgz#1c505f8df3a312beb41c88d74209d5b6d537fa3d" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-replace-supers" "7.0.0-beta.46" + "@babel/plugin-syntax-class-properties" "7.0.0-beta.46" + + "@babel/plugin-proposal-object-rest-spread@7.0.0-beta.46", "@babel/plugin-proposal-object-rest-spread@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.0.0-beta.46.tgz#fb3979488a52c1246cdced4a438ace0f47ac985b" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/plugin-syntax-object-rest-spread" "7.0.0-beta.46" + + "@babel/plugin-proposal-optional-catch-binding@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-catch-binding/-/plugin-proposal-optional-catch-binding-7.0.0-beta.46.tgz#fda50deaab3272500a8a1c7088d7d55148f54048" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/plugin-syntax-optional-catch-binding" "7.0.0-beta.46" + + "@babel/plugin-proposal-unicode-property-regex@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.0.0-beta.46.tgz#b422a602094d7feeea4a7b81e7e32d1687337123" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-regex" "7.0.0-beta.46" + regexpu-core "^4.1.3" + + "@babel/plugin-syntax-async-generators@7.0.0-beta.46", "@babel/plugin-syntax-async-generators@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.0.0-beta.46.tgz#b35149e02748922d8e39506b0ac001a27bf449ed" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-syntax-class-properties@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-class-properties/-/plugin-syntax-class-properties-7.0.0-beta.46.tgz#dad4df6c31b65ba359fec3b02fb8413896e75efc" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-syntax-flow@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.0.0-beta.46.tgz#f9940274770945cc758a947944949e70ea530e7f" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-syntax-object-rest-spread@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.0.0-beta.46.tgz#03d46637f549757b2d6877b6449901698059d7d8" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-syntax-optional-catch-binding@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-catch-binding/-/plugin-syntax-optional-catch-binding-7.0.0-beta.46.tgz#701ba500cc154dd87c4d16a41fa858e9ffc6db89" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-arrow-functions@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.0.0-beta.46.tgz#130e79b1d4508767c47e5febb809f8dca80c05f5" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-async-to-generator@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-async-to-generator/-/plugin-transform-async-to-generator-7.0.0-beta.46.tgz#29fd5967f5056ca80f3a97db4d2ffa38a0dc2dce" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-remap-async-to-generator" "7.0.0-beta.46" + + "@babel/plugin-transform-block-scoped-functions@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-block-scoped-functions/-/plugin-transform-block-scoped-functions-7.0.0-beta.46.tgz#0925a549931f61b45880618b0b42da4790b7c0b3" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-block-scoping@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-block-scoping/-/plugin-transform-block-scoping-7.0.0-beta.46.tgz#da42dd17fbed675c72233988dbad9ace5ab9e4a7" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/plugin-transform-classes@7.0.0-beta.46", "@babel/plugin-transform-classes@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-classes/-/plugin-transform-classes-7.0.0-beta.46.tgz#00c856feda2ee756c4cc6ef8c97d17d070acebf7" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.46" + "@babel/helper-define-map" "7.0.0-beta.46" + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/helper-optimise-call-expression" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-replace-supers" "7.0.0-beta.46" + "@babel/helper-split-export-declaration" "7.0.0-beta.46" + globals "^11.1.0" + + "@babel/plugin-transform-computed-properties@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-computed-properties/-/plugin-transform-computed-properties-7.0.0-beta.46.tgz#ca1ece27615f7324345713fb6a93dd288788e891" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-destructuring@7.0.0-beta.46", "@babel/plugin-transform-destructuring@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-destructuring/-/plugin-transform-destructuring-7.0.0-beta.46.tgz#6e6a097da31063f545f7818afe48ef09165ce5ff" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-dotall-regex@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-dotall-regex/-/plugin-transform-dotall-regex-7.0.0-beta.46.tgz#e5bbd78c1a94455e6d5dd1c77f32357b84355e06" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-regex" "7.0.0-beta.46" + regexpu-core "^4.1.3" + + "@babel/plugin-transform-duplicate-keys@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-duplicate-keys/-/plugin-transform-duplicate-keys-7.0.0-beta.46.tgz#7e94e42099b099742617838237b0d6e1a9b2690f" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-exponentiation-operator@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-exponentiation-operator/-/plugin-transform-exponentiation-operator-7.0.0-beta.46.tgz#95ae2e03456e417d2f5eace6d05a8fccb7af1bcc" + dependencies: + "@babel/helper-builder-binary-assignment-operator-visitor" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-flow-strip-types@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.0.0-beta.46.tgz#3c26def3c4027d5c0c3f98c3b6f161c715ab7fff" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/plugin-syntax-flow" "7.0.0-beta.46" + + "@babel/plugin-transform-for-of@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-for-of/-/plugin-transform-for-of-7.0.0-beta.46.tgz#ce643487384c96d1bd1f57a112b2ccba6c34da5c" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-function-name@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-function-name/-/plugin-transform-function-name-7.0.0-beta.46.tgz#2479f5188de9ab1f99396bce83b3b9d39bc13bdb" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-literals@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-literals/-/plugin-transform-literals-7.0.0-beta.46.tgz#84f5bcfe914b9fd4385c0ddf469f9ed403ee68bd" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-modules-amd@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-amd/-/plugin-transform-modules-amd-7.0.0-beta.46.tgz#01aeb4887c7df7059cefe4a206eefdf190c79f48" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-modules-commonjs@7.0.0-beta.46", "@babel/plugin-transform-modules-commonjs@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-commonjs/-/plugin-transform-modules-commonjs-7.0.0-beta.46.tgz#9dcb42e1282b281c1a2075f98b4a850533acfd9c" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-simple-access" "7.0.0-beta.46" + + "@babel/plugin-transform-modules-systemjs@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-systemjs/-/plugin-transform-modules-systemjs-7.0.0-beta.46.tgz#313e13e8edccaae6c645e3798a043521cf73df04" + dependencies: + "@babel/helper-hoist-variables" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-modules-umd@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-umd/-/plugin-transform-modules-umd-7.0.0-beta.46.tgz#ad0ef488a123f479825c1ffe75c5bba9954a449c" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-new-target@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-new-target/-/plugin-transform-new-target-7.0.0-beta.46.tgz#e3219c15a2175a29afa33b9b2f4c18dc1ae3c8cc" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-object-super@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-super/-/plugin-transform-object-super-7.0.0-beta.46.tgz#b5376fe93f5e154b765468f1a58a717717f95827" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-replace-supers" "7.0.0-beta.46" + + "@babel/plugin-transform-parameters@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-parameters/-/plugin-transform-parameters-7.0.0-beta.46.tgz#33bbd2e3bd499d99016034dcaf8c6b72c2a69ec3" + dependencies: + "@babel/helper-call-delegate" "7.0.0-beta.46" + "@babel/helper-get-function-arity" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-regenerator@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.0.0-beta.46.tgz#875ceb5b37ec0e898c23b60af760715d9d462b4f" + dependencies: + regenerator-transform "^0.12.3" + + "@babel/plugin-transform-shorthand-properties@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.0.0-beta.46.tgz#aa21512b0fef7b916fc5cbc87df717465c25515c" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-spread@7.0.0-beta.46", "@babel/plugin-transform-spread@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-spread/-/plugin-transform-spread-7.0.0-beta.46.tgz#48eabb219f1e0c16e9b0a6166072ae9d4c7cd397" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-sticky-regex@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-sticky-regex/-/plugin-transform-sticky-regex-7.0.0-beta.46.tgz#c96c41f31272ec1cdc47dd91a22c6d75c4db70d2" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-regex" "7.0.0-beta.46" + + "@babel/plugin-transform-template-literals@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-template-literals/-/plugin-transform-template-literals-7.0.0-beta.46.tgz#e8bcc798dece29807893e8ee27ccf3176f658c62" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-typeof-symbol@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typeof-symbol/-/plugin-transform-typeof-symbol-7.0.0-beta.46.tgz#643529184cbb07199237c94537c89ea9a721fa0a" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + + "@babel/plugin-transform-unicode-regex@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.0.0-beta.46.tgz#10e6edcc8eb0db71ff2f0e3fc87ed88337d24fb9" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/helper-regex" "7.0.0-beta.46" + regexpu-core "^4.1.3" + + "@babel/polyfill@7.0.0-beta.46", "@babel/polyfill@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.0.0-beta.46.tgz#5a4203a3abee8ddfb80afd3cf6f5ff1391750695" + dependencies: + core-js "^2.5.3" + regenerator-runtime "^0.11.1" + + "@babel/preset-env@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.0.0-beta.46.tgz#ae1b731ef71c2bb50c47e0cda4b6359ea2c61f09" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.46" + "@babel/helper-plugin-utils" "7.0.0-beta.46" + "@babel/plugin-proposal-async-generator-functions" "7.0.0-beta.46" + "@babel/plugin-proposal-object-rest-spread" "7.0.0-beta.46" + "@babel/plugin-proposal-optional-catch-binding" "7.0.0-beta.46" + "@babel/plugin-proposal-unicode-property-regex" "7.0.0-beta.46" + "@babel/plugin-syntax-async-generators" "7.0.0-beta.46" + "@babel/plugin-syntax-object-rest-spread" "7.0.0-beta.46" + "@babel/plugin-syntax-optional-catch-binding" "7.0.0-beta.46" + "@babel/plugin-transform-arrow-functions" "7.0.0-beta.46" + "@babel/plugin-transform-async-to-generator" "7.0.0-beta.46" + "@babel/plugin-transform-block-scoped-functions" "7.0.0-beta.46" + "@babel/plugin-transform-block-scoping" "7.0.0-beta.46" + "@babel/plugin-transform-classes" "7.0.0-beta.46" + "@babel/plugin-transform-computed-properties" "7.0.0-beta.46" + "@babel/plugin-transform-destructuring" "7.0.0-beta.46" + "@babel/plugin-transform-dotall-regex" "7.0.0-beta.46" + "@babel/plugin-transform-duplicate-keys" "7.0.0-beta.46" + "@babel/plugin-transform-exponentiation-operator" "7.0.0-beta.46" + "@babel/plugin-transform-for-of" "7.0.0-beta.46" + "@babel/plugin-transform-function-name" "7.0.0-beta.46" + "@babel/plugin-transform-literals" "7.0.0-beta.46" + "@babel/plugin-transform-modules-amd" "7.0.0-beta.46" + "@babel/plugin-transform-modules-commonjs" "7.0.0-beta.46" + "@babel/plugin-transform-modules-systemjs" "7.0.0-beta.46" + "@babel/plugin-transform-modules-umd" "7.0.0-beta.46" + "@babel/plugin-transform-new-target" "7.0.0-beta.46" + "@babel/plugin-transform-object-super" "7.0.0-beta.46" + "@babel/plugin-transform-parameters" "7.0.0-beta.46" + "@babel/plugin-transform-regenerator" "7.0.0-beta.46" + "@babel/plugin-transform-shorthand-properties" "7.0.0-beta.46" + "@babel/plugin-transform-spread" "7.0.0-beta.46" + "@babel/plugin-transform-sticky-regex" "7.0.0-beta.46" + "@babel/plugin-transform-template-literals" "7.0.0-beta.46" + "@babel/plugin-transform-typeof-symbol" "7.0.0-beta.46" + "@babel/plugin-transform-unicode-regex" "7.0.0-beta.46" + browserslist "^3.0.0" + invariant "^2.2.2" + semver "^5.3.0" + + "@babel/register@7.0.0-beta.46", "@babel/register@^7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/register/-/register-7.0.0-beta.46.tgz#695629b28902b832be02b418c96e17e6b099e9d5" + dependencies: + core-js "^2.5.3" + find-cache-dir "^1.0.0" + home-or-tmp "^3.0.0" + lodash "^4.2.0" + mkdirp "^0.5.1" + pirates "^3.0.1" + source-map-support "^0.4.2" + + "@babel/template@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f" + dependencies: + "@babel/code-frame" "7.0.0-beta.44" + "@babel/types" "7.0.0-beta.44" + babylon "7.0.0-beta.44" + lodash "^4.2.0" + + "@babel/template@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.46.tgz#8b23982411d5b5dbfa479437bfe414adb1411bb9" + dependencies: + "@babel/code-frame" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + babylon "7.0.0-beta.46" + lodash "^4.2.0" + + "@babel/traverse@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.44.tgz#a970a2c45477ad18017e2e465a0606feee0d2966" + dependencies: + "@babel/code-frame" "7.0.0-beta.44" + "@babel/generator" "7.0.0-beta.44" + "@babel/helper-function-name" "7.0.0-beta.44" + "@babel/helper-split-export-declaration" "7.0.0-beta.44" + "@babel/types" "7.0.0-beta.44" + babylon "7.0.0-beta.44" + debug "^3.1.0" + globals "^11.1.0" + invariant "^2.2.0" + lodash "^4.2.0" + + "@babel/traverse@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.46.tgz#29a0c0395b3642f0297e6f8e475bde89f9343755" + dependencies: + "@babel/code-frame" "7.0.0-beta.46" + "@babel/generator" "7.0.0-beta.46" + "@babel/helper-function-name" "7.0.0-beta.46" + "@babel/helper-split-export-declaration" "7.0.0-beta.46" + "@babel/types" "7.0.0-beta.46" + babylon "7.0.0-beta.46" + debug "^3.1.0" + globals "^11.1.0" + invariant "^2.2.0" + lodash "^4.2.0" + + "@babel/types@7.0.0-beta.44": + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.44.tgz#6b1b164591f77dec0a0342aca995f2d046b3a757" + dependencies: + esutils "^2.0.2" + lodash "^4.2.0" + to-fast-properties "^2.0.0" + + "@babel/types@7.0.0-beta.46": + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.46.tgz#eb84399a699af9fcb244440cce78e1acbeb40e0c" + dependencies: + esutils "^2.0.2" + lodash "^4.2.0" + to-fast-properties "^2.0.0" + + abbrev@1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" + + acorn-jsx@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-3.0.1.tgz#afdf9488fb1ecefc8348f6fb22f464e32a58b36b" + dependencies: + acorn "^3.0.4" + + acorn@^3.0.4: + version "3.3.0" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" + + acorn@^5.5.0: + version "5.5.3" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.5.3.tgz#f473dd47e0277a08e28e9bec5aeeb04751f0b8c9" + + ajv-keywords@^2.1.0: + version "2.1.1" + resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762" + + ajv@^4.9.1: + version "4.11.8" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-4.11.8.tgz#82ffb02b29e662ae53bdc20af15947706739c536" + dependencies: + co "^4.6.0" + json-stable-stringify "^1.0.1" + + ajv@^5.1.0, ajv@^5.3.0: + version "5.5.2" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965" + dependencies: + co "^4.6.0" + fast-deep-equal "^1.0.0" + fast-json-stable-stringify "^2.0.0" + json-schema-traverse "^0.3.0" + + ajv@^5.2.3: + version "5.5.0" + resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.0.tgz#eb2840746e9dc48bd5e063a36e3fd400c5eab5a9" + dependencies: + co "^4.6.0" + fast-deep-equal "^1.0.0" + fast-json-stable-stringify "^2.0.0" + json-schema-traverse "^0.3.0" + + align-text@^0.1.1, align-text@^0.1.3: + version "0.1.4" + resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117" + dependencies: + kind-of "^3.0.2" + longest "^1.0.1" + repeat-string "^1.5.2" + + amdefine@>=0.0.4: + version "1.0.1" + resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5" + + ansi-escapes@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.0.0.tgz#ec3e8b4e9f8064fc02c3ac9b65f1c275bda8ef92" + + ansi-regex@^2.0.0: + version "2.1.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-2.1.1.tgz#c3b33ab5ee360d86e0e628f0468ae7ef27d654df" + + ansi-regex@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-3.0.0.tgz#ed0317c322064f79466c02966bddb605ab37d998" + + ansi-styles@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe" + + ansi-styles@^3.1.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88" + dependencies: + color-convert "^1.9.0" + + anymatch@^1.3.0: + version "1.3.2" + resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a" + dependencies: + micromatch "^2.1.5" + normalize-path "^2.0.0" + + anymatch@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb" + dependencies: + micromatch "^3.1.4" + normalize-path "^2.1.1" + + append-transform@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/append-transform/-/append-transform-0.4.0.tgz#d76ebf8ca94d276e247a36bad44a4b74ab611991" + dependencies: + default-require-extensions "^1.0.0" + + aproba@^1.0.3: + version "1.2.0" + resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a" + + archy@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/archy/-/archy-1.0.0.tgz#f9c8c13757cc1dd7bc379ac77b2c62a5c2868c40" + + are-we-there-yet@~1.1.2: + version "1.1.4" + resolved "https://registry.yarnpkg.com/are-we-there-yet/-/are-we-there-yet-1.1.4.tgz#bb5dca382bb94f05e15194373d16fd3ba1ca110d" + dependencies: + delegates "^1.0.0" + readable-stream "^2.0.6" + + argparse@^1.0.7: + version "1.0.9" + resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.9.tgz#73d83bc263f86e97f8cc4f6bae1b0e90a7d22c86" + dependencies: + sprintf-js "~1.0.2" + + arr-diff@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-2.0.0.tgz#8f3b827f955a8bd669697e4a4256ac3ceae356cf" + dependencies: + arr-flatten "^1.0.1" + + arr-diff@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/arr-diff/-/arr-diff-4.0.0.tgz#d6461074febfec71e7e15235761a329a5dc7c520" + + arr-flatten@^1.0.1, arr-flatten@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/arr-flatten/-/arr-flatten-1.1.0.tgz#36048bbff4e7b47e136644316c99669ea5ae91f1" + + arr-union@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/arr-union/-/arr-union-3.1.0.tgz#e39b09aea9def866a8f206e288af63919bae39c4" + + array-union@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/array-union/-/array-union-1.0.2.tgz#9a34410e4f4e3da23dea375be5be70f24778ec39" + dependencies: + array-uniq "^1.0.1" + + array-uniq@^1.0.1: + version "1.0.3" + resolved "https://registry.yarnpkg.com/array-uniq/-/array-uniq-1.0.3.tgz#af6ac877a25cc7f74e058894753858dfdb24fdb6" + + array-unique@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.2.1.tgz#a1d97ccafcbc2625cc70fadceb36a50c58b01a53" + + array-unique@^0.3.2: + version "0.3.2" + resolved "https://registry.yarnpkg.com/array-unique/-/array-unique-0.3.2.tgz#a894b75d4bc4f6cd679ef3244a9fd8f46ae2d428" + + arrify@^1.0.0, arrify@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d" + + asn1@~0.2.3: + version "0.2.3" + resolved "https://registry.yarnpkg.com/asn1/-/asn1-0.2.3.tgz#dac8787713c9966849fc8180777ebe9c1ddf3b86" + + assert-plus@1.0.0, assert-plus@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-1.0.0.tgz#f12e0f3c5d77b0b1cdd9146942e4e96c1e4dd525" + + assert-plus@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-0.2.0.tgz#d74e1b87e7affc0db8aadb7021f3fe48101ab234" + + assertion-error@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.2.tgz#13ca515d86206da0bac66e834dd397d87581094c" + + assign-symbols@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/assign-symbols/-/assign-symbols-1.0.0.tgz#59667f41fadd4f20ccbc2bb96b8d4f7f78ec0367" + + async-each@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d" + + async@^1.4.0: + version "1.5.2" + resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" + + asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + + atob@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.0.tgz#ab2b150e51d7b122b9efc8d7340c06b6c41076bc" + + aws-sign2@~0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f" + + aws-sign2@~0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" + + aws4@^1.2.1, aws4@^1.6.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e" + + babel-code-frame@^6.22.0, babel-code-frame@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" + dependencies: + chalk "^1.1.3" + esutils "^2.0.2" + js-tokens "^3.0.2" + + babel-eslint@8.2.3: + version "8.2.3" + resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.3.tgz#1a2e6681cc9bc4473c32899e59915e19cd6733cf" + dependencies: + "@babel/code-frame" "7.0.0-beta.44" + "@babel/traverse" "7.0.0-beta.44" + "@babel/types" "7.0.0-beta.44" + babylon "7.0.0-beta.44" + eslint-scope "~3.7.1" + eslint-visitor-keys "^1.0.0" + + babel-generator@^6.18.0: + version "6.26.1" + resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90" + dependencies: + babel-messages "^6.23.0" + babel-runtime "^6.26.0" + babel-types "^6.26.0" + detect-indent "^4.0.0" + jsesc "^1.3.0" + lodash "^4.17.4" + source-map "^0.5.7" + trim-right "^1.0.1" + + babel-messages@^6.23.0: + version "6.23.0" + resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" + dependencies: + babel-runtime "^6.22.0" + + babel-plugin-syntax-async-functions@6.13.0: + version "6.13.0" + resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" + + babel-runtime@^6.22.0, babel-runtime@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" + dependencies: + core-js "^2.4.0" + regenerator-runtime "^0.11.0" + + babel-template@^6.16.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02" + dependencies: + babel-runtime "^6.26.0" + babel-traverse "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + lodash "^4.17.4" + + babel-traverse@^6.18.0, babel-traverse@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" + dependencies: + babel-code-frame "^6.26.0" + babel-messages "^6.23.0" + babel-runtime "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + debug "^2.6.8" + globals "^9.18.0" + invariant "^2.2.2" + lodash "^4.17.4" + + babel-types@^6.18.0, babel-types@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497" + dependencies: + babel-runtime "^6.26.0" + esutils "^2.0.2" + lodash "^4.17.4" + to-fast-properties "^1.0.3" + + babylon@7.0.0-beta.44: + version "7.0.0-beta.44" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.44.tgz#89159e15e6e30c5096e22d738d8c0af8a0e8ca1d" + + babylon@7.0.0-beta.46: + version "7.0.0-beta.46" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.46.tgz#b6ddaba81bbb130313932757ff9c195d527088b6" + + babylon@^6.18.0: + version "6.18.0" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" + + balanced-match@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" + + base@^0.11.1: + version "0.11.2" + resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f" + dependencies: + cache-base "^1.0.1" + class-utils "^0.3.5" + component-emitter "^1.2.1" + define-property "^1.0.0" + isobject "^3.0.1" + mixin-deep "^1.2.0" + pascalcase "^0.1.1" + + bcrypt-pbkdf@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.1.tgz#63bc5dcb61331b92bc05fd528953c33462a06f8d" + dependencies: + tweetnacl "^0.14.3" + + beautify-benchmark@0.2.4: + version "0.2.4" + resolved "https://registry.yarnpkg.com/beautify-benchmark/-/beautify-benchmark-0.2.4.tgz#3151def14c1a2e0d07ff2e476861c7ed0e1ae39b" + + benchmark@2.1.4: + version "2.1.4" + resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629" + dependencies: + lodash "^4.17.4" + platform "^1.3.3" + + binary-extensions@^1.0.0: + version "1.11.0" + resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.11.0.tgz#46aa1751fb6a2f93ee5e689bb1087d4b14c6c205" + + block-stream@*: + version "0.0.9" + resolved "https://registry.yarnpkg.com/block-stream/-/block-stream-0.0.9.tgz#13ebfe778a03205cfe03751481ebb4b3300c126a" + dependencies: + inherits "~2.0.0" + + boom@2.x.x: + version "2.10.1" + resolved "https://registry.yarnpkg.com/boom/-/boom-2.10.1.tgz#39c8918ceff5799f83f9492a848f625add0c766f" + dependencies: + hoek "2.x.x" + + boom@4.x.x: + version "4.3.1" + resolved "https://registry.yarnpkg.com/boom/-/boom-4.3.1.tgz#4f8a3005cb4a7e3889f749030fd25b96e01d2e31" + dependencies: + hoek "4.x.x" + + boom@5.x.x: + version "5.2.0" + resolved "https://registry.yarnpkg.com/boom/-/boom-5.2.0.tgz#5dd9da6ee3a5f302077436290cb717d3f4a54e02" + dependencies: + hoek "4.x.x" + + brace-expansion@^1.1.7: + version "1.1.8" + resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.8.tgz#c07b211c7c952ec1f8efd51a77ef0d1d3990a292" + dependencies: + balanced-match "^1.0.0" + concat-map "0.0.1" + + braces@^1.8.2: + version "1.8.5" + resolved "https://registry.yarnpkg.com/braces/-/braces-1.8.5.tgz#ba77962e12dff969d6b76711e914b737857bf6a7" + dependencies: + expand-range "^1.8.1" + preserve "^0.2.0" + repeat-element "^1.1.2" + + braces@^2.3.1: + version "2.3.2" + resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729" + dependencies: + arr-flatten "^1.1.0" + array-unique "^0.3.2" + extend-shallow "^2.0.1" + fill-range "^4.0.0" + isobject "^3.0.1" + repeat-element "^1.1.2" + snapdragon "^0.8.1" + snapdragon-node "^2.0.1" + split-string "^3.0.2" + to-regex "^3.0.1" + + browser-stdout@1.3.1: + version "1.3.1" + resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60" + + browserslist@^3.0.0: + version "3.2.7" + resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-3.2.7.tgz#aa488634d320b55e88bab0256184dbbcca1e6de9" + dependencies: + caniuse-lite "^1.0.30000835" + electron-to-chromium "^1.3.45" + + bser@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/bser/-/bser-2.0.0.tgz#9ac78d3ed5d915804fd87acb158bc797147a1719" + dependencies: + node-int64 "^0.4.0" + + builtin-modules@^1.0.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" + + cache-base@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2" + dependencies: + collection-visit "^1.0.0" + component-emitter "^1.2.1" + get-value "^2.0.6" + has-value "^1.0.0" + isobject "^3.0.1" + set-value "^2.0.0" + to-object-path "^0.3.0" + union-value "^1.0.0" + unset-value "^1.0.0" + + caching-transform@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/caching-transform/-/caching-transform-1.0.1.tgz#6dbdb2f20f8d8fbce79f3e94e9d1742dcdf5c0a1" + dependencies: + md5-hex "^1.2.0" + mkdirp "^0.5.1" + write-file-atomic "^1.1.4" + + caller-path@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/caller-path/-/caller-path-0.1.0.tgz#94085ef63581ecd3daa92444a8fe94e82577751f" + dependencies: + callsites "^0.2.0" + + callsites@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/callsites/-/callsites-0.2.0.tgz#afab96262910a7f33c19a5775825c69f34e350ca" + + camelcase@^1.0.2: + version "1.2.1" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-1.2.1.tgz#9bb5304d2e0b56698b2c758b08a3eaa9daa58a39" + + camelcase@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" + + caniuse-lite@^1.0.30000835: + version "1.0.30000840" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000840.tgz#344513f8f843536cf99694964c09811277eee395" + + caseless@~0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" + + center-align@^0.1.1: + version "0.1.3" + resolved "https://registry.yarnpkg.com/center-align/-/center-align-0.1.3.tgz#aa0d32629b6ee972200411cbd4461c907bc2b7ad" + dependencies: + align-text "^0.1.3" + lazy-cache "^1.0.3" + + chai@4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c" + dependencies: + assertion-error "^1.0.1" + check-error "^1.0.1" + deep-eql "^3.0.0" + get-func-name "^2.0.0" + pathval "^1.0.0" + type-detect "^4.0.0" + + chalk@^1.1.3: + version "1.1.3" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" + dependencies: + ansi-styles "^2.2.1" + escape-string-regexp "^1.0.2" + has-ansi "^2.0.0" + strip-ansi "^3.0.0" + supports-color "^2.0.0" + + chalk@^2.0.0, chalk@^2.1.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.3.0.tgz#b5ea48efc9c1793dccc9b4767c93914d3f2d52ba" + dependencies: + ansi-styles "^3.1.0" + escape-string-regexp "^1.0.5" + supports-color "^4.0.0" + + chardet@^0.4.0: + version "0.4.2" + resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.4.2.tgz#b5473b33dc97c424e5d98dc87d55d4d8a29c8bf2" + + check-error@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" + + chokidar@^1.6.1: + version "1.7.0" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" + dependencies: + anymatch "^1.3.0" + async-each "^1.0.0" + glob-parent "^2.0.0" + inherits "^2.0.1" + is-binary-path "^1.0.0" + is-glob "^2.0.0" + path-is-absolute "^1.0.0" + readdirp "^2.0.0" + optionalDependencies: + fsevents "^1.0.0" + + circular-json@^0.3.1: + version "0.3.3" + resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.3.3.tgz#815c99ea84f6809529d2f45791bdf82711352d66" + + class-utils@^0.3.5: + version "0.3.6" + resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463" + dependencies: + arr-union "^3.1.0" + define-property "^0.2.5" + isobject "^3.0.0" + static-extend "^0.1.1" + + cli-cursor@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5" + dependencies: + restore-cursor "^2.0.0" + + cli-width@^2.0.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639" + + cliui@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/cliui/-/cliui-2.1.0.tgz#4b475760ff80264c762c3a1719032e91c7fea0d1" + dependencies: + center-align "^0.1.1" + right-align "^0.1.1" + wordwrap "0.0.2" + + cliui@^4.0.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/cliui/-/cliui-4.1.0.tgz#348422dbe82d800b3022eef4f6ac10bf2e4d1b49" + dependencies: + string-width "^2.1.1" + strip-ansi "^4.0.0" + wrap-ansi "^2.0.0" + + co@^4.6.0: + version "4.6.0" + resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" + + code-point-at@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" + + collection-visit@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/collection-visit/-/collection-visit-1.0.0.tgz#4bc0373c164bc3291b4d368c829cf1a80a59dca0" + dependencies: + map-visit "^1.0.0" + object-visit "^1.0.0" + + color-convert@^1.9.0: + version "1.9.1" + resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed" + dependencies: + color-name "^1.1.1" + + color-name@^1.1.1: + version "1.1.3" + resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" + + combined-stream@^1.0.5, combined-stream@~1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009" + dependencies: + delayed-stream "~1.0.0" + + commander@2.11.0: + version "2.11.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" + + commander@^2.8.1: + version "2.15.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" + + commondir@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" + + component-emitter@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6" + + concat-map@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" + + concat-stream@^1.6.0: + version "1.6.0" + resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.0.tgz#0aac662fd52be78964d5532f694784e70110acf7" + dependencies: + inherits "^2.0.3" + readable-stream "^2.2.2" + typedarray "^0.0.6" + + console-control-strings@^1.0.0, console-control-strings@~1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" + + convert-source-map@^1.1.0, convert-source-map@^1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5" + + copy-descriptor@^0.1.0: + version "0.1.1" + resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" + + core-js@^2.4.0: + version "2.5.1" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.1.tgz#ae6874dc66937789b80754ff5428df66819ca50b" + + core-js@^2.5.3: + version "2.5.6" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.6.tgz#0fe6d45bf3cac3ac364a9d72de7576f4eb221b9d" + + core-util-is@1.0.2, core-util-is@~1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" + + coveralls@3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.0.tgz#22ef730330538080d29b8c151dc9146afde88a99" + dependencies: + js-yaml "^3.6.1" + lcov-parse "^0.0.10" + log-driver "^1.2.5" + minimist "^1.2.0" + request "^2.79.0" + + cross-spawn@^4: + version "4.0.2" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41" + dependencies: + lru-cache "^4.0.1" + which "^1.2.9" + + cross-spawn@^5.0.1, cross-spawn@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" + dependencies: + lru-cache "^4.0.1" + shebang-command "^1.2.0" + which "^1.2.9" + + cryptiles@2.x.x: + version "2.0.5" + resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8" + dependencies: + boom "2.x.x" + + cryptiles@3.x.x: + version "3.1.2" + resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-3.1.2.tgz#a89fbb220f5ce25ec56e8c4aa8a4fd7b5b0d29fe" + dependencies: + boom "5.x.x" + + dashdash@^1.12.0: + version "1.14.1" + resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0" + dependencies: + assert-plus "^1.0.0" + + debug-log@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/debug-log/-/debug-log-1.0.1.tgz#2307632d4c04382b8df8a32f70b895046d52745f" + + debug@3.1.0, debug@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" + dependencies: + ms "2.0.0" + + debug@^2.2.0, debug@^2.3.3, debug@^2.6.8: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + dependencies: + ms "2.0.0" + + decamelize@^1.0.0, decamelize@^1.1.1: + version "1.2.0" + resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" + + decode-uri-component@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545" + + deep-eql@^3.0.0: + version "3.0.1" + resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df" + dependencies: + type-detect "^4.0.0" + + deep-extend@~0.4.0: + version "0.4.2" + resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.4.2.tgz#48b699c27e334bf89f10892be432f6e4c7d34a7f" + + deep-is@~0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34" + + default-require-extensions@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/default-require-extensions/-/default-require-extensions-1.0.0.tgz#f37ea15d3e13ffd9b437d33e1a75b5fb97874cb8" + dependencies: + strip-bom "^2.0.0" + + define-property@^0.2.5: + version "0.2.5" + resolved "https://registry.yarnpkg.com/define-property/-/define-property-0.2.5.tgz#c35b1ef918ec3c990f9a5bc57be04aacec5c8116" + dependencies: + is-descriptor "^0.1.0" + + define-property@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/define-property/-/define-property-1.0.0.tgz#769ebaaf3f4a63aad3af9e8d304c9bbe79bfb0e6" + dependencies: + is-descriptor "^1.0.0" + + define-property@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/define-property/-/define-property-2.0.2.tgz#d459689e8d654ba77e02a817f8710d702cb16e9d" + dependencies: + is-descriptor "^1.0.2" + isobject "^3.0.1" + + del@^2.0.2: + version "2.2.2" + resolved "https://registry.yarnpkg.com/del/-/del-2.2.2.tgz#c12c981d067846c84bcaf862cff930d907ffd1a8" + dependencies: + globby "^5.0.0" + is-path-cwd "^1.0.0" + is-path-in-cwd "^1.0.0" + object-assign "^4.0.1" + pify "^2.0.0" + pinkie-promise "^2.0.0" + rimraf "^2.2.8" + + delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + + delegates@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a" + + detect-indent@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-4.0.0.tgz#f76d064352cdf43a1cb6ce619c4ee3a9475de208" + dependencies: + repeating "^2.0.0" + + detect-libc@^1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b" + + diff@3.5.0: + version "3.5.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" + + doctrine@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d" + dependencies: + esutils "^2.0.2" + + ecc-jsbn@~0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.1.tgz#0fc73a9ed5f0d53c38193398523ef7e543777505" + dependencies: + jsbn "~0.1.0" + + electron-to-chromium@^1.3.45: + version "1.3.45" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.45.tgz#458ac1b1c5c760ce8811a16d2bfbd97ec30bafb8" + + error-ex@^1.2.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" + dependencies: + is-arrayish "^0.2.1" + + escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" + + eslint-plugin-babel@5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-babel/-/eslint-plugin-babel-5.1.0.tgz#9c76e476162041e50b6ba69aa4eae3bdd6a4e1c3" + dependencies: + eslint-rule-composer "^0.3.0" + + eslint-plugin-flowtype@2.46.3: + version "2.46.3" + resolved "https://registry.yarnpkg.com/eslint-plugin-flowtype/-/eslint-plugin-flowtype-2.46.3.tgz#7e84131d87ef18b496b1810448593374860b4e8e" + dependencies: + lodash "^4.15.0" + + eslint-plugin-prettier@2.6.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-2.6.0.tgz#33e4e228bdb06142d03c560ce04ec23f6c767dd7" + dependencies: + fast-diff "^1.1.1" + jest-docblock "^21.0.0" + + eslint-rule-composer@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/eslint-rule-composer/-/eslint-rule-composer-0.3.0.tgz#79320c927b0c5c0d3d3d2b76c8b4a488f25bbaf9" + + eslint-scope@^3.7.1, eslint-scope@~3.7.1: + version "3.7.1" + resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8" + dependencies: + esrecurse "^4.1.0" + estraverse "^4.1.1" + + eslint-visitor-keys@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#3f3180fb2e291017716acb4c9d6d5b5c34a6a81d" + + eslint@4.19.1: + version "4.19.1" + resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.19.1.tgz#32d1d653e1d90408854bfb296f076ec7e186a300" + dependencies: + ajv "^5.3.0" + babel-code-frame "^6.22.0" + chalk "^2.1.0" + concat-stream "^1.6.0" + cross-spawn "^5.1.0" + debug "^3.1.0" + doctrine "^2.1.0" + eslint-scope "^3.7.1" + eslint-visitor-keys "^1.0.0" + espree "^3.5.4" + esquery "^1.0.0" + esutils "^2.0.2" + file-entry-cache "^2.0.0" + functional-red-black-tree "^1.0.1" + glob "^7.1.2" + globals "^11.0.1" + ignore "^3.3.3" + imurmurhash "^0.1.4" + inquirer "^3.0.6" + is-resolvable "^1.0.0" + js-yaml "^3.9.1" + json-stable-stringify-without-jsonify "^1.0.1" + levn "^0.3.0" + lodash "^4.17.4" + minimatch "^3.0.2" + mkdirp "^0.5.1" + natural-compare "^1.4.0" + optionator "^0.8.2" + path-is-inside "^1.0.2" + pluralize "^7.0.0" + progress "^2.0.0" + regexpp "^1.0.1" + require-uncached "^1.0.3" + semver "^5.3.0" + strip-ansi "^4.0.0" + strip-json-comments "~2.0.1" + table "4.0.2" + text-table "~0.2.0" + + espree@^3.5.4: + version "3.5.4" + resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.4.tgz#b0f447187c8a8bed944b815a660bddf5deb5d1a7" + dependencies: + acorn "^5.5.0" + acorn-jsx "^3.0.0" + + esprima@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.0.tgz#4499eddcd1110e0b218bacf2fa7f7f59f55ca804" + + esquery@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/esquery/-/esquery-1.0.0.tgz#cfba8b57d7fba93f17298a8a006a04cda13d80fa" + dependencies: + estraverse "^4.0.0" + + esrecurse@^4.1.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/esrecurse/-/esrecurse-4.2.0.tgz#fa9568d98d3823f9a41d91e902dcab9ea6e5b163" + dependencies: + estraverse "^4.1.0" + object-assign "^4.0.1" + + estraverse@^4.0.0, estraverse@^4.1.0, estraverse@^4.1.1: + version "4.2.0" + resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.2.0.tgz#0dee3fed31fcd469618ce7342099fc1afa0bdb13" + + esutils@^2.0.2: + version "2.0.2" + resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b" + + exec-sh@^0.2.0: + version "0.2.1" + resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.2.1.tgz#163b98a6e89e6b65b47c2a28d215bc1f63989c38" + dependencies: + merge "^1.1.3" + + execa@^0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777" + dependencies: + cross-spawn "^5.0.1" + get-stream "^3.0.0" + is-stream "^1.1.0" + npm-run-path "^2.0.0" + p-finally "^1.0.0" + signal-exit "^3.0.0" + strip-eof "^1.0.0" + + expand-brackets@^0.1.4: + version "0.1.5" + resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-0.1.5.tgz#df07284e342a807cd733ac5af72411e581d1177b" + dependencies: + is-posix-bracket "^0.1.0" + + expand-brackets@^2.1.4: + version "2.1.4" + resolved "https://registry.yarnpkg.com/expand-brackets/-/expand-brackets-2.1.4.tgz#b77735e315ce30f6b6eff0f83b04151a22449622" + dependencies: + debug "^2.3.3" + define-property "^0.2.5" + extend-shallow "^2.0.1" + posix-character-classes "^0.1.0" + regex-not "^1.0.0" + snapdragon "^0.8.1" + to-regex "^3.0.1" + + expand-range@^1.8.1: + version "1.8.2" + resolved "https://registry.yarnpkg.com/expand-range/-/expand-range-1.8.2.tgz#a299effd335fe2721ebae8e257ec79644fc85337" + dependencies: + fill-range "^2.1.0" + + extend-shallow@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" + dependencies: + is-extendable "^0.1.0" + + extend-shallow@^3.0.0, extend-shallow@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-3.0.2.tgz#26a71aaf073b39fb2127172746131c2704028db8" + dependencies: + assign-symbols "^1.0.0" + is-extendable "^1.0.1" + + extend@~3.0.0, extend@~3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444" + + external-editor@^2.0.4: + version "2.1.0" + resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-2.1.0.tgz#3d026a21b7f95b5726387d4200ac160d372c3b48" + dependencies: + chardet "^0.4.0" + iconv-lite "^0.4.17" + tmp "^0.0.33" + + extglob@^0.3.1: + version "0.3.2" + resolved "https://registry.yarnpkg.com/extglob/-/extglob-0.3.2.tgz#2e18ff3d2f49ab2765cec9023f011daa8d8349a1" + dependencies: + is-extglob "^1.0.0" + + extglob@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/extglob/-/extglob-2.0.4.tgz#ad00fe4dc612a9232e8718711dc5cb5ab0285543" + dependencies: + array-unique "^0.3.2" + define-property "^1.0.0" + expand-brackets "^2.1.4" + extend-shallow "^2.0.1" + fragment-cache "^0.2.1" + regex-not "^1.0.0" + snapdragon "^0.8.1" + to-regex "^3.0.1" + + extsprintf@1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.3.0.tgz#96918440e3041a7a414f8c52e3c574eb3c3e1e05" + + extsprintf@^1.2.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f" + + fast-deep-equal@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff" + + fast-diff@^1.1.1: + version "1.1.2" + resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.1.2.tgz#4b62c42b8e03de3f848460b639079920695d0154" + + fast-json-stable-stringify@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz#d5142c0caee6b1189f87d3a76111064f86c8bbf2" + + fast-levenshtein@~2.0.4: + version "2.0.6" + resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917" + + fb-watchman@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/fb-watchman/-/fb-watchman-2.0.0.tgz#54e9abf7dfa2f26cd9b1636c588c1afc05de5d58" + dependencies: + bser "^2.0.0" + + figures@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962" + dependencies: + escape-string-regexp "^1.0.5" + + file-entry-cache@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-2.0.0.tgz#c392990c3e684783d838b8c84a45d8a048458361" + dependencies: + flat-cache "^1.2.1" + object-assign "^4.0.1" + + filename-regex@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" + + fill-range@^2.1.0: + version "2.2.3" + resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-2.2.3.tgz#50b77dfd7e469bc7492470963699fe7a8485a723" + dependencies: + is-number "^2.1.0" + isobject "^2.0.0" + randomatic "^1.1.3" + repeat-element "^1.1.2" + repeat-string "^1.5.2" + + fill-range@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7" + dependencies: + extend-shallow "^2.0.1" + is-number "^3.0.0" + repeat-string "^1.6.1" + to-regex-range "^2.1.0" + + find-cache-dir@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-0.1.1.tgz#c8defae57c8a52a8a784f9e31c57c742e993a0b9" + dependencies: + commondir "^1.0.1" + mkdirp "^0.5.1" + pkg-dir "^1.0.0" + + find-cache-dir@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-1.0.0.tgz#9288e3e9e3cc3748717d39eade17cf71fc30ee6f" + dependencies: + commondir "^1.0.1" + make-dir "^1.0.0" + pkg-dir "^2.0.0" + + find-up@^1.0.0: + version "1.1.2" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" + dependencies: + path-exists "^2.0.0" + pinkie-promise "^2.0.0" + + find-up@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-2.1.0.tgz#45d1b7e506c717ddd482775a2b77920a3c0c57a7" + dependencies: + locate-path "^2.0.0" + + flat-cache@^1.2.1: + version "1.3.0" + resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-1.3.0.tgz#d3030b32b38154f4e3b7e9c709f490f7ef97c481" + dependencies: + circular-json "^0.3.1" + del "^2.0.2" + graceful-fs "^4.1.2" + write "^0.2.1" + + flow-bin@0.71.0: + version "0.71.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.71.0.tgz#fd1b27a6458c3ebaa5cb811853182ed631918b70" + + for-in@^1.0.1, for-in@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" + + for-own@^0.1.4: + version "0.1.5" + resolved "https://registry.yarnpkg.com/for-own/-/for-own-0.1.5.tgz#5265c681a4f294dabbf17c9509b6763aa84510ce" + dependencies: + for-in "^1.0.1" + + foreground-child@^1.5.3, foreground-child@^1.5.6: + version "1.5.6" + resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-1.5.6.tgz#4fd71ad2dfde96789b980a5c0a295937cb2f5ce9" + dependencies: + cross-spawn "^4" + signal-exit "^3.0.0" + + forever-agent@~0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" + + form-data@~2.1.1: + version "2.1.4" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1" + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.5" + mime-types "^2.1.12" + + form-data@~2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.1.tgz#6fb94fbd71885306d73d15cc497fe4cc4ecd44bf" + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.5" + mime-types "^2.1.12" + + fragment-cache@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/fragment-cache/-/fragment-cache-0.2.1.tgz#4290fad27f13e89be7f33799c6bc5a0abfff0d19" + dependencies: + map-cache "^0.2.2" + + fs-readdir-recursive@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz#e32fc030a2ccee44a6b5371308da54be0b397d27" + + fs.realpath@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" + + fsevents@^1.0.0, fsevents@^1.1.1: + version "1.1.3" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.1.3.tgz#11f82318f5fe7bb2cd22965a108e9306208216d8" + dependencies: + nan "^2.3.0" + node-pre-gyp "^0.6.39" + + fstream-ignore@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/fstream-ignore/-/fstream-ignore-1.0.5.tgz#9c31dae34767018fe1d249b24dada67d092da105" + dependencies: + fstream "^1.0.0" + inherits "2" + minimatch "^3.0.0" + + fstream@^1.0.0, fstream@^1.0.10, fstream@^1.0.2: + version "1.0.11" + resolved "https://registry.yarnpkg.com/fstream/-/fstream-1.0.11.tgz#5c1fb1f117477114f0632a0eb4b71b3cb0fd3171" + dependencies: + graceful-fs "^4.1.2" + inherits "~2.0.0" + mkdirp ">=0.5 0" + rimraf "2" + + functional-red-black-tree@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327" + + gauge@~2.7.3: + version "2.7.4" + resolved "https://registry.yarnpkg.com/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7" + dependencies: + aproba "^1.0.3" + console-control-strings "^1.0.0" + has-unicode "^2.0.0" + object-assign "^4.1.0" + signal-exit "^3.0.0" + string-width "^1.0.1" + strip-ansi "^3.0.1" + wide-align "^1.1.0" + + get-caller-file@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5" + + get-func-name@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41" + + get-stream@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14" + + get-value@^2.0.3, get-value@^2.0.6: + version "2.0.6" + resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" + + getpass@^0.1.1: + version "0.1.7" + resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa" + dependencies: + assert-plus "^1.0.0" + + glob-base@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/glob-base/-/glob-base-0.3.0.tgz#dbb164f6221b1c0b1ccf82aea328b497df0ea3c4" + dependencies: + glob-parent "^2.0.0" + is-glob "^2.0.0" + + glob-parent@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-2.0.0.tgz#81383d72db054fcccf5336daa902f182f6edbb28" + dependencies: + is-glob "^2.0.0" + + glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.0.6, glob@^7.1.2: + version "7.1.2" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + + globals@^11.0.1, globals@^11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-11.1.0.tgz#632644457f5f0e3ae711807183700ebf2e4633e4" + + globals@^9.18.0: + version "9.18.0" + resolved "https://registry.yarnpkg.com/globals/-/globals-9.18.0.tgz#aa3896b3e69b487f17e31ed2143d69a8e30c2d8a" + + globby@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/globby/-/globby-5.0.0.tgz#ebd84667ca0dbb330b99bcfc68eac2bc54370e0d" + dependencies: + array-union "^1.0.1" + arrify "^1.0.0" + glob "^7.0.3" + object-assign "^4.0.1" + pify "^2.0.0" + pinkie-promise "^2.0.0" + + graceful-fs@^4.1.11, graceful-fs@^4.1.2: + version "4.1.11" + resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" + + growl@1.10.3: + version "1.10.3" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.3.tgz#1926ba90cf3edfe2adb4927f5880bc22c66c790f" + + handlebars@^4.0.3: + version "4.0.11" + resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc" + dependencies: + async "^1.4.0" + optimist "^0.6.1" + source-map "^0.4.4" + optionalDependencies: + uglify-js "^2.6" + + har-schema@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-1.0.5.tgz#d263135f43307c02c602afc8fe95970c0151369e" + + har-schema@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92" + + har-validator@~4.2.1: + version "4.2.1" + resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-4.2.1.tgz#33481d0f1bbff600dd203d75812a6a5fba002e2a" + dependencies: + ajv "^4.9.1" + har-schema "^1.0.5" + + har-validator@~5.0.3: + version "5.0.3" + resolved "https://registry.yarnpkg.com/har-validator/-/har-validator-5.0.3.tgz#ba402c266194f15956ef15e0fcf242993f6a7dfd" + dependencies: + ajv "^5.1.0" + har-schema "^2.0.0" + + has-ansi@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" + dependencies: + ansi-regex "^2.0.0" + + has-flag@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-1.0.0.tgz#9d9e793165ce017a00f00418c43f942a7b1d11fa" + + has-flag@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51" + + has-unicode@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/has-unicode/-/has-unicode-2.0.1.tgz#e0e6fe6a28cf51138855e086d1691e771de2a8b9" + + has-value@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/has-value/-/has-value-0.3.1.tgz#7b1f58bada62ca827ec0a2078025654845995e1f" + dependencies: + get-value "^2.0.3" + has-values "^0.1.4" + isobject "^2.0.0" + + has-value@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/has-value/-/has-value-1.0.0.tgz#18b281da585b1c5c51def24c930ed29a0be6b177" + dependencies: + get-value "^2.0.6" + has-values "^1.0.0" + isobject "^3.0.0" + + has-values@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/has-values/-/has-values-0.1.4.tgz#6d61de95d91dfca9b9a02089ad384bff8f62b771" + + has-values@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/has-values/-/has-values-1.0.0.tgz#95b0b63fec2146619a6fe57fe75628d5a39efe4f" + dependencies: + is-number "^3.0.0" + kind-of "^4.0.0" + + hawk@3.1.3, hawk@~3.1.3: + version "3.1.3" + resolved "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4" + dependencies: + boom "2.x.x" + cryptiles "2.x.x" + hoek "2.x.x" + sntp "1.x.x" + + hawk@~6.0.2: + version "6.0.2" + resolved "https://registry.yarnpkg.com/hawk/-/hawk-6.0.2.tgz#af4d914eb065f9b5ce4d9d11c1cb2126eecc3038" + dependencies: + boom "4.x.x" + cryptiles "3.x.x" + hoek "4.x.x" + sntp "2.x.x" + + he@1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" + + hoek@2.x.x: + version "2.16.3" + resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed" + + hoek@4.x.x: + version "4.2.0" + resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.0.tgz#72d9d0754f7fe25ca2d01ad8f8f9a9449a89526d" + + home-or-tmp@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-3.0.0.tgz#57a8fe24cf33cdd524860a15821ddc25c86671fb" + + homedir-polyfill@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.1.tgz#4c2bbc8a758998feebf5ed68580f76d46768b4bc" + dependencies: + parse-passwd "^1.0.0" + + hosted-git-info@^2.1.4: + version "2.6.0" + resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.6.0.tgz#23235b29ab230c576aab0d4f13fc046b0b038222" + + http-signature@~1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.1.1.tgz#df72e267066cd0ac67fb76adf8e134a8fbcf91bf" + dependencies: + assert-plus "^0.2.0" + jsprim "^1.2.2" + sshpk "^1.7.0" + + http-signature@~1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1" + dependencies: + assert-plus "^1.0.0" + jsprim "^1.2.2" + sshpk "^1.7.0" + + iconv-lite@^0.4.17: + version "0.4.19" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" + + ignore@^3.3.3: + version "3.3.7" + resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021" + + imurmurhash@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" + + inflight@^1.0.4: + version "1.0.6" + resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" + dependencies: + once "^1.3.0" + wrappy "1" + + inherits@2, inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.0, inherits@~2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" + + ini@~1.3.0: + version "1.3.5" + resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927" + + inquirer@^3.0.6: + version "3.3.0" + resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-3.3.0.tgz#9dd2f2ad765dcab1ff0443b491442a20ba227dc9" + dependencies: + ansi-escapes "^3.0.0" + chalk "^2.0.0" + cli-cursor "^2.1.0" + cli-width "^2.0.0" + external-editor "^2.0.4" + figures "^2.0.0" + lodash "^4.3.0" + mute-stream "0.0.7" + run-async "^2.2.0" + rx-lite "^4.0.8" + rx-lite-aggregates "^4.0.8" + string-width "^2.1.0" + strip-ansi "^4.0.0" + through "^2.3.6" + + invariant@^2.2.0, invariant@^2.2.2: + version "2.2.2" + resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360" + dependencies: + loose-envify "^1.0.0" + + invert-kv@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6" + + is-accessor-descriptor@^0.1.6: + version "0.1.6" + resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz#a9e12cb3ae8d876727eeef3843f8a0897b5c98d6" + dependencies: + kind-of "^3.0.2" + + is-accessor-descriptor@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-accessor-descriptor/-/is-accessor-descriptor-1.0.0.tgz#169c2f6d3df1f992618072365c9b0ea1f6878656" + dependencies: + kind-of "^6.0.0" + + is-arrayish@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d" + + is-binary-path@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/is-binary-path/-/is-binary-path-1.0.1.tgz#75f16642b480f187a711c814161fd3a4a7655898" + dependencies: + binary-extensions "^1.0.0" + + is-buffer@^1.1.5: + version "1.1.6" + resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" + + is-builtin-module@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe" + dependencies: + builtin-modules "^1.0.0" + + is-data-descriptor@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56" + dependencies: + kind-of "^3.0.2" + + is-data-descriptor@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-1.0.0.tgz#d84876321d0e7add03990406abbbbd36ba9268c7" + dependencies: + kind-of "^6.0.0" + + is-descriptor@^0.1.0: + version "0.1.6" + resolved "https://registry.yarnpkg.com/is-descriptor/-/is-descriptor-0.1.6.tgz#366d8240dde487ca51823b1ab9f07a10a78251ca" + dependencies: + is-accessor-descriptor "^0.1.6" + is-data-descriptor "^0.1.4" + kind-of "^5.0.0" + + is-descriptor@^1.0.0, is-descriptor@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/is-descriptor/-/is-descriptor-1.0.2.tgz#3b159746a66604b04f8c81524ba365c5f14d86ec" + dependencies: + is-accessor-descriptor "^1.0.0" + is-data-descriptor "^1.0.0" + kind-of "^6.0.2" + + is-dotfile@^1.0.0: + version "1.0.3" + resolved "https://registry.yarnpkg.com/is-dotfile/-/is-dotfile-1.0.3.tgz#a6a2f32ffd2dfb04f5ca25ecd0f6b83cf798a1e1" + + is-equal-shallow@^0.1.3: + version "0.1.3" + resolved "https://registry.yarnpkg.com/is-equal-shallow/-/is-equal-shallow-0.1.3.tgz#2238098fc221de0bcfa5d9eac4c45d638aa1c534" + dependencies: + is-primitive "^2.0.0" + + is-extendable@^0.1.0, is-extendable@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89" + + is-extendable@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-1.0.1.tgz#a7470f9e426733d81bd81e1155264e3a3507cab4" + dependencies: + is-plain-object "^2.0.4" + + is-extglob@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0" + + is-finite@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/is-finite/-/is-finite-1.0.2.tgz#cc6677695602be550ef11e8b4aa6305342b6d0aa" + dependencies: + number-is-nan "^1.0.0" + + is-fullwidth-code-point@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz#ef9e31386f031a7f0d643af82fde50c457ef00cb" + dependencies: + number-is-nan "^1.0.0" + + is-fullwidth-code-point@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" + + is-glob@^2.0.0, is-glob@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-2.0.1.tgz#d096f926a3ded5600f3fdfd91198cb0888c2d863" + dependencies: + is-extglob "^1.0.0" + + is-number@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f" + dependencies: + kind-of "^3.0.2" + + is-number@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-3.0.0.tgz#24fd6201a4782cf50561c810276afc7d12d71195" + dependencies: + kind-of "^3.0.2" + + is-number@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/is-number/-/is-number-4.0.0.tgz#0026e37f5454d73e356dfe6564699867c6a7f0ff" + + is-odd@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-odd/-/is-odd-2.0.0.tgz#7646624671fd7ea558ccd9a2795182f2958f1b24" + dependencies: + is-number "^4.0.0" + + is-path-cwd@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-path-cwd/-/is-path-cwd-1.0.0.tgz#d225ec23132e89edd38fda767472e62e65f1106d" + + is-path-in-cwd@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-path-in-cwd/-/is-path-in-cwd-1.0.0.tgz#6477582b8214d602346094567003be8a9eac04dc" + dependencies: + is-path-inside "^1.0.0" + + is-path-inside@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/is-path-inside/-/is-path-inside-1.0.1.tgz#8ef5b7de50437a3fdca6b4e865ef7aa55cb48036" + dependencies: + path-is-inside "^1.0.1" + + is-plain-obj@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e" + + is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677" + dependencies: + isobject "^3.0.1" + + is-posix-bracket@^0.1.0: + version "0.1.1" + resolved "https://registry.yarnpkg.com/is-posix-bracket/-/is-posix-bracket-0.1.1.tgz#3334dc79774368e92f016e6fbc0a88f5cd6e6bc4" + + is-primitive@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/is-primitive/-/is-primitive-2.0.0.tgz#207bab91638499c07b2adf240a41a87210034575" + + is-promise@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa" + + is-resolvable@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.0.0.tgz#8df57c61ea2e3c501408d100fb013cf8d6e0cc62" + dependencies: + tryit "^1.0.1" + + is-stream@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" + + is-typedarray@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a" + + is-utf8@^0.2.0: + version "0.2.1" + resolved "https://registry.yarnpkg.com/is-utf8/-/is-utf8-0.2.1.tgz#4b0da1442104d1b336340e80797e865cf39f7d72" + + is-windows@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" + + isarray@1.0.0, isarray@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" + + isexe@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" + + isobject@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/isobject/-/isobject-2.1.0.tgz#f065561096a3f1da2ef46272f815c840d87e0c89" + dependencies: + isarray "1.0.0" + + isobject@^3.0.0, isobject@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df" + + isstream@~0.1.2: + version "0.1.2" + resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" + + istanbul-lib-coverage@^1.1.2, istanbul-lib-coverage@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/istanbul-lib-coverage/-/istanbul-lib-coverage-1.2.0.tgz#f7d8f2e42b97e37fe796114cb0f9d68b5e3a4341" + + istanbul-lib-hook@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/istanbul-lib-hook/-/istanbul-lib-hook-1.1.0.tgz#8538d970372cb3716d53e55523dd54b557a8d89b" + dependencies: + append-transform "^0.4.0" + + istanbul-lib-instrument@^1.10.0: + version "1.10.1" + resolved "https://registry.yarnpkg.com/istanbul-lib-instrument/-/istanbul-lib-instrument-1.10.1.tgz#724b4b6caceba8692d3f1f9d0727e279c401af7b" + dependencies: + babel-generator "^6.18.0" + babel-template "^6.16.0" + babel-traverse "^6.18.0" + babel-types "^6.18.0" + babylon "^6.18.0" + istanbul-lib-coverage "^1.2.0" + semver "^5.3.0" + + istanbul-lib-report@^1.1.3: + version "1.1.3" + resolved "https://registry.yarnpkg.com/istanbul-lib-report/-/istanbul-lib-report-1.1.3.tgz#2df12188c0fa77990c0d2176d2d0ba3394188259" + dependencies: + istanbul-lib-coverage "^1.1.2" + mkdirp "^0.5.1" + path-parse "^1.0.5" + supports-color "^3.1.2" + + istanbul-lib-source-maps@^1.2.3: + version "1.2.3" + resolved "https://registry.yarnpkg.com/istanbul-lib-source-maps/-/istanbul-lib-source-maps-1.2.3.tgz#20fb54b14e14b3fb6edb6aca3571fd2143db44e6" + dependencies: + debug "^3.1.0" + istanbul-lib-coverage "^1.1.2" + mkdirp "^0.5.1" + rimraf "^2.6.1" + source-map "^0.5.3" + + istanbul-reports@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-1.4.0.tgz#3d7b44b912ecbe7652a603662b962120739646a1" + dependencies: + handlebars "^4.0.3" + + iterall@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7" + + jest-docblock@^21.0.0: + version "21.2.0" + resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414" + + js-tokens@^3.0.0, js-tokens@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" + + js-yaml@^3.6.1, js-yaml@^3.9.1: + version "3.10.0" + resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" + dependencies: + argparse "^1.0.7" + esprima "^4.0.0" + + jsbn@~0.1.0: + version "0.1.1" + resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513" + + jsesc@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b" + + jsesc@^2.5.1: + version "2.5.1" + resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.1.tgz#e421a2a8e20d6b0819df28908f782526b96dd1fe" + + jsesc@~0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d" + + json-schema-traverse@^0.3.0: + version "0.3.1" + resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340" + + json-schema@0.2.3: + version "0.2.3" + resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13" + + json-stable-stringify-without-jsonify@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651" + + json-stable-stringify@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/json-stable-stringify/-/json-stable-stringify-1.0.1.tgz#9a759d39c5f2ff503fd5300646ed445f88c4f9af" + dependencies: + jsonify "~0.0.0" + + json-stringify-safe@~5.0.1: + version "5.0.1" + resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" + + json5@^0.5.0: + version "0.5.1" + resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" + + jsonify@~0.0.0: + version "0.0.0" + resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" + + jsprim@^1.2.2: + version "1.4.1" + resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2" + dependencies: + assert-plus "1.0.0" + extsprintf "1.3.0" + json-schema "0.2.3" + verror "1.10.0" + + kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0: + version "3.2.2" + resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64" + dependencies: + is-buffer "^1.1.5" + + kind-of@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-4.0.0.tgz#20813df3d712928b207378691a45066fae72dd57" + dependencies: + is-buffer "^1.1.5" + + kind-of@^5.0.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.1.0.tgz#729c91e2d857b7a419a1f9aa65685c4c33f5845d" + + kind-of@^6.0.0, kind-of@^6.0.2: + version "6.0.2" + resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051" + + lazy-cache@^1.0.3: + version "1.0.4" + resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e" + + lcid@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835" + dependencies: + invert-kv "^1.0.0" + + lcov-parse@^0.0.10: + version "0.0.10" + resolved "https://registry.yarnpkg.com/lcov-parse/-/lcov-parse-0.0.10.tgz#1b0b8ff9ac9c7889250582b70b71315d9da6d9a3" + + levn@^0.3.0, levn@~0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee" + dependencies: + prelude-ls "~1.1.2" + type-check "~0.3.2" + + load-json-file@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0" + dependencies: + graceful-fs "^4.1.2" + parse-json "^2.2.0" + pify "^2.0.0" + pinkie-promise "^2.0.0" + strip-bom "^2.0.0" + + locate-path@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e" + dependencies: + p-locate "^2.0.0" + path-exists "^3.0.0" + + lodash@^4.15.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: + version "4.17.4" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" + + log-driver@^1.2.5: + version "1.2.5" + resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.5.tgz#7ae4ec257302fd790d557cb10c97100d857b0056" + + longest@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" + + loose-envify@^1.0.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.1.tgz#d1a8ad33fa9ce0e713d65fdd0ac8b748d478c848" + dependencies: + js-tokens "^3.0.0" + + lru-cache@^4.0.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.1.tgz#622e32e82488b49279114a4f9ecf45e7cd6bba55" + dependencies: + pseudomap "^1.0.2" + yallist "^2.1.2" + + make-dir@^1.0.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c" + dependencies: + pify "^3.0.0" + + makeerror@1.0.x: + version "1.0.11" + resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" + dependencies: + tmpl "1.0.x" + + map-cache@^0.2.2: + version "0.2.2" + resolved "https://registry.yarnpkg.com/map-cache/-/map-cache-0.2.2.tgz#c32abd0bd6525d9b051645bb4f26ac5dc98a0dbf" + + map-visit@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/map-visit/-/map-visit-1.0.0.tgz#ecdca8f13144e660f1b5bd41f12f3479d98dfb8f" + dependencies: + object-visit "^1.0.0" + + md5-hex@^1.2.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/md5-hex/-/md5-hex-1.3.0.tgz#d2c4afe983c4370662179b8cad145219135046c4" + dependencies: + md5-o-matic "^0.1.1" + + md5-o-matic@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/md5-o-matic/-/md5-o-matic-0.1.1.tgz#822bccd65e117c514fab176b25945d54100a03c3" + + mem@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/mem/-/mem-1.1.0.tgz#5edd52b485ca1d900fe64895505399a0dfa45f76" + dependencies: + mimic-fn "^1.0.0" + + merge-source-map@^1.0.2: + version "1.1.0" + resolved "https://registry.yarnpkg.com/merge-source-map/-/merge-source-map-1.1.0.tgz#2fdde7e6020939f70906a68f2d7ae685e4c8c646" + dependencies: + source-map "^0.6.1" + + merge@^1.1.3: + version "1.2.0" + resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da" + + micromatch@^2.1.5, micromatch@^2.3.11: + version "2.3.11" + resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565" + dependencies: + arr-diff "^2.0.0" + array-unique "^0.2.1" + braces "^1.8.2" + expand-brackets "^0.1.4" + extglob "^0.3.1" + filename-regex "^2.0.0" + is-extglob "^1.0.0" + is-glob "^2.0.1" + kind-of "^3.0.2" + normalize-path "^2.0.1" + object.omit "^2.0.0" + parse-glob "^3.0.4" + regex-cache "^0.4.2" + + micromatch@^3.1.4, micromatch@^3.1.8: + version "3.1.10" + resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.10.tgz#70859bc95c9840952f359a068a3fc49f9ecfac23" + dependencies: + arr-diff "^4.0.0" + array-unique "^0.3.2" + braces "^2.3.1" + define-property "^2.0.2" + extend-shallow "^3.0.2" + extglob "^2.0.4" + fragment-cache "^0.2.1" + kind-of "^6.0.2" + nanomatch "^1.2.9" + object.pick "^1.3.0" + regex-not "^1.0.0" + snapdragon "^0.8.1" + to-regex "^3.0.2" + + mime-db@~1.30.0: + version "1.30.0" + resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.30.0.tgz#74c643da2dd9d6a45399963465b26d5ca7d71f01" + + mime-types@^2.1.12, mime-types@~2.1.17, mime-types@~2.1.7: + version "2.1.17" + resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.17.tgz#09d7a393f03e995a79f8af857b70a9e0ab16557a" + dependencies: + mime-db "~1.30.0" + + mimic-fn@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.1.0.tgz#e667783d92e89dbd342818b5230b9d62a672ad18" + + minimatch@3.0.4, minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" + dependencies: + brace-expansion "^1.1.7" + + minimist@0.0.8: + version "0.0.8" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" + + minimist@^1.1.1, minimist@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" + + minimist@~0.0.1: + version "0.0.10" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" + + mixin-deep@^1.2.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe" + dependencies: + for-in "^1.0.2" + is-extendable "^1.0.1" + + mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1: + version "0.5.1" + resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" + dependencies: + minimist "0.0.8" + + mocha@5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.1.1.tgz#b774c75609dac05eb48f4d9ba1d827b97fde8a7b" + dependencies: + browser-stdout "1.3.1" + commander "2.11.0" + debug "3.1.0" + diff "3.5.0" + escape-string-regexp "1.0.5" + glob "7.1.2" + growl "1.10.3" + he "1.1.1" + minimatch "3.0.4" + mkdirp "0.5.1" + supports-color "4.4.0" + + ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + + mute-stream@0.0.7: + version "0.0.7" + resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" + + nan@^2.3.0: + version "2.8.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a" + + nanomatch@^1.2.9: + version "1.2.9" + resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.9.tgz#879f7150cb2dab7a471259066c104eee6e0fa7c2" + dependencies: + arr-diff "^4.0.0" + array-unique "^0.3.2" + define-property "^2.0.2" + extend-shallow "^3.0.2" + fragment-cache "^0.2.1" + is-odd "^2.0.0" + is-windows "^1.0.2" + kind-of "^6.0.2" + object.pick "^1.3.0" + regex-not "^1.0.0" + snapdragon "^0.8.1" + to-regex "^3.0.1" + + natural-compare@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" + + node-int64@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" + + node-modules-regexp@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40" + + node-pre-gyp@^0.6.39: + version "0.6.39" + resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649" + dependencies: + detect-libc "^1.0.2" + hawk "3.1.3" + mkdirp "^0.5.1" + nopt "^4.0.1" + npmlog "^4.0.2" + rc "^1.1.7" + request "2.81.0" + rimraf "^2.6.1" + semver "^5.3.0" + tar "^2.2.1" + tar-pack "^3.4.0" + + nopt@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d" + dependencies: + abbrev "1" + osenv "^0.1.4" + + normalize-package-data@^2.3.2: + version "2.4.0" + resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.4.0.tgz#12f95a307d58352075a04907b84ac8be98ac012f" + dependencies: + hosted-git-info "^2.1.4" + is-builtin-module "^1.0.0" + semver "2 || 3 || 4 || 5" + validate-npm-package-license "^3.0.1" + + normalize-path@^2.0.0, normalize-path@^2.0.1, normalize-path@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9" + dependencies: + remove-trailing-separator "^1.0.1" + + npm-run-path@^2.0.0: + version "2.0.2" + resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f" + dependencies: + path-key "^2.0.0" + + npmlog@^4.0.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b" + dependencies: + are-we-there-yet "~1.1.2" + console-control-strings "~1.1.0" + gauge "~2.7.3" + set-blocking "~2.0.0" + + number-is-nan@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d" + + nyc@^11.7.1: + version "11.7.1" + resolved "https://registry.yarnpkg.com/nyc/-/nyc-11.7.1.tgz#7cb0a422e501b88ff2c1634341dec2560299d67b" + dependencies: + archy "^1.0.0" + arrify "^1.0.1" + caching-transform "^1.0.0" + convert-source-map "^1.5.1" + debug-log "^1.0.1" + default-require-extensions "^1.0.0" + find-cache-dir "^0.1.1" + find-up "^2.1.0" + foreground-child "^1.5.3" + glob "^7.0.6" + istanbul-lib-coverage "^1.1.2" + istanbul-lib-hook "^1.1.0" + istanbul-lib-instrument "^1.10.0" + istanbul-lib-report "^1.1.3" + istanbul-lib-source-maps "^1.2.3" + istanbul-reports "^1.4.0" + md5-hex "^1.2.0" + merge-source-map "^1.0.2" + micromatch "^2.3.11" + mkdirp "^0.5.0" + resolve-from "^2.0.0" + rimraf "^2.5.4" + signal-exit "^3.0.1" + spawn-wrap "^1.4.2" + test-exclude "^4.2.0" + yargs "11.1.0" + yargs-parser "^8.0.0" + + oauth-sign@~0.8.1, oauth-sign@~0.8.2: + version "0.8.2" + resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43" + + object-assign@^4.0.1, object-assign@^4.1.0: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + + object-copy@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c" + dependencies: + copy-descriptor "^0.1.0" + define-property "^0.2.5" + kind-of "^3.0.3" + + object-visit@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb" + dependencies: + isobject "^3.0.0" + + object.omit@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/object.omit/-/object.omit-2.0.1.tgz#1a9c744829f39dbb858c76ca3579ae2a54ebd1fa" + dependencies: + for-own "^0.1.4" + is-extendable "^0.1.1" + + object.pick@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/object.pick/-/object.pick-1.3.0.tgz#87a10ac4c1694bd2e1cbf53591a66141fb5dd747" + dependencies: + isobject "^3.0.1" + + once@^1.3.0, once@^1.3.3: + version "1.4.0" + resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" + dependencies: + wrappy "1" + + onetime@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/onetime/-/onetime-2.0.1.tgz#067428230fd67443b2794b22bba528b6867962d4" + dependencies: + mimic-fn "^1.0.0" + + optimist@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686" + dependencies: + minimist "~0.0.1" + wordwrap "~0.0.2" + + optionator@^0.8.2: + version "0.8.2" + resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.2.tgz#364c5e409d3f4d6301d6c0b4c05bba50180aeb64" + dependencies: + deep-is "~0.1.3" + fast-levenshtein "~2.0.4" + levn "~0.3.0" + prelude-ls "~1.1.2" + type-check "~0.3.2" + wordwrap "~1.0.0" + + os-homedir@^1.0.0, os-homedir@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" + + os-locale@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-2.1.0.tgz#42bc2900a6b5b8bd17376c8e882b65afccf24bf2" + dependencies: + execa "^0.7.0" + lcid "^1.0.0" + mem "^1.1.0" + + os-tmpdir@^1.0.0, os-tmpdir@~1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" + + osenv@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.4.tgz#42fe6d5953df06c8064be6f176c3d05aaaa34644" + dependencies: + os-homedir "^1.0.0" + os-tmpdir "^1.0.0" + + output-file-sync@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/output-file-sync/-/output-file-sync-2.0.1.tgz#f53118282f5f553c2799541792b723a4c71430c0" + dependencies: + graceful-fs "^4.1.11" + is-plain-obj "^1.1.0" + mkdirp "^0.5.1" + + p-finally@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae" + + p-limit@^1.1.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.2.0.tgz#0e92b6bedcb59f022c13d0f1949dc82d15909f1c" + dependencies: + p-try "^1.0.0" + + p-locate@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43" + dependencies: + p-limit "^1.1.0" + + p-try@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/p-try/-/p-try-1.0.0.tgz#cbc79cdbaf8fd4228e13f621f2b1a237c1b207b3" + + parse-glob@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/parse-glob/-/parse-glob-3.0.4.tgz#b2c376cfb11f35513badd173ef0bb6e3a388391c" + dependencies: + glob-base "^0.3.0" + is-dotfile "^1.0.0" + is-extglob "^1.0.0" + is-glob "^2.0.0" + + parse-json@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-2.2.0.tgz#f480f40434ef80741f8469099f8dea18f55a4dc9" + dependencies: + error-ex "^1.2.0" + + parse-passwd@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" + + pascalcase@^0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14" + + path-exists@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b" + dependencies: + pinkie-promise "^2.0.0" + + path-exists@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" + + path-is-absolute@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" + + path-is-inside@^1.0.1, path-is-inside@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53" + + path-key@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" + + path-parse@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" + + path-type@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441" + dependencies: + graceful-fs "^4.1.2" + pify "^2.0.0" + pinkie-promise "^2.0.0" + + pathval@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0" + + performance-now@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-0.2.0.tgz#33ef30c5c77d4ea21c5a53869d91b56d8f2555e5" + + performance-now@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" + + pify@^2.0.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" + + pify@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" + + pinkie-promise@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa" + dependencies: + pinkie "^2.0.0" + + pinkie@^2.0.0: + version "2.0.4" + resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" + + pirates@^3.0.1: + version "3.0.2" + resolved "https://registry.yarnpkg.com/pirates/-/pirates-3.0.2.tgz#7e6f85413fd9161ab4e12b539b06010d85954bb9" + dependencies: + node-modules-regexp "^1.0.0" + + pkg-dir@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-1.0.0.tgz#7a4b508a8d5bb2d629d447056ff4e9c9314cf3d4" + dependencies: + find-up "^1.0.0" + + pkg-dir@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b" + dependencies: + find-up "^2.1.0" + + platform@^1.3.3: + version "1.3.5" + resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444" + + pluralize@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-7.0.0.tgz#298b89df8b93b0221dbf421ad2b1b1ea23fc6777" + + posix-character-classes@^0.1.0: + version "0.1.1" + resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" + + prelude-ls@~1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" + + preserve@^0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" + + prettier@1.12.1: + version "1.12.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325" + + private@^0.1.6: + version "0.1.8" + resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff" + + process-nextick-args@~1.0.6: + version "1.0.7" + resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3" + + progress@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.0.tgz#8a1be366bf8fc23db2bd23f10c6fe920b4389d1f" + + pseudomap@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" + + punycode@^1.4.1: + version "1.4.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" + + qs@~6.4.0: + version "6.4.0" + resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233" + + qs@~6.5.1: + version "6.5.1" + resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8" + + randomatic@^1.1.3: + version "1.1.7" + resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.7.tgz#c7abe9cc8b87c0baa876b19fde83fd464797e38c" + dependencies: + is-number "^3.0.0" + kind-of "^4.0.0" + + rc@^1.1.7: + version "1.2.2" + resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.2.tgz#d8ce9cb57e8d64d9c7badd9876c7c34cbe3c7077" + dependencies: + deep-extend "~0.4.0" + ini "~1.3.0" + minimist "^1.2.0" + strip-json-comments "~2.0.1" + + read-pkg-up@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02" + dependencies: + find-up "^1.0.0" + read-pkg "^1.0.0" + + read-pkg@^1.0.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/read-pkg/-/read-pkg-1.1.0.tgz#f5ffaa5ecd29cb31c0474bca7d756b6bb29e3f28" + dependencies: + load-json-file "^1.0.0" + normalize-package-data "^2.3.2" + path-type "^1.0.0" + + readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.2.2: + version "2.3.3" + resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c" + dependencies: + core-util-is "~1.0.0" + inherits "~2.0.3" + isarray "~1.0.0" + process-nextick-args "~1.0.6" + safe-buffer "~5.1.1" + string_decoder "~1.0.3" + util-deprecate "~1.0.1" + + readdirp@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.1.0.tgz#4ed0ad060df3073300c48440373f72d1cc642d78" + dependencies: + graceful-fs "^4.1.2" + minimatch "^3.0.2" + readable-stream "^2.0.2" + set-immediate-shim "^1.0.1" + + regenerate-unicode-properties@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-6.0.0.tgz#0fc26f9d5142289df4e177dec58f303d2d097c16" + dependencies: + regenerate "^1.3.3" + + regenerate@^1.3.3, regenerate@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11" + + regenerator-runtime@^0.11.0: + version "0.11.0" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz#7e54fe5b5ccd5d6624ea6255c3473be090b802e1" + + regenerator-runtime@^0.11.1: + version "0.11.1" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" + + regenerator-transform@^0.12.3: + version "0.12.3" + resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.12.3.tgz#459adfb64f6a27164ab991b7873f45ab969eca8b" + dependencies: + private "^0.1.6" + + regex-cache@^0.4.2: + version "0.4.4" + resolved "https://registry.yarnpkg.com/regex-cache/-/regex-cache-0.4.4.tgz#75bdc58a2a1496cec48a12835bc54c8d562336dd" + dependencies: + is-equal-shallow "^0.1.3" + + regex-not@^1.0.0, regex-not@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c" + dependencies: + extend-shallow "^3.0.2" + safe-regex "^1.1.0" + + regexpp@^1.0.1: + version "1.1.0" + resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-1.1.0.tgz#0e3516dd0b7904f413d2d4193dce4618c3a689ab" + + regexpu-core@^4.1.3: + version "4.1.4" + resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-4.1.4.tgz#f6c847261ba4cf869a7874442f9d25251b713fc8" + dependencies: + regenerate "^1.4.0" + regenerate-unicode-properties "^6.0.0" + regjsgen "^0.4.0" + regjsparser "^0.3.0" + unicode-match-property-ecmascript "^1.0.3" + unicode-match-property-value-ecmascript "^1.0.1" + + regjsgen@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/regjsgen/-/regjsgen-0.4.0.tgz#c1eb4c89a209263f8717c782591523913ede2561" + + regjsparser@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/regjsparser/-/regjsparser-0.3.0.tgz#3c326da7fcfd69fa0d332575a41c8c0cdf588c96" + dependencies: + jsesc "~0.5.0" + + remove-trailing-separator@^1.0.1: + version "1.1.0" + resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef" + + repeat-element@^1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/repeat-element/-/repeat-element-1.1.2.tgz#ef089a178d1483baae4d93eb98b4f9e4e11d990a" + + repeat-string@^1.5.2, repeat-string@^1.6.1: + version "1.6.1" + resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637" + + repeating@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/repeating/-/repeating-2.0.1.tgz#5214c53a926d3552707527fbab415dbc08d06dda" + dependencies: + is-finite "^1.0.0" + + request@2.81.0: + version "2.81.0" + resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0" + dependencies: + aws-sign2 "~0.6.0" + aws4 "^1.2.1" + caseless "~0.12.0" + combined-stream "~1.0.5" + extend "~3.0.0" + forever-agent "~0.6.1" + form-data "~2.1.1" + har-validator "~4.2.1" + hawk "~3.1.3" + http-signature "~1.1.0" + is-typedarray "~1.0.0" + isstream "~0.1.2" + json-stringify-safe "~5.0.1" + mime-types "~2.1.7" + oauth-sign "~0.8.1" + performance-now "^0.2.0" + qs "~6.4.0" + safe-buffer "^5.0.1" + stringstream "~0.0.4" + tough-cookie "~2.3.0" + tunnel-agent "^0.6.0" + uuid "^3.0.0" + + request@^2.79.0: + version "2.83.0" + resolved "https://registry.yarnpkg.com/request/-/request-2.83.0.tgz#ca0b65da02ed62935887808e6f510381034e3356" + dependencies: + aws-sign2 "~0.7.0" + aws4 "^1.6.0" + caseless "~0.12.0" + combined-stream "~1.0.5" + extend "~3.0.1" + forever-agent "~0.6.1" + form-data "~2.3.1" + har-validator "~5.0.3" + hawk "~6.0.2" + http-signature "~1.2.0" + is-typedarray "~1.0.0" + isstream "~0.1.2" + json-stringify-safe "~5.0.1" + mime-types "~2.1.17" + oauth-sign "~0.8.2" + performance-now "^2.1.0" + qs "~6.5.1" + safe-buffer "^5.1.1" + stringstream "~0.0.5" + tough-cookie "~2.3.3" + tunnel-agent "^0.6.0" + uuid "^3.1.0" + + require-directory@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42" + + require-main-filename@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" + + require-uncached@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/require-uncached/-/require-uncached-1.0.3.tgz#4e0d56d6c9662fd31e43011c4b95aa49955421d3" + dependencies: + caller-path "^0.1.0" + resolve-from "^1.0.0" + + resolve-from@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-1.0.1.tgz#26cbfe935d1aeeeabb29bc3fe5aeb01e93d44226" + + resolve-from@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-2.0.0.tgz#9480ab20e94ffa1d9e80a804c7ea147611966b57" + + resolve-url@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" + + resolve@^1.3.2: + version "1.7.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.7.1.tgz#aadd656374fd298aee895bc026b8297418677fd3" + dependencies: + path-parse "^1.0.5" + + restore-cursor@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf" + dependencies: + onetime "^2.0.0" + signal-exit "^3.0.2" + + ret@~0.1.10: + version "0.1.15" + resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" + + right-align@^0.1.1: + version "0.1.3" + resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef" + dependencies: + align-text "^0.1.1" + + rimraf@2, rimraf@^2.2.8, rimraf@^2.5.1, rimraf@^2.5.4, rimraf@^2.6.1, rimraf@^2.6.2: + version "2.6.2" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36" + dependencies: + glob "^7.0.5" + + run-async@^2.2.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.3.0.tgz#0371ab4ae0bdd720d4166d7dfda64ff7a445a6c0" + dependencies: + is-promise "^2.1.0" + + rx-lite-aggregates@^4.0.8: + version "4.0.8" + resolved "https://registry.yarnpkg.com/rx-lite-aggregates/-/rx-lite-aggregates-4.0.8.tgz#753b87a89a11c95467c4ac1626c4efc4e05c67be" + dependencies: + rx-lite "*" + + rx-lite@*, rx-lite@^4.0.8: + version "4.0.8" + resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444" + + safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853" + + safe-regex@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e" + dependencies: + ret "~0.1.10" + + sane@2.5.0: + version "2.5.0" + resolved "https://registry.yarnpkg.com/sane/-/sane-2.5.0.tgz#6359cd676f5efd9988b264d8ce3b827dd6b27bec" + dependencies: + anymatch "^2.0.0" + exec-sh "^0.2.0" + fb-watchman "^2.0.0" + micromatch "^3.1.4" + minimist "^1.1.1" + walker "~1.0.5" + watch "~0.18.0" + optionalDependencies: + fsevents "^1.1.1" + + "semver@2 || 3 || 4 || 5", semver@^5.4.1: + version "5.5.0" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab" + + semver@^5.3.0: + version "5.4.1" + resolved "https://registry.yarnpkg.com/semver/-/semver-5.4.1.tgz#e059c09d8571f0540823733433505d3a2f00b18e" + + set-blocking@^2.0.0, set-blocking@~2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/set-blocking/-/set-blocking-2.0.0.tgz#045f9782d011ae9a6803ddd382b24392b3d890f7" + + set-immediate-shim@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/set-immediate-shim/-/set-immediate-shim-1.0.1.tgz#4b2b1b27eb808a9f8dcc481a58e5e56f599f3f61" + + set-value@^0.4.3: + version "0.4.3" + resolved "https://registry.yarnpkg.com/set-value/-/set-value-0.4.3.tgz#7db08f9d3d22dc7f78e53af3c3bf4666ecdfccf1" + dependencies: + extend-shallow "^2.0.1" + is-extendable "^0.1.1" + is-plain-object "^2.0.1" + to-object-path "^0.3.0" + + set-value@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/set-value/-/set-value-2.0.0.tgz#71ae4a88f0feefbbf52d1ea604f3fb315ebb6274" + dependencies: + extend-shallow "^2.0.1" + is-extendable "^0.1.1" + is-plain-object "^2.0.3" + split-string "^3.0.1" + + shebang-command@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" + dependencies: + shebang-regex "^1.0.0" + + shebang-regex@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" + + signal-exit@^3.0.0, signal-exit@^3.0.1, signal-exit@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d" + + slash@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55" + + slice-ansi@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-1.0.0.tgz#044f1a49d8842ff307aad6b505ed178bd950134d" + dependencies: + is-fullwidth-code-point "^2.0.0" + + slide@^1.1.5: + version "1.1.6" + resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707" + + snapdragon-node@^2.0.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b" + dependencies: + define-property "^1.0.0" + isobject "^3.0.0" + snapdragon-util "^3.0.1" + + snapdragon-util@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/snapdragon-util/-/snapdragon-util-3.0.1.tgz#f956479486f2acd79700693f6f7b805e45ab56e2" + dependencies: + kind-of "^3.2.0" + + snapdragon@^0.8.1: + version "0.8.2" + resolved "https://registry.yarnpkg.com/snapdragon/-/snapdragon-0.8.2.tgz#64922e7c565b0e14204ba1aa7d6964278d25182d" + dependencies: + base "^0.11.1" + debug "^2.2.0" + define-property "^0.2.5" + extend-shallow "^2.0.1" + map-cache "^0.2.2" + source-map "^0.5.6" + source-map-resolve "^0.5.0" + use "^3.1.0" + + sntp@1.x.x: + version "1.0.9" + resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198" + dependencies: + hoek "2.x.x" + + sntp@2.x.x: + version "2.1.0" + resolved "https://registry.yarnpkg.com/sntp/-/sntp-2.1.0.tgz#2c6cec14fedc2222739caf9b5c3d85d1cc5a2cc8" + dependencies: + hoek "4.x.x" + + source-map-resolve@^0.5.0: + version "0.5.1" + resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.1.tgz#7ad0f593f2281598e854df80f19aae4b92d7a11a" + dependencies: + atob "^2.0.0" + decode-uri-component "^0.2.0" + resolve-url "^0.2.1" + source-map-url "^0.4.0" + urix "^0.1.0" + + source-map-support@^0.4.2: + version "0.4.18" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f" + dependencies: + source-map "^0.5.6" + + source-map-url@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/source-map-url/-/source-map-url-0.4.0.tgz#3e935d7ddd73631b97659956d55128e87b5084a3" + + source-map@^0.4.4: + version "0.4.4" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b" + dependencies: + amdefine ">=0.0.4" + + source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7, source-map@~0.5.1: + version "0.5.7" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc" + + source-map@^0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" + + spawn-wrap@^1.4.2: + version "1.4.2" + resolved "https://registry.yarnpkg.com/spawn-wrap/-/spawn-wrap-1.4.2.tgz#cff58e73a8224617b6561abdc32586ea0c82248c" + dependencies: + foreground-child "^1.5.6" + mkdirp "^0.5.0" + os-homedir "^1.0.1" + rimraf "^2.6.2" + signal-exit "^3.0.2" + which "^1.3.0" + + spdx-correct@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.0.0.tgz#05a5b4d7153a195bc92c3c425b69f3b2a9524c82" + dependencies: + spdx-expression-parse "^3.0.0" + spdx-license-ids "^3.0.0" + + spdx-exceptions@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/spdx-exceptions/-/spdx-exceptions-2.1.0.tgz#2c7ae61056c714a5b9b9b2b2af7d311ef5c78fe9" + + spdx-expression-parse@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/spdx-expression-parse/-/spdx-expression-parse-3.0.0.tgz#99e119b7a5da00e05491c9fa338b7904823b41d0" + dependencies: + spdx-exceptions "^2.1.0" + spdx-license-ids "^3.0.0" + + spdx-license-ids@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.0.tgz#7a7cd28470cc6d3a1cfe6d66886f6bc430d3ac87" + + split-string@^3.0.1, split-string@^3.0.2: + version "3.1.0" + resolved "https://registry.yarnpkg.com/split-string/-/split-string-3.1.0.tgz#7cb09dda3a86585705c64b39a6466038682e8fe2" + dependencies: + extend-shallow "^3.0.0" + + sprintf-js@~1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" + + sshpk@^1.7.0: + version "1.13.1" + resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.1.tgz#512df6da6287144316dc4c18fe1cf1d940739be3" + dependencies: + asn1 "~0.2.3" + assert-plus "^1.0.0" + dashdash "^1.12.0" + getpass "^0.1.1" + optionalDependencies: + bcrypt-pbkdf "^1.0.0" + ecc-jsbn "~0.1.1" + jsbn "~0.1.0" + tweetnacl "~0.14.0" + + static-extend@^0.1.1: + version "0.1.2" + resolved "https://registry.yarnpkg.com/static-extend/-/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6" + dependencies: + define-property "^0.2.5" + object-copy "^0.1.0" + + string-width@^1.0.1, string-width@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" + dependencies: + code-point-at "^1.0.0" + is-fullwidth-code-point "^1.0.0" + strip-ansi "^3.0.0" + + string-width@^2.0.0, string-width@^2.1.0, string-width@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e" + dependencies: + is-fullwidth-code-point "^2.0.0" + strip-ansi "^4.0.0" + + string_decoder@~1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.0.3.tgz#0fc67d7c141825de94282dd536bec6b9bce860ab" + dependencies: + safe-buffer "~5.1.0" + + stringstream@~0.0.4, stringstream@~0.0.5: + version "0.0.5" + resolved "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.5.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878" + + strip-ansi@^3.0.0, strip-ansi@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" + dependencies: + ansi-regex "^2.0.0" + + strip-ansi@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-4.0.0.tgz#a8479022eb1ac368a871389b635262c505ee368f" + dependencies: + ansi-regex "^3.0.0" + + strip-bom@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e" + dependencies: + is-utf8 "^0.2.0" + + strip-eof@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/strip-eof/-/strip-eof-1.0.0.tgz#bb43ff5598a6eb05d89b59fcd129c983313606bf" + + strip-json-comments@~2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" + + supports-color@4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e" + dependencies: + has-flag "^2.0.0" + + supports-color@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" + + supports-color@^3.1.2: + version "3.2.3" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.2.3.tgz#65ac0504b3954171d8a64946b2ae3cbb8a5f54f6" + dependencies: + has-flag "^1.0.0" + + supports-color@^4.0.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.5.0.tgz#be7a0de484dec5c5cddf8b3d59125044912f635b" + dependencies: + has-flag "^2.0.0" + + table@4.0.2: + version "4.0.2" + resolved "https://registry.yarnpkg.com/table/-/table-4.0.2.tgz#a33447375391e766ad34d3486e6e2aedc84d2e36" + dependencies: + ajv "^5.2.3" + ajv-keywords "^2.1.0" + chalk "^2.1.0" + lodash "^4.17.4" + slice-ansi "1.0.0" + string-width "^2.1.1" + + tar-pack@^3.4.0: + version "3.4.1" + resolved "https://registry.yarnpkg.com/tar-pack/-/tar-pack-3.4.1.tgz#e1dbc03a9b9d3ba07e896ad027317eb679a10a1f" + dependencies: + debug "^2.2.0" + fstream "^1.0.10" + fstream-ignore "^1.0.5" + once "^1.3.3" + readable-stream "^2.1.4" + rimraf "^2.5.1" + tar "^2.2.1" + uid-number "^0.0.6" + + tar@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/tar/-/tar-2.2.1.tgz#8e4d2a256c0e2185c6b18ad694aec968b83cb1d1" + dependencies: + block-stream "*" + fstream "^1.0.2" + inherits "2" + + test-exclude@^4.2.0: + version "4.2.1" + resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.1.tgz#dfa222f03480bca69207ca728b37d74b45f724fa" + dependencies: + arrify "^1.0.1" + micromatch "^3.1.8" + object-assign "^4.1.0" + read-pkg-up "^1.0.1" + require-main-filename "^1.0.1" + + text-table@~0.2.0: + version "0.2.0" + resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" + + through@^2.3.6: + version "2.3.8" + resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" + + tmp@^0.0.33: + version "0.0.33" + resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9" + dependencies: + os-tmpdir "~1.0.2" + + tmpl@1.0.x: + version "1.0.4" + resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1" + + to-fast-properties@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47" + + to-fast-properties@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e" + + to-object-path@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af" + dependencies: + kind-of "^3.0.2" + + to-regex-range@^2.1.0: + version "2.1.1" + resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-2.1.1.tgz#7c80c17b9dfebe599e27367e0d4dd5590141db38" + dependencies: + is-number "^3.0.0" + repeat-string "^1.6.1" + + to-regex@^3.0.1, to-regex@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/to-regex/-/to-regex-3.0.2.tgz#13cfdd9b336552f30b51f33a8ae1b42a7a7599ce" + dependencies: + define-property "^2.0.2" + extend-shallow "^3.0.2" + regex-not "^1.0.2" + safe-regex "^1.1.0" + + tough-cookie@~2.3.0, tough-cookie@~2.3.3: + version "2.3.3" + resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.3.tgz#0b618a5565b6dea90bf3425d04d55edc475a7561" + dependencies: + punycode "^1.4.1" + + trim-right@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" + + tryit@^1.0.1: + version "1.0.3" + resolved "https://registry.yarnpkg.com/tryit/-/tryit-1.0.3.tgz#393be730a9446fd1ead6da59a014308f36c289cb" + + tunnel-agent@^0.6.0: + version "0.6.0" + resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd" + dependencies: + safe-buffer "^5.0.1" + + tweetnacl@^0.14.3, tweetnacl@~0.14.0: + version "0.14.5" + resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" + + type-check@~0.3.2: + version "0.3.2" + resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72" + dependencies: + prelude-ls "~1.1.2" + + type-detect@^4.0.0: + version "4.0.5" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.5.tgz#d70e5bc81db6de2a381bcaca0c6e0cbdc7635de2" + + typedarray@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" + + uglify-js@^2.6: + version "2.8.29" + resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd" + dependencies: + source-map "~0.5.1" + yargs "~3.10.0" + optionalDependencies: + uglify-to-browserify "~1.0.0" + + uglify-to-browserify@~1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7" + + uid-number@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" + + unicode-canonical-property-names-ecmascript@^1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.3.tgz#f6119f417467593c0086357c85546b6ad5abc583" + + unicode-match-property-ecmascript@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-match-property-ecmascript/-/unicode-match-property-ecmascript-1.0.3.tgz#db9b1cb4ffc67e0c5583780b1b59370e4cbe97b9" + dependencies: + unicode-canonical-property-names-ecmascript "^1.0.2" + unicode-property-aliases-ecmascript "^1.0.3" + + unicode-match-property-value-ecmascript@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/unicode-match-property-value-ecmascript/-/unicode-match-property-value-ecmascript-1.0.1.tgz#fea059120a016f403afd3bf586162b4db03e0604" + + unicode-property-aliases-ecmascript@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-1.0.3.tgz#ac3522583b9e630580f916635333e00c5ead690d" + + union-value@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" + dependencies: + arr-union "^3.1.0" + get-value "^2.0.6" + is-extendable "^0.1.1" + set-value "^0.4.3" + + unset-value@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559" + dependencies: + has-value "^0.3.1" + isobject "^3.0.0" + + urix@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" + + use@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/use/-/use-3.1.0.tgz#14716bf03fdfefd03040aef58d8b4b85f3a7c544" + dependencies: + kind-of "^6.0.2" + + util-deprecate@~1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" + + uuid@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.1.0.tgz#3dd3d3e790abc24d7b0d3a034ffababe28ebbc04" + + uuid@^3.1.0: + version "3.2.1" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14" + + v8flags@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-3.1.0.tgz#246a34a8158c0e1390dcb758e1140e5d004e230b" + dependencies: + homedir-polyfill "^1.0.1" + + validate-npm-package-license@^3.0.1: + version "3.0.3" + resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.3.tgz#81643bcbef1bdfecd4623793dc4648948ba98338" + dependencies: + spdx-correct "^3.0.0" + spdx-expression-parse "^3.0.0" + + verror@1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400" + dependencies: + assert-plus "^1.0.0" + core-util-is "1.0.2" + extsprintf "^1.2.0" + + walker@~1.0.5: + version "1.0.7" + resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb" + dependencies: + makeerror "1.0.x" + + watch@~0.18.0: + version "0.18.0" + resolved "https://registry.yarnpkg.com/watch/-/watch-0.18.0.tgz#28095476c6df7c90c963138990c0a5423eb4b986" + dependencies: + exec-sh "^0.2.0" + minimist "^1.2.0" + + which-module@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a" + + which@^1.2.9, which@^1.3.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/which/-/which-1.3.0.tgz#ff04bdfc010ee547d780bec38e1ac1c2777d253a" + dependencies: + isexe "^2.0.0" + + wide-align@^1.1.0: + version "1.1.2" + resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.2.tgz#571e0f1b0604636ebc0dfc21b0339bbe31341710" + dependencies: + string-width "^1.0.2" + + window-size@0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d" + + wordwrap@0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f" + + wordwrap@~0.0.2: + version "0.0.3" + resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" + + wordwrap@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb" + + wrap-ansi@^2.0.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85" + dependencies: + string-width "^1.0.1" + strip-ansi "^3.0.1" + + wrappy@1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" + + write-file-atomic@^1.1.4: + version "1.3.4" + resolved "https://registry.yarnpkg.com/write-file-atomic/-/write-file-atomic-1.3.4.tgz#f807a4f0b1d9e913ae7a48112e6cc3af1991b45f" + dependencies: + graceful-fs "^4.1.11" + imurmurhash "^0.1.4" + slide "^1.1.5" + + write@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/write/-/write-0.2.1.tgz#5fc03828e264cea3fe91455476f7a3c566cb0757" + dependencies: + mkdirp "^0.5.1" + + y18n@^3.2.1: + version "3.2.1" + resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41" + + yallist@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" + + yargs-parser@^8.0.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950" + dependencies: + camelcase "^4.1.0" + + yargs-parser@^9.0.2: + version "9.0.2" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-9.0.2.tgz#9ccf6a43460fe4ed40a9bb68f48d43b8a68cc077" + dependencies: + camelcase "^4.1.0" + + yargs@11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-11.1.0.tgz#90b869934ed6e871115ea2ff58b03f4724ed2d77" + dependencies: + cliui "^4.0.0" + decamelize "^1.1.1" + find-up "^2.1.0" + get-caller-file "^1.0.1" + os-locale "^2.0.0" + require-directory "^2.1.1" + require-main-filename "^1.0.1" + set-blocking "^2.0.0" + string-width "^2.0.0" + which-module "^2.0.0" + y18n "^3.2.1" + yargs-parser "^9.0.2" + + yargs@~3.10.0: + version "3.10.0" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1" + dependencies: + camelcase "^1.0.2" + cliui "^2.1.0" + decamelize "^1.0.0" + window-size "0.1.0" + +Trace: + Error: Command failed. + Exit code: 1 + Command: sh + Arguments: -c npm run build:clean && npm run build:cp && npm run build:package-json && npm run build:cjs && npm run build:mjs && npm run build:flow + Directory: /home/aaron/Documents/graphql-js + Output: + + at ProcessTermError.MessageError (/usr/share/yarn/lib/cli.js:186:110) + at new ProcessTermError (/usr/share/yarn/lib/cli.js:226:113) + at ChildProcess. (/usr/share/yarn/lib/cli.js:30281:17) + at ChildProcess.emit (events.js:160:13) + at maybeClose (internal/child_process.js:943:16) + at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5) diff --git a/yarn.lock b/yarn.lock index cbaa78e1af..90bddcc8cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,12 +2,53 @@ # yarn lockfile v1 +"@babel/cli@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.0.0-beta.47.tgz#e00cc40ffd9084a5243c8fbded3f5049bc970e4c" + dependencies: + commander "^2.8.1" + convert-source-map "^1.1.0" + fs-readdir-recursive "^1.0.0" + glob "^7.0.0" + lodash "^4.17.5" + output-file-sync "^2.0.0" + slash "^1.0.0" + source-map "^0.5.0" + optionalDependencies: + chokidar "^2.0.3" + "@babel/code-frame@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.44.tgz#2a02643368de80916162be70865c97774f3adbd9" dependencies: "@babel/highlight" "7.0.0-beta.44" +"@babel/code-frame@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0-beta.47.tgz#d18c2f4c4ba8d093a2bcfab5616593bfe2441a27" + dependencies: + "@babel/highlight" "7.0.0-beta.47" + +"@babel/core@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.0.0-beta.47.tgz#b9c164fb9a1e1083f067c236a9da1d7a7d759271" + dependencies: + "@babel/code-frame" "7.0.0-beta.47" + "@babel/generator" "7.0.0-beta.47" + "@babel/helpers" "7.0.0-beta.47" + "@babel/template" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + babylon "7.0.0-beta.47" + convert-source-map "^1.1.0" + debug "^3.1.0" + json5 "^0.5.0" + lodash "^4.17.5" + micromatch "^2.3.11" + resolve "^1.3.2" + semver "^5.4.1" + source-map "^0.5.0" + "@babel/generator@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.44.tgz#c7e67b9b5284afcf69b309b50d7d37f3e5033d42" @@ -18,6 +59,52 @@ source-map "^0.5.0" trim-right "^1.0.1" +"@babel/generator@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.0.0-beta.47.tgz#1835709f377cc4d2a4affee6d9258a10bbf3b9d1" + dependencies: + "@babel/types" "7.0.0-beta.47" + jsesc "^2.5.1" + lodash "^4.17.5" + source-map "^0.5.0" + trim-right "^1.0.1" + +"@babel/helper-annotate-as-pure@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.0.0-beta.47.tgz#354fb596055d9db369211bf075f0d5e93904d6f6" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-builder-binary-assignment-operator-visitor@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-builder-binary-assignment-operator-visitor/-/helper-builder-binary-assignment-operator-visitor-7.0.0-beta.47.tgz#d5917c29ee3d68abc2c72f604bc043f6e056e907" + dependencies: + "@babel/helper-explode-assignable-expression" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-call-delegate@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-call-delegate/-/helper-call-delegate-7.0.0-beta.47.tgz#96b7804397075f722a4030d3876f51ec19d8829b" + dependencies: + "@babel/helper-hoist-variables" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-define-map@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-define-map/-/helper-define-map-7.0.0-beta.47.tgz#43a9def87c5166dc29630d51b3da9cc4320c131c" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + lodash "^4.17.5" + +"@babel/helper-explode-assignable-expression@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.0.0-beta.47.tgz#56b688e282a698f4d1cf135453a11ae8af870a19" + dependencies: + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + "@babel/helper-function-name@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.44.tgz#e18552aaae2231100a6e485e03854bc3532d44dd" @@ -26,18 +113,128 @@ "@babel/template" "7.0.0-beta.44" "@babel/types" "7.0.0-beta.44" +"@babel/helper-function-name@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.0.0-beta.47.tgz#8057d63e951e85c57c02cdfe55ad7608d73ffb7d" + dependencies: + "@babel/helper-get-function-arity" "7.0.0-beta.47" + "@babel/template" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + "@babel/helper-get-function-arity@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.44.tgz#d03ca6dd2b9f7b0b1e6b32c56c72836140db3a15" dependencies: "@babel/types" "7.0.0-beta.44" +"@babel/helper-get-function-arity@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.0.0-beta.47.tgz#2de04f97c14b094b55899d3fa83144a16d207510" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-hoist-variables@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.0.0-beta.47.tgz#ce295d1d723fe22b2820eaec748ed701aa5ae3d0" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-member-expression-to-functions@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.0.0-beta.47.tgz#35bfcf1d16dce481ef3dec66d5a1ae6a7d80bb45" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-module-imports@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.0.0-beta.47.tgz#5af072029ffcfbece6ffbaf5d9984c75580f3f04" + dependencies: + "@babel/types" "7.0.0-beta.47" + lodash "^4.17.5" + +"@babel/helper-module-transforms@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.0.0-beta.47.tgz#7eff91fc96873bd7b8d816698f1a69bbc01f3c38" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.47" + "@babel/helper-simple-access" "7.0.0-beta.47" + "@babel/helper-split-export-declaration" "7.0.0-beta.47" + "@babel/template" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + lodash "^4.17.5" + +"@babel/helper-optimise-call-expression@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-optimise-call-expression/-/helper-optimise-call-expression-7.0.0-beta.47.tgz#085d864d0613c5813c1b7c71b61bea36f195929e" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-plugin-utils@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.0.0-beta.47.tgz#4f564117ec39f96cf60fafcde35c9ddce0e008fd" + +"@babel/helper-regex@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-regex/-/helper-regex-7.0.0-beta.47.tgz#b8e3b53132c4edbb04804242c02ffe4d60316971" + dependencies: + lodash "^4.17.5" + +"@babel/helper-remap-async-to-generator@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-remap-async-to-generator/-/helper-remap-async-to-generator-7.0.0-beta.47.tgz#444dc362f61470bd61a745ebb364431d9ca186c2" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.47" + "@babel/helper-wrap-function" "7.0.0-beta.47" + "@babel/template" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-replace-supers@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-replace-supers/-/helper-replace-supers-7.0.0-beta.47.tgz#310b206a302868a792b659455ceba27db686cbb7" + dependencies: + "@babel/helper-member-expression-to-functions" "7.0.0-beta.47" + "@babel/helper-optimise-call-expression" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-simple-access@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.0.0-beta.47.tgz#234d754acbda9251a10db697ef50181eab125042" + dependencies: + "@babel/template" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + lodash "^4.17.5" + "@babel/helper-split-export-declaration@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.44.tgz#c0b351735e0fbcb3822c8ad8db4e583b05ebd9dc" dependencies: "@babel/types" "7.0.0-beta.44" +"@babel/helper-split-export-declaration@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.0.0-beta.47.tgz#e11277855472d8d83baf22f2d0186c4a2059b09a" + dependencies: + "@babel/types" "7.0.0-beta.47" + +"@babel/helper-wrap-function@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helper-wrap-function/-/helper-wrap-function-7.0.0-beta.47.tgz#6528b44a3ccb4f3aeeb79add0a88192f7eb81161" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/template" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + +"@babel/helpers@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.0.0-beta.47.tgz#f9b42ed2e4d5f75ec0fb2e792c173e451e8d40fd" + dependencies: + "@babel/template" "7.0.0-beta.47" + "@babel/traverse" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + "@babel/highlight@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.44.tgz#18c94ce543916a80553edcdcf681890b200747d5" @@ -46,6 +243,353 @@ esutils "^2.0.2" js-tokens "^3.0.0" +"@babel/highlight@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.0.0-beta.47.tgz#8fbc83fb2a21f0bd2b95cdbeb238cf9689cad494" + dependencies: + chalk "^2.0.0" + esutils "^2.0.2" + js-tokens "^3.0.0" + +"@babel/node@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/node/-/node-7.0.0-beta.47.tgz#fe0425303a78beb455d5c8be72212d3172c48a9b" + dependencies: + "@babel/polyfill" "7.0.0-beta.47" + "@babel/register" "7.0.0-beta.47" + commander "^2.8.1" + fs-readdir-recursive "^1.0.0" + lodash "^4.17.5" + output-file-sync "^2.0.0" + v8flags "^3.0.0" + +"@babel/plugin-proposal-async-generator-functions@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.0.0-beta.47.tgz#571142284708c5ad4ec904d9aa705461a010be53" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-remap-async-to-generator" "7.0.0-beta.47" + "@babel/plugin-syntax-async-generators" "7.0.0-beta.47" + +"@babel/plugin-proposal-class-properties@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.0.0-beta.47.tgz#08c1a1dfc92d0f5c37b39096c6fb883e1ca4b0f5" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-replace-supers" "7.0.0-beta.47" + "@babel/plugin-syntax-class-properties" "7.0.0-beta.47" + +"@babel/plugin-proposal-object-rest-spread@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-object-rest-spread/-/plugin-proposal-object-rest-spread-7.0.0-beta.47.tgz#e1529fddc88e948868ee1d0edaa27ebd9502322d" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/plugin-syntax-object-rest-spread" "7.0.0-beta.47" + +"@babel/plugin-proposal-optional-catch-binding@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-optional-catch-binding/-/plugin-proposal-optional-catch-binding-7.0.0-beta.47.tgz#8c6453919537517ea773bb8f3fceda4250795efa" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/plugin-syntax-optional-catch-binding" "7.0.0-beta.47" + +"@babel/plugin-proposal-unicode-property-regex@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-unicode-property-regex/-/plugin-proposal-unicode-property-regex-7.0.0-beta.47.tgz#34d7e4811bdc4f512400bb29d01051842528c8d5" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-regex" "7.0.0-beta.47" + regexpu-core "^4.1.4" + +"@babel/plugin-syntax-async-generators@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.0.0-beta.47.tgz#8ab94852bf348badc866af85bd852221f0961256" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-syntax-class-properties@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-class-properties/-/plugin-syntax-class-properties-7.0.0-beta.47.tgz#de52bed12fd472c848e1562f57dd4a202fe27f11" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-syntax-flow@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.0.0-beta.47.tgz#9d0b09b9af6fec87a7b22e406bf948089d58c188" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-syntax-object-rest-spread@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.0.0-beta.47.tgz#21da514d94c138b2261ca09f0dec9abadce16185" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-syntax-optional-catch-binding@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-optional-catch-binding/-/plugin-syntax-optional-catch-binding-7.0.0-beta.47.tgz#0b1c52b066aa36893c41450773a5adb904cd4024" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-arrow-functions@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.0.0-beta.47.tgz#d6eecda4c652b909e3088f0983ebaf8ec292984b" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-async-to-generator@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-async-to-generator/-/plugin-transform-async-to-generator-7.0.0-beta.47.tgz#5723816ea1e91fa313a84e6ee9cc12ff31d46610" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-remap-async-to-generator" "7.0.0-beta.47" + +"@babel/plugin-transform-block-scoped-functions@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-block-scoped-functions/-/plugin-transform-block-scoped-functions-7.0.0-beta.47.tgz#e422278e06c797b43c45f459d83c7af9d6237002" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-block-scoping@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-block-scoping/-/plugin-transform-block-scoping-7.0.0-beta.47.tgz#b737cc58a81bea57efd5bda0baef9a43a25859ad" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + lodash "^4.17.5" + +"@babel/plugin-transform-classes@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-classes/-/plugin-transform-classes-7.0.0-beta.47.tgz#7aff9cbe7b26fd94d7a9f97fa90135ef20c93fb6" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.47" + "@babel/helper-define-map" "7.0.0-beta.47" + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/helper-optimise-call-expression" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-replace-supers" "7.0.0-beta.47" + "@babel/helper-split-export-declaration" "7.0.0-beta.47" + globals "^11.1.0" + +"@babel/plugin-transform-computed-properties@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-computed-properties/-/plugin-transform-computed-properties-7.0.0-beta.47.tgz#56ef2a021769a2b65e90a3e12fd10b791da9f3e0" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-destructuring@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-destructuring/-/plugin-transform-destructuring-7.0.0-beta.47.tgz#452b607775fd1c4d10621997837189efc0a6d428" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-dotall-regex@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-dotall-regex/-/plugin-transform-dotall-regex-7.0.0-beta.47.tgz#d8da9b706d4bfc68dec9d565661f83e6e8036636" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-regex" "7.0.0-beta.47" + regexpu-core "^4.1.3" + +"@babel/plugin-transform-duplicate-keys@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-duplicate-keys/-/plugin-transform-duplicate-keys-7.0.0-beta.47.tgz#4aabeda051ca3007e33a207db08f1a0cf9bd253b" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-exponentiation-operator@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-exponentiation-operator/-/plugin-transform-exponentiation-operator-7.0.0-beta.47.tgz#930e1abf5db9f4db5b63dbf97f3581ad0be1e907" + dependencies: + "@babel/helper-builder-binary-assignment-operator-visitor" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-flow-strip-types@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.0.0-beta.47.tgz#fa45811094c10d70c84efdd0eac62ebd2a634bf7" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/plugin-syntax-flow" "7.0.0-beta.47" + +"@babel/plugin-transform-for-of@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-for-of/-/plugin-transform-for-of-7.0.0-beta.47.tgz#527d5dc24e4a4ad0fc1d0a3990d29968cb984e76" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-function-name@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-function-name/-/plugin-transform-function-name-7.0.0-beta.47.tgz#fb443c81cc77f3206a863b730b35c8c553ce5041" + dependencies: + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-literals@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-literals/-/plugin-transform-literals-7.0.0-beta.47.tgz#448fad196f062163684a38f10f14e83315892e9c" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-modules-amd@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-amd/-/plugin-transform-modules-amd-7.0.0-beta.47.tgz#84564419b11c1be6b9fcd4c7b3a6737f2335aac4" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-modules-commonjs@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-commonjs/-/plugin-transform-modules-commonjs-7.0.0-beta.47.tgz#dfe5c6d867aa9614e55f7616736073edb3aab887" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-simple-access" "7.0.0-beta.47" + +"@babel/plugin-transform-modules-systemjs@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-systemjs/-/plugin-transform-modules-systemjs-7.0.0-beta.47.tgz#8514dbcdfca3345abd690059e7e8544e16ecbf05" + dependencies: + "@babel/helper-hoist-variables" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-modules-umd@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-umd/-/plugin-transform-modules-umd-7.0.0-beta.47.tgz#6dcfb9661fdd131b20b721044746a7a309882918" + dependencies: + "@babel/helper-module-transforms" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-new-target@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-new-target/-/plugin-transform-new-target-7.0.0-beta.47.tgz#4b5cb7ce30d7bffa105a1f43ed07d6ae206a4155" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-object-super@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-object-super/-/plugin-transform-object-super-7.0.0-beta.47.tgz#ca8e5f326c5011c879f3a6ed749e58bd10fff05d" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-replace-supers" "7.0.0-beta.47" + +"@babel/plugin-transform-parameters@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-parameters/-/plugin-transform-parameters-7.0.0-beta.47.tgz#46a4236040a6552a5f165fb3ddd60368954b0ddd" + dependencies: + "@babel/helper-call-delegate" "7.0.0-beta.47" + "@babel/helper-get-function-arity" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-regenerator@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-regenerator/-/plugin-transform-regenerator-7.0.0-beta.47.tgz#86500e1c404055fb98fc82b73b09bd053cacb516" + dependencies: + regenerator-transform "^0.12.3" + +"@babel/plugin-transform-shorthand-properties@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.0.0-beta.47.tgz#00be44c4fad8fe2c00ed18ea15ea3c88dd519dbb" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-spread@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-spread/-/plugin-transform-spread-7.0.0-beta.47.tgz#3feadb02292ed1e9b75090d651b9df88a7ab5c50" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-sticky-regex@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-sticky-regex/-/plugin-transform-sticky-regex-7.0.0-beta.47.tgz#c0aa347d76b5dc87d3b37ac016ada3f950605131" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-regex" "7.0.0-beta.47" + +"@babel/plugin-transform-template-literals@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-template-literals/-/plugin-transform-template-literals-7.0.0-beta.47.tgz#5f7b5badf64c4c5da79026aeab03001e62a6ee5f" + dependencies: + "@babel/helper-annotate-as-pure" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-typeof-symbol@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typeof-symbol/-/plugin-transform-typeof-symbol-7.0.0-beta.47.tgz#03c612ec09213eb386a81d5fa67c234ee4b2034c" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + +"@babel/plugin-transform-unicode-regex@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.0.0-beta.47.tgz#efed0b2f1dfbf28283502234a95b4be88f7fdcb6" + dependencies: + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/helper-regex" "7.0.0-beta.47" + regexpu-core "^4.1.3" + +"@babel/polyfill@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.0.0-beta.47.tgz#2ef0a6ee02a23a0ab98fc4eb6aed7e88560bc35d" + dependencies: + core-js "^2.5.3" + regenerator-runtime "^0.11.1" + +"@babel/preset-env@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/preset-env/-/preset-env-7.0.0-beta.47.tgz#a3dab3b5fac4de56e3510bdbcb528f1cbdedbe2d" + dependencies: + "@babel/helper-module-imports" "7.0.0-beta.47" + "@babel/helper-plugin-utils" "7.0.0-beta.47" + "@babel/plugin-proposal-async-generator-functions" "7.0.0-beta.47" + "@babel/plugin-proposal-object-rest-spread" "7.0.0-beta.47" + "@babel/plugin-proposal-optional-catch-binding" "7.0.0-beta.47" + "@babel/plugin-proposal-unicode-property-regex" "7.0.0-beta.47" + "@babel/plugin-syntax-async-generators" "7.0.0-beta.47" + "@babel/plugin-syntax-object-rest-spread" "7.0.0-beta.47" + "@babel/plugin-syntax-optional-catch-binding" "7.0.0-beta.47" + "@babel/plugin-transform-arrow-functions" "7.0.0-beta.47" + "@babel/plugin-transform-async-to-generator" "7.0.0-beta.47" + "@babel/plugin-transform-block-scoped-functions" "7.0.0-beta.47" + "@babel/plugin-transform-block-scoping" "7.0.0-beta.47" + "@babel/plugin-transform-classes" "7.0.0-beta.47" + "@babel/plugin-transform-computed-properties" "7.0.0-beta.47" + "@babel/plugin-transform-destructuring" "7.0.0-beta.47" + "@babel/plugin-transform-dotall-regex" "7.0.0-beta.47" + "@babel/plugin-transform-duplicate-keys" "7.0.0-beta.47" + "@babel/plugin-transform-exponentiation-operator" "7.0.0-beta.47" + "@babel/plugin-transform-for-of" "7.0.0-beta.47" + "@babel/plugin-transform-function-name" "7.0.0-beta.47" + "@babel/plugin-transform-literals" "7.0.0-beta.47" + "@babel/plugin-transform-modules-amd" "7.0.0-beta.47" + "@babel/plugin-transform-modules-commonjs" "7.0.0-beta.47" + "@babel/plugin-transform-modules-systemjs" "7.0.0-beta.47" + "@babel/plugin-transform-modules-umd" "7.0.0-beta.47" + "@babel/plugin-transform-new-target" "7.0.0-beta.47" + "@babel/plugin-transform-object-super" "7.0.0-beta.47" + "@babel/plugin-transform-parameters" "7.0.0-beta.47" + "@babel/plugin-transform-regenerator" "7.0.0-beta.47" + "@babel/plugin-transform-shorthand-properties" "7.0.0-beta.47" + "@babel/plugin-transform-spread" "7.0.0-beta.47" + "@babel/plugin-transform-sticky-regex" "7.0.0-beta.47" + "@babel/plugin-transform-template-literals" "7.0.0-beta.47" + "@babel/plugin-transform-typeof-symbol" "7.0.0-beta.47" + "@babel/plugin-transform-unicode-regex" "7.0.0-beta.47" + browserslist "^3.0.0" + invariant "^2.2.2" + semver "^5.3.0" + +"@babel/register@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/register/-/register-7.0.0-beta.47.tgz#ac53bc357ca59979db0e306aa5d3121aa612a7a2" + dependencies: + core-js "^2.5.3" + find-cache-dir "^1.0.0" + home-or-tmp "^3.0.0" + lodash "^4.17.5" + mkdirp "^0.5.1" + pirates "^3.0.1" + source-map-support "^0.4.2" + "@babel/template@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f" @@ -55,6 +599,15 @@ babylon "7.0.0-beta.44" lodash "^4.2.0" +"@babel/template@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.47.tgz#0473970a7c0bee7a1a18c1ca999d3ba5e5bad83d" + dependencies: + "@babel/code-frame" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + babylon "7.0.0-beta.47" + lodash "^4.17.5" + "@babel/traverse@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.44.tgz#a970a2c45477ad18017e2e465a0606feee0d2966" @@ -70,6 +623,21 @@ invariant "^2.2.0" lodash "^4.2.0" +"@babel/traverse@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.0.0-beta.47.tgz#0e57fdbb9ff3a909188b6ebf1e529c641e6c82a4" + dependencies: + "@babel/code-frame" "7.0.0-beta.47" + "@babel/generator" "7.0.0-beta.47" + "@babel/helper-function-name" "7.0.0-beta.47" + "@babel/helper-split-export-declaration" "7.0.0-beta.47" + "@babel/types" "7.0.0-beta.47" + babylon "7.0.0-beta.47" + debug "^3.1.0" + globals "^11.1.0" + invariant "^2.2.0" + lodash "^4.17.5" + "@babel/types@7.0.0-beta.44": version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.44.tgz#6b1b164591f77dec0a0342aca995f2d046b3a757" @@ -78,6 +646,14 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" +"@babel/types@7.0.0-beta.47": + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.0.0-beta.47.tgz#e6fcc1a691459002c2671d558a586706dddaeef8" + dependencies: + esutils "^2.0.2" + lodash "^4.17.5" + to-fast-properties "^2.0.0" + abbrev@1: version "1.1.1" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" @@ -159,13 +735,6 @@ ansi-styles@^3.1.0: dependencies: color-convert "^1.9.0" -anymatch@^1.3.0: - version "1.3.2" - resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a" - dependencies: - micromatch "^2.1.5" - normalize-path "^2.0.0" - anymatch@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb" @@ -288,27 +857,6 @@ aws4@^1.2.1, aws4@^1.6.0: version "1.6.0" resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e" -babel-cli@6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-cli/-/babel-cli-6.26.0.tgz#502ab54874d7db88ad00b887a06383ce03d002f1" - dependencies: - babel-core "^6.26.0" - babel-polyfill "^6.26.0" - babel-register "^6.26.0" - babel-runtime "^6.26.0" - commander "^2.11.0" - convert-source-map "^1.5.0" - fs-readdir-recursive "^1.0.0" - glob "^7.1.2" - lodash "^4.17.4" - output-file-sync "^1.1.2" - path-is-absolute "^1.0.1" - slash "^1.0.0" - source-map "^0.5.6" - v8flags "^2.1.1" - optionalDependencies: - chokidar "^1.6.1" - babel-code-frame@^6.22.0, babel-code-frame@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" @@ -317,30 +865,6 @@ babel-code-frame@^6.22.0, babel-code-frame@^6.26.0: esutils "^2.0.2" js-tokens "^3.0.2" -babel-core@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.0.tgz#af32f78b31a6fcef119c87b0fd8d9753f03a0bb8" - dependencies: - babel-code-frame "^6.26.0" - babel-generator "^6.26.0" - babel-helpers "^6.24.1" - babel-messages "^6.23.0" - babel-register "^6.26.0" - babel-runtime "^6.26.0" - babel-template "^6.26.0" - babel-traverse "^6.26.0" - babel-types "^6.26.0" - babylon "^6.18.0" - convert-source-map "^1.5.0" - debug "^2.6.8" - json5 "^0.5.1" - lodash "^4.17.4" - minimatch "^3.0.4" - path-is-absolute "^1.0.1" - private "^0.1.7" - slash "^1.0.0" - source-map "^0.5.6" - babel-eslint@8.2.3: version "8.2.3" resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-8.2.3.tgz#1a2e6681cc9bc4473c32899e59915e19cd6733cf" @@ -365,443 +889,24 @@ babel-generator@^6.18.0: source-map "^0.5.7" trim-right "^1.0.1" -babel-generator@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.0.tgz#ac1ae20070b79f6e3ca1d3269613053774f20dc5" - dependencies: - babel-messages "^6.23.0" - babel-runtime "^6.26.0" - babel-types "^6.26.0" - detect-indent "^4.0.0" - jsesc "^1.3.0" - lodash "^4.17.4" - source-map "^0.5.6" - trim-right "^1.0.1" - -babel-helper-builder-binary-assignment-operator-visitor@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-builder-binary-assignment-operator-visitor/-/babel-helper-builder-binary-assignment-operator-visitor-6.24.1.tgz#cce4517ada356f4220bcae8a02c2b346f9a56664" - dependencies: - babel-helper-explode-assignable-expression "^6.24.1" - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-helper-call-delegate@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-call-delegate/-/babel-helper-call-delegate-6.24.1.tgz#ece6aacddc76e41c3461f88bfc575bd0daa2df8d" - dependencies: - babel-helper-hoist-variables "^6.24.1" - babel-runtime "^6.22.0" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-helper-define-map@^6.24.1: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-helper-define-map/-/babel-helper-define-map-6.26.0.tgz#a5f56dab41a25f97ecb498c7ebaca9819f95be5f" - dependencies: - babel-helper-function-name "^6.24.1" - babel-runtime "^6.26.0" - babel-types "^6.26.0" - lodash "^4.17.4" - -babel-helper-explode-assignable-expression@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-explode-assignable-expression/-/babel-helper-explode-assignable-expression-6.24.1.tgz#f25b82cf7dc10433c55f70592d5746400ac22caa" - dependencies: - babel-runtime "^6.22.0" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-helper-function-name@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-function-name/-/babel-helper-function-name-6.24.1.tgz#d3475b8c03ed98242a25b48351ab18399d3580a9" - dependencies: - babel-helper-get-function-arity "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-helper-get-function-arity@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-get-function-arity/-/babel-helper-get-function-arity-6.24.1.tgz#8f7782aa93407c41d3aa50908f89b031b1b6853d" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-helper-hoist-variables@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-hoist-variables/-/babel-helper-hoist-variables-6.24.1.tgz#1ecb27689c9d25513eadbc9914a73f5408be7a76" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-helper-optimise-call-expression@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-optimise-call-expression/-/babel-helper-optimise-call-expression-6.24.1.tgz#f7a13427ba9f73f8f4fa993c54a97882d1244257" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-helper-regex@^6.24.1: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-helper-regex/-/babel-helper-regex-6.26.0.tgz#325c59f902f82f24b74faceed0363954f6495e72" - dependencies: - babel-runtime "^6.26.0" - babel-types "^6.26.0" - lodash "^4.17.4" - -babel-helper-remap-async-to-generator@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-remap-async-to-generator/-/babel-helper-remap-async-to-generator-6.24.1.tgz#5ec581827ad723fecdd381f1c928390676e4551b" - dependencies: - babel-helper-function-name "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-helper-replace-supers@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helper-replace-supers/-/babel-helper-replace-supers-6.24.1.tgz#bf6dbfe43938d17369a213ca8a8bf74b6a90ab1a" - dependencies: - babel-helper-optimise-call-expression "^6.24.1" - babel-messages "^6.23.0" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-helpers@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-helpers/-/babel-helpers-6.24.1.tgz#3471de9caec388e5c850e597e58a26ddf37602b2" - dependencies: - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-messages@^6.23.0: version "6.23.0" resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e" dependencies: babel-runtime "^6.22.0" -babel-plugin-check-es2015-constants@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-check-es2015-constants/-/babel-plugin-check-es2015-constants-6.22.0.tgz#35157b101426fd2ffd3da3f75c7d1e91835bbf8a" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-syntax-async-functions@6.13.0, babel-plugin-syntax-async-functions@^6.8.0: +babel-plugin-syntax-async-functions@6.13.0: version "6.13.0" resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95" -babel-plugin-syntax-async-generators@6.13.0: - version "6.13.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-generators/-/babel-plugin-syntax-async-generators-6.13.0.tgz#6bc963ebb16eccbae6b92b596eb7f35c342a8b9a" - -babel-plugin-syntax-class-properties@^6.8.0: - version "6.13.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de" - -babel-plugin-syntax-exponentiation-operator@^6.8.0: - version "6.13.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de" - -babel-plugin-syntax-flow@^6.18.0: - version "6.18.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-flow/-/babel-plugin-syntax-flow-6.18.0.tgz#4c3ab20a2af26aa20cd25995c398c4eb70310c8d" - -babel-plugin-syntax-object-rest-spread@^6.8.0: - version "6.13.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5" - -babel-plugin-syntax-trailing-function-commas@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz#ba0360937f8d06e40180a43fe0d5616fff532cf3" - -babel-plugin-transform-async-to-generator@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.24.1.tgz#6536e378aff6cb1d5517ac0e40eb3e9fc8d08761" - dependencies: - babel-helper-remap-async-to-generator "^6.24.1" - babel-plugin-syntax-async-functions "^6.8.0" - babel-runtime "^6.22.0" - -babel-plugin-transform-class-properties@6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-class-properties/-/babel-plugin-transform-class-properties-6.24.1.tgz#6a79763ea61d33d36f37b611aa9def81a81b46ac" - dependencies: - babel-helper-function-name "^6.24.1" - babel-plugin-syntax-class-properties "^6.8.0" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-plugin-transform-es2015-arrow-functions@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz#452692cb711d5f79dc7f85e440ce41b9f244d221" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-block-scoped-functions@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-block-scoped-functions/-/babel-plugin-transform-es2015-block-scoped-functions-6.22.0.tgz#bbc51b49f964d70cb8d8e0b94e820246ce3a6141" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-block-scoping@^6.23.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-block-scoping/-/babel-plugin-transform-es2015-block-scoping-6.26.0.tgz#d70f5299c1308d05c12f463813b0a09e73b1895f" - dependencies: - babel-runtime "^6.26.0" - babel-template "^6.26.0" - babel-traverse "^6.26.0" - babel-types "^6.26.0" - lodash "^4.17.4" - -babel-plugin-transform-es2015-classes@^6.23.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-classes/-/babel-plugin-transform-es2015-classes-6.24.1.tgz#5a4c58a50c9c9461e564b4b2a3bfabc97a2584db" - dependencies: - babel-helper-define-map "^6.24.1" - babel-helper-function-name "^6.24.1" - babel-helper-optimise-call-expression "^6.24.1" - babel-helper-replace-supers "^6.24.1" - babel-messages "^6.23.0" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-computed-properties@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-computed-properties/-/babel-plugin-transform-es2015-computed-properties-6.24.1.tgz#6fe2a8d16895d5634f4cd999b6d3480a308159b3" - dependencies: - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-plugin-transform-es2015-destructuring@^6.23.0: - version "6.23.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-destructuring/-/babel-plugin-transform-es2015-destructuring-6.23.0.tgz#997bb1f1ab967f682d2b0876fe358d60e765c56d" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-duplicate-keys@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-duplicate-keys/-/babel-plugin-transform-es2015-duplicate-keys-6.24.1.tgz#73eb3d310ca969e3ef9ec91c53741a6f1576423e" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-for-of@^6.23.0: - version "6.23.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-for-of/-/babel-plugin-transform-es2015-for-of-6.23.0.tgz#f47c95b2b613df1d3ecc2fdb7573623c75248691" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-function-name@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-function-name/-/babel-plugin-transform-es2015-function-name-6.24.1.tgz#834c89853bc36b1af0f3a4c5dbaa94fd8eacaa8b" - dependencies: - babel-helper-function-name "^6.24.1" - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-literals@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-literals/-/babel-plugin-transform-es2015-literals-6.22.0.tgz#4f54a02d6cd66cf915280019a31d31925377ca2e" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-modules-amd@^6.22.0, babel-plugin-transform-es2015-modules-amd@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-amd/-/babel-plugin-transform-es2015-modules-amd-6.24.1.tgz#3b3e54017239842d6d19c3011c4bd2f00a00d154" - dependencies: - babel-plugin-transform-es2015-modules-commonjs "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-plugin-transform-es2015-modules-commonjs@6.26.0, babel-plugin-transform-es2015-modules-commonjs@^6.23.0, babel-plugin-transform-es2015-modules-commonjs@^6.24.1: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.0.tgz#0d8394029b7dc6abe1a97ef181e00758dd2e5d8a" - dependencies: - babel-plugin-transform-strict-mode "^6.24.1" - babel-runtime "^6.26.0" - babel-template "^6.26.0" - babel-types "^6.26.0" - -babel-plugin-transform-es2015-modules-systemjs@^6.23.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-systemjs/-/babel-plugin-transform-es2015-modules-systemjs-6.24.1.tgz#ff89a142b9119a906195f5f106ecf305d9407d23" - dependencies: - babel-helper-hoist-variables "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-plugin-transform-es2015-modules-umd@^6.23.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-modules-umd/-/babel-plugin-transform-es2015-modules-umd-6.24.1.tgz#ac997e6285cd18ed6176adb607d602344ad38468" - dependencies: - babel-plugin-transform-es2015-modules-amd "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - -babel-plugin-transform-es2015-object-super@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-object-super/-/babel-plugin-transform-es2015-object-super-6.24.1.tgz#24cef69ae21cb83a7f8603dad021f572eb278f8d" - dependencies: - babel-helper-replace-supers "^6.24.1" - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-parameters@^6.23.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-parameters/-/babel-plugin-transform-es2015-parameters-6.24.1.tgz#57ac351ab49caf14a97cd13b09f66fdf0a625f2b" - dependencies: - babel-helper-call-delegate "^6.24.1" - babel-helper-get-function-arity "^6.24.1" - babel-runtime "^6.22.0" - babel-template "^6.24.1" - babel-traverse "^6.24.1" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-shorthand-properties@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-shorthand-properties/-/babel-plugin-transform-es2015-shorthand-properties-6.24.1.tgz#24f875d6721c87661bbd99a4622e51f14de38aa0" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-spread@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-spread/-/babel-plugin-transform-es2015-spread-6.22.0.tgz#d6d68a99f89aedc4536c81a542e8dd9f1746f8d1" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-sticky-regex@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-sticky-regex/-/babel-plugin-transform-es2015-sticky-regex-6.24.1.tgz#00c1cdb1aca71112cdf0cf6126c2ed6b457ccdbc" - dependencies: - babel-helper-regex "^6.24.1" - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-plugin-transform-es2015-template-literals@^6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-template-literals/-/babel-plugin-transform-es2015-template-literals-6.22.0.tgz#a84b3450f7e9f8f1f6839d6d687da84bb1236d8d" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-typeof-symbol@^6.23.0: - version "6.23.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-typeof-symbol/-/babel-plugin-transform-es2015-typeof-symbol-6.23.0.tgz#dec09f1cddff94b52ac73d505c84df59dcceb372" - dependencies: - babel-runtime "^6.22.0" - -babel-plugin-transform-es2015-unicode-regex@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-unicode-regex/-/babel-plugin-transform-es2015-unicode-regex-6.24.1.tgz#d38b12f42ea7323f729387f18a7c5ae1faeb35e9" - dependencies: - babel-helper-regex "^6.24.1" - babel-runtime "^6.22.0" - regexpu-core "^2.0.0" - -babel-plugin-transform-exponentiation-operator@^6.22.0: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-exponentiation-operator/-/babel-plugin-transform-exponentiation-operator-6.24.1.tgz#2ab0c9c7f3098fa48907772bb813fe41e8de3a0e" - dependencies: - babel-helper-builder-binary-assignment-operator-visitor "^6.24.1" - babel-plugin-syntax-exponentiation-operator "^6.8.0" - babel-runtime "^6.22.0" - -babel-plugin-transform-flow-strip-types@6.22.0: - version "6.22.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-strip-types/-/babel-plugin-transform-flow-strip-types-6.22.0.tgz#84cb672935d43714fdc32bce84568d87441cf7cf" - dependencies: - babel-plugin-syntax-flow "^6.18.0" - babel-runtime "^6.22.0" - -babel-plugin-transform-object-rest-spread@6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06" - dependencies: - babel-plugin-syntax-object-rest-spread "^6.8.0" - babel-runtime "^6.26.0" - -babel-plugin-transform-regenerator@^6.22.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f" - dependencies: - regenerator-transform "^0.10.0" - -babel-plugin-transform-strict-mode@^6.24.1: - version "6.24.1" - resolved "https://registry.yarnpkg.com/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758" - dependencies: - babel-runtime "^6.22.0" - babel-types "^6.24.1" - -babel-polyfill@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-polyfill/-/babel-polyfill-6.26.0.tgz#379937abc67d7895970adc621f284cd966cf2153" - dependencies: - babel-runtime "^6.26.0" - core-js "^2.5.0" - regenerator-runtime "^0.10.5" - -babel-preset-env@^1.5.2: - version "1.6.1" - resolved "https://registry.yarnpkg.com/babel-preset-env/-/babel-preset-env-1.6.1.tgz#a18b564cc9b9afdf4aae57ae3c1b0d99188e6f48" - dependencies: - babel-plugin-check-es2015-constants "^6.22.0" - babel-plugin-syntax-trailing-function-commas "^6.22.0" - babel-plugin-transform-async-to-generator "^6.22.0" - babel-plugin-transform-es2015-arrow-functions "^6.22.0" - babel-plugin-transform-es2015-block-scoped-functions "^6.22.0" - babel-plugin-transform-es2015-block-scoping "^6.23.0" - babel-plugin-transform-es2015-classes "^6.23.0" - babel-plugin-transform-es2015-computed-properties "^6.22.0" - babel-plugin-transform-es2015-destructuring "^6.23.0" - babel-plugin-transform-es2015-duplicate-keys "^6.22.0" - babel-plugin-transform-es2015-for-of "^6.23.0" - babel-plugin-transform-es2015-function-name "^6.22.0" - babel-plugin-transform-es2015-literals "^6.22.0" - babel-plugin-transform-es2015-modules-amd "^6.22.0" - babel-plugin-transform-es2015-modules-commonjs "^6.23.0" - babel-plugin-transform-es2015-modules-systemjs "^6.23.0" - babel-plugin-transform-es2015-modules-umd "^6.23.0" - babel-plugin-transform-es2015-object-super "^6.22.0" - babel-plugin-transform-es2015-parameters "^6.23.0" - babel-plugin-transform-es2015-shorthand-properties "^6.22.0" - babel-plugin-transform-es2015-spread "^6.22.0" - babel-plugin-transform-es2015-sticky-regex "^6.22.0" - babel-plugin-transform-es2015-template-literals "^6.22.0" - babel-plugin-transform-es2015-typeof-symbol "^6.23.0" - babel-plugin-transform-es2015-unicode-regex "^6.22.0" - babel-plugin-transform-exponentiation-operator "^6.22.0" - babel-plugin-transform-regenerator "^6.22.0" - browserslist "^2.1.2" - invariant "^2.2.2" - semver "^5.3.0" - -babel-register@^6.26.0: - version "6.26.0" - resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.26.0.tgz#6ed021173e2fcb486d7acb45c6009a856f647071" - dependencies: - babel-core "^6.26.0" - babel-runtime "^6.26.0" - core-js "^2.5.0" - home-or-tmp "^2.0.0" - lodash "^4.17.4" - mkdirp "^0.5.1" - source-map-support "^0.4.15" - -babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0: +babel-runtime@^6.22.0, babel-runtime@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" dependencies: core-js "^2.4.0" regenerator-runtime "^0.11.0" -babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0: +babel-template@^6.16.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02" dependencies: @@ -811,7 +916,7 @@ babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0: babylon "^6.18.0" lodash "^4.17.4" -babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-traverse@^6.26.0: +babel-traverse@^6.18.0, babel-traverse@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" dependencies: @@ -825,7 +930,7 @@ babel-traverse@^6.18.0, babel-traverse@^6.24.1, babel-traverse@^6.26.0: invariant "^2.2.2" lodash "^4.17.4" -babel-types@^6.18.0, babel-types@^6.19.0, babel-types@^6.24.1, babel-types@^6.26.0: +babel-types@^6.18.0, babel-types@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-types/-/babel-types-6.26.0.tgz#a3b073f94ab49eb6fa55cd65227a334380632497" dependencies: @@ -838,6 +943,10 @@ babylon@7.0.0-beta.44: version "7.0.0-beta.44" resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.44.tgz#89159e15e6e30c5096e22d738d8c0af8a0e8ca1d" +babylon@7.0.0-beta.47: + version "7.0.0-beta.47" + resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.47.tgz#6d1fa44f0abec41ab7c780481e62fd9aafbdea80" + babylon@^6.18.0: version "6.18.0" resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.18.0.tgz#af2f3b88fa6f5c1e4c634d1a0f8eac4f55b395e3" @@ -918,7 +1027,7 @@ braces@^1.8.2: preserve "^0.2.0" repeat-element "^1.1.2" -braces@^2.3.1: +braces@^2.3.0, braces@^2.3.1: version "2.3.2" resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.2.tgz#5979fd3f14cd531565e5fa2df1abfff1dfaee729" dependencies: @@ -937,12 +1046,12 @@ browser-stdout@1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60" -browserslist@^2.1.2: - version "2.9.1" - resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-2.9.1.tgz#b72d3982ab01b5cd24da62ff6d45573886aff275" +browserslist@^3.0.0: + version "3.2.7" + resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-3.2.7.tgz#aa488634d320b55e88bab0256184dbbcca1e6de9" dependencies: - caniuse-lite "^1.0.30000770" - electron-to-chromium "^1.3.27" + caniuse-lite "^1.0.30000835" + electron-to-chromium "^1.3.45" bser@^2.0.0: version "2.0.0" @@ -994,9 +1103,9 @@ camelcase@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" -caniuse-lite@^1.0.30000770: - version "1.0.30000777" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000777.tgz#31c18a4a8cd49782ebb305c8e8a93e6b3b3e4f13" +caniuse-lite@^1.0.30000835: + version "1.0.30000840" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000840.tgz#344513f8f843536cf99694964c09811277eee395" caseless@~0.12.0: version "0.12.0" @@ -1046,20 +1155,27 @@ check-error@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" -chokidar@^1.6.1: - version "1.7.0" - resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" +chokidar@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.0.3.tgz#dcbd4f6cbb2a55b4799ba8a840ac527e5f4b1176" dependencies: - anymatch "^1.3.0" + anymatch "^2.0.0" async-each "^1.0.0" - glob-parent "^2.0.0" + braces "^2.3.0" + glob-parent "^3.1.0" inherits "^2.0.1" is-binary-path "^1.0.0" - is-glob "^2.0.0" + is-glob "^4.0.0" + normalize-path "^2.1.1" path-is-absolute "^1.0.0" readdirp "^2.0.0" + upath "^1.0.0" optionalDependencies: - fsevents "^1.0.0" + fsevents "^1.1.2" + +chownr@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.0.1.tgz#e2a75042a9551908bebd25b8523d5f9769d79181" circular-json@^0.3.1: version "0.3.3" @@ -1135,9 +1251,9 @@ commander@2.11.0: version "2.11.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" -commander@^2.11.0: - version "2.12.2" - resolved "https://registry.yarnpkg.com/commander/-/commander-2.12.2.tgz#0f5946c427ed9ec0d91a46bb9def53e54650e555" +commander@^2.8.1: + version "2.15.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" commondir@^1.0.1: version "1.0.1" @@ -1163,7 +1279,7 @@ console-control-strings@^1.0.0, console-control-strings@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" -convert-source-map@^1.5.0, convert-source-map@^1.5.1: +convert-source-map@^1.1.0, convert-source-map@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5" @@ -1171,10 +1287,14 @@ copy-descriptor@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d" -core-js@^2.4.0, core-js@^2.5.0: +core-js@^2.4.0: version "2.5.1" resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.1.tgz#ae6874dc66937789b80754ff5428df66819ca50b" +core-js@^2.5.3: + version "2.5.6" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.6.tgz#0fe6d45bf3cac3ac364a9d72de7576f4eb221b9d" + core-util-is@1.0.2, core-util-is@~1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" @@ -1232,7 +1352,7 @@ debug@3.1.0, debug@^3.1.0: dependencies: ms "2.0.0" -debug@^2.2.0, debug@^2.3.3, debug@^2.6.8: +debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" dependencies: @@ -1331,9 +1451,9 @@ ecc-jsbn@~0.1.1: dependencies: jsbn "~0.1.0" -electron-to-chromium@^1.3.27: - version "1.3.27" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.27.tgz#78ecb8a399066187bb374eede35d9c70565a803d" +electron-to-chromium@^1.3.45: + version "1.3.45" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.45.tgz#458ac1b1c5c760ce8811a16d2bfbd97ec30bafb8" error-ex@^1.2.0: version "1.3.1" @@ -1614,6 +1734,14 @@ find-cache-dir@^0.1.1: mkdirp "^0.5.1" pkg-dir "^1.0.0" +find-cache-dir@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-1.0.0.tgz#9288e3e9e3cc3748717d39eade17cf71fc30ee6f" + dependencies: + commondir "^1.0.1" + make-dir "^1.0.0" + pkg-dir "^2.0.0" + find-up@^1.0.0: version "1.1.2" resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f" @@ -1683,6 +1811,12 @@ fragment-cache@^0.2.1: dependencies: map-cache "^0.2.2" +fs-minipass@^1.2.5: + version "1.2.5" + resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d" + dependencies: + minipass "^2.2.1" + fs-readdir-recursive@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz#e32fc030a2ccee44a6b5371308da54be0b397d27" @@ -1691,13 +1825,20 @@ fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" -fsevents@^1.0.0, fsevents@^1.1.1: +fsevents@^1.1.1: version "1.1.3" resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.1.3.tgz#11f82318f5fe7bb2cd22965a108e9306208216d8" dependencies: nan "^2.3.0" node-pre-gyp "^0.6.39" +fsevents@^1.1.2: + version "1.2.4" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-1.2.4.tgz#f41dcb1af2582af3692da36fc55cbd8e1041c426" + dependencies: + nan "^2.9.2" + node-pre-gyp "^0.10.0" + fstream-ignore@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/fstream-ignore/-/fstream-ignore-1.0.5.tgz#9c31dae34767018fe1d249b24dada67d092da105" @@ -1767,7 +1908,14 @@ glob-parent@^2.0.0: dependencies: is-glob "^2.0.0" -glob@7.1.2, glob@^7.0.3, glob@^7.0.5, glob@^7.0.6, glob@^7.1.2: +glob-parent@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-3.1.0.tgz#9e6af6299d8d3bd2bd40430832bd113df906c5ae" + dependencies: + is-glob "^3.1.0" + path-dirname "^1.0.0" + +glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.0.6, glob@^7.1.2: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -1797,7 +1945,7 @@ globby@^5.0.0: pify "^2.0.0" pinkie-promise "^2.0.0" -graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4: +graceful-fs@^4.1.11, graceful-fs@^4.1.2: version "4.1.11" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" @@ -1912,12 +2060,15 @@ hoek@4.x.x: version "4.2.0" resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.0.tgz#72d9d0754f7fe25ca2d01ad8f8f9a9449a89526d" -home-or-tmp@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8" +home-or-tmp@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-3.0.0.tgz#57a8fe24cf33cdd524860a15821ddc25c86671fb" + +homedir-polyfill@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.1.tgz#4c2bbc8a758998feebf5ed68580f76d46768b4bc" dependencies: - os-homedir "^1.0.0" - os-tmpdir "^1.0.1" + parse-passwd "^1.0.0" hosted-git-info@^2.1.4: version "2.6.0" @@ -1943,6 +2094,18 @@ iconv-lite@^0.4.17: version "0.4.19" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" +iconv-lite@^0.4.4: + version "0.4.23" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.23.tgz#297871f63be507adcfbfca715d0cd0eed84e9a63" + dependencies: + safer-buffer ">= 2.1.2 < 3" + +ignore-walk@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.1.tgz#a83e62e7d272ac0e3b551aaa82831a19b69f82f8" + dependencies: + minimatch "^3.0.4" + ignore@^3.3.3: version "3.3.7" resolved "https://registry.yarnpkg.com/ignore/-/ignore-3.3.7.tgz#612289bfb3c220e186a58118618d5be8c1bab021" @@ -2079,6 +2242,10 @@ is-extglob@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0" +is-extglob@^2.1.0, is-extglob@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2" + is-finite@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/is-finite/-/is-finite-1.0.2.tgz#cc6677695602be550ef11e8b4aa6305342b6d0aa" @@ -2101,6 +2268,18 @@ is-glob@^2.0.0, is-glob@^2.0.1: dependencies: is-extglob "^1.0.0" +is-glob@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-3.1.0.tgz#7ba5ae24217804ac70707b96922567486cc3e84a" + dependencies: + is-extglob "^2.1.0" + +is-glob@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.0.tgz#9521c76845cc2610a85203ddf080a958c2ffabc0" + dependencies: + is-extglob "^2.1.1" + is-number@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f" @@ -2139,6 +2318,10 @@ is-path-inside@^1.0.0: dependencies: path-is-inside "^1.0.1" +is-plain-obj@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e" + is-plain-object@^2.0.1, is-plain-object@^2.0.3, is-plain-object@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677" @@ -2305,7 +2488,7 @@ json-stringify-safe@~5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" -json5@^0.5.1: +json5@^0.5.0: version "0.5.1" resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" @@ -2384,6 +2567,10 @@ lodash@^4.15.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0: version "4.17.4" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" +lodash@^4.17.5: + version "4.17.10" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7" + log-driver@^1.2.5: version "1.2.5" resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.5.tgz#7ae4ec257302fd790d557cb10c97100d857b0056" @@ -2405,6 +2592,12 @@ lru-cache@^4.0.1: pseudomap "^1.0.2" yallist "^2.1.2" +make-dir@^1.0.0: + version "1.3.0" + resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-1.3.0.tgz#79c1033b80515bd6d24ec9933e860ca75ee27f0c" + dependencies: + pify "^3.0.0" + makeerror@1.0.x: version "1.0.11" resolved "https://registry.yarnpkg.com/makeerror/-/makeerror-1.0.11.tgz#e01a5c9109f2af79660e4e8b9587790184f5a96c" @@ -2447,7 +2640,7 @@ merge@^1.1.3: version "1.2.0" resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da" -micromatch@^2.1.5, micromatch@^2.3.11: +micromatch@^2.3.11: version "2.3.11" resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565" dependencies: @@ -2515,6 +2708,19 @@ minimist@~0.0.1: version "0.0.10" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" +minipass@^2.2.1, minipass@^2.2.4: + version "2.3.0" + resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.3.0.tgz#2e11b1c46df7fe7f1afbe9a490280add21ffe384" + dependencies: + safe-buffer "^5.1.1" + yallist "^3.0.0" + +minizlib@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.1.0.tgz#11e13658ce46bc3a70a267aac58359d1e0c29ceb" + dependencies: + minipass "^2.2.1" + mixin-deep@^1.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/mixin-deep/-/mixin-deep-1.3.1.tgz#a49e7268dce1a0d9698e45326c5626df3543d0fe" @@ -2556,6 +2762,10 @@ nan@^2.3.0: version "2.8.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a" +nan@^2.9.2: + version "2.10.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f" + nanomatch@^1.2.9: version "1.2.9" resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.9.tgz#879f7150cb2dab7a471259066c104eee6e0fa7c2" @@ -2577,10 +2787,37 @@ natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" +needle@^2.2.0: + version "2.2.1" + resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.1.tgz#b5e325bd3aae8c2678902fa296f729455d1d3a7d" + dependencies: + debug "^2.1.2" + iconv-lite "^0.4.4" + sax "^1.2.4" + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" +node-modules-regexp@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40" + +node-pre-gyp@^0.10.0: + version "0.10.0" + resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.10.0.tgz#6e4ef5bb5c5203c6552448828c852c40111aac46" + dependencies: + detect-libc "^1.0.2" + mkdirp "^0.5.1" + needle "^2.2.0" + nopt "^4.0.1" + npm-packlist "^1.1.6" + npmlog "^4.0.2" + rc "^1.1.7" + rimraf "^2.6.1" + semver "^5.3.0" + tar "^4" + node-pre-gyp@^0.6.39: version "0.6.39" resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649" @@ -2613,12 +2850,23 @@ normalize-package-data@^2.3.2: semver "2 || 3 || 4 || 5" validate-npm-package-license "^3.0.1" -normalize-path@^2.0.0, normalize-path@^2.0.1, normalize-path@^2.1.1: +normalize-path@^2.0.1, normalize-path@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9" dependencies: remove-trailing-separator "^1.0.1" +npm-bundled@^1.0.1: + version "1.0.3" + resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.3.tgz#7e71703d973af3370a9591bafe3a63aca0be2308" + +npm-packlist@^1.1.6: + version "1.1.10" + resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.1.10.tgz#1039db9e985727e464df066f4cf0ab6ef85c398a" + dependencies: + ignore-walk "^3.0.1" + npm-bundled "^1.0.1" + npm-run-path@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f" @@ -2747,7 +2995,7 @@ os-locale@^2.0.0: lcid "^1.0.0" mem "^1.1.0" -os-tmpdir@^1.0.0, os-tmpdir@^1.0.1, os-tmpdir@~1.0.2: +os-tmpdir@^1.0.0, os-tmpdir@~1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" @@ -2758,13 +3006,13 @@ osenv@^0.1.4: os-homedir "^1.0.0" os-tmpdir "^1.0.0" -output-file-sync@^1.1.2: - version "1.1.2" - resolved "https://registry.yarnpkg.com/output-file-sync/-/output-file-sync-1.1.2.tgz#d0a33eefe61a205facb90092e826598d5245ce76" +output-file-sync@^2.0.0: + version "2.0.1" + resolved "https://registry.yarnpkg.com/output-file-sync/-/output-file-sync-2.0.1.tgz#f53118282f5f553c2799541792b723a4c71430c0" dependencies: - graceful-fs "^4.1.4" + graceful-fs "^4.1.11" + is-plain-obj "^1.1.0" mkdirp "^0.5.1" - object-assign "^4.1.0" p-finally@^1.0.0: version "1.0.0" @@ -2801,10 +3049,18 @@ parse-json@^2.2.0: dependencies: error-ex "^1.2.0" +parse-passwd@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6" + pascalcase@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14" +path-dirname@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0" + path-exists@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b" @@ -2815,7 +3071,7 @@ path-exists@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" -path-is-absolute@^1.0.0, path-is-absolute@^1.0.1: +path-is-absolute@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" @@ -2855,6 +3111,10 @@ pify@^2.0.0: version "2.3.0" resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" +pify@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176" + pinkie-promise@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa" @@ -2865,12 +3125,24 @@ pinkie@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" +pirates@^3.0.1: + version "3.0.2" + resolved "https://registry.yarnpkg.com/pirates/-/pirates-3.0.2.tgz#7e6f85413fd9161ab4e12b539b06010d85954bb9" + dependencies: + node-modules-regexp "^1.0.0" + pkg-dir@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-1.0.0.tgz#7a4b508a8d5bb2d629d447056ff4e9c9314cf3d4" dependencies: find-up "^1.0.0" +pkg-dir@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b" + dependencies: + find-up "^2.1.0" + platform@^1.3.3: version "1.3.5" resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444" @@ -2895,7 +3167,7 @@ prettier@1.12.1: version "1.12.1" resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.12.1.tgz#c1ad20e803e7749faf905a409d2367e06bbe7325" -private@^0.1.6, private@^0.1.7: +private@^0.1.6: version "0.1.8" resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff" @@ -2975,24 +3247,28 @@ readdirp@^2.0.0: readable-stream "^2.0.2" set-immediate-shim "^1.0.1" -regenerate@^1.2.1: - version "1.3.3" - resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.3.tgz#0c336d3980553d755c39b586ae3b20aa49c82b7f" +regenerate-unicode-properties@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-6.0.0.tgz#0fc26f9d5142289df4e177dec58f303d2d097c16" + dependencies: + regenerate "^1.3.3" -regenerator-runtime@^0.10.5: - version "0.10.5" - resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.10.5.tgz#336c3efc1220adcedda2c9fab67b5a7955a33658" +regenerate@^1.3.3, regenerate@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11" regenerator-runtime@^0.11.0: version "0.11.0" resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz#7e54fe5b5ccd5d6624ea6255c3473be090b802e1" -regenerator-transform@^0.10.0: - version "0.10.1" - resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.10.1.tgz#1e4996837231da8b7f3cf4114d71b5691a0680dd" +regenerator-runtime@^0.11.1: + version "0.11.1" + resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9" + +regenerator-transform@^0.12.3: + version "0.12.3" + resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.12.3.tgz#459adfb64f6a27164ab991b7873f45ab969eca8b" dependencies: - babel-runtime "^6.18.0" - babel-types "^6.19.0" private "^0.1.6" regex-cache@^0.4.2: @@ -3012,21 +3288,35 @@ regexpp@^1.0.1: version "1.1.0" resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-1.1.0.tgz#0e3516dd0b7904f413d2d4193dce4618c3a689ab" -regexpu-core@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-2.0.0.tgz#49d038837b8dcf8bfa5b9a42139938e6ea2ae240" - dependencies: - regenerate "^1.2.1" - regjsgen "^0.2.0" - regjsparser "^0.1.4" - -regjsgen@^0.2.0: - version "0.2.0" - resolved "https://registry.yarnpkg.com/regjsgen/-/regjsgen-0.2.0.tgz#6c016adeac554f75823fe37ac05b92d5a4edb1f7" +regexpu-core@^4.1.3: + version "4.1.4" + resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-4.1.4.tgz#f6c847261ba4cf869a7874442f9d25251b713fc8" + dependencies: + regenerate "^1.4.0" + regenerate-unicode-properties "^6.0.0" + regjsgen "^0.4.0" + regjsparser "^0.3.0" + unicode-match-property-ecmascript "^1.0.3" + unicode-match-property-value-ecmascript "^1.0.1" + +regexpu-core@^4.1.4: + version "4.1.5" + resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-4.1.5.tgz#57fdfe1148f8a7a069086228515130cf1820ddd0" + dependencies: + regenerate "^1.4.0" + regenerate-unicode-properties "^6.0.0" + regjsgen "^0.4.0" + regjsparser "^0.3.0" + unicode-match-property-ecmascript "^1.0.3" + unicode-match-property-value-ecmascript "^1.0.1" + +regjsgen@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/regjsgen/-/regjsgen-0.4.0.tgz#c1eb4c89a209263f8717c782591523913ede2561" -regjsparser@^0.1.4: - version "0.1.5" - resolved "https://registry.yarnpkg.com/regjsparser/-/regjsparser-0.1.5.tgz#7ee8f84dc6fa792d3fd0ae228d24bd949ead205c" +regjsparser@^0.3.0: + version "0.3.0" + resolved "https://registry.yarnpkg.com/regjsparser/-/regjsparser-0.3.0.tgz#3c326da7fcfd69fa0d332575a41c8c0cdf588c96" dependencies: jsesc "~0.5.0" @@ -3129,6 +3419,12 @@ resolve-url@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" +resolve@^1.3.2: + version "1.7.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.7.1.tgz#aadd656374fd298aee895bc026b8297418677fd3" + dependencies: + path-parse "^1.0.5" + restore-cursor@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf" @@ -3172,12 +3468,20 @@ safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853" +safe-buffer@^5.1.2: + version "5.1.2" + resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d" + safe-regex@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/safe-regex/-/safe-regex-1.1.0.tgz#40a3669f3b077d1e943d44629e157dd48023bf2e" dependencies: ret "~0.1.10" +"safer-buffer@>= 2.1.2 < 3": + version "2.1.2" + resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a" + sane@2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/sane/-/sane-2.5.0.tgz#6359cd676f5efd9988b264d8ce3b827dd6b27bec" @@ -3192,7 +3496,11 @@ sane@2.5.0: optionalDependencies: fsevents "^1.1.1" -"semver@2 || 3 || 4 || 5": +sax@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" + +"semver@2 || 3 || 4 || 5", semver@^5.4.1: version "5.5.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab" @@ -3303,7 +3611,7 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" -source-map-support@^0.4.15: +source-map-support@^0.4.2: version "0.4.18" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f" dependencies: @@ -3496,6 +3804,18 @@ tar@^2.2.1: fstream "^1.0.2" inherits "2" +tar@^4: + version "4.4.2" + resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.2.tgz#60685211ba46b38847b1ae7ee1a24d744a2cd462" + dependencies: + chownr "^1.0.1" + fs-minipass "^1.2.5" + minipass "^2.2.4" + minizlib "^1.1.0" + mkdirp "^0.5.0" + safe-buffer "^5.1.2" + yallist "^3.0.2" + test-exclude@^4.2.0: version "4.2.1" resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.1.tgz#dfa222f03480bca69207ca728b37d74b45f724fa" @@ -3609,6 +3929,25 @@ uid-number@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/uid-number/-/uid-number-0.0.6.tgz#0ea10e8035e8eb5b8e4449f06da1c730663baa81" +unicode-canonical-property-names-ecmascript@^1.0.2: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-1.0.3.tgz#f6119f417467593c0086357c85546b6ad5abc583" + +unicode-match-property-ecmascript@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-match-property-ecmascript/-/unicode-match-property-ecmascript-1.0.3.tgz#db9b1cb4ffc67e0c5583780b1b59370e4cbe97b9" + dependencies: + unicode-canonical-property-names-ecmascript "^1.0.2" + unicode-property-aliases-ecmascript "^1.0.3" + +unicode-match-property-value-ecmascript@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/unicode-match-property-value-ecmascript/-/unicode-match-property-value-ecmascript-1.0.1.tgz#fea059120a016f403afd3bf586162b4db03e0604" + +unicode-property-aliases-ecmascript@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-1.0.3.tgz#ac3522583b9e630580f916635333e00c5ead690d" + union-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" @@ -3625,6 +3964,10 @@ unset-value@^1.0.0: has-value "^0.3.1" isobject "^3.0.0" +upath@^1.0.0: + version "1.0.5" + resolved "https://registry.yarnpkg.com/upath/-/upath-1.0.5.tgz#02cab9ecebe95bbec6d5fc2566325725ab6d1a73" + urix@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" @@ -3635,10 +3978,6 @@ use@^3.1.0: dependencies: kind-of "^6.0.2" -user-home@^1.1.1: - version "1.1.1" - resolved "https://registry.yarnpkg.com/user-home/-/user-home-1.1.1.tgz#2b5be23a32b63a7c9deb8d0f28d485724a3df190" - util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" @@ -3651,11 +3990,11 @@ uuid@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14" -v8flags@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-2.1.1.tgz#aab1a1fa30d45f88dd321148875ac02c0b55e5b4" +v8flags@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/v8flags/-/v8flags-3.1.0.tgz#246a34a8158c0e1390dcb758e1140e5d004e230b" dependencies: - user-home "^1.1.1" + homedir-polyfill "^1.0.1" validate-npm-package-license@^3.0.1: version "3.0.3" @@ -3750,6 +4089,10 @@ yallist@^2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" +yallist@^3.0.0, yallist@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.2.tgz#8452b4bb7e83c7c188d8041c1a837c773d6d8bb9" + yargs-parser@^8.0.0: version "8.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950"