@graphql-inspector/action
Version:
GraphQL Inspector functionality for GitHub Actions
199 lines (198 loc) • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldTypeChanged = fieldTypeChanged;
exports.fieldRemoved = fieldRemoved;
exports.fieldAdded = fieldAdded;
exports.fieldArgumentAdded = fieldArgumentAdded;
exports.fieldArgumentTypeChanged = fieldArgumentTypeChanged;
exports.fieldArgumentDescriptionChanged = fieldArgumentDescriptionChanged;
exports.fieldArgumentDefaultChanged = fieldArgumentDefaultChanged;
exports.fieldArgumentRemoved = fieldArgumentRemoved;
exports.fieldDescriptionAdded = fieldDescriptionAdded;
exports.fieldDescriptionRemoved = fieldDescriptionRemoved;
exports.fieldDescriptionChanged = fieldDescriptionChanged;
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 fieldTypeChanged(change, nodeByPath, config, _context) {
const node = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.FIELD_DEFINITION, config);
if (node) {
const currentReturnType = (0, graphql_1.print)(node.type);
if (change.meta.oldFieldType !== currentReturnType) {
config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.FIELD_DEFINITION, change.meta.oldFieldType, currentReturnType), change);
}
node.type = (0, graphql_1.parseType)(change.meta.newFieldType);
}
}
function fieldRemoved(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), 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;
}
const beforeLength = typeNode.fields?.length ?? 0;
typeNode.fields = typeNode.fields?.filter(f => f.name.value !== change.meta.removedFieldName);
if (beforeLength === (typeNode.fields?.length ?? 0)) {
config.onError(new errors_js_1.DeletedAttributeNotFoundError(change.path, change.type, 'fields', change.meta.removedFieldName), change);
}
else {
// delete the reference to the removed field.
nodeByPath.delete(change.path);
}
}
function fieldAdded(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const changedNode = nodeByPath.get(change.path);
if (changedNode) {
if (changedNode.kind === graphql_1.Kind.FIELD_DEFINITION) {
if ((0, graphql_1.print)(changedNode.type) === change.meta.addedFieldReturnType) {
config.onError(new errors_js_1.AddedCoordinateAlreadyExistsError(change.path, change.type), change);
}
else {
config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.FIELD_DEFINITION, undefined, change.meta.addedFieldReturnType), change);
}
}
else {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.FIELD_DEFINITION, changedNode.kind), change);
}
return;
}
const typeNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path));
if (!typeNode) {
config.onError(new errors_js_1.ChangedAncestorCoordinateNotFoundError(change.path, change.type, change.meta.addedFieldName), change);
return;
}
if (typeNode.kind !== graphql_1.Kind.OBJECT_TYPE_DEFINITION &&
typeNode.kind !== graphql_1.Kind.INTERFACE_TYPE_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.ENUM_TYPE_DEFINITION, typeNode.kind), change);
return;
}
const node = {
kind: graphql_1.Kind.FIELD_DEFINITION,
name: (0, node_templates_js_1.nameNode)(change.meta.addedFieldName),
type: (0, graphql_1.parseType)(change.meta.addedFieldReturnType),
// description: change.meta.addedFieldDescription
// ? stringNode(change.meta.addedFieldDescription)
// : undefined,
};
typeNode.fields = [...(typeNode.fields ?? []), node];
// add new field to the node set
nodeByPath.set(change.path, node);
}
function fieldArgumentAdded(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 fieldNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path));
if (!fieldNode) {
config.onError(new errors_js_1.AddedAttributeCoordinateNotFoundError(change.path, change.type, change.meta.addedArgumentName), change);
return;
}
if (fieldNode.kind !== graphql_1.Kind.FIELD_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.FIELD_DEFINITION, fieldNode.kind), change);
return;
}
const node = {
kind: graphql_1.Kind.INPUT_VALUE_DEFINITION,
name: (0, node_templates_js_1.nameNode)(change.meta.addedArgumentName),
type: (0, graphql_1.parseType)(change.meta.addedArgumentType),
// description: change.meta.addedArgumentDescription
// ? stringNode(change.meta.addedArgumentDescription)
// : undefined,
};
fieldNode.arguments = [...(fieldNode.arguments ?? []), node];
// add new field to the node set
nodeByPath.set(change.path, node);
}
function fieldArgumentTypeChanged(change, nodeByPath, config, _context) {
const existingArg = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config);
if (existingArg) {
(0, utils_js_1.assertValueMatch)(change, graphql_1.Kind.INPUT_VALUE_DEFINITION, change.meta.oldArgumentType, (0, graphql_1.print)(existingArg.type), config);
existingArg.type = (0, graphql_1.parseType)(change.meta.newArgumentType);
}
}
function fieldArgumentDescriptionChanged(change, nodeByPath, config, _context) {
const existingArg = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config);
if (existingArg) {
(0, utils_js_1.assertValueMatch)(change, graphql_1.Kind.INPUT_VALUE_DEFINITION, change.meta.oldDescription ?? undefined, existingArg.description?.value, config);
existingArg.description = change.meta.newDescription
? (0, node_templates_js_1.stringNode)(change.meta.newDescription)
: undefined;
}
}
function fieldArgumentDefaultChanged(change, nodeByPath, config, _context) {
const existingArg = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.INPUT_VALUE_DEFINITION, config);
if (existingArg) {
(0, utils_js_1.assertValueMatch)(change, graphql_1.Kind.INPUT_VALUE_DEFINITION, change.meta.oldDefaultValue, existingArg.defaultValue && (0, graphql_1.print)(existingArg.defaultValue), config);
existingArg.defaultValue = change.meta.newDefaultValue
? (0, graphql_1.parseConstValue)(change.meta.newDefaultValue)
: undefined;
}
}
function fieldArgumentRemoved(change, nodeByPath, config, _context) {
const existing = (0, utils_js_1.getDeletedNodeOfKind)(change, nodeByPath, graphql_1.Kind.ARGUMENT, config);
if (!existing) {
config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path ?? '', change.type), change);
return;
}
const fieldNode = nodeByPath.get((0, utils_js_1.parentPath)(change.path));
if (!fieldNode) {
config.onError(new errors_js_1.DeletedAncestorCoordinateNotFoundError(change.path, // asserted by "getDeletedNodeOfKind"
change.type, change.meta.removedFieldArgumentName), change);
return;
}
if (fieldNode.kind !== graphql_1.Kind.FIELD_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.FIELD_DEFINITION, fieldNode.kind), change);
}
fieldNode.arguments = fieldNode.arguments?.filter(a => a.name.value === change.meta.removedFieldArgumentName);
// add new field to the node set
nodeByPath.delete(change.path);
}
function fieldDescriptionAdded(change, nodeByPath, config, _context) {
const fieldNode = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.FIELD_DEFINITION, config);
if (fieldNode) {
fieldNode.description = change.meta.addedDescription
? (0, node_templates_js_1.stringNode)(change.meta.addedDescription)
: undefined;
}
}
function fieldDescriptionRemoved(change, nodeByPath, config, _context) {
if (!change.path) {
config.onError(new errors_js_1.ChangePathMissingError(change), change);
return;
}
const fieldNode = nodeByPath.get(change.path);
if (!fieldNode) {
config.onError(new errors_js_1.DeletedCoordinateNotFound(change.path, change.type), change);
return;
}
if (fieldNode.kind !== graphql_1.Kind.FIELD_DEFINITION) {
config.onError(new errors_js_1.ChangedCoordinateKindMismatchError(graphql_1.Kind.FIELD_DEFINITION, fieldNode.kind), change);
return;
}
fieldNode.description = undefined;
}
function fieldDescriptionChanged(change, nodeByPath, config, _context) {
const fieldNode = (0, utils_js_1.getChangedNodeOfKind)(change, nodeByPath, graphql_1.Kind.FIELD_DEFINITION, config);
if (!fieldNode) {
return;
}
if (fieldNode.description?.value !== change.meta.oldDescription) {
config.onError(new errors_js_1.ValueMismatchError(graphql_1.Kind.FIELD_DEFINITION, change.meta.oldDescription, fieldNode.description?.value), change);
}
fieldNode.description = (0, node_templates_js_1.stringNode)(change.meta.newDescription);
}