123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.buildClientSchema = buildClientSchema;
- var _objectValues = _interopRequireDefault(require("../polyfills/objectValues"));
- var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
- var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
- var _keyValMap = _interopRequireDefault(require("../jsutils/keyValMap"));
- var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
- var _parser = require("../language/parser");
- var _directives = require("../type/directives");
- var _scalars = require("../type/scalars");
- var _introspection = require("../type/introspection");
- var _schema = require("../type/schema");
- var _definition = require("../type/definition");
- var _valueFromAST = require("./valueFromAST");
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function buildClientSchema(introspection, options) {
- (0, _isObjectLike.default)(introspection) && (0, _isObjectLike.default)(introspection.__schema) || (0, _devAssert.default)(0, 'Invalid or incomplete introspection result. Ensure that you are passing "data" property of introspection response and no "errors" was returned alongside: ' + (0, _inspect.default)(introspection));
- var schemaIntrospection = introspection.__schema;
- var typeMap = (0, _keyValMap.default)(schemaIntrospection.types, function (typeIntrospection) {
- return typeIntrospection.name;
- }, function (typeIntrospection) {
- return buildType(typeIntrospection);
- });
- for (var _i2 = 0, _ref2 = [].concat(_scalars.specifiedScalarTypes, _introspection.introspectionTypes); _i2 < _ref2.length; _i2++) {
- var stdType = _ref2[_i2];
- if (typeMap[stdType.name]) {
- typeMap[stdType.name] = stdType;
- }
- }
- var queryType = schemaIntrospection.queryType ? getObjectType(schemaIntrospection.queryType) : null;
- var mutationType = schemaIntrospection.mutationType ? getObjectType(schemaIntrospection.mutationType) : null;
- var subscriptionType = schemaIntrospection.subscriptionType ? getObjectType(schemaIntrospection.subscriptionType) : null;
-
- var directives = schemaIntrospection.directives ? schemaIntrospection.directives.map(buildDirective) : [];
- return new _schema.GraphQLSchema({
- query: queryType,
- mutation: mutationType,
- subscription: subscriptionType,
- types: (0, _objectValues.default)(typeMap),
- directives: directives,
- assumeValid: options && options.assumeValid,
- allowedLegacyNames: options && options.allowedLegacyNames
- });
-
- 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: ' + (0, _inspect.default)(typeRef));
- }
- return getNamedType(typeRef.name);
- }
- function getNamedType(typeName) {
- var type = typeMap[typeName];
- if (!type) {
- 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."));
- }
- return type;
- }
- function getInputType(typeRef) {
- var type = getType(typeRef);
- if ((0, _definition.isInputType)(type)) {
- return type;
- }
- throw new Error('Introspection must provide input type for arguments, but received: ' + (0, _inspect.default)(type) + '.');
- }
- function getOutputType(typeRef) {
- var type = getType(typeRef);
- if ((0, _definition.isOutputType)(type)) {
- return type;
- }
- throw new Error('Introspection must provide output type for fields, but received: ' + (0, _inspect.default)(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);
- }
-
- 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:' + (0, _inspect.default)(type));
- }
- function buildScalarDef(scalarIntrospection) {
- return new _definition.GraphQLScalarType({
- name: scalarIntrospection.name,
- description: scalarIntrospection.description
- });
- }
- function buildObjectDef(objectIntrospection) {
- if (!objectIntrospection.interfaces) {
- throw new Error('Introspection result missing interfaces: ' + (0, _inspect.default)(objectIntrospection));
- }
- return new _definition.GraphQLObjectType({
- name: objectIntrospection.name,
- description: objectIntrospection.description,
- interfaces: function interfaces() {
- return 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: ' + (0, _inspect.default)(unionIntrospection));
- }
- return new _definition.GraphQLUnionType({
- name: unionIntrospection.name,
- description: unionIntrospection.description,
- types: function types() {
- return unionIntrospection.possibleTypes.map(getObjectType);
- }
- });
- }
- function buildEnumDef(enumIntrospection) {
- if (!enumIntrospection.enumValues) {
- throw new Error('Introspection result missing enumValues: ' + (0, _inspect.default)(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: ' + (0, _inspect.default)(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: ' + (0, _inspect.default)(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: ' + (0, _inspect.default)(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 {
- description: inputValueIntrospection.description,
- type: type,
- defaultValue: defaultValue
- };
- }
- function buildDirective(directiveIntrospection) {
- if (!directiveIntrospection.args) {
- throw new Error('Introspection result missing directive args: ' + (0, _inspect.default)(directiveIntrospection));
- }
- if (!directiveIntrospection.locations) {
- throw new Error('Introspection result missing directive locations: ' + (0, _inspect.default)(directiveIntrospection));
- }
- return new _directives.GraphQLDirective({
- name: directiveIntrospection.name,
- description: directiveIntrospection.description,
- locations: directiveIntrospection.locations.slice(),
- args: buildInputValueDefMap(directiveIntrospection.args)
- });
- }
- }
|