UNPKG

@graphql-inspector/cli

Version:

Tooling for GraphQL. Compare GraphQL Schemas, check documents, find breaking changes, find similar types.

141 lines (140 loc) • 7.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.inputFieldAdded = inputFieldAdded; exports.inputFieldRemoved = inputFieldRemoved; exports.inputFieldDescriptionAdded = inputFieldDescriptionAdded; exports.inputFieldTypeChanged = inputFieldTypeChanged; exports.inputFieldDefaultValueChanged = inputFieldDefaultValueChanged; exports.inputFieldDescriptionChanged = inputFieldDescriptionChanged; exports.inputFieldDescriptionRemoved = inputFieldDescriptionRemoved; const graphql_1 = require("graphql"); const errors_js_1 = require("../errors.js"); const node_templates_js_1 = require("../node-templates.js"); const utils_js_1 = require("../utils.js"); function inputFieldAdded(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const existingNode = nodeByPath.get(change.path); if (existingNode) { if (existingNode.kind === graphql_1.Kind.INPUT_VALUE_DEFINITION) { config.onError(new errors_js_1.AddedCoordinateAlreadyExistsError(change.path, change.type), change); } else { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.INPUT_VALUE_DEFINITION, existingNode.kind), change); } return; } const typeNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path)); if (!typeNode) { config.onError(new errors_js_1.AddedAttributeCoordinateNotFoundError(change.path, change.type, change.meta.addedInputFieldName), change); return; } if (typeNode.kind !== graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, typeNode.kind), change); return; } const node = { kind: graphql_1.Kind.INPUT_VALUE_DEFINITION, name: (0, node_templates_js_1.nameNode)(change.meta.addedInputFieldName), type: (0, graphql_1.parseType)(change.meta.addedInputFieldType), // description: change.meta.addedInputFieldDescription // ? stringNode(change.meta.addedInputFieldDescription) // : undefined, }; typeNode.fields = [...(typeNode.fields ?? []), node]; // add new field to the node set nodeByPath.set(change.path, node); } function inputFieldRemoved(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const existingNode = nodeByPath.get(change.path); if (!existingNode) { config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path, change.type), change); return; } const typeNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path)); if (!typeNode) { config.onError(new errors_js_1.DeletedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.removedFieldName), change); return; } if (typeNode.kind !== graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION, typeNode.kind), change); return; } typeNode.fields = typeNode.fields?.filter(f => f.name.value !== change.meta.removedFieldName); // add new field to the node set nodeByPath.delete(change.path); } function inputFieldDescriptionAdded(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const existingNode = nodeByPath.get(change.path); if (!existingNode) { config.onError(new errors_js_1.DeletedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedInputFieldDescription), change); return; } if (existingNode.kind !== graphql_1.Kind.INPUT_VALUE_DEFINITION) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.INPUT_VALUE_DEFINITION, existingNode.kind), change); return; } existingNode.description = (0, node_templates_js_1.stringNode)(change.meta.addedInputFieldDescription); } function inputFieldTypeChanged(change, nodeByPath, config, _context) { const inputFieldNode = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config); if (inputFieldNode) { (0, utils_js_1.assertValueMatch)(change, graphql_1.Kind.INPUT_VALUE_DEFINITION, change.meta.oldInputFieldType, (0, graphql_1.print)(inputFieldNode.type), config); inputFieldNode.type = (0, graphql_1.parseType)(change.meta.newInputFieldType); } } function inputFieldDefaultValueChanged(change, nodeByPath, config, _context) { if (!change.path) { config.onError(new errors_js_1.ChangePathMissingError(change), change); return; } const existingNode = nodeByPath.get(change.path); if (!existingNode) { config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.newDefaultValue ?? null), change); return; } if (existingNode.kind !== graphql_1.Kind.INPUT_VALUE_DEFINITION) { config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.INPUT_VALUE_DEFINITION, existingNode.kind), change); return; } const oldValueMatches = (existingNode.defaultValue && (0, graphql_1.print)(existingNode.defaultValue)) === change.meta.oldDefaultValue; if (!oldValueMatches) { config.onError(new errors_js_1.ValueMismatchError(existingNode.defaultValue?.kind ?? graphql_1.Kind.INPUT_VALUE_DEFINITION, change.meta.oldDefaultValue, existingNode.defaultValue && (0, graphql_1.print)(existingNode.defaultValue)), change); } existingNode.defaultValue = change.meta.newDefaultValue ? (0, graphql_1.parseConstValue)(change.meta.newDefaultValue) : undefined; } function inputFieldDescriptionChanged(change, nodeByPath, config, _context) { const existingNode = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config); if (!existingNode) { return; } if (existingNode.description?.value !== change.meta.oldInputFieldDescription) { config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.STRING, change.meta.oldInputFieldDescription, existingNode.description?.value), change); } existingNode.description = (0, node_templates_js_1.stringNode)(change.meta.newInputFieldDescription); } function inputFieldDescriptionRemoved(change, nodeByPath, config, _context) { const existingNode = (0, utils_js_1.getDeletedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config); if (!existingNode) { return; } if (existingNode.description === undefined) { console.warn(`Cannot remove a description at ${change.path} because no description is set.`); } else if (existingNode.description.value !== change.meta.removedDescription) { console.warn(`Description at ${change.path} does not match expected description, but proceeding with description removal anyways.`); } existingNode.description = undefined; }