获取GraphQL查询中特定字段的路径。

huangapple go评论70阅读模式
英文:

get the path of a specific field in a graphql query

问题

我想获取GraphQL查询中特定参数的路径,并将其返回为列表。

例如,有这样一个查询:

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

我需要一个Python代码,返回例如"__typename"的路径列表。

像这样:[['books', '__typename'], ['users', '__typename']]

有人可以帮助我吗?

英文:

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?

答案1

得分: 1

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 []
英文:

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 []

huangapple
  • 本文由 发表于 2023年4月10日 19:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定