I am not totally sure that this solution would work for your problem, however I'll take a stab at it, and hopefully this helps!
One of my favorite resources so far for learning about transformers is this post http://nlp.seas.harvard.edu/2018/04/03/attention.html. I was able to follow along in a jupyter notebook and the only changes I remember I had to make to get it working were removing references to Variable. There is a section in there called Positional Encoding which may be relevant for what you want to do. Some people alternatively call this Positional Embedding. The choice of positional encoding is somewhat flexible. I have seen projects, such as Enformer from DeepMind (https://github.com/deepmind/deepmind-research/tree/master/enformer), which use custom position embeddings for the DNA sequence domain, which is centered in the middle of a sequence and extends out to the sides.
The above implementation/paper was very helpful for learning. Once I was ready to implement a transformer for a project I was working on, I found this implementation in the pytorch examples package a great place to start. In there you can see the class for the positional embedding and modify that as needed. https://github.com/pytorch/examples/tree/main/word_language_model
Now for your specific problem I did some googling for positional embedding/encoding and graph transformer, and I found a project that looks promising https://github.com/inria-thoth/GraphiT. Maybe that just does what you want, and their paper https://arxiv.org/abs/2106.05667 may have some useful background for you.
Hopefully these pointers help in your search!