graphql
Version:
A Query Language and Runtime which can target any service.
148 lines (121 loc) • 4.17 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
var _createClass = require('babel-runtime/helpers/create-class')['default'];
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
var _Object$keys = require('babel-runtime/core-js/object/keys')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports.typeMapReducer = typeMapReducer;
var _definition = require('./definition');
var _directives2 = require('./directives');
var _introspection = require('./introspection');
var _utilsFind = require('../utils/find');
var _utilsFind2 = _interopRequireDefault(_utilsFind);
/**
* 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:
*
* var MyAppSchema = new GraphQLSchema({
* query: MyAppQueryRootType
* mutation: MyAppMutationRootType
* });
*
*/
var GraphQLSchema = (function () {
function GraphQLSchema(config) {
_classCallCheck(this, GraphQLSchema);
this._schemaConfig = config;
}
_createClass(GraphQLSchema, [{
key: 'getQueryType',
value: function getQueryType() {
return this._schemaConfig.query;
}
}, {
key: 'getMutationType',
value: function getMutationType() {
return this._schemaConfig.mutation;
}
}, {
key: 'getTypeMap',
value: function getTypeMap() {
return this._typeMap || (this._typeMap = [this.getQueryType(), this.getMutationType(), _introspection.__Schema].reduce(typeMapReducer, {}));
}
}, {
key: 'getType',
value: function getType(name) {
return this.getTypeMap()[name];
}
}, {
key: 'getDirectives',
value: function getDirectives() {
return this._directives || (this._directives = [_directives2.GraphQLIfDirective, _directives2.GraphQLUnlessDirective]);
}
}, {
key: 'getDirective',
value: function getDirective(name) {
return (0, _utilsFind2['default'])(this.getDirectives(), function (directive) {
return directive.name === name;
});
}
}]);
return GraphQLSchema;
})();
exports.GraphQLSchema = GraphQLSchema;
function typeMapReducer(_x, _x2) {
var _again = true;
_function: while (_again) {
var map = _x,
type = _x2;
reducedMap = fieldMap = undefined;
_again = false;
if (type instanceof _definition.GraphQLList || type instanceof _definition.GraphQLNonNull) {
_x = map;
_x2 = type.ofType;
_again = true;
continue _function;
}
if (!type || map[type.name]) {
return map;
}
map[type.name] = type;
var reducedMap = map;
if (type instanceof _definition.GraphQLUnionType || type instanceof _definition.GraphQLInterfaceType) {
reducedMap = type.getPossibleTypes().reduce(typeMapReducer, reducedMap);
}
if (type instanceof _definition.GraphQLObjectType) {
reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap);
}
if (type instanceof _definition.GraphQLObjectType || type instanceof _definition.GraphQLInterfaceType) {
var fieldMap = type.getFields();
_Object$keys(fieldMap).forEach(function (fieldName) {
var field = fieldMap[fieldName];
if (!field.args) {
console.log('WTF ' + field.name + ' has no args?');
}
var fieldArgTypes = field.args.map(function (arg) {
return arg.type;
});
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
reducedMap = typeMapReducer(reducedMap, field.type);
});
}
return reducedMap;
}
}