xyflow: Edges do not render initially, 'Couldn't create edge for source handle id: "null"' when using ELK

What platform were you using when you found the bug?

  • React Flow / Svelte Flow version: 12.0.4
  • Browser and version: Chrome 127.0.6533.100
  • OS and version: Windows 11 23H2

Live code example

No response

Describe the Bug

When using ELK js, all of my nodes render in a tree format as expected, and the edges update properly using setEdges, but they do not render and display the following error:

image_2024-08-12_100021532

(4 edges logged, expected, but not rendering),

However when I make a change to my code and save, the edges render propertly 🤔

image_2024-08-12_100149815

And this only happens when using the formattedNodes from ELK, if I use my nodes from before formatting, it does work as expected, just without the tree formatting I look to achieve from ELK. Just to note, neither the ELK formatted edges or the initial edges work in this scenario, both throw the same error, so it is likely related to ELK and the Nodes rather than the edges.

image_2024-08-12_100651656

Code:

Here is some of the code, if anyone needs any more let me know.

Here is my useEffect after fetching the data

        let orgMemberRelationships: Edge[] = [];

        orgMemberSupervisors.forEach((supervisor) => {
            Object.values(supervisor["-SUPERVISOR_TO->User"]!).forEach(
                (supervised) => {
                    orgMemberRelationships.push({
                        id: supervisor.nodeId + "->" + supervised.nodeId,
                        source: supervisor.nodeId,
                        target: supervised.nodeId,
                        sourceHandle: 'a',
                        ...edgeStyles,
                    });
                },
            );
        });

        const orgMemberNodes = orgDataArray.map((orgMember, idx) => {
            const depth = Math.floor(Math.log2(idx + 1.5));
            const positionInLevel = idx - (Math.pow(2, depth) - 1);

            return {
                id: orgMember.nodeId,
                type: "orgMember",
                data: orgMember,
                position: {
                    x: 100 + positionInLevel * 400 * Math.pow(-1, idx),
                    y: 400 + depth * 400,
                },
            };
        });

        // Use an async function inside useEffect to handle the Promise
        const applyLayout = async () => {
            try {
                const { nodes: layoutedNodes, edges: layoutedEdges } =
                    await getLayoutedGraph(
                        orgMemberNodes,
                        orgMemberRelationships,
                    );
                    setNodes(layoutedNodes as Node[])
                    console.log(layoutedNodes)
            } catch (error) {
                console.error("Error applying layout:", error);
            }
        };

        

        applyLayout().then(() => {
            console.log('applying');
            setTimeout(() => {
                applyEdges(orgMemberRelationships);
            }, 3000);
        });
        

My ELK formatting function


const elk = new ELK();

const elkOptions = {
    "elk.algorithm": "layered",
    "elk.layered.spacing.nodeNodeBetweenLayers": "100",
    "elk.spacing.nodeNode": "80",
    "elk.direction": "DOWN",
};


const getLayoutedGraph = async (nodes: Node[], edges: Edge[]) => {
    const graph = {
        id: "root",
        layoutOptions: elkOptions,
        children: nodes.map((node) => ({
            ...node,
            // Adjust the target and source handle positions based on the layout
            // direction.

            // Hardcode a width and height for elk to use when layouting.
            width: 350,
            height: 250,
            sourcePosition: "bottom",
            targetPosition: "top",
        })),
        edges: edges.map((edge) => {
            return {
                ...edge,
                sourceHandle: 'a',
                sources: [edge.source],
                targets: [edge.target],
            };
        }),
        
    };

    const layoutedGraph = await elk.layout(graph);

    if (layoutedGraph) {
        return {
            nodes: layoutedGraph.children!.map((node) => ({
                ...node,
                // React Flow expects a position property on the node instead of `x`
                // and `y` fields.
                position: { x: node.x, y: node.y },
            })),

            edges: layoutedGraph.edges,
        };
    } else {
        return { nodes, edges };
    }
};

Additionally, I’ve tried manually setting the node handle Ids and that did not make any difference.

Steps to reproduce the bug or issue

  1. Create a set of nodes and edges
  2. Format using ELK js
  3. Call setNodes and setEdges

Expected behavior

I expected all of the edges to render properly when setEdges is called, not with a change to the code.

Screenshots or Videos

No response

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@moklick I’ll try to make a codesandbox if I get the chance