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

Replacement for ast_to_dict? #136

Closed
ptrhck opened this issue Jul 13, 2021 · 2 comments
Closed

Replacement for ast_to_dict? #136

ptrhck opened this issue Jul 13, 2021 · 2 comments
Assignees
Labels
feature Feature request good first issue Good for newcomers

Comments

@ptrhck
Copy link

ptrhck commented Jul 13, 2021

I have been using this function in the past, but it seems that ast_to_dict has been removed. Is there a replacement for ast_to_dict or a new function that is similiar to the one of the gist?

@Cito
Copy link
Member

Cito commented Jul 13, 2021

That function ast_to_dict does not exist in GraphQL.js, but was added to the legacy version of GraphQL-core because it makes sense to have this (only) in Python.

So yes, might be a good idea to add this to the modern GraphQL-core as well. The function could look like this, you can use this as a workaround for the time being:

def ast_to_dict(node, with_location=False):
    if isinstance(node, Node):
        res = {key: ast_to_dict(getattr(node, key), with_location)
               for key in node.keys if key != 'loc'}
        if with_location:
            loc = node.loc
            if loc:
                res['loc'] = dict(start=loc.start, end=loc.end)
        res['kind'] = node.kind
        return res
    if isinstance(node, list):
        return [ast_to_dict(item, with_location) for item in node]
    return node

Btw, an ast_to_code function was also added, we should consider adding this as well.

@Cito Cito self-assigned this Jul 13, 2021
@Cito Cito added feature Feature request good first issue Good for newcomers labels Jul 13, 2021
Cito added a commit that referenced this issue Jan 14, 2022
This function existed in the legacy version 2 and may be sometimes useful.
@Cito
Copy link
Member

Cito commented Jan 14, 2022

Added this back now, will be available in v3.2.

@Cito Cito closed this as completed Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants