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

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

get the path of a specific field in a graphql query

问题

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

例如,有这样一个查询:

  1. query = '''
  2. query {
  3. books {
  4. __typename
  5. title
  6. }
  7. users {
  8. __typename
  9. name
  10. age
  11. }
  12. }
  13. '''

我需要一个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:

  1. query = '''
  2. query {
  3. books {
  4. __typename
  5. title
  6. }
  7. users {
  8. __typename
  9. name
  10. age
  11. }
  12. }
  13. '''

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.

  1. @strawberry.type
  2. class Query:
  3. @strawberry.field
  4. def users(self, info: Info) -> list[User]:
  5. for field in info.selected_fields:
  6. print('field', field.name)
  7. for selection in field.selections:
  8. print(' selection', selection.name)
  9. return []
英文:

The execution context is available in the Info object.

  1. @strawberry.type
  2. class Query:
  3. @strawberry.field
  4. def users(self, info: Info) -> list[User]:
  5. for field in info.selected_fields:
  6. print('field', field.name)
  7. for selection in field.selections:
  8. print(' selection', selection.name)
  9. 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:

确定