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

Set name of query dynamically #9

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

Set name of query dynamically #9

iwconfig opened this issue Oct 7, 2022 · 1 comment

Comments

@iwconfig
Copy link

iwconfig commented Oct 7, 2022

I'd like to be able to change the name of the class as it's represented in the query.

Example:

class HeroAndDroid(NamedTuple):
    hero: Hero
    droid: Droid

and instead of

print(GQL(QUERY | HeroAndDroid).query)

# 'query HeroAndDroid{hero{name}droid{name}}'

I'd like to do something like

print(GQL(QUERY | HeroAndDroid * AS * 'asdf').query)

# 'query asdf{hero{name}droid{name}}'

or

print(GQL(QUERY | HeroAndDroid * AS * ANONYMOUS).query)

# 'query {hero{name}droid{name}}'

Of course I can do it like this

#RENAME = lambda cls, name: type(name, (cls,), {})
RENAME = lambda cls, name: (setattr(cls, '__name__', name), cls)[-1]

print(GQL(QUERY | RENAME(HeroAndDroid, 'I_Prefer_This_Name')).query)

# 'query I_Prefer_This_Name{hero{name}droid{name}}'

but I think using the AS function for this is much more suitable.

@iwconfig
Copy link
Author

iwconfig commented Oct 7, 2022

A possible solution:

@mul_infix
def AS(a: Binding, b: str) -> Binding:
    if isinstance(a, Binding):
        return a._replace(variable_alias=b)
    a.__name__ = b
    return a

Not sure if it's enough though but it works fine with my basic example

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