I’m not sure I understand your description of the situation that you have.
What is most common for what I think you are trying to do is to override the RelinkingTool.doNoRelink method.
RelinkingTool | GoJS API
Here’s a complete example:
<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2022 by Northwoods Software Corporation. -->
</head>
<body>
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
const $ = go.GraphObject.make;
const myDiagram =
$(go.Diagram, "myDiagramDiv",
{
layout: $(go.ForceDirectedLayout),
"relinkingTool.doNoRelink": function(link, toend) {
const pt = this.diagram.lastInput.documentPoint;
const ndata = { text: "new node", loc: go.Point.stringify(pt) };
this.diagram.model.addNodeData(ndata);
const node = this.diagram.findNodeForData(ndata);
if (toend) {
link.toNode = node;
} else {
link.fromNode = node;
}
this.transactionResult = "with new node";
},
"undoManager.isEnabled": true
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
$(go.Shape,
{ fill: "white", portId: "", cursor: "pointer", fromLinkable: true, toLinkable: true },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 8 },
new go.Binding("text"))
);
myDiagram.linkTemplate =
$(go.Link,
{ relinkableFrom: true, relinkableTo: true },
$(go.Shape, { strokeWidth: 2 }),
$(go.Shape, { toArrow: "Standard" })
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, text: "Beta", color: "orange" },
{ key: 3, text: "Gamma", color: "lightgreen" },
{ key: 4, text: "Delta", color: "pink" }
],
[
{ from: 1, to: 2 },
{ from: 1, to: 3 },
{ from: 2, to: 2 },
{ from: 3, to: 4 },
{ from: 4, to: 1 }
]);
</script>
</body>
</html>
You might also want to do a similar thing when the user tries to draw a new link and there’s no existing node that it can validly connect with. In that case you’ll want to override the LinkingTool.doNoLink method.
LinkingTool | GoJS API