英文:
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 []
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论