pythongraphqlstrawberry-graphql

get the path of a specific field in a graphql query


I want to get the path of a specific parameter of a graphql query and return it as a list.

for example, there is query like this:

query = '''
    query {
        books {
            __typename
            title
        }
        users {
            __typename
            name
            age
        }
    }
'''

I need a code in python that returns the path of for example "__typename" in list.

like: [[books, __typename], [users, __typename]]

can someone please give me a hand?


Solution

  • The execution context is available in the Info object.

    @strawberry.type
    class Query:
        @strawberry.field
        def users(self, info: Info) -> list[User]:
            for field in info.selected_fields:
                print('field', field.name)
                for selection in field.selections:
                    print('  selection', selection.name)
            return []