Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field aliases #10

Open
iwconfig opened this issue Oct 7, 2022 · 1 comment
Open

Field aliases #10

iwconfig opened this issue Oct 7, 2022 · 1 comment

Comments

@iwconfig
Copy link

iwconfig commented Oct 7, 2022

Is it possible to set aliases to fields?

Such as this

query HeroAndDroid($droidId: ID!) {
  MYHERO: hero {
    name
  }
  droid(id: $droidId) {
    name
  }
}

I was looking through the source code and found

input_attr_name=alias if alias else attr_name,

What is Binding.variable_alias meant for?

If this is not already possible, maybe reading NamedTuple._field_defaults should suffice? Like so:

class HeroAndDroid(NamedTuple):
    droid: Droid
    hero: Hero = 'MYHERO'

Or might this already be on the roadmap?

EDIT:
Ah ok Binding.variable_alias and input_attr_name=alias if alias else attr_name, is for * AS * 'my_alias'

@iwconfig
Copy link
Author

iwconfig commented Oct 7, 2022

EDIT: No, it does not. And I am blind!

I poked around a bit

dsl.py

class ResolvedBinding(NamedTuple):
    ...
    attr_name_alias: str

...

class GraphQLQueryConstructor(NamedTuple):
    ...
    def prepare_bindings(self, expr: Expr) -> Mapping[str, Iterable[ResolvedBinding]]:
            ...
            rv[resolved_expr].append(resolved_input)
        return rv

    def resolve_binding(self, typ: Type[Any], field: FieldReference, alias: Optional[str]) -> ResolvedBinding:
        ...
        return ResolvedBinding(attr_name=overrider(name),
                               attr_name_alias=typ._field_defaults.get(attr_name),
                               input_attr_name=alias if alias else attr_name,
                               type_name=type_name,
                               is_optional=is_optional)

translator.py

def translate_begin_type(x: Token, bindings: Mapping[Any, Any]) -> str:
    if bindings:
        expr_bindings = next(iter(bindings))
        python_name_alias = expr_bindings.attr_name_alias
        all_input_bindings = chain.from_iterable(bindings.values())

    ...

    if python_name_alias:
        return f'{python_name_alias}{x.python_name}{vars}{{'
    return f'{x.python_name}{vars}{{'
  1. typ._field_defaults.get(attr_name) is assigned to dsl.ResolvedBinding.attr_name_alias in dsl.GraphQLQueryConstructor.resolve_binding.
  2. To the defaultdict returned by dsl.GraphQLQueryConstructor.prepare_bindings, the whole resolved_expr instance is passed as a key instead of only resolved_expr.attr_name,
  3. which is then used by translator.translate_begin_type.

I'm not sure if this is a good solution but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant