UNPKG

@graphql-inspector/patch

Version:

Applies changes output from @graphql-inspect/diff

104 lines (103 loc) 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.typeAdded = typeAdded; exports.typeRemoved = typeRemoved; exports.typeDescriptionAdded = typeDescriptionAdded; exports.typeDescriptionChanged = typeDescriptionChanged; exports.typeDescriptionRemoved = typeDescriptionRemoved; const graphql_1 = require("graphql"); const errors_js_1 = require("../errors.js"); const node_templates_js_1 = require("../node-templates.js"); function typeAdded(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const existing = nodeByPath.get(change.path); if (existing) { config.onError(new errors_js_1.AddedCoordinateAlreadyExistsError(change.path, change.type), change); return; } const node = { name: (0, node_templates_js_1.nameNode)(change.meta.addedTypeName), kind: change.meta.addedTypeKind, }; nodeByPath.set(change.path, node); } function typeRemoved(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const removedNode = nodeByPath.get(change.path); if (!removedNode) { config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path, change.type), change); return; } if (!(0, graphql_1.isTypeDefinitionNode)(removedNode)) { config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path, change.type), change); return; } // delete the reference to the removed field. for (const key of nodeByPath.keys()) { if (key.startsWith(change.path)) { nodeByPath.delete(key); } } } function typeDescriptionAdded(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const typeNode = nodeByPath.get(change.path); if (!typeNode) { config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedTypeDescription), change); return; } if (!(0, graphql_1.isTypeDefinitionNode)(typeNode)) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.OBJECT_TYPE_DEFINITION, typeNode.kind), change); return; } typeNode.description = change.meta.addedTypeDescription ? (0, node_templates_js_1.stringNode)(change.meta.addedTypeDescription) : undefined; } function typeDescriptionChanged(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const typeNode = nodeByPath.get(change.path); if (!typeNode) { config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.newTypeDescription), change); return; } if (!(0, graphql_1.isTypeDefinitionNode)(typeNode)) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.OBJECT_TYPE_DEFINITION, typeNode.kind), change); return; } if (typeNode.description?.value !== change.meta.oldTypeDescription) { config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.STRING, change.meta.oldTypeDescription, typeNode.description?.value), change); } typeNode.description = (0, node_templates_js_1.stringNode)(change.meta.newTypeDescription); } function typeDescriptionRemoved(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const typeNode = nodeByPath.get(change.path); if (!typeNode) { config.onError(new errors_js_1.DeletedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.removedTypeDescription), change); return; } if (!(0, graphql_1.isTypeDefinitionNode)(typeNode)) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.OBJECT_TYPE_DEFINITION, typeNode.kind), change); return; } if (typeNode.description?.value !== change.meta.removedTypeDescription) { config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.STRING, change.meta.removedTypeDescription, typeNode.description?.value), change); } typeNode.description = undefined; }