I've been using zref to handle my references. It has allowed me to customize a reference the way I want through its property lists. In particular, I reference a variety of structures in my document that are created in an automated way. Here's a MWE that provides the setup and illustrates my current problem:
\documentclass{article}
\usepackage{zref}% http://ctan.org/pkg/zref
\makeatletter
\newcounter{thegraph}% Graph counter
\zref@newlist{graph}% Graph reference stored in list 'graph'
\zref@newprop*{type}[??]{??}% Create new 'graph type' property
\zref@newprop*{number}[??]{\thethegraph}% Create new 'graph number' property
\zref@addprops{graph}{type, number}% Graph reference (or property list) has type & number
\newcommand{\graph}[1]{\zref@setcurrent{type}{G}\newgraph{1}{#1}}%
\newcommand{\digraph}[1]{\zref@setcurrent{type}{D}\newgraph{2}{#1}}%
\newcommand{\newgraph}[2]{% New graph #1=type; #2=ref
\refstepcounter{thegraph}% Increase counter
% zref label has to written immediately (not at page shipout), since more than one new graph may appear on page
\zref@wrapper@immediate{\zref@labelbylist{#2}{graph}}% Write graph list to .aux (using zref)
\label{#2}% Label newgraph for reference
\ifnum#1>0\ensuremath{\zref@getcurrent{type}_{\thethegraph}}\fi% Print graph symbol (if requested)
}
\newcommand{\graphref}[1]{\ensuremath{\zref@extract{#1}{type}_{\zref@extract{#1}{number}}}}% Reference to a graph
\newcommand{\complementg@aux}[2][3]{{}\mkern#1mu\overline{\mkern-#1mu#2}}
\newcommand{\complementg}[2][3]{\edef\@tempa{#2}\expandafter\complementg@aux\expandafter[\expandafter#1\expandafter]\@tempa}
\makeatother
\begin{document}
This is~\graph{first} and~\digraph{second}. Now see~\graphref{first} and~\graphref{second}.
What about~$\complementg{\graphref{first}}$ and~$\complementg{\graphref{second}}$?
\end{document}
The above MWE gives the following output

while I'd like it to look like this:

This is~$G_1$ and~$D_2$. Now see~$G_1$ and~$D_2$.
What about~$\complementg{G_1}$ and~$\complementg{D_2}$?
The choice of \complementg stems from a discussion in chat and because it worked in a non-zref implementation.
My question is: How can I define \complementg such that allows for being used in all of the following formats while only providing a "modified \overline" for the first character:
$\complementg{G_1}$% Basic math content with subscript manual subscript
$\complementg{\graphref{lab}}$% Referenced zref label (like above)
$\complementg{\mathcal{G}}$% "Complex" formatting of content
In a typographic sense, this question is somewhat related to What's the proper way to write a vector with a single character subscript?

\ensuremathfrom the definition of\graphref. Have I already said that\ensuremathis to be used carefully? ;-) – egreg Apr 20 '13 at 09:01\digraph. Hence, this is about graph theory and compound TeX arguments. (+1) Interesting approach! – e-birk Apr 20 '13 at 13:32